blob: 6bd96fac737c9e71c9408d5b68a205292a229c33 [file] [log] [blame]
Radek Krejci19a96102018-11-15 13:38:09 +01001/**
2 * @file tree_schema.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema tree implementation
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#include "common.h"
16
Radek Krejcie7b95092019-05-15 11:03:07 +020017#include <assert.h>
Radek Krejci19a96102018-11-15 13:38:09 +010018#include <ctype.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include <stddef.h>
20#include <stdint.h>
Radek Krejci19a96102018-11-15 13:38:09 +010021#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdlib.h>
23#include <string.h>
Radek Krejci19a96102018-11-15 13:38:09 +010024
Radek Krejcie7b95092019-05-15 11:03:07 +020025#include "dict.h"
26#include "log.h"
Radek Krejci0935f412019-08-20 16:15:18 +020027#include "plugins_exts.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "set.h"
29#include "plugins_types.h"
Radek Krejci0935f412019-08-20 16:15:18 +020030#include "plugins_exts_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020031#include "tree.h"
32#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010033#include "tree_schema_internal.h"
34#include "xpath.h"
35
36/**
37 * @brief Duplicate string into dictionary
38 * @param[in] CTX libyang context of the dictionary.
39 * @param[in] ORIG String to duplicate.
40 * @param[out] DUP Where to store the result.
41 */
42#define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);}
43
Radek Krejciec4da802019-05-02 13:02:41 +020044#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010045 if (ARRAY_P) { \
46 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010047 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
Radek Krejci19a96102018-11-15 13:38:09 +010048 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
49 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020050 RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010051 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
52 } \
53 }
54
Radek Krejciec4da802019-05-02 13:02:41 +020055#define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010056 if (ARRAY_P) { \
57 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
58 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
59 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
60 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020061 RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010062 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
63 } \
64 }
65
Radek Krejci0935f412019-08-20 16:15:18 +020066#define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \
67 if (EXTS_P) { \
68 LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_SIZE(EXTS_P), RET, GOTO); \
69 for (uint32_t __exts_iter = 0, __array_offset = LY_ARRAY_SIZE(EXT_C); __exts_iter < LY_ARRAY_SIZE(EXTS_P); ++__exts_iter) { \
70 LY_ARRAY_INCREMENT(EXT_C); \
71 RET = lys_compile_ext(CTX, &(EXTS_P)[__exts_iter], &(EXT_C)[__exts_iter + __array_offset], PARENT, PARENT_TYPE); \
72 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
73 } \
74 }
75
Radek Krejciec4da802019-05-02 13:02:41 +020076#define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejcid05cbd92018-12-05 14:26:40 +010077 if (ARRAY_P) { \
78 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
79 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
80 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
81 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020082 RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejci19a96102018-11-15 13:38:09 +010083 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
84 } \
85 }
86
Radek Krejciec4da802019-05-02 13:02:41 +020087#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010088 if (MEMBER_P) { \
89 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
90 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
Radek Krejciec4da802019-05-02 13:02:41 +020091 RET = FUNC(CTX, MEMBER_P, MEMBER_C); \
Radek Krejci19a96102018-11-15 13:38:09 +010092 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
93 }
94
Radek Krejciec4da802019-05-02 13:02:41 +020095#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \
Radek Krejci00b874b2019-02-12 10:54:50 +010096 if (MEMBER_P) { \
97 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
98 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
99 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +0200100 RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100101 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
102 }
103
Radek Krejcid05cbd92018-12-05 14:26:40 +0100104#define COMPILE_CHECK_UNIQUENESS(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
105 if (ARRAY) { \
Radek Krejci0af46292019-01-11 16:02:31 +0100106 for (unsigned int u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \
107 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100108 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
109 return LY_EVALID; \
110 } \
111 } \
112 }
113
Radek Krejci19a96102018-11-15 13:38:09 +0100114static struct lysc_ext_instance *
115lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
116{
Radek Krejcie7b95092019-05-15 11:03:07 +0200117 /* TODO - extensions */
Radek Krejci19a96102018-11-15 13:38:09 +0100118 (void) ctx;
119 (void) orig;
120 return NULL;
121}
122
Radek Krejcib56c7502019-02-13 14:19:54 +0100123/**
Radek Krejci474f9b82019-07-24 11:36:37 +0200124 * @brief Add record into the compile context's list of incomplete default values.
125 * @param[in] ctx Compile context with the incomplete default values list.
126 * @param[in] context_node Context schema node to store in the record.
127 * @param[in] dflt Incomplete default value to store in the record.
128 * @param[in] dflt_mod Module of the default value definition to store in the record.
129 * @return LY_EMEM in case of memory allocation failure.
130 * @return LY_SUCCESS
131 */
132static LY_ERR
133lysc_incomplete_dflts_add(struct lysc_ctx *ctx, struct lysc_node *context_node, struct lyd_value *dflt, struct lys_module *dflt_mod)
134{
135 struct lysc_incomplete_dflt *r;
136 r = malloc(sizeof *r);
137 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
138 r->context_node = context_node;
139 r->dflt = dflt;
140 r->dflt_mod = dflt_mod;
141 ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST);
142
143 return LY_SUCCESS;
144}
145
146/**
147 * @brief Remove record of the given default value from the compile context's list of incomplete default values.
148 * @param[in] ctx Compile context with the incomplete default values list.
149 * @param[in] dflt Incomplete default values identifying the record to remove.
150 */
151static void
152lysc_incomplete_dflts_remove(struct lysc_ctx *ctx, struct lyd_value *dflt)
153{
154 unsigned int u;
155 struct lysc_incomplete_dflt *r;
156
157 for (u = 0; u < ctx->dflts.count; ++u) {
158 r = (struct lysc_incomplete_dflt*)ctx->dflts.objs[u];
159 if (r->dflt == dflt) {
160 free(ctx->dflts.objs[u]);
161 memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs);
162 --ctx->dflts.count;
163 return;
164 }
165 }
166}
167
Radek Krejci0e59c312019-08-15 15:34:15 +0200168void
Radek Krejci327de162019-06-14 12:52:07 +0200169lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name)
170{
171 int len;
172 int nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
173
174 if (!name) {
175 /* removing last path segment */
176 if (ctx->path[ctx->path_len - 1] == '}') {
177 for (; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len);
178 if (ctx->path[ctx->path_len] == '=') {
179 ctx->path[ctx->path_len++] = '}';
180 } else {
181 /* not a top-level special tag, remove also preceiding '/' */
182 goto remove_nodelevel;
183 }
184 } else {
185remove_nodelevel:
186 for (; ctx->path[ctx->path_len] != '/' ; --ctx->path_len);
187 if (ctx->path_len == 0) {
188 /* top-level (last segment) */
Radek Krejciacc79042019-07-25 14:14:57 +0200189 ctx->path_len = 1;
Radek Krejci327de162019-06-14 12:52:07 +0200190 }
191 }
192 /* set new terminating NULL-byte */
193 ctx->path[ctx->path_len] = '\0';
194 } else {
195 if (ctx->path_len > 1) {
196 if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') {
197 /* extension of the special tag */
198 nextlevel = 2;
199 --ctx->path_len;
200 } else {
201 /* there is already some path, so add next level */
202 nextlevel = 1;
203 }
204 } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
205
206 if (nextlevel != 2) {
207 if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) {
208 /* module not changed, print the name unprefixed */
Radek Krejci70ee9152019-07-25 11:27:27 +0200209 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name);
Radek Krejci327de162019-06-14 12:52:07 +0200210 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200211 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s:%s", nextlevel ? "/" : "", ctx->mod->name, name);
Radek Krejci327de162019-06-14 12:52:07 +0200212 }
213 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200214 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
Radek Krejci327de162019-06-14 12:52:07 +0200215 }
Radek Krejciacc79042019-07-25 14:14:57 +0200216 if (len >= LYSC_CTX_BUFSIZE - ctx->path_len) {
217 /* output truncated */
218 ctx->path_len = LYSC_CTX_BUFSIZE - 1;
219 } else {
220 ctx->path_len += len;
221 }
Radek Krejci327de162019-06-14 12:52:07 +0200222 }
223}
224
225/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100226 * @brief Duplicate the compiled pattern structure.
227 *
228 * Instead of duplicating memory, the reference counter in the @p orig is increased.
229 *
230 * @param[in] orig The pattern structure to duplicate.
231 * @return The duplicated structure to use.
232 */
Radek Krejci19a96102018-11-15 13:38:09 +0100233static struct lysc_pattern*
234lysc_pattern_dup(struct lysc_pattern *orig)
235{
236 ++orig->refcount;
237 return orig;
238}
239
Radek Krejcib56c7502019-02-13 14:19:54 +0100240/**
241 * @brief Duplicate the array of compiled patterns.
242 *
243 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
244 *
245 * @param[in] ctx Libyang context for logging.
246 * @param[in] orig The patterns sized array to duplicate.
247 * @return New sized array as a copy of @p orig.
248 * @return NULL in case of memory allocation error.
249 */
Radek Krejci19a96102018-11-15 13:38:09 +0100250static struct lysc_pattern**
251lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
252{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100253 struct lysc_pattern **dup = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100254 unsigned int u;
255
Radek Krejcib56c7502019-02-13 14:19:54 +0100256 assert(orig);
257
Radek Krejci19a96102018-11-15 13:38:09 +0100258 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL);
259 LY_ARRAY_FOR(orig, u) {
260 dup[u] = lysc_pattern_dup(orig[u]);
261 LY_ARRAY_INCREMENT(dup);
262 }
263 return dup;
264}
265
Radek Krejcib56c7502019-02-13 14:19:54 +0100266/**
267 * @brief Duplicate compiled range structure.
268 *
269 * @param[in] ctx Libyang context for logging.
270 * @param[in] orig The range structure to be duplicated.
271 * @return New compiled range structure as a copy of @p orig.
272 * @return NULL in case of memory allocation error.
273 */
Radek Krejci19a96102018-11-15 13:38:09 +0100274struct lysc_range*
275lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
276{
277 struct lysc_range *dup;
278 LY_ERR ret;
279
Radek Krejcib56c7502019-02-13 14:19:54 +0100280 assert(orig);
281
Radek Krejci19a96102018-11-15 13:38:09 +0100282 dup = calloc(1, sizeof *dup);
283 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
284 if (orig->parts) {
285 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup);
286 LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts);
287 memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts);
288 }
289 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
290 DUP_STRING(ctx, orig->emsg, dup->emsg);
291 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
292
293 return dup;
294cleanup:
295 free(dup);
296 (void) ret; /* set but not used due to the return type */
297 return NULL;
298}
299
Radek Krejcib56c7502019-02-13 14:19:54 +0100300/**
301 * @brief Stack for processing if-feature expressions.
302 */
Radek Krejci19a96102018-11-15 13:38:09 +0100303struct iff_stack {
Radek Krejcib56c7502019-02-13 14:19:54 +0100304 int size; /**< number of items in the stack */
305 int index; /**< first empty item */
306 uint8_t *stack;/**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100307};
308
Radek Krejcib56c7502019-02-13 14:19:54 +0100309/**
310 * @brief Add @ref ifftokens into the stack.
311 * @param[in] stack The if-feature stack to use.
312 * @param[in] value One of the @ref ifftokens to store in the stack.
313 * @return LY_EMEM in case of memory allocation error
314 * @return LY_ESUCCESS if the value successfully stored.
315 */
Radek Krejci19a96102018-11-15 13:38:09 +0100316static LY_ERR
317iff_stack_push(struct iff_stack *stack, uint8_t value)
318{
319 if (stack->index == stack->size) {
320 stack->size += 4;
321 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
322 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
323 }
324 stack->stack[stack->index++] = value;
325 return LY_SUCCESS;
326}
327
Radek Krejcib56c7502019-02-13 14:19:54 +0100328/**
329 * @brief Get (and remove) the last item form the stack.
330 * @param[in] stack The if-feature stack to use.
331 * @return The value from the top of the stack.
332 */
Radek Krejci19a96102018-11-15 13:38:09 +0100333static uint8_t
334iff_stack_pop(struct iff_stack *stack)
335{
Radek Krejcib56c7502019-02-13 14:19:54 +0100336 assert(stack && stack->index);
337
Radek Krejci19a96102018-11-15 13:38:09 +0100338 stack->index--;
339 return stack->stack[stack->index];
340}
341
Radek Krejcib56c7502019-02-13 14:19:54 +0100342/**
343 * @brief Clean up the stack.
344 * @param[in] stack The if-feature stack to use.
345 */
Radek Krejci19a96102018-11-15 13:38:09 +0100346static void
347iff_stack_clean(struct iff_stack *stack)
348{
349 stack->size = 0;
350 free(stack->stack);
351}
352
Radek Krejcib56c7502019-02-13 14:19:54 +0100353/**
354 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
355 * (libyang format of the if-feature expression).
356 * @param[in,out] list The 2bits array to modify.
357 * @param[in] op The operand (@ref ifftokens) to store.
358 * @param[in] pos Position (0-based) where to store the given @p op.
359 */
Radek Krejci19a96102018-11-15 13:38:09 +0100360static void
361iff_setop(uint8_t *list, uint8_t op, int pos)
362{
363 uint8_t *item;
364 uint8_t mask = 3;
365
366 assert(pos >= 0);
367 assert(op <= 3); /* max 2 bits */
368
369 item = &list[pos / 4];
370 mask = mask << 2 * (pos % 4);
371 *item = (*item) & ~mask;
372 *item = (*item) | (op << 2 * (pos % 4));
373}
374
Radek Krejcib56c7502019-02-13 14:19:54 +0100375#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
376#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100377
Radek Krejci0af46292019-01-11 16:02:31 +0100378/**
379 * @brief Find a feature of the given name and referenced in the given module.
380 *
381 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
382 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
383 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
384 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
385 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
386 * assigned till that time will be still valid.
387 *
388 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
389 * @param[in] name Name of the feature including possible prefix.
390 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
391 * @return Pointer to the feature structure if found, NULL otherwise.
392 */
Radek Krejci19a96102018-11-15 13:38:09 +0100393static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100394lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100395{
396 size_t i;
Radek Krejci0af46292019-01-11 16:02:31 +0100397 struct lysc_feature *f, *flist;
Radek Krejci19a96102018-11-15 13:38:09 +0100398
399 for (i = 0; i < len; ++i) {
400 if (name[i] == ':') {
401 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100402 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100403 LY_CHECK_RET(!mod, NULL);
404
405 name = &name[i + 1];
406 len = len - i - 1;
407 }
408 }
409
410 /* we have the correct module, get the feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100411 if (mod->implemented) {
412 /* module is implemented so there is already the compiled schema */
413 flist = mod->compiled->features;
414 } else {
415 flist = mod->off_features;
416 }
417 LY_ARRAY_FOR(flist, i) {
418 f = &flist[i];
Radek Krejci7f9b6512019-09-18 13:11:09 +0200419 if (!ly_strncmp(f->name, name, len)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100420 return f;
421 }
422 }
423
424 return NULL;
425}
426
427static LY_ERR
Radek Krejci0935f412019-08-20 16:15:18 +0200428lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent, LYEXT_PARENT parent_type)
Radek Krejci19a96102018-11-15 13:38:09 +0100429{
430 const char *name;
431 unsigned int u;
432 const struct lys_module *mod;
Radek Krejci0935f412019-08-20 16:15:18 +0200433 struct lysc_ext *elist = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100434
435 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
436 ext->insubstmt = ext_p->insubstmt;
437 ext->insubstmt_index = ext_p->insubstmt_index;
Radek Krejci28681fa2019-09-06 13:08:45 +0200438 ext->module = ctx->mod_def;
Radek Krejci0935f412019-08-20 16:15:18 +0200439 ext->parent = parent;
440 ext->parent_type = parent_type;
Radek Krejci19a96102018-11-15 13:38:09 +0100441
Radek Krejcif56e2a42019-09-09 14:15:25 +0200442 lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node*)ext->parent : NULL, "{extension}");
443 lysc_update_path(ctx, NULL, ext_p->name );
444
Radek Krejci19a96102018-11-15 13:38:09 +0100445 /* get module where the extension definition should be placed */
446 for (u = 0; ext_p->name[u] != ':'; ++u);
Radek Krejcie86bf772018-12-14 11:39:53 +0100447 mod = lys_module_find_prefix(ctx->mod_def, ext_p->name, u);
Radek Krejci19a96102018-11-15 13:38:09 +0100448 LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
449 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name),
450 LY_EVALID);
451 LY_CHECK_ERR_RET(!mod->parsed->extensions,
452 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
453 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100454 ext_p->name, mod->name),
Radek Krejci19a96102018-11-15 13:38:09 +0100455 LY_EVALID);
456 name = &ext_p->name[u + 1];
Radek Krejci0935f412019-08-20 16:15:18 +0200457
Radek Krejci19a96102018-11-15 13:38:09 +0100458 /* find the extension definition there */
Radek Krejci0935f412019-08-20 16:15:18 +0200459 if (mod->off_extensions) {
460 elist = mod->off_extensions;
461 } else {
462 elist = mod->compiled->extensions;
463 }
464 LY_ARRAY_FOR(elist, u) {
465 if (!strcmp(name, elist[u].name)) {
466 ext->def = &elist[u];
Radek Krejci19a96102018-11-15 13:38:09 +0100467 break;
468 }
469 }
Radek Krejci0935f412019-08-20 16:15:18 +0200470 LY_CHECK_ERR_RET(!ext->def,
471 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
472 "Extension definition of extension instance \"%s\" not found.", ext_p->name),
Radek Krejci19a96102018-11-15 13:38:09 +0100473 LY_EVALID);
Radek Krejci0935f412019-08-20 16:15:18 +0200474
Radek Krejcif56e2a42019-09-09 14:15:25 +0200475 /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible
476 * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have
477 * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */
478 if (ext_p->yin && ext->def->argument) {
479 /* Schema was parsed from YIN and an argument is expected, ... */
480 struct lysp_stmt *stmt = NULL;
481
482 if (ext->def->flags & LYS_YINELEM_TRUE) {
483 /* ... argument was the first XML child element */
484 if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) {
485 /* TODO check namespace of the statement */
486 if (!strcmp(ext_p->child->stmt, ext->def->argument)) {
487 stmt = ext_p->child;
488 }
489 }
490 } else {
491 /* ... argument was one of the XML attributes which are represented as child stmt
492 * with LYS_YIN_ATTR flag */
493 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
494 if (!strcmp(stmt->stmt, ext->def->argument)) {
495 /* this is the extension's argument */
496 ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0);
497 break;
498 }
499 }
500 }
501 if (!stmt) {
502 /* missing extension's argument */
503 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
504 "Extension instance \"%s\" misses argument \"%s\".", ext_p->name, ext->def->argument);
505 return LY_EVALID;
506
507 }
508 ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0);
509 stmt->flags |= LYS_YIN_ARGUMENT;
510 }
511
Radek Krejci0935f412019-08-20 16:15:18 +0200512 if (ext->def->plugin && ext->def->plugin->compile) {
Radek Krejciad5963b2019-09-06 16:03:05 +0200513 if (ext->argument) {
514 lysc_update_path(ctx, (struct lysc_node*)ext, ext->argument);
515 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200516 LY_CHECK_RET(ext->def->plugin->compile(ctx, ext_p, ext), LY_EVALID);
Radek Krejciad5963b2019-09-06 16:03:05 +0200517 if (ext->argument) {
518 lysc_update_path(ctx, NULL, NULL);
519 }
Radek Krejci0935f412019-08-20 16:15:18 +0200520 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200521 ext_p->compiled = ext;
522
523 lysc_update_path(ctx, NULL, NULL);
524 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0935f412019-08-20 16:15:18 +0200525
526 return LY_SUCCESS;
527}
528
529/**
530 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
531 */
532static LY_ERR
533lys_compile_extension(struct lysc_ctx *ctx, struct lysp_ext *ext_p, struct lysc_ext *ext)
534{
535 LY_ERR ret = LY_SUCCESS;
536
537 DUP_STRING(ctx->ctx, ext_p->name, ext->name);
538 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
539 ext->module = ctx->mod_def;
540 COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext->exts, ext, LYEXT_PAR_EXT, ret, done);
541
542done:
543 return ret;
544}
545
546/**
547 * @brief Link the extensions definitions with the available extension plugins.
548 *
549 * This is done only in the compiled (implemented) module. Extensions of a non-implemented modules
550 * are not connected with even available extension plugins.
551 *
552 * @param[in] extensions List of extensions to be processed ([sized array](@ref sizedarrays)).
553 */
554static void
555lys_compile_extension_plugins(struct lysc_ext *extensions)
556{
557 unsigned int u;
558
559 LY_ARRAY_FOR(extensions, u) {
560 extensions[u].plugin = lyext_get_plugin(&extensions[u]);
561 }
562}
563
564LY_ERR
565lys_extension_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
566 struct lysp_ext *extensions_p, struct lysc_ext **extensions)
567{
568 unsigned int offset = 0, u;
569 struct lysc_ctx context = {0};
570
571 assert(ctx_sc || ctx);
572
573 if (!ctx_sc) {
574 context.ctx = ctx;
575 context.mod = module;
576 context.path_len = 1;
577 context.path[0] = '/';
578 ctx_sc = &context;
579 }
580
581 if (!extensions_p) {
582 return LY_SUCCESS;
583 }
584 if (*extensions) {
585 offset = LY_ARRAY_SIZE(*extensions);
586 }
587
588 lysc_update_path(ctx_sc, NULL, "{extension}");
589 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *extensions, LY_ARRAY_SIZE(extensions_p), LY_EMEM);
590 LY_ARRAY_FOR(extensions_p, u) {
591 lysc_update_path(ctx_sc, NULL, extensions_p[u].name);
592 LY_ARRAY_INCREMENT(*extensions);
593 COMPILE_CHECK_UNIQUENESS(ctx_sc, *extensions, name, &(*extensions)[offset + u], "extension", extensions_p[u].name);
594 LY_CHECK_RET(lys_compile_extension(ctx_sc, &extensions_p[u], &(*extensions)[offset + u]));
595 lysc_update_path(ctx_sc, NULL, NULL);
596 }
597 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100598
599 return LY_SUCCESS;
600}
601
Radek Krejcib56c7502019-02-13 14:19:54 +0100602/**
603 * @brief Compile information from the if-feature statement
604 * @param[in] ctx Compile context.
605 * @param[in] value The if-feature argument to process. It is pointer-to-pointer-to-char just to unify the compile functions.
Radek Krejcib56c7502019-02-13 14:19:54 +0100606 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
607 * @return LY_ERR value.
608 */
Radek Krejci19a96102018-11-15 13:38:09 +0100609static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200610lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff)
Radek Krejci19a96102018-11-15 13:38:09 +0100611{
612 const char *c = *value;
613 int r, rc = EXIT_FAILURE;
614 int i, j, last_not, checkversion = 0;
615 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
616 uint8_t op;
617 struct iff_stack stack = {0, 0, NULL};
618 struct lysc_feature *f;
619
620 assert(c);
621
622 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
623 for (i = j = last_not = 0; c[i]; i++) {
624 if (c[i] == '(') {
625 j++;
626 checkversion = 1;
627 continue;
628 } else if (c[i] == ')') {
629 j--;
630 continue;
631 } else if (isspace(c[i])) {
632 checkversion = 1;
633 continue;
634 }
635
636 if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200637 int sp;
638 for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++);
639 if (c[i + r + sp] == '\0') {
Radek Krejci19a96102018-11-15 13:38:09 +0100640 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
641 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
642 return LY_EVALID;
643 } else if (!isspace(c[i + r])) {
644 /* feature name starting with the not/and/or */
645 last_not = 0;
646 f_size++;
647 } else if (c[i] == 'n') { /* not operation */
648 if (last_not) {
649 /* double not */
650 expr_size = expr_size - 2;
651 last_not = 0;
652 } else {
653 last_not = 1;
654 }
655 } else { /* and, or */
Radek Krejci6788abc2019-06-14 13:56:49 +0200656 if (f_exp != f_size) {
657 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
658 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]);
659 return LY_EVALID;
660 }
Radek Krejci19a96102018-11-15 13:38:09 +0100661 f_exp++;
Radek Krejci6788abc2019-06-14 13:56:49 +0200662
Radek Krejci19a96102018-11-15 13:38:09 +0100663 /* not a not operation */
664 last_not = 0;
665 }
666 i += r;
667 } else {
668 f_size++;
669 last_not = 0;
670 }
671 expr_size++;
672
673 while (!isspace(c[i])) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200674 if (!c[i] || c[i] == ')' || c[i] == '(') {
Radek Krejci19a96102018-11-15 13:38:09 +0100675 i--;
676 break;
677 }
678 i++;
679 }
680 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200681 if (j) {
Radek Krejci19a96102018-11-15 13:38:09 +0100682 /* not matching count of ( and ) */
683 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
684 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
685 return LY_EVALID;
686 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200687 if (f_exp != f_size) {
688 /* features do not match the needed arguments for the logical operations */
689 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
690 "Invalid value \"%s\" of if-feature - number of features in expression does not match "
691 "the required number of operands for the operations.", *value);
692 return LY_EVALID;
693 }
Radek Krejci19a96102018-11-15 13:38:09 +0100694
695 if (checkversion || expr_size > 1) {
696 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100697 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100698 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
699 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
700 return LY_EVALID;
701 }
702 }
703
704 /* allocate the memory */
705 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
706 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
707 stack.stack = malloc(expr_size * sizeof *stack.stack);
708 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
709
710 stack.size = expr_size;
711 f_size--; expr_size--; /* used as indexes from now */
712
713 for (i--; i >= 0; i--) {
714 if (c[i] == ')') {
715 /* push it on stack */
716 iff_stack_push(&stack, LYS_IFF_RP);
717 continue;
718 } else if (c[i] == '(') {
719 /* pop from the stack into result all operators until ) */
720 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
721 iff_setop(iff->expr, op, expr_size--);
722 }
723 continue;
724 } else if (isspace(c[i])) {
725 continue;
726 }
727
728 /* end of operator or operand -> find beginning and get what is it */
729 j = i + 1;
730 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
731 i--;
732 }
733 i++; /* go back by one step */
734
735 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
736 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
737 /* double not */
738 iff_stack_pop(&stack);
739 } else {
740 /* not has the highest priority, so do not pop from the stack
741 * as in case of AND and OR */
742 iff_stack_push(&stack, LYS_IFF_NOT);
743 }
744 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
745 /* as for OR - pop from the stack all operators with the same or higher
746 * priority and store them to the result, then push the AND to the stack */
747 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
748 op = iff_stack_pop(&stack);
749 iff_setop(iff->expr, op, expr_size--);
750 }
751 iff_stack_push(&stack, LYS_IFF_AND);
752 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
753 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
754 op = iff_stack_pop(&stack);
755 iff_setop(iff->expr, op, expr_size--);
756 }
757 iff_stack_push(&stack, LYS_IFF_OR);
758 } else {
759 /* feature name, length is j - i */
760
761 /* add it to the expression */
762 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
763
764 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100765 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
766 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
767 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
768 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100769 iff->features[f_size] = f;
770 LY_ARRAY_INCREMENT(iff->features);
771 f_size--;
772 }
773 }
774 while (stack.index) {
775 op = iff_stack_pop(&stack);
776 iff_setop(iff->expr, op, expr_size--);
777 }
778
779 if (++expr_size || ++f_size) {
780 /* not all expected operators and operands found */
781 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
782 "Invalid value \"%s\" of if-feature - processing error.", *value);
783 rc = LY_EINT;
784 } else {
785 rc = LY_SUCCESS;
786 }
787
788error:
789 /* cleanup */
790 iff_stack_clean(&stack);
791
792 return rc;
793}
794
Radek Krejcib56c7502019-02-13 14:19:54 +0100795/**
796 * @brief Compile information from the when statement
797 * @param[in] ctx Compile context.
798 * @param[in] when_p The parsed when statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100799 * @param[out] when Pointer where to store pointer to the created compiled when structure.
800 * @return LY_ERR value.
801 */
Radek Krejci19a96102018-11-15 13:38:09 +0100802static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200803lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, struct lysc_when **when)
Radek Krejci19a96102018-11-15 13:38:09 +0100804{
Radek Krejci19a96102018-11-15 13:38:09 +0100805 LY_ERR ret = LY_SUCCESS;
806
Radek Krejci00b874b2019-02-12 10:54:50 +0100807 *when = calloc(1, sizeof **when);
808 (*when)->refcount = 1;
809 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond);
Radek Krejcia0f704a2019-09-09 16:12:23 +0200810 (*when)->module = ctx->mod_def;
Radek Krejci00b874b2019-02-12 10:54:50 +0100811 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
812 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
813 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci0935f412019-08-20 16:15:18 +0200814 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100815
816done:
817 return ret;
818}
819
Radek Krejcib56c7502019-02-13 14:19:54 +0100820/**
821 * @brief Compile information from the must statement
822 * @param[in] ctx Compile context.
823 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100824 * @param[in,out] must Prepared (empty) compiled must structure to fill.
825 * @return LY_ERR value.
826 */
Radek Krejci19a96102018-11-15 13:38:09 +0100827static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200828lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100829{
Radek Krejci19a96102018-11-15 13:38:09 +0100830 LY_ERR ret = LY_SUCCESS;
831
832 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg);
833 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci462cf252019-07-24 09:49:08 +0200834 must->module = ctx->mod_def;
Radek Krejci19a96102018-11-15 13:38:09 +0100835 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
836 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100837 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
838 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejci0935f412019-08-20 16:15:18 +0200839 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100840
841done:
842 return ret;
843}
844
Radek Krejcib56c7502019-02-13 14:19:54 +0100845/**
846 * @brief Compile information from the import statement
847 * @param[in] ctx Compile context.
848 * @param[in] imp_p The parsed import statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100849 * @param[in,out] imp Prepared (empty) compiled import structure to fill.
850 * @return LY_ERR value.
851 */
Radek Krejci19a96102018-11-15 13:38:09 +0100852static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200853lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, struct lysc_import *imp)
Radek Krejci19a96102018-11-15 13:38:09 +0100854{
Radek Krejci19a96102018-11-15 13:38:09 +0100855 struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100856 LY_ERR ret = LY_SUCCESS;
857
858 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
Radek Krejci0935f412019-08-20 16:15:18 +0200859 COMPILE_EXTS_GOTO(ctx, imp_p->exts, imp->exts, imp, LYEXT_PAR_IMPORT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100860 imp->module = imp_p->module;
861
Radek Krejci7f2a5362018-11-28 13:05:37 +0100862 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
863 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100864 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100865 if (!imp->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100866 /* try to use filepath if present */
867 if (imp->module->filepath) {
868 mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath,
869 !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
Radek Krejci19a96102018-11-15 13:38:09 +0100870 if (mod != imp->module) {
871 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100872 imp->module->filepath, imp->module->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100873 mod = NULL;
874 }
875 }
876 if (!mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100877 if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, &mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100878 LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100879 imp->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100880 return LY_ENOTFOUND;
881 }
882 }
Radek Krejci19a96102018-11-15 13:38:09 +0100883 }
884
885done:
886 return ret;
887}
888
Radek Krejcib56c7502019-02-13 14:19:54 +0100889/**
890 * @brief Compile information from the identity statement
891 *
892 * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases().
893 *
894 * @param[in] ctx Compile context.
895 * @param[in] ident_p The parsed identity statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100896 * @param[in] idents List of so far compiled identities to check the name uniqueness.
897 * @param[in,out] ident Prepared (empty) compiled identity structure to fill.
898 * @return LY_ERR value.
899 */
Radek Krejci19a96102018-11-15 13:38:09 +0100900static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200901lys_compile_identity(struct lysc_ctx *ctx, struct lysp_ident *ident_p, struct lysc_ident *idents, struct lysc_ident *ident)
Radek Krejci19a96102018-11-15 13:38:09 +0100902{
903 unsigned int u;
904 LY_ERR ret = LY_SUCCESS;
905
Radek Krejci327de162019-06-14 12:52:07 +0200906 lysc_update_path(ctx, NULL, ident_p->name);
907
Radek Krejcid05cbd92018-12-05 14:26:40 +0100908 COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100909 DUP_STRING(ctx->ctx, ident_p->name, ident->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200910 DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc);
911 DUP_STRING(ctx->ctx, ident_p->ref, ident->ref);
Radek Krejci693262f2019-04-29 15:23:20 +0200912 ident->module = ctx->mod;
Radek Krejciec4da802019-05-02 13:02:41 +0200913 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100914 /* backlings (derived) can be added no sooner than when all the identities in the current module are present */
Radek Krejci0935f412019-08-20 16:15:18 +0200915 COMPILE_EXTS_GOTO(ctx, ident_p->exts, ident->exts, ident, LYEXT_PAR_IDENT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100916 ident->flags = ident_p->flags;
917
Radek Krejci327de162019-06-14 12:52:07 +0200918 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100919done:
920 return ret;
921}
922
Radek Krejcib56c7502019-02-13 14:19:54 +0100923/**
924 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
925 *
926 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
927 *
928 * @param[in] ctx Compile context for logging.
929 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
930 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
931 * @return LY_SUCCESS if everything is ok.
932 * @return LY_EVALID if the identity is derived from itself.
933 */
Radek Krejci38222632019-02-12 16:55:05 +0100934static LY_ERR
935lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
936{
937 LY_ERR ret = LY_EVALID;
938 unsigned int u, v;
939 struct ly_set recursion = {0};
940 struct lysc_ident *drv;
941
942 if (!derived) {
943 return LY_SUCCESS;
944 }
945
946 for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) {
947 if (ident == derived[u]) {
948 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
949 "Identity \"%s\" is indirectly derived from itself.", ident->name);
950 goto cleanup;
951 }
952 ly_set_add(&recursion, derived[u], 0);
953 }
954
955 for (v = 0; v < recursion.count; ++v) {
956 drv = recursion.objs[v];
957 if (!drv->derived) {
958 continue;
959 }
960 for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) {
961 if (ident == drv->derived[u]) {
962 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
963 "Identity \"%s\" is indirectly derived from itself.", ident->name);
964 goto cleanup;
965 }
966 ly_set_add(&recursion, drv->derived[u], 0);
967 }
968 }
969 ret = LY_SUCCESS;
970
971cleanup:
972 ly_set_erase(&recursion, NULL);
973 return ret;
974}
975
Radek Krejcia3045382018-11-22 14:30:31 +0100976/**
977 * @brief Find and process the referenced base identities from another identity or identityref
978 *
Radek Krejciaca74032019-06-04 08:53:06 +0200979 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +0100980 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
981 * to distinguish these two use cases.
982 *
983 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
984 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
985 * @param[in] ident Referencing identity to work with.
986 * @param[in] bases Array of bases of identityref to fill in.
987 * @return LY_ERR value.
988 */
Radek Krejci19a96102018-11-15 13:38:09 +0100989static LY_ERR
Radek Krejci555cb5b2018-11-16 14:54:33 +0100990lys_compile_identity_bases(struct lysc_ctx *ctx, const char **bases_p, struct lysc_ident *ident, struct lysc_ident ***bases)
Radek Krejci19a96102018-11-15 13:38:09 +0100991{
Radek Krejci555cb5b2018-11-16 14:54:33 +0100992 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +0100993 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +0100994 struct lys_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100995 struct lysc_ident **idref;
996
997 assert(ident || bases);
998
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100999 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001000 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1001 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
1002 return LY_EVALID;
1003 }
1004
1005 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
1006 s = strchr(bases_p[u], ':');
1007 if (s) {
1008 /* prefixed identity */
1009 name = &s[1];
Radek Krejcie86bf772018-12-14 11:39:53 +01001010 mod = lys_module_find_prefix(ctx->mod_def, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001011 } else {
1012 name = bases_p[u];
Radek Krejcie86bf772018-12-14 11:39:53 +01001013 mod = ctx->mod_def;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001014 }
1015 if (!mod) {
1016 if (ident) {
1017 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1018 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
1019 } else {
1020 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1021 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
1022 }
1023 return LY_EVALID;
1024 }
1025 idref = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001026 if (mod->compiled && mod->compiled->identities) {
1027 for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) {
1028 if (!strcmp(name, mod->compiled->identities[v].name)) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001029 if (ident) {
Radek Krejci38222632019-02-12 16:55:05 +01001030 if (ident == &mod->compiled->identities[v]) {
1031 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1032 "Identity \"%s\" is derived from itself.", ident->name);
1033 return LY_EVALID;
1034 }
1035 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->compiled->identities[v], ident->derived));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001036 /* we have match! store the backlink */
Radek Krejcie86bf772018-12-14 11:39:53 +01001037 LY_ARRAY_NEW_RET(ctx->ctx, mod->compiled->identities[v].derived, idref, LY_EMEM);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001038 *idref = ident;
1039 } else {
1040 /* we have match! store the found identity */
1041 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01001042 *idref = &mod->compiled->identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +01001043 }
1044 break;
1045 }
1046 }
1047 }
1048 if (!idref || !(*idref)) {
1049 if (ident) {
1050 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1051 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
1052 } else {
1053 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1054 "Unable to find base (%s) of identityref.", bases_p[u]);
1055 }
1056 return LY_EVALID;
1057 }
1058 }
1059 return LY_SUCCESS;
1060}
1061
Radek Krejcia3045382018-11-22 14:30:31 +01001062/**
1063 * @brief For the given array of identities, set the backlinks from all their base identities.
1064 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1065 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
1066 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
1067 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1068 */
Radek Krejci555cb5b2018-11-16 14:54:33 +01001069static LY_ERR
1070lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
1071{
1072 unsigned int i;
Radek Krejci19a96102018-11-15 13:38:09 +01001073
1074 for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) {
1075 if (!idents_p[i].bases) {
1076 continue;
1077 }
Radek Krejci327de162019-06-14 12:52:07 +02001078 lysc_update_path(ctx, NULL, idents[i].name);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001079 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL));
Radek Krejci327de162019-06-14 12:52:07 +02001080 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001081 }
1082 return LY_SUCCESS;
1083}
1084
Radek Krejci0af46292019-01-11 16:02:31 +01001085LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02001086lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module, struct lysp_feature *features_p, struct lysc_feature **features)
Radek Krejci0af46292019-01-11 16:02:31 +01001087{
1088 unsigned int offset = 0, u;
1089 struct lysc_ctx context = {0};
1090
Radek Krejci327de162019-06-14 12:52:07 +02001091 assert(ctx_sc || ctx);
1092
1093 if (!ctx_sc) {
1094 context.ctx = ctx;
1095 context.mod = module;
1096 context.path_len = 1;
1097 context.path[0] = '/';
1098 ctx_sc = &context;
1099 }
Radek Krejci0af46292019-01-11 16:02:31 +01001100
1101 if (!features_p) {
1102 return LY_SUCCESS;
1103 }
1104 if (*features) {
1105 offset = LY_ARRAY_SIZE(*features);
1106 }
1107
Radek Krejci327de162019-06-14 12:52:07 +02001108 lysc_update_path(ctx_sc, NULL, "{feature}");
1109 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM);
Radek Krejci0af46292019-01-11 16:02:31 +01001110 LY_ARRAY_FOR(features_p, u) {
Radek Krejci327de162019-06-14 12:52:07 +02001111 lysc_update_path(ctx_sc, NULL, features_p[u].name);
1112
Radek Krejci0af46292019-01-11 16:02:31 +01001113 LY_ARRAY_INCREMENT(*features);
Radek Krejci327de162019-06-14 12:52:07 +02001114 COMPILE_CHECK_UNIQUENESS(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
1115 DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name);
1116 DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc);
1117 DUP_STRING(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref);
Radek Krejci0af46292019-01-11 16:02:31 +01001118 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci327de162019-06-14 12:52:07 +02001119 (*features)[offset + u].module = ctx_sc->mod;
1120
1121 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001122 }
Radek Krejci327de162019-06-14 12:52:07 +02001123 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001124
1125 return LY_SUCCESS;
1126}
1127
Radek Krejcia3045382018-11-22 14:30:31 +01001128/**
Radek Krejci09a1fc52019-02-13 10:55:17 +01001129 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +01001130 *
1131 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
1132 *
Radek Krejci09a1fc52019-02-13 10:55:17 +01001133 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +01001134 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
1135 * being currently processed).
1136 * @param[in] depfeatures The list of depending features of the feature being currently processed (not the one provided as @p feature)
Radek Krejci09a1fc52019-02-13 10:55:17 +01001137 * @return LY_SUCCESS if everything is ok.
1138 * @return LY_EVALID if the feature references indirectly itself.
1139 */
1140static LY_ERR
1141lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
1142{
1143 LY_ERR ret = LY_EVALID;
1144 unsigned int u, v;
1145 struct ly_set recursion = {0};
1146 struct lysc_feature *drv;
1147
1148 if (!depfeatures) {
1149 return LY_SUCCESS;
1150 }
1151
1152 for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) {
1153 if (feature == depfeatures[u]) {
1154 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1155 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1156 goto cleanup;
1157 }
1158 ly_set_add(&recursion, depfeatures[u], 0);
1159 }
1160
1161 for (v = 0; v < recursion.count; ++v) {
1162 drv = recursion.objs[v];
1163 if (!drv->depfeatures) {
1164 continue;
1165 }
1166 for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) {
1167 if (feature == drv->depfeatures[u]) {
1168 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1169 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1170 goto cleanup;
1171 }
1172 ly_set_add(&recursion, drv->depfeatures[u], 0);
1173 }
1174 }
1175 ret = LY_SUCCESS;
1176
1177cleanup:
1178 ly_set_erase(&recursion, NULL);
1179 return ret;
1180}
1181
1182/**
Radek Krejci0af46292019-01-11 16:02:31 +01001183 * @brief Create pre-compiled features array.
1184 *
1185 * See lys_feature_precompile() for more details.
1186 *
Radek Krejcia3045382018-11-22 14:30:31 +01001187 * @param[in] ctx Compile context.
1188 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +01001189 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +01001190 * @return LY_ERR value.
1191 */
Radek Krejci19a96102018-11-15 13:38:09 +01001192static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001193lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +01001194{
Radek Krejci0af46292019-01-11 16:02:31 +01001195 unsigned int u, v, x;
1196 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +01001197 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01001198
Radek Krejci327de162019-06-14 12:52:07 +02001199
Radek Krejci0af46292019-01-11 16:02:31 +01001200 /* find the preprecompiled feature */
1201 LY_ARRAY_FOR(features, x) {
1202 if (strcmp(features[x].name, feature_p->name)) {
1203 continue;
1204 }
1205 feature = &features[x];
Radek Krejci327de162019-06-14 12:52:07 +02001206 lysc_update_path(ctx, NULL, "{feature}");
1207 lysc_update_path(ctx, NULL, feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001208
Radek Krejci0af46292019-01-11 16:02:31 +01001209 /* finish compilation started in lys_feature_precompile() */
Radek Krejci0935f412019-08-20 16:15:18 +02001210 COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02001211 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001212 if (feature->iffeatures) {
1213 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
1214 if (feature->iffeatures[u].features) {
1215 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001216 /* check for circular dependency - direct reference first,... */
1217 if (feature == feature->iffeatures[u].features[v]) {
1218 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1219 "Feature \"%s\" is referenced from itself.", feature->name);
1220 return LY_EVALID;
1221 }
1222 /* ... and indirect circular reference */
1223 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1224
Radek Krejci0af46292019-01-11 16:02:31 +01001225 /* add itself into the dependants list */
1226 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1227 *df = feature;
1228 }
Radek Krejci19a96102018-11-15 13:38:09 +01001229 }
Radek Krejci19a96102018-11-15 13:38:09 +01001230 }
1231 }
Radek Krejci327de162019-06-14 12:52:07 +02001232 lysc_update_path(ctx, NULL, NULL);
1233 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001234 done:
1235 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01001236 }
Radek Krejci0af46292019-01-11 16:02:31 +01001237
1238 LOGINT(ctx->ctx);
1239 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +01001240}
1241
Radek Krejcib56c7502019-02-13 14:19:54 +01001242/**
1243 * @brief Revert compiled list of features back to the precompiled state.
1244 *
1245 * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status.
1246 * The features are supposed to be stored again as off_features in ::lys_module structure.
1247 *
1248 * @param[in] ctx Compilation context.
1249 * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema
1250 * and supposed to hold the off_features list.
1251 */
Radek Krejci95710c92019-02-11 15:49:55 +01001252static void
1253lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1254{
1255 unsigned int u, v;
1256
1257 /* keep the off_features list until the complete lys_module is freed */
1258 mod->off_features = mod->compiled->features;
1259 mod->compiled->features = NULL;
1260
1261 /* in the off_features list, remove all the parts (from finished compiling process)
1262 * which may points into the data being freed here */
1263 LY_ARRAY_FOR(mod->off_features, u) {
1264 LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) {
1265 lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]);
1266 }
1267 LY_ARRAY_FREE(mod->off_features[u].iffeatures);
1268 mod->off_features[u].iffeatures = NULL;
1269
1270 LY_ARRAY_FOR(mod->off_features[u].exts, v) {
1271 lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]);
1272 }
1273 LY_ARRAY_FREE(mod->off_features[u].exts);
1274 mod->off_features[u].exts = NULL;
1275 }
1276}
1277
Radek Krejcia3045382018-11-22 14:30:31 +01001278/**
1279 * @brief Validate and normalize numeric value from a range definition.
1280 * @param[in] ctx Compile context.
1281 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
1282 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
1283 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
1284 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
1285 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1286 * @param[in] value String value of the range boundary.
1287 * @param[out] len Number of the processed bytes from the value. Processing stops on the first character which is not part of the number boundary.
1288 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1289 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1290 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001291LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001292range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value, size_t *len, char **valcopy)
Radek Krejci19a96102018-11-15 13:38:09 +01001293{
Radek Krejci6cba4292018-11-15 17:33:29 +01001294 size_t fraction = 0, size;
1295
Radek Krejci19a96102018-11-15 13:38:09 +01001296 *len = 0;
1297
1298 assert(value);
1299 /* parse value */
1300 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1301 return LY_EVALID;
1302 }
1303
1304 if ((value[*len] == '-') || (value[*len] == '+')) {
1305 ++(*len);
1306 }
1307
1308 while (isdigit(value[*len])) {
1309 ++(*len);
1310 }
1311
1312 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001313 if (basetype == LY_TYPE_DEC64) {
1314 goto decimal;
1315 } else {
1316 *valcopy = strndup(value, *len);
1317 return LY_SUCCESS;
1318 }
Radek Krejci19a96102018-11-15 13:38:09 +01001319 }
1320 fraction = *len;
1321
1322 ++(*len);
1323 while (isdigit(value[*len])) {
1324 ++(*len);
1325 }
1326
Radek Krejci6cba4292018-11-15 17:33:29 +01001327 if (basetype == LY_TYPE_DEC64) {
1328decimal:
1329 assert(frdigits);
Radek Krejci943177f2019-06-14 16:32:43 +02001330 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001331 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1332 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1333 *len, value, frdigits);
1334 return LY_EINVAL;
1335 }
1336 if (fraction) {
1337 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1338 } else {
1339 size = (*len) + frdigits + 1;
1340 }
1341 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001342 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1343
Radek Krejci6cba4292018-11-15 17:33:29 +01001344 (*valcopy)[size - 1] = '\0';
1345 if (fraction) {
1346 memcpy(&(*valcopy)[0], &value[0], fraction);
1347 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1348 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1349 } else {
1350 memcpy(&(*valcopy)[0], &value[0], *len);
1351 memset(&(*valcopy)[*len], '0', frdigits);
1352 }
Radek Krejci19a96102018-11-15 13:38:09 +01001353 }
1354 return LY_SUCCESS;
1355}
1356
Radek Krejcia3045382018-11-22 14:30:31 +01001357/**
1358 * @brief Check that values in range are in ascendant order.
1359 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001360 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1361 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001362 * @param[in] value Current value to check.
1363 * @param[in] prev_value The last seen value.
1364 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1365 */
Radek Krejci19a96102018-11-15 13:38:09 +01001366static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001367range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001368{
1369 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001370 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001371 return LY_EEXIST;
1372 }
1373 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001374 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001375 return LY_EEXIST;
1376 }
1377 }
1378 return LY_SUCCESS;
1379}
1380
Radek Krejcia3045382018-11-22 14:30:31 +01001381/**
1382 * @brief Set min/max value of the range part.
1383 * @param[in] ctx Compile context.
1384 * @param[in] part Range part structure to fill.
1385 * @param[in] max Flag to distinguish if storing min or max value.
1386 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1387 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1388 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1389 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1390 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001391 * @param[in] base_range Range from the type from which the current type is derived (if not built-in) to get type's min and max values.
Radek Krejcia3045382018-11-22 14:30:31 +01001392 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1393 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1394 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1395 * frdigits value), LY_EMEM.
1396 */
Radek Krejci19a96102018-11-15 13:38:09 +01001397static LY_ERR
1398range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, int max, int64_t prev, LY_DATA_TYPE basetype, int first, int length_restr,
Radek Krejci5969f272018-11-23 10:03:58 +01001399 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001400{
1401 LY_ERR ret = LY_SUCCESS;
1402 char *valcopy = NULL;
1403 size_t len;
1404
1405 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001406 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001407 LY_CHECK_GOTO(ret, finalize);
1408 }
1409 if (!valcopy && base_range) {
1410 if (max) {
1411 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
1412 } else {
1413 part->min_64 = base_range->parts[0].min_64;
1414 }
1415 if (!first) {
1416 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1417 }
1418 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001419 }
1420
1421 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001422 case LY_TYPE_INT8: /* range */
1423 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001424 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64);
Radek Krejci19a96102018-11-15 13:38:09 +01001425 } else if (max) {
1426 part->max_64 = INT64_C(127);
1427 } else {
1428 part->min_64 = INT64_C(-128);
1429 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001430 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001431 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001432 }
1433 break;
1434 case LY_TYPE_INT16: /* range */
1435 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001436 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64);
Radek Krejci19a96102018-11-15 13:38:09 +01001437 } else if (max) {
1438 part->max_64 = INT64_C(32767);
1439 } else {
1440 part->min_64 = INT64_C(-32768);
1441 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001442 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001443 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001444 }
1445 break;
1446 case LY_TYPE_INT32: /* range */
1447 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001448 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64);
Radek Krejci19a96102018-11-15 13:38:09 +01001449 } else if (max) {
1450 part->max_64 = INT64_C(2147483647);
1451 } else {
1452 part->min_64 = INT64_C(-2147483648);
1453 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001454 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001455 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001456 }
1457 break;
1458 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001459 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001460 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001461 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001462 max ? &part->max_64 : &part->min_64);
1463 } else if (max) {
1464 part->max_64 = INT64_C(9223372036854775807);
1465 } else {
1466 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1467 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001468 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001469 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001470 }
1471 break;
1472 case LY_TYPE_UINT8: /* range */
1473 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001474 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64);
Radek Krejci19a96102018-11-15 13:38:09 +01001475 } else if (max) {
1476 part->max_u64 = UINT64_C(255);
1477 } else {
1478 part->min_u64 = UINT64_C(0);
1479 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001480 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001481 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001482 }
1483 break;
1484 case LY_TYPE_UINT16: /* range */
1485 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001486 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64);
Radek Krejci19a96102018-11-15 13:38:09 +01001487 } else if (max) {
1488 part->max_u64 = UINT64_C(65535);
1489 } else {
1490 part->min_u64 = UINT64_C(0);
1491 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001492 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001493 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001494 }
1495 break;
1496 case LY_TYPE_UINT32: /* range */
1497 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001498 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64);
Radek Krejci19a96102018-11-15 13:38:09 +01001499 } else if (max) {
1500 part->max_u64 = UINT64_C(4294967295);
1501 } else {
1502 part->min_u64 = UINT64_C(0);
1503 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001504 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001505 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001506 }
1507 break;
1508 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001509 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001510 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001511 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001512 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64);
Radek Krejci19a96102018-11-15 13:38:09 +01001513 } else if (max) {
1514 part->max_u64 = UINT64_C(18446744073709551615);
1515 } else {
1516 part->min_u64 = UINT64_C(0);
1517 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001518 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001519 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001520 }
1521 break;
1522 default:
1523 LOGINT(ctx->ctx);
1524 ret = LY_EINT;
1525 }
1526
Radek Krejci5969f272018-11-23 10:03:58 +01001527finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001528 if (ret == LY_EDENIED) {
1529 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1530 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1531 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1532 } else if (ret == LY_EVALID) {
1533 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1534 "Invalid %s restriction - invalid value \"%s\".",
1535 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1536 } else if (ret == LY_EEXIST) {
1537 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1538 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001539 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001540 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001541 } else if (!ret && value) {
1542 *value = *value + len;
1543 }
1544 free(valcopy);
1545 return ret;
1546}
1547
Radek Krejcia3045382018-11-22 14:30:31 +01001548/**
1549 * @brief Compile the parsed range restriction.
1550 * @param[in] ctx Compile context.
1551 * @param[in] range_p Parsed range structure to compile.
1552 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1553 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1554 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1555 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1556 * range restriction must be more restrictive than the base_range.
1557 * @param[in,out] range Pointer to the created current range structure.
1558 * @return LY_ERR value.
1559 */
Radek Krejci19a96102018-11-15 13:38:09 +01001560static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001561lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, int length_restr, uint8_t frdigits,
Radek Krejci19a96102018-11-15 13:38:09 +01001562 struct lysc_range *base_range, struct lysc_range **range)
1563{
1564 LY_ERR ret = LY_EVALID;
1565 const char *expr;
1566 struct lysc_range_part *parts = NULL, *part;
1567 int range_expected = 0, uns;
1568 unsigned int parts_done = 0, u, v;
1569
1570 assert(range);
1571 assert(range_p);
1572
1573 expr = range_p->arg;
1574 while(1) {
1575 if (isspace(*expr)) {
1576 ++expr;
1577 } else if (*expr == '\0') {
1578 if (range_expected) {
1579 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1580 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1581 length_restr ? "length" : "range", range_p->arg);
1582 goto cleanup;
1583 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
1584 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1585 "Invalid %s restriction - unexpected end of the expression (%s).",
1586 length_restr ? "length" : "range", range_p->arg);
1587 goto cleanup;
1588 }
1589 parts_done++;
1590 break;
1591 } else if (!strncmp(expr, "min", 3)) {
1592 if (parts) {
1593 /* min cannot be used elsewhere than in the first part */
1594 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1595 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1596 expr - range_p->arg, range_p->arg);
1597 goto cleanup;
1598 }
1599 expr += 3;
1600
1601 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001602 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, 0, basetype, 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001603 part->max_64 = part->min_64;
1604 } else if (*expr == '|') {
1605 if (!parts || range_expected) {
1606 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1607 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1608 goto cleanup;
1609 }
1610 expr++;
1611 parts_done++;
1612 /* process next part of the expression */
1613 } else if (!strncmp(expr, "..", 2)) {
1614 expr += 2;
1615 while (isspace(*expr)) {
1616 expr++;
1617 }
1618 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
1619 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1620 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1621 goto cleanup;
1622 }
1623 /* continue expecting the upper boundary */
1624 range_expected = 1;
1625 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1626 /* number */
1627 if (range_expected) {
1628 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001629 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001630 range_expected = 0;
1631 } else {
1632 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1633 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0,
Radek Krejci5969f272018-11-23 10:03:58 +01001634 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001635 part->max_64 = part->min_64;
1636 }
1637
1638 /* continue with possible another expression part */
1639 } else if (!strncmp(expr, "max", 3)) {
1640 expr += 3;
1641 while (isspace(*expr)) {
1642 expr++;
1643 }
1644 if (*expr != '\0') {
1645 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1646 length_restr ? "length" : "range", expr);
1647 goto cleanup;
1648 }
1649 if (range_expected) {
1650 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001651 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001652 range_expected = 0;
1653 } else {
1654 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1655 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0,
Radek Krejci5969f272018-11-23 10:03:58 +01001656 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001657 part->min_64 = part->max_64;
1658 }
1659 } else {
1660 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1661 length_restr ? "length" : "range", expr);
1662 goto cleanup;
1663 }
1664 }
1665
1666 /* check with the previous range/length restriction */
1667 if (base_range) {
1668 switch (basetype) {
1669 case LY_TYPE_BINARY:
1670 case LY_TYPE_UINT8:
1671 case LY_TYPE_UINT16:
1672 case LY_TYPE_UINT32:
1673 case LY_TYPE_UINT64:
1674 case LY_TYPE_STRING:
1675 uns = 1;
1676 break;
1677 case LY_TYPE_DEC64:
1678 case LY_TYPE_INT8:
1679 case LY_TYPE_INT16:
1680 case LY_TYPE_INT32:
1681 case LY_TYPE_INT64:
1682 uns = 0;
1683 break;
1684 default:
1685 LOGINT(ctx->ctx);
1686 ret = LY_EINT;
1687 goto cleanup;
1688 }
1689 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1690 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1691 goto baseerror;
1692 }
1693 /* current lower bound is not lower than the base */
1694 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1695 /* base has single value */
1696 if (base_range->parts[v].min_64 == parts[u].min_64) {
1697 /* both lower bounds are the same */
1698 if (parts[u].min_64 != parts[u].max_64) {
1699 /* current continues with a range */
1700 goto baseerror;
1701 } else {
1702 /* equal single values, move both forward */
1703 ++v;
1704 continue;
1705 }
1706 } else {
1707 /* base is single value lower than current range, so the
1708 * value from base range is removed in the current,
1709 * move only base and repeat checking */
1710 ++v;
1711 --u;
1712 continue;
1713 }
1714 } else {
1715 /* base is the range */
1716 if (parts[u].min_64 == parts[u].max_64) {
1717 /* current is a single value */
1718 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1719 /* current is behind the base range, so base range is omitted,
1720 * move the base and keep the current for further check */
1721 ++v;
1722 --u;
1723 } /* else it is within the base range, so move the current, but keep the base */
1724 continue;
1725 } else {
1726 /* both are ranges - check the higher bound, the lower was already checked */
1727 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1728 /* higher bound is higher than the current higher bound */
1729 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1730 /* but the current lower bound is also higher, so the base range is omitted,
1731 * continue with the same current, but move the base */
1732 --u;
1733 ++v;
1734 continue;
1735 }
1736 /* current range starts within the base range but end behind it */
1737 goto baseerror;
1738 } else {
1739 /* current range is smaller than the base,
1740 * move current, but stay with the base */
1741 continue;
1742 }
1743 }
1744 }
1745 }
1746 if (u != parts_done) {
1747baseerror:
1748 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1749 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1750 length_restr ? "length" : "range", range_p->arg);
1751 goto cleanup;
1752 }
1753 }
1754
1755 if (!(*range)) {
1756 *range = calloc(1, sizeof **range);
1757 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1758 }
1759
Radek Krejcic8b31002019-01-08 10:24:45 +01001760 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001761 if (range_p->eapptag) {
1762 lydict_remove(ctx->ctx, (*range)->eapptag);
1763 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1764 }
1765 if (range_p->emsg) {
1766 lydict_remove(ctx->ctx, (*range)->emsg);
1767 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1768 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001769 if (range_p->dsc) {
1770 lydict_remove(ctx->ctx, (*range)->dsc);
1771 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1772 }
1773 if (range_p->ref) {
1774 lydict_remove(ctx->ctx, (*range)->ref);
1775 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1776 }
Radek Krejci19a96102018-11-15 13:38:09 +01001777 /* extensions are taken only from the last range by the caller */
1778
1779 (*range)->parts = parts;
1780 parts = NULL;
1781 ret = LY_SUCCESS;
1782cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001783 LY_ARRAY_FREE(parts);
1784
1785 return ret;
1786}
1787
1788/**
1789 * @brief Checks pattern syntax.
1790 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02001791 * @param[in] ctx Context.
1792 * @param[in] log_path Path for logging errors.
Radek Krejci19a96102018-11-15 13:38:09 +01001793 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001794 * @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 +01001795 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001796 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001797LY_ERR
1798lys_compile_type_pattern_check(struct ly_ctx *ctx, const char *log_path, const char *pattern, pcre2_code **code)
Radek Krejci19a96102018-11-15 13:38:09 +01001799{
Radek Krejci54579462019-04-30 12:47:06 +02001800 int idx, idx2, start, end, count;
Radek Krejci19a96102018-11-15 13:38:09 +01001801 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001802 int err_code;
1803 const char *orig_ptr;
1804 PCRE2_SIZE err_offset;
1805 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001806#define URANGE_LEN 19
1807 char *ublock2urange[][2] = {
1808 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1809 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1810 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1811 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1812 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1813 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1814 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1815 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1816 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1817 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1818 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1819 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1820 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1821 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1822 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1823 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1824 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1825 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1826 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1827 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1828 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1829 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1830 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1831 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1832 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1833 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1834 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1835 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1836 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1837 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1838 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1839 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1840 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1841 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1842 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1843 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1844 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1845 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1846 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1847 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1848 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1849 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1850 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1851 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1852 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1853 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1854 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1855 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1856 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1857 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1858 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1859 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1860 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1861 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1862 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1863 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1864 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1865 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1866 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1867 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1868 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1869 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1870 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1871 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1872 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1873 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1874 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1875 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1876 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1877 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1878 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1879 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1880 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1881 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1882 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1883 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1884 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1885 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1886 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1887 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1888 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1889 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1890 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1891 {NULL, NULL}
1892 };
1893
1894 /* adjust the expression to a Perl equivalent
1895 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1896
Michal Vasko03ff5a72019-09-11 13:49:33 +02001897 /* we need to replace all "$" and "^" with "\$" and "\^", count them now */
Radek Krejci19a96102018-11-15 13:38:09 +01001898 for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$"));
1899
1900 perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001901 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01001902 perl_regex[0] = '\0';
1903
1904 ptr = perl_regex;
1905
Radek Krejci19a96102018-11-15 13:38:09 +01001906 for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) {
1907 if (orig_ptr[0] == '$') {
1908 ptr += sprintf(ptr, "\\$");
1909 } else if (orig_ptr[0] == '^') {
1910 ptr += sprintf(ptr, "\\^");
1911 } else {
1912 ptr[0] = orig_ptr[0];
1913 ++ptr;
1914 }
1915 }
Radek Krejci5819f7c2019-05-31 14:53:29 +02001916 ptr[0] = '\0';
1917 ++ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01001918
1919 /* substitute Unicode Character Blocks with exact Character Ranges */
1920 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1921 start = ptr - perl_regex;
1922
1923 ptr = strchr(ptr, '}');
1924 if (!ptr) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001925 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01001926 pattern, perl_regex + start + 2, "unterminated character property");
1927 free(perl_regex);
1928 return LY_EVALID;
1929 }
1930 end = (ptr - perl_regex) + 1;
1931
1932 /* need more space */
1933 if (end - start < URANGE_LEN) {
1934 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001935 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01001936 }
1937
1938 /* find our range */
1939 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1940 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1941 break;
1942 }
1943 }
1944 if (!ublock2urange[idx][0]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001945 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01001946 pattern, perl_regex + start + 5, "unknown block name");
1947 free(perl_regex);
1948 return LY_EVALID;
1949 }
1950
1951 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1952 for (idx2 = 0, count = 0; idx2 < start; ++idx2) {
1953 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1954 ++count;
1955 }
1956 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1957 --count;
1958 }
1959 }
1960 if (count) {
1961 /* skip brackets */
1962 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1963 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1964 } else {
1965 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1966 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1967 }
1968 }
1969
1970 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02001971 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
1972 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02001973 &err_code, &err_offset, NULL);
1974 if (!code_local) {
1975 PCRE2_UCHAR err_msg[256] = {0};
1976 pcre2_get_error_message(err_code, err_msg, 256);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001977 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Radek Krejci19a96102018-11-15 13:38:09 +01001978 free(perl_regex);
1979 return LY_EVALID;
1980 }
1981 free(perl_regex);
1982
Radek Krejci54579462019-04-30 12:47:06 +02001983 if (code) {
1984 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001985 } else {
Radek Krejci54579462019-04-30 12:47:06 +02001986 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01001987 }
1988
1989 return LY_SUCCESS;
1990
1991#undef URANGE_LEN
1992}
1993
Radek Krejcia3045382018-11-22 14:30:31 +01001994/**
1995 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1996 * @param[in] ctx Compile context.
1997 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01001998 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1999 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
2000 * @param[out] patterns Pointer to the storage for the patterns of the current type.
2001 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
2002 */
Radek Krejci19a96102018-11-15 13:38:09 +01002003static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002004lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci19a96102018-11-15 13:38:09 +01002005 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
2006{
2007 struct lysc_pattern **pattern;
Radek Krejci0935f412019-08-20 16:15:18 +02002008 unsigned int u;
Radek Krejci19a96102018-11-15 13:38:09 +01002009 LY_ERR ret = LY_SUCCESS;
2010
2011 /* first, copy the patterns from the base type */
2012 if (base_patterns) {
2013 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
2014 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
2015 }
2016
2017 LY_ARRAY_FOR(patterns_p, u) {
2018 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
2019 *pattern = calloc(1, sizeof **pattern);
2020 ++(*pattern)->refcount;
2021
Michal Vasko03ff5a72019-09-11 13:49:33 +02002022 ret = lys_compile_type_pattern_check(ctx->ctx, ctx->path, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01002023 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01002024
2025 if (patterns_p[u].arg[0] == 0x15) {
2026 (*pattern)->inverted = 1;
2027 }
Radek Krejci54579462019-04-30 12:47:06 +02002028 DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +01002029 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
2030 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01002031 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
2032 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejci0935f412019-08-20 16:15:18 +02002033 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002034 }
2035done:
2036 return ret;
2037}
2038
Radek Krejcia3045382018-11-22 14:30:31 +01002039/**
2040 * @brief map of the possible restrictions combination for the specific built-in type.
2041 */
Radek Krejci19a96102018-11-15 13:38:09 +01002042static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
2043 0 /* LY_TYPE_UNKNOWN */,
2044 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01002045 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
2046 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
2047 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
2048 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
2049 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01002050 LYS_SET_BIT /* LY_TYPE_BITS */,
2051 0 /* LY_TYPE_BOOL */,
2052 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
2053 0 /* LY_TYPE_EMPTY */,
2054 LYS_SET_ENUM /* LY_TYPE_ENUM */,
2055 LYS_SET_BASE /* LY_TYPE_IDENT */,
2056 LYS_SET_REQINST /* LY_TYPE_INST */,
2057 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01002058 LYS_SET_TYPE /* LY_TYPE_UNION */,
2059 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002060 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002061 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01002062 LYS_SET_RANGE /* LY_TYPE_INT64 */
2063};
2064
2065/**
2066 * @brief stringification of the YANG built-in data types
2067 */
2068const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
2069 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
2070 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01002071};
2072
Radek Krejcia3045382018-11-22 14:30:31 +01002073/**
2074 * @brief Compile parsed type's enum structures (for enumeration and bits types).
2075 * @param[in] ctx Compile context.
2076 * @param[in] enums_p Array of the parsed enum structures to compile.
2077 * @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 +01002078 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
2079 * @param[out] enums Newly created array of the compiled enums information for the current type.
2080 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2081 */
Radek Krejci19a96102018-11-15 13:38:09 +01002082static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002083lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci693262f2019-04-29 15:23:20 +02002084 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01002085{
2086 LY_ERR ret = LY_SUCCESS;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02002087 unsigned int u, v, match = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002088 int32_t value = 0;
2089 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02002090 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01002091
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002092 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002093 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
2094 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
2095 return LY_EVALID;
2096 }
2097
2098 LY_ARRAY_FOR(enums_p, u) {
2099 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
2100 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01002101 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
2102 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci693262f2019-04-29 15:23:20 +02002103 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01002104 if (base_enums) {
2105 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
2106 LY_ARRAY_FOR(base_enums, v) {
2107 if (!strcmp(e->name, base_enums[v].name)) {
2108 break;
2109 }
2110 }
2111 if (v == LY_ARRAY_SIZE(base_enums)) {
2112 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2113 "Invalid %s - derived type adds new item \"%s\".",
2114 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
2115 return LY_EVALID;
2116 }
2117 match = v;
2118 }
2119
2120 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02002121 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01002122 if (enums_p[u].flags & LYS_SET_VALUE) {
2123 e->value = (int32_t)enums_p[u].value;
2124 if (!u || e->value >= value) {
2125 value = e->value + 1;
2126 }
2127 /* check collision with other values */
2128 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
2129 if (e->value == (*enums)[v].value) {
2130 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2131 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
2132 e->value, e->name, (*enums)[v].name);
2133 return LY_EVALID;
2134 }
2135 }
2136 } else if (base_enums) {
2137 /* inherit the assigned value */
2138 e->value = base_enums[match].value;
2139 if (!u || e->value >= value) {
2140 value = e->value + 1;
2141 }
2142 } else {
2143 /* assign value automatically */
2144 if (u && value == INT32_MIN) {
2145 /* counter overflow */
2146 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2147 "Invalid enumeration - it is not possible to auto-assign enum value for "
2148 "\"%s\" since the highest value is already 2147483647.", e->name);
2149 return LY_EVALID;
2150 }
2151 e->value = value++;
2152 }
2153 } else { /* LY_TYPE_BITS */
2154 if (enums_p[u].flags & LYS_SET_VALUE) {
2155 e->value = (int32_t)enums_p[u].value;
2156 if (!u || (uint32_t)e->value >= position) {
2157 position = (uint32_t)e->value + 1;
2158 }
2159 /* check collision with other values */
2160 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
2161 if (e->value == (*enums)[v].value) {
2162 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2163 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
2164 (uint32_t)e->value, e->name, (*enums)[v].name);
2165 return LY_EVALID;
2166 }
2167 }
2168 } else if (base_enums) {
2169 /* inherit the assigned value */
2170 e->value = base_enums[match].value;
2171 if (!u || (uint32_t)e->value >= position) {
2172 position = (uint32_t)e->value + 1;
2173 }
2174 } else {
2175 /* assign value automatically */
2176 if (u && position == 0) {
2177 /* counter overflow */
2178 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2179 "Invalid bits - it is not possible to auto-assign bit position for "
2180 "\"%s\" since the highest value is already 4294967295.", e->name);
2181 return LY_EVALID;
2182 }
2183 e->value = position++;
2184 }
2185 }
2186
2187 if (base_enums) {
2188 /* the assigned values must not change from the derived type */
2189 if (e->value != base_enums[match].value) {
2190 if (basetype == LY_TYPE_ENUM) {
2191 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2192 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
2193 e->name, base_enums[match].value, e->value);
2194 } else {
2195 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2196 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
2197 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
2198 }
2199 return LY_EVALID;
2200 }
2201 }
2202
Radek Krejciec4da802019-05-02 13:02:41 +02002203 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002204 COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, basetype == LY_TYPE_ENUM ? LYEXT_PAR_TYPE_ENUM : LYEXT_PAR_TYPE_BIT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002205
2206 if (basetype == LY_TYPE_BITS) {
2207 /* keep bits ordered by position */
2208 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
2209 if (v != u) {
2210 memcpy(&storage, e, sizeof *e);
2211 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
2212 memcpy(&(*enums)[v], &storage, sizeof storage);
2213 }
2214 }
2215 }
2216
2217done:
2218 return ret;
2219}
2220
Radek Krejcia3045382018-11-22 14:30:31 +01002221#define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \
2222 for ((NODE) = (NODE)->parent; \
Michal Vasko03ff5a72019-09-11 13:49:33 +02002223 (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_NOTIF | LYS_ACTION)); \
Radek Krejcia3045382018-11-22 14:30:31 +01002224 (NODE) = (NODE)->parent); \
2225 if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \
2226 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \
2227 TERM; \
2228 }
2229
2230/**
2231 * @brief Validate the predicate(s) from the leafref path.
2232 * @param[in] ctx Compile context
2233 * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s).
2234 * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated.
Radek Krejci4ce68932018-11-28 12:53:36 +01002235 * @param[in] start_node Path context node (where the path is instantiated).
Radek Krejcia3045382018-11-22 14:30:31 +01002236 * @param[in] context_node Predicate context node (where the predicate is placed).
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002237 * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes.
Radek Krejcia3045382018-11-22 14:30:31 +01002238 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2239 */
2240static LY_ERR
Radek Krejci4ce68932018-11-28 12:53:36 +01002241lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate, const struct lysc_node *start_node,
Radek Krejcibade82a2018-12-05 10:13:48 +01002242 const struct lysc_node_list *context_node, const struct lys_module *path_context)
Radek Krejcia3045382018-11-22 14:30:31 +01002243{
2244 LY_ERR ret = LY_EVALID;
2245 const struct lys_module *mod;
2246 const struct lysc_node *src_node, *dst_node;
2247 const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix;
2248 size_t src_len, src_prefix_len, dst_len, dst_prefix_len;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002249 unsigned int dest_parent_times, c;
Radek Krejcia3045382018-11-22 14:30:31 +01002250 const char *start, *end, *pke_end;
2251 struct ly_set keys = {0};
2252 int i;
2253
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002254 assert(path_context);
2255
Radek Krejcia3045382018-11-22 14:30:31 +01002256 while (**predicate == '[') {
2257 start = (*predicate)++;
2258
2259 while (isspace(**predicate)) {
2260 ++(*predicate);
2261 }
Radek Krejcib4a4a272019-06-10 12:44:52 +02002262 LY_CHECK_GOTO(ly_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup);
Radek Krejcia3045382018-11-22 14:30:31 +01002263 while (isspace(**predicate)) {
2264 ++(*predicate);
2265 }
2266 if (**predicate != '=') {
2267 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002268 "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.",
2269 *predicate - start + 1, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002270 goto cleanup;
2271 }
2272 ++(*predicate);
2273 while (isspace(**predicate)) {
2274 ++(*predicate);
2275 }
2276
2277 if ((end = pke_end = strchr(*predicate, ']')) == NULL) {
2278 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2279 "Invalid leafref path predicate \"%s\" - missing predicate termination.", start);
2280 goto cleanup;
2281 }
2282 --pke_end;
2283 while (isspace(*pke_end)) {
2284 --pke_end;
2285 }
Radek Krejci7adf4ff2018-12-05 08:55:00 +01002286 ++pke_end;
Radek Krejcia3045382018-11-22 14:30:31 +01002287 /* localize path-key-expr */
2288 pke_start = path_key_expr = *predicate;
2289 /* move after the current predicate */
2290 *predicate = end + 1;
2291
2292 /* source (must be leaf or leaf-list) */
2293 if (src_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002294 mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002295 if (!mod) {
2296 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2297 "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002298 *predicate - start, start, src_prefix_len, src_prefix, path_context->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002299 goto cleanup;
2300 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002301 if (!mod->implemented) {
2302 /* make the module implemented */
Radek Krejci77a8bcd2019-09-11 11:20:02 +02002303 lys_set_implemented_internal((struct lys_module*)mod, 2);
Radek Krejci92cc8512019-04-25 09:57:06 +02002304 }
Radek Krejcia3045382018-11-22 14:30:31 +01002305 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002306 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002307 }
Radek Krejcibade82a2018-12-05 10:13:48 +01002308 src_node = NULL;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002309 if (!(context_node->flags & LYS_KEYLESS)) {
2310 struct lysc_node *key;
2311 for (key = context_node->child; key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY); key = key->next) {
Radek Krejci7f9b6512019-09-18 13:11:09 +02002312 if (!ly_strncmp(key->name, src, src_len)) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02002313 src_node = key;
Radek Krejcibade82a2018-12-05 10:13:48 +01002314 break;
2315 }
2316 }
2317 }
Radek Krejcia3045382018-11-22 14:30:31 +01002318 if (!src_node) {
2319 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002320 "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002321 *predicate - start, start, src_len, src, mod->name);
Radek Krejcia3045382018-11-22 14:30:31 +01002322 goto cleanup;
2323 }
Radek Krejcia3045382018-11-22 14:30:31 +01002324
2325 /* check that there is only one predicate for the */
Radek Krejcibade82a2018-12-05 10:13:48 +01002326 c = keys.count;
Radek Krejcia3045382018-11-22 14:30:31 +01002327 i = ly_set_add(&keys, (void*)src_node, 0);
2328 LY_CHECK_GOTO(i == -1, cleanup);
Radek Krejcibade82a2018-12-05 10:13:48 +01002329 if (keys.count == c) { /* node was already present in the set */
Radek Krejcia3045382018-11-22 14:30:31 +01002330 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002331 "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".",
Radek Krejcia3045382018-11-22 14:30:31 +01002332 *predicate - start, start, src_node->name);
2333 goto cleanup;
2334 }
2335
2336 /* destination */
2337 dest_parent_times = 0;
Radek Krejci4ce68932018-11-28 12:53:36 +01002338 dst_node = start_node;
Radek Krejcia3045382018-11-22 14:30:31 +01002339
2340 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002341 if (strncmp(path_key_expr, "current", 7)) {
2342error_current_function_invocation:
Radek Krejcia3045382018-11-22 14:30:31 +01002343 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2344 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
2345 *predicate - start, start);
2346 goto cleanup;
2347 }
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002348 for (path_key_expr += 7; isspace(*path_key_expr); ++path_key_expr);
2349 if (*path_key_expr != '(') {
2350 goto error_current_function_invocation;
Radek Krejcia3045382018-11-22 14:30:31 +01002351 }
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002352 for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr);
2353 if (*path_key_expr != ')') {
2354 goto error_current_function_invocation;
2355 }
2356 for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr);
Radek Krejcia3045382018-11-22 14:30:31 +01002357
2358 if (*path_key_expr != '/') {
2359 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2360 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
2361 *predicate - start, start);
2362 goto cleanup;
2363 }
2364 ++path_key_expr;
2365 while (isspace(*path_key_expr)) {
2366 ++path_key_expr;
2367 }
2368
2369 /* rel-path-keyexpr:
2370 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
2371 while (!strncmp(path_key_expr, "..", 2)) {
2372 ++dest_parent_times;
2373 path_key_expr += 2;
2374 while (isspace(*path_key_expr)) {
2375 ++path_key_expr;
2376 }
2377 if (*path_key_expr != '/') {
2378 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2379 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
2380 *predicate - start, start);
2381 goto cleanup;
2382 }
2383 ++path_key_expr;
2384 while (isspace(*path_key_expr)) {
2385 ++path_key_expr;
2386 }
2387
2388 /* path is supposed to be evaluated in data tree, so we have to skip
2389 * all schema nodes that cannot be instantiated in data tree */
2390 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
2391 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
2392 *predicate - start, start);
2393 }
2394 if (!dest_parent_times) {
2395 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2396 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
2397 *predicate - start, start);
2398 goto cleanup;
2399 }
2400 if (path_key_expr == pke_end) {
2401 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2402 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
2403 *predicate - start, start);
2404 goto cleanup;
2405 }
2406
2407 while(path_key_expr != pke_end) {
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002408 for (;*path_key_expr == '/' || isspace(*path_key_expr); ++path_key_expr);
Radek Krejcib4a4a272019-06-10 12:44:52 +02002409 if (ly_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +01002410 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcibade82a2018-12-05 10:13:48 +01002411 "Invalid node identifier in leafref path predicate - character %d (of %.*s).",
2412 path_key_expr - start + 1, *predicate - start, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002413 goto cleanup;
2414 }
2415
2416 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002417 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002418 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002419 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002420 }
2421 if (!mod) {
2422 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002423 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002424 *predicate - start, start, dst_len, dst);
2425 goto cleanup;
2426 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002427 if (!mod->implemented) {
2428 /* make the module implemented */
Radek Krejci77a8bcd2019-09-11 11:20:02 +02002429 lys_set_implemented_internal((struct lys_module*)mod, 2);
Radek Krejci92cc8512019-04-25 09:57:06 +02002430 }
Radek Krejcia3045382018-11-22 14:30:31 +01002431
2432 dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
2433 if (!dst_node) {
2434 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002435 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002436 *predicate - start, start, path_key_expr - pke_start, pke_start);
2437 goto cleanup;
2438 }
2439 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002440 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 +01002441 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002442 "Invalid leafref path predicate \"%.*s\" - rel-path-keyexpr \"%.*s\" refers %s instead of leaf.",
Radek Krejcia3045382018-11-22 14:30:31 +01002443 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
2444 goto cleanup;
2445 }
2446 }
2447
2448 ret = LY_SUCCESS;
2449cleanup:
2450 ly_set_erase(&keys, NULL);
2451 return ret;
2452}
2453
2454/**
2455 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2456 *
2457 * path-arg = absolute-path / relative-path
2458 * absolute-path = 1*("/" (node-identifier *path-predicate))
2459 * relative-path = 1*(".." "/") descendant-path
2460 *
2461 * @param[in,out] path Path to parse.
2462 * @param[out] prefix Prefix of the token, NULL if there is not any.
2463 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2464 * @param[out] name Name of the token.
2465 * @param[out] nam_len Length of the name.
2466 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2467 * must not be changed between consecutive calls. -1 if the
2468 * path is absolute.
2469 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2470 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2471 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002472LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002473lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2474 int *parent_times, int *has_predicate)
2475{
2476 int par_times = 0;
2477
2478 assert(path && *path);
2479 assert(parent_times);
2480 assert(prefix);
2481 assert(prefix_len);
2482 assert(name);
2483 assert(name_len);
2484 assert(has_predicate);
2485
2486 *prefix = NULL;
2487 *prefix_len = 0;
2488 *name = NULL;
2489 *name_len = 0;
2490 *has_predicate = 0;
2491
2492 if (!*parent_times) {
2493 if (!strncmp(*path, "..", 2)) {
2494 *path += 2;
2495 ++par_times;
2496 while (!strncmp(*path, "/..", 3)) {
2497 *path += 3;
2498 ++par_times;
2499 }
2500 }
2501 if (par_times) {
2502 *parent_times = par_times;
2503 } else {
2504 *parent_times = -1;
2505 }
2506 }
2507
2508 if (**path != '/') {
2509 return LY_EINVAL;
2510 }
2511 /* skip '/' */
2512 ++(*path);
2513
2514 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002515 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002516
2517 if ((**path == '/' && (*path)[1]) || !**path) {
2518 /* path continues by another token or this is the last token */
2519 return LY_SUCCESS;
2520 } else if ((*path)[0] != '[') {
2521 /* unexpected character */
2522 return LY_EINVAL;
2523 } else {
2524 /* predicate starting with [ */
2525 *has_predicate = 1;
2526 return LY_SUCCESS;
2527 }
2528}
2529
2530/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002531 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2532 *
2533 * The set of features used for target must be a subset of features used for the leafref.
2534 * This is not a perfect, we should compare the truth tables but it could require too much resources
2535 * and RFC 7950 does not require it explicitely, so we simplify that.
2536 *
2537 * @param[in] refnode The leafref node.
2538 * @param[in] target Tha target node of the leafref.
2539 * @return LY_SUCCESS or LY_EVALID;
2540 */
2541static LY_ERR
2542lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2543{
2544 LY_ERR ret = LY_EVALID;
2545 const struct lysc_node *iter;
Radek Krejci58d171e2018-11-23 13:50:55 +01002546 unsigned int u, v, count;
2547 struct ly_set features = {0};
2548
2549 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002550 if (iter->iffeatures) {
2551 LY_ARRAY_FOR(iter->iffeatures, u) {
2552 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2553 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002554 }
2555 }
2556 }
2557 }
2558
2559 /* we should have, in features set, a superset of features applicable to the target node.
2560 * So when adding features applicable to the target into the features set, we should not be
2561 * able to actually add any new feature, otherwise it is not a subset of features applicable
2562 * to the leafref itself. */
2563 count = features.count;
2564 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002565 if (iter->iffeatures) {
2566 LY_ARRAY_FOR(iter->iffeatures, u) {
2567 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2568 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002569 /* new feature was added (or LY_EMEM) */
2570 goto cleanup;
2571 }
2572 }
2573 }
2574 }
2575 }
2576 ret = LY_SUCCESS;
2577
2578cleanup:
2579 ly_set_erase(&features, NULL);
2580 return ret;
2581}
2582
2583/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002584 * @brief Check whether a leafref has an external dependency or not.
2585 *
2586 * @param[in] startnode Node with the leafref.
2587 * @param[in] local_mod Local module for the leafref path.
2588 * @param[in] first_node First found node when resolving the leafref.
2589 * @param[in] abs_path Whether the leafref path is absolute or relative.
2590 * @return 0 is the leafref does not require an external dependency, non-zero is it requires.
2591 */
2592static int
2593lys_compile_leafref_has_dep_flag(const struct lysc_node *startnode, const struct lys_module *local_mod,
2594 const struct lysc_node *first_node, int abs_path)
2595{
2596 int dep1, dep2;
2597 const struct lysc_node *op_node, *node;
2598
2599 /* find operation schema we are in */
2600 for (op_node = startnode->parent;
2601 op_node && !(op_node->nodetype & (LYS_ACTION | LYS_NOTIF));
2602 op_node = op_node->parent);
2603
2604 if (!op_node) {
2605 /* leafref pointing to a different module */
2606 if (local_mod != first_node->module) {
2607 return 1;
2608 }
2609 } else if (op_node->parent) {
2610 /* inner operation (notif/action) */
2611 if (abs_path) {
2612 return 1;
2613 } else {
2614 /* compare depth of both nodes */
2615 for (dep1 = 0, node = op_node; node->parent; node = node->parent);
2616 for (dep2 = 0, node = first_node; node->parent; node = node->parent);
2617 if ((dep2 > dep1) || ((dep2 == dep1) && (op_node != first_node))) {
2618 return 1;
2619 }
2620 }
2621 } else {
2622 /* top-level operation (notif/rpc) */
2623 if (op_node != first_node) {
2624 return 1;
2625 }
2626 }
2627
2628 return 0;
2629}
2630
2631/**
Radek Krejcia3045382018-11-22 14:30:31 +01002632 * @brief Validate the leafref path.
2633 * @param[in] ctx Compile context
2634 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01002635 * @param[in] leafref Leafref to validate.
Radek Krejcia3045382018-11-22 14:30:31 +01002636 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2637 */
2638static LY_ERR
Radek Krejci412ddfa2018-11-23 11:44:11 +01002639lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref)
Radek Krejcia3045382018-11-22 14:30:31 +01002640{
Michal Vasko03ff5a72019-09-11 13:49:33 +02002641 const struct lysc_node *node = NULL, *parent = NULL, *tmp_parent;
Radek Krejcia3045382018-11-22 14:30:31 +01002642 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002643 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01002644 const char *id, *prefix, *name;
2645 size_t prefix_len, name_len;
2646 int parent_times = 0, has_predicate;
2647 unsigned int iter, u;
2648 LY_ERR ret = LY_SUCCESS;
2649
2650 assert(ctx);
2651 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01002652 assert(leafref);
2653
Radek Krejci1c0c3442019-07-23 16:08:47 +02002654 ctx->path[0] = '\0';
Michal Vasko03ff5a72019-09-11 13:49:33 +02002655 lysc_path(startnode, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci1c0c3442019-07-23 16:08:47 +02002656 ctx->path_len = strlen(ctx->path);
Radek Krejci327de162019-06-14 12:52:07 +02002657
Radek Krejcia3045382018-11-22 14:30:31 +01002658 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002659 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01002660 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
2661 if (!iter) { /* first iteration */
2662 /* precess ".." in relative paths */
2663 if (parent_times > 0) {
2664 /* move from the context node */
2665 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
2666 /* path is supposed to be evaluated in data tree, so we have to skip
2667 * all schema nodes that cannot be instantiated in data tree */
2668 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002669 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002670 }
2671 }
2672 }
2673
2674 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01002675 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002676 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002677 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002678 }
2679 if (!mod) {
2680 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002681 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
2682 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002683 return LY_EVALID;
2684 }
Radek Krejci3d929562019-02-21 11:25:55 +01002685 if (!mod->implemented) {
2686 /* make the module implemented */
Radek Krejci77a8bcd2019-09-11 11:20:02 +02002687 lys_set_implemented_internal((struct lys_module*)mod, 2);
Radek Krejci3d929562019-02-21 11:25:55 +01002688 }
Radek Krejcia3045382018-11-22 14:30:31 +01002689
2690 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
2691 if (!node) {
2692 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002693 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002694 return LY_EVALID;
2695 }
2696 parent = node;
2697
Michal Vasko03ff5a72019-09-11 13:49:33 +02002698 if (!iter) {
2699 /* find module whose data will actually contain this leafref */
2700 for (tmp_parent = parent; tmp_parent->parent; tmp_parent = tmp_parent->parent);
2701
2702 /* set external dependency flag, we can decide based on the first found node */
2703 if (lys_compile_leafref_has_dep_flag(startnode, tmp_parent->module, node, (parent_times == -1 ? 1 : 0))) {
2704 startnode->flags |= LYS_LEAFREF_DEP;
2705 }
2706 }
2707
Radek Krejcia3045382018-11-22 14:30:31 +01002708 if (has_predicate) {
2709 /* we have predicate, so the current result must be list */
2710 if (node->nodetype != LYS_LIST) {
2711 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2712 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002713 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002714 return LY_EVALID;
2715 }
2716
Radek Krejcibade82a2018-12-05 10:13:48 +01002717 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context),
2718 LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01002719 }
2720
2721 ++iter;
2722 }
2723 if (ret) {
2724 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002725 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002726 return LY_EVALID;
2727 }
2728
2729 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2730 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2731 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002732 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002733 return LY_EVALID;
2734 }
2735
2736 /* check status */
2737 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2738 return LY_EVALID;
2739 }
2740
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002741 /* check config */
2742 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2743 if (node->flags & LYS_CONFIG_R) {
2744 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2745 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2746 leafref->path);
2747 return LY_EVALID;
2748 }
2749 }
2750
Radek Krejci412ddfa2018-11-23 11:44:11 +01002751 /* store the target's type and check for circular chain of leafrefs */
2752 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2753 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2754 if (type == (struct lysc_type*)leafref) {
2755 /* circular chain detected */
2756 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2757 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2758 return LY_EVALID;
2759 }
2760 }
2761
Radek Krejci58d171e2018-11-23 13:50:55 +01002762 /* check if leafref and its target are under common if-features */
2763 if (lys_compile_leafref_features_validate(startnode, node)) {
2764 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2765 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2766 leafref->path);
2767 return LY_EVALID;
2768 }
2769
Radek Krejci327de162019-06-14 12:52:07 +02002770 ctx->path_len = 1;
2771 ctx->path[1] = '\0';
Radek Krejcia3045382018-11-22 14:30:31 +01002772 return LY_SUCCESS;
2773}
2774
Radek Krejcicdfecd92018-11-26 11:27:32 +01002775static 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 +02002776 struct lysp_type *type_p, struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002777/**
2778 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2779 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002780 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2781 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2782 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2783 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002784 * @param[in] type_p Parsed type to compile.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002785 * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path)
Radek Krejcia3045382018-11-22 14:30:31 +01002786 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002787 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2788 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2789 * @param[out] type Newly created type structure with the filled information about the type.
2790 * @return LY_ERR value.
2791 */
Radek Krejci19a96102018-11-15 13:38:09 +01002792static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002793lys_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 +02002794 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002795 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002796{
2797 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002798 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002799 struct lysc_type_bin *bin;
2800 struct lysc_type_num *num;
2801 struct lysc_type_str *str;
2802 struct lysc_type_bits *bits;
2803 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002804 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002805 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002806 struct lysc_type_union *un, *un_aux;
2807 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002808
2809 switch (basetype) {
2810 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002811 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002812
2813 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002814 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002815 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2816 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002817 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002818 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, LYEXT_PAR_LENGTH, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002819 }
2820 }
2821
2822 if (tpdfname) {
2823 type_p->compiled = *type;
2824 *type = calloc(1, sizeof(struct lysc_type_bin));
2825 }
2826 break;
2827 case LY_TYPE_BITS:
2828 /* RFC 7950 9.7 - bits */
2829 bits = (struct lysc_type_bits*)(*type);
2830 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002831 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002832 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2833 (struct lysc_type_bitenum_item**)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002834 }
2835
Radek Krejci555cb5b2018-11-16 14:54:33 +01002836 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002837 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002838 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002839 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002840 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002841 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002842 free(*type);
2843 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002844 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002845 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002846 }
2847
2848 if (tpdfname) {
2849 type_p->compiled = *type;
2850 *type = calloc(1, sizeof(struct lysc_type_bits));
2851 }
2852 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002853 case LY_TYPE_DEC64:
2854 dec = (struct lysc_type_dec*)(*type);
2855
2856 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002857 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002858 if (!type_p->fraction_digits) {
2859 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002860 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002861 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002862 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002863 free(*type);
2864 *type = NULL;
2865 }
2866 return LY_EVALID;
2867 }
2868 } else if (type_p->fraction_digits) {
2869 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002870 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002871 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2872 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002873 tpdfname);
2874 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002875 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2876 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002877 free(*type);
2878 *type = NULL;
2879 }
2880 return LY_EVALID;
2881 }
2882 dec->fraction_digits = type_p->fraction_digits;
2883
2884 /* RFC 7950 9.2.4 - range */
2885 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002886 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2887 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002888 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002889 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, LYEXT_PAR_RANGE, ret, done);
Radek Krejci6cba4292018-11-15 17:33:29 +01002890 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002891 }
2892
2893 if (tpdfname) {
2894 type_p->compiled = *type;
2895 *type = calloc(1, sizeof(struct lysc_type_dec));
2896 }
2897 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002898 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002899 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002900
2901 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002902 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002903 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2904 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002905 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002906 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, LYEXT_PAR_LENGTH, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002907 }
2908 } else if (base && ((struct lysc_type_str*)base)->length) {
2909 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2910 }
2911
2912 /* RFC 7950 9.4.5 - pattern */
2913 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002914 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002915 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002916 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2917 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2918 }
2919
2920 if (tpdfname) {
2921 type_p->compiled = *type;
2922 *type = calloc(1, sizeof(struct lysc_type_str));
2923 }
2924 break;
2925 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002926 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002927
2928 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002929 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002930 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002931 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002932 }
2933
Radek Krejci555cb5b2018-11-16 14:54:33 +01002934 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002935 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002936 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002937 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002938 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002939 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002940 free(*type);
2941 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002942 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002943 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002944 }
2945
2946 if (tpdfname) {
2947 type_p->compiled = *type;
2948 *type = calloc(1, sizeof(struct lysc_type_enum));
2949 }
2950 break;
2951 case LY_TYPE_INT8:
2952 case LY_TYPE_UINT8:
2953 case LY_TYPE_INT16:
2954 case LY_TYPE_UINT16:
2955 case LY_TYPE_INT32:
2956 case LY_TYPE_UINT32:
2957 case LY_TYPE_INT64:
2958 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002959 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002960
2961 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002962 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002963 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
2964 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002965 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002966 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, LYEXT_PAR_RANGE, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002967 }
2968 }
2969
2970 if (tpdfname) {
2971 type_p->compiled = *type;
2972 *type = calloc(1, sizeof(struct lysc_type_num));
2973 }
2974 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002975 case LY_TYPE_IDENT:
2976 idref = (struct lysc_type_identityref*)(*type);
2977
2978 /* RFC 7950 9.10.2 - base */
2979 if (type_p->bases) {
2980 if (base) {
2981 /* only the directly derived identityrefs can contain base specification */
2982 if (tpdfname) {
2983 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002984 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002985 tpdfname);
2986 } else {
2987 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002988 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002989 free(*type);
2990 *type = NULL;
2991 }
2992 return LY_EVALID;
2993 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002994 LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002995 }
2996
2997 if (!base && !type_p->flags) {
2998 /* type derived from identityref built-in type must contain at least one base */
2999 if (tpdfname) {
3000 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
3001 } else {
3002 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
3003 free(*type);
3004 *type = NULL;
3005 }
3006 return LY_EVALID;
3007 }
3008
3009 if (tpdfname) {
3010 type_p->compiled = *type;
3011 *type = calloc(1, sizeof(struct lysc_type_identityref));
3012 }
3013 break;
Radek Krejcia3045382018-11-22 14:30:31 +01003014 case LY_TYPE_LEAFREF:
3015 /* RFC 7950 9.9.3 - require-instance */
3016 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003017 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003018 if (tpdfname) {
3019 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3020 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
3021 } else {
3022 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3023 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
3024 free(*type);
3025 *type = NULL;
3026 }
3027 return LY_EVALID;
3028 }
Radek Krejcia3045382018-11-22 14:30:31 +01003029 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01003030 } else if (base) {
3031 /* inherit */
3032 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01003033 } else {
3034 /* default is true */
3035 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
3036 }
3037 if (type_p->path) {
3038 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01003039 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01003040 } else if (base) {
3041 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01003042 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01003043 } else if (tpdfname) {
3044 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
3045 return LY_EVALID;
3046 } else {
3047 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
3048 free(*type);
3049 *type = NULL;
3050 return LY_EVALID;
3051 }
3052 if (tpdfname) {
3053 type_p->compiled = *type;
3054 *type = calloc(1, sizeof(struct lysc_type_leafref));
3055 }
3056 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01003057 case LY_TYPE_INST:
3058 /* RFC 7950 9.9.3 - require-instance */
3059 if (type_p->flags & LYS_SET_REQINST) {
3060 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
3061 } else {
3062 /* default is true */
3063 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
3064 }
3065
3066 if (tpdfname) {
3067 type_p->compiled = *type;
3068 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3069 }
3070 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003071 case LY_TYPE_UNION:
3072 un = (struct lysc_type_union*)(*type);
3073
3074 /* RFC 7950 7.4 - type */
3075 if (type_p->types) {
3076 if (base) {
3077 /* only the directly derived union can contain types specification */
3078 if (tpdfname) {
3079 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
3080 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
3081 tpdfname);
3082 } else {
3083 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
3084 "Invalid type substatement for the type not directly derived from union built-in type.");
3085 free(*type);
3086 *type = NULL;
3087 }
3088 return LY_EVALID;
3089 }
3090 /* compile the type */
3091 additional = 0;
3092 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
3093 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Radek Krejciec4da802019-05-02 13:02:41 +02003094 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 +01003095 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
3096 /* add space for additional types from the union subtype */
3097 un_aux = (struct lysc_type_union *)un->types[u + additional];
3098 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)));
3099 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
3100 un->types = (void*)((uint32_t*)(p) + 1);
3101
3102 /* copy subtypes of the subtype union */
3103 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
3104 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
3105 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
3106 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
3107 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
3108 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
3109 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
3110 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
3111 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
3112 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
3113 /* TODO extensions */
3114
3115 } else {
3116 un->types[u + additional] = un_aux->types[v];
3117 ++un_aux->types[v]->refcount;
3118 }
3119 ++additional;
3120 LY_ARRAY_INCREMENT(un->types);
3121 }
3122 /* compensate u increment in main loop */
3123 --additional;
3124
3125 /* free the replaced union subtype */
3126 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
3127 } else {
3128 LY_ARRAY_INCREMENT(un->types);
3129 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01003130 }
3131 }
3132
3133 if (!base && !type_p->flags) {
3134 /* type derived from union built-in type must contain at least one type */
3135 if (tpdfname) {
3136 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
3137 } else {
3138 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
3139 free(*type);
3140 *type = NULL;
3141 }
3142 return LY_EVALID;
3143 }
3144
3145 if (tpdfname) {
3146 type_p->compiled = *type;
3147 *type = calloc(1, sizeof(struct lysc_type_union));
3148 }
3149 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01003150 case LY_TYPE_BOOL:
3151 case LY_TYPE_EMPTY:
3152 case LY_TYPE_UNKNOWN: /* just to complete switch */
3153 break;
3154 }
3155 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
3156done:
3157 return ret;
3158}
3159
Radek Krejcia3045382018-11-22 14:30:31 +01003160/**
3161 * @brief Compile information about the leaf/leaf-list's type.
3162 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01003163 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
3164 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
3165 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
3166 * @param[in] context_name Name of the context node or referencing typedef for logging.
3167 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01003168 * @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 +01003169 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01003170 * @return LY_ERR value.
3171 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01003172static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01003173lys_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 +02003174 struct lysp_type *type_p, struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01003175{
3176 LY_ERR ret = LY_SUCCESS;
3177 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003178 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01003179 struct type_context {
3180 const struct lysp_tpdf *tpdf;
3181 struct lysp_node *node;
3182 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003183 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01003184 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01003185 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003186 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01003187 const char *dflt = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02003188 struct lys_module *dflt_mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01003189
3190 (*type) = NULL;
3191
3192 tctx = calloc(1, sizeof *tctx);
3193 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01003194 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01003195 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
3196 ret == LY_SUCCESS;
3197 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
3198 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
3199 if (basetype) {
3200 break;
3201 }
3202
3203 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003204 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
3205 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01003206 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
3207
Radek Krejcicdfecd92018-11-26 11:27:32 +01003208 if (units && !*units) {
3209 /* inherit units */
3210 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
3211 }
Radek Krejci01342af2019-01-03 15:18:08 +01003212 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003213 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01003214 dflt = tctx->tpdf->dflt;
Radek Krejcia1911222019-07-22 17:24:50 +02003215 dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003216 }
Radek Krejci01342af2019-01-03 15:18:08 +01003217 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003218 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
3219 break;
3220 }
3221
Radek Krejci19a96102018-11-15 13:38:09 +01003222 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003223 /* it is not necessary to continue, the rest of the chain was already compiled,
3224 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01003225 basetype = tctx->tpdf->type.compiled->basetype;
3226 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01003227 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003228 dummyloops = 1;
3229 goto preparenext;
3230 } else {
3231 tctx = NULL;
3232 break;
3233 }
Radek Krejci19a96102018-11-15 13:38:09 +01003234 }
3235
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003236 /* circular typedef reference detection */
3237 for (u = 0; u < tpdf_chain.count; u++) {
3238 /* local part */
3239 tctx_iter = (struct type_context*)tpdf_chain.objs[u];
3240 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3241 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3242 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3243 free(tctx);
3244 ret = LY_EVALID;
3245 goto cleanup;
3246 }
3247 }
3248 for (u = 0; u < ctx->tpdf_chain.count; u++) {
3249 /* global part for unions corner case */
3250 tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u];
3251 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3252 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3253 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3254 free(tctx);
3255 ret = LY_EVALID;
3256 goto cleanup;
3257 }
3258 }
3259
Radek Krejci19a96102018-11-15 13:38:09 +01003260 /* store information for the following processing */
3261 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3262
Radek Krejcicdfecd92018-11-26 11:27:32 +01003263preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01003264 /* prepare next loop */
3265 tctx_prev = tctx;
3266 tctx = calloc(1, sizeof *tctx);
3267 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
3268 }
3269 free(tctx);
3270
3271 /* allocate type according to the basetype */
3272 switch (basetype) {
3273 case LY_TYPE_BINARY:
3274 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01003275 break;
3276 case LY_TYPE_BITS:
3277 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01003278 break;
3279 case LY_TYPE_BOOL:
3280 case LY_TYPE_EMPTY:
3281 *type = calloc(1, sizeof(struct lysc_type));
3282 break;
3283 case LY_TYPE_DEC64:
3284 *type = calloc(1, sizeof(struct lysc_type_dec));
3285 break;
3286 case LY_TYPE_ENUM:
3287 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01003288 break;
3289 case LY_TYPE_IDENT:
3290 *type = calloc(1, sizeof(struct lysc_type_identityref));
3291 break;
3292 case LY_TYPE_INST:
3293 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3294 break;
3295 case LY_TYPE_LEAFREF:
3296 *type = calloc(1, sizeof(struct lysc_type_leafref));
3297 break;
3298 case LY_TYPE_STRING:
3299 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01003300 break;
3301 case LY_TYPE_UNION:
3302 *type = calloc(1, sizeof(struct lysc_type_union));
3303 break;
3304 case LY_TYPE_INT8:
3305 case LY_TYPE_UINT8:
3306 case LY_TYPE_INT16:
3307 case LY_TYPE_UINT16:
3308 case LY_TYPE_INT32:
3309 case LY_TYPE_UINT32:
3310 case LY_TYPE_INT64:
3311 case LY_TYPE_UINT64:
3312 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003313 break;
3314 case LY_TYPE_UNKNOWN:
3315 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3316 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3317 ret = LY_EVALID;
3318 goto cleanup;
3319 }
3320 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003321 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003322 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3323 ly_data_type2str[basetype]);
3324 free(*type);
3325 (*type) = NULL;
3326 ret = LY_EVALID;
3327 goto cleanup;
3328 }
3329
3330 /* get restrictions from the referred typedefs */
3331 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
3332 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003333
3334 /* remember the typedef context for circular check */
3335 ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3336
Radek Krejci43699232018-11-23 14:59:46 +01003337 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003338 base = tctx->tpdf->type.compiled;
3339 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003340 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003341 /* no change, just use the type information from the base */
3342 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
3343 ++base->refcount;
3344 continue;
3345 }
3346
3347 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003348 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3349 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3350 tctx->tpdf->name, ly_data_type2str[basetype]);
3351 ret = LY_EVALID;
3352 goto cleanup;
3353 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3354 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3355 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3356 tctx->tpdf->name, tctx->tpdf->dflt);
3357 ret = LY_EVALID;
3358 goto cleanup;
3359 }
3360
Radek Krejci19a96102018-11-15 13:38:09 +01003361 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003362 /* TODO user type plugins */
3363 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003364 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003365 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 +01003366 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003367 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003368 LY_CHECK_GOTO(ret, cleanup);
3369 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003370 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003371 /* remove the processed typedef contexts from the stack for circular check */
3372 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003373
Radek Krejcic5c27e52018-11-15 14:38:11 +01003374 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003375 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003376 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003377 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003378 /* TODO user type plugins */
3379 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003380 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003381 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 +01003382 LY_CHECK_GOTO(ret, cleanup);
3383 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01003384 /* no specific restriction in leaf's type definition, copy from the base */
3385 free(*type);
3386 (*type) = base;
3387 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003388 }
Radek Krejcia1911222019-07-22 17:24:50 +02003389 if (dflt && !(*type)->dflt) {
3390 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003391 (*type)->dflt_mod = dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003392 (*type)->dflt = calloc(1, sizeof *(*type)->dflt);
3393 (*type)->dflt->realtype = (*type);
3394 ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt),
3395 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003396 lys_resolve_prefix, (void*)(*type)->dflt_mod, LYD_XML, NULL, NULL, (*type)->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003397 if (err) {
3398 ly_err_print(err);
3399 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3400 "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg);
3401 ly_err_free(err);
3402 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003403 if (ret == LY_EINCOMPLETE) {
3404 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003405 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, NULL, (*type)->dflt, (*type)->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003406
3407 /* but in general result is so far ok */
3408 ret = LY_SUCCESS;
3409 }
Radek Krejcia1911222019-07-22 17:24:50 +02003410 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci01342af2019-01-03 15:18:08 +01003411 }
Radek Krejci19a96102018-11-15 13:38:09 +01003412
Radek Krejci0935f412019-08-20 16:15:18 +02003413 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003414
3415cleanup:
3416 ly_set_erase(&tpdf_chain, free);
3417 return ret;
3418}
3419
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003420/**
3421 * @brief Compile status information of the given node.
3422 *
3423 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3424 * has the status correctly set during the compilation.
3425 *
3426 * @param[in] ctx Compile context
3427 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3428 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3429 * the compatibility with the parent's status value.
3430 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3431 * @return LY_ERR value.
3432 */
3433static LY_ERR
3434lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3435{
3436 /* status - it is not inherited by specification, but it does not make sense to have
3437 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3438 if (!((*node_flags) & LYS_STATUS_MASK)) {
3439 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3440 if ((parent_flags & 0x3) != 0x3) {
3441 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3442 * combination of bits (0x3) which marks the uses_status value */
3443 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3444 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3445 }
3446 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3447 } else {
3448 (*node_flags) |= LYS_STATUS_CURR;
3449 }
3450 } else if (parent_flags & LYS_STATUS_MASK) {
3451 /* check status compatibility with the parent */
3452 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3453 if ((*node_flags) & LYS_STATUS_CURR) {
3454 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3455 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3456 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3457 } else { /* LYS_STATUS_DEPRC */
3458 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3459 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3460 }
3461 return LY_EVALID;
3462 }
3463 }
3464 return LY_SUCCESS;
3465}
3466
Radek Krejci8cce8532019-03-05 11:27:45 +01003467/**
3468 * @brief Check uniqness of the node/action/notification name.
3469 *
3470 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3471 * structures, but they share the namespace so we need to check their name collisions.
3472 *
3473 * @param[in] ctx Compile context.
3474 * @param[in] children List (linked list) of data nodes to go through.
3475 * @param[in] actions List (sized array) of actions or RPCs to go through.
3476 * @param[in] notifs List (sized array) of Notifications to go through.
3477 * @param[in] name Name of the item to find in the given lists.
3478 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3479 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3480 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3481 */
3482static LY_ERR
3483lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children,
3484 const struct lysc_action *actions, const struct lysc_notif *notifs,
3485 const char *name, void *exclude)
3486{
3487 const struct lysc_node *iter;
3488 unsigned int u;
3489
3490 LY_LIST_FOR(children, iter) {
3491 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3492 goto error;
3493 }
3494 }
3495 LY_ARRAY_FOR(actions, u) {
3496 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3497 goto error;
3498 }
3499 }
3500 LY_ARRAY_FOR(notifs, u) {
3501 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3502 goto error;
3503 }
3504 }
3505 return LY_SUCCESS;
3506error:
3507 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification");
3508 return LY_EEXIST;
3509}
3510
Radek Krejcia0f704a2019-09-09 16:12:23 +02003511/**
3512 * @brief Get the XPath context node for the given schema node.
3513 * @param[in] start The schema node where the XPath expression appears.
3514 * @return The context node to evaluate XPath expression in given schema node.
3515 * @return NULL in case the context node is the root node.
3516 */
3517static struct lysc_node *
3518lysc_xpath_context(struct lysc_node *start)
3519{
3520 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF));
3521 start = start->parent);
3522 return start;
3523}
3524
Radek Krejciec4da802019-05-02 13:02:41 +02003525static 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 +01003526
Radek Krejcia3045382018-11-22 14:30:31 +01003527/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003528 * @brief Compile parsed RPC/action schema node information.
3529 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003530 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003531 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003532 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3533 * @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).
3534 * Zero means no uses, non-zero value with no status bit set mean the default status.
3535 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3536 */
3537static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003538lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003539 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3540{
3541 LY_ERR ret = LY_SUCCESS;
3542 struct lysp_node *child_p;
3543 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003544 int opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003545
Radek Krejci327de162019-06-14 12:52:07 +02003546 lysc_update_path(ctx, parent, action_p->name);
3547
Radek Krejci8cce8532019-03-05 11:27:45 +01003548 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3549 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3550 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3551 action_p->name, action)) {
3552 return LY_EVALID;
3553 }
3554
Radek Krejciec4da802019-05-02 13:02:41 +02003555 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003556 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003557 "Action \"%s\" is placed inside %s.", action_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003558 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003559 return LY_EVALID;
3560 }
3561
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003562 action->nodetype = LYS_ACTION;
3563 action->module = ctx->mod;
3564 action->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003565 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003566 action->sp = action_p;
3567 }
3568 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3569
3570 /* status - it is not inherited by specification, but it does not make sense to have
3571 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3572 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3573
3574 DUP_STRING(ctx->ctx, action_p->name, action->name);
3575 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3576 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003577 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003578 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003579
3580 /* input */
Radek Krejci327de162019-06-14 12:52:07 +02003581 lysc_update_path(ctx, (struct lysc_node*)action, "input");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003582 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003583 COMPILE_EXTS_GOTO(ctx, action_p->input.exts, action->input_exts, &action->input, LYEXT_PAR_INPUT, ret, cleanup);
Radek Krejciec4da802019-05-02 13:02:41 +02003584 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003585 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003586 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003587 }
Radek Krejci327de162019-06-14 12:52:07 +02003588 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003589 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003590
3591 /* output */
Radek Krejci327de162019-06-14 12:52:07 +02003592 lysc_update_path(ctx, (struct lysc_node*)action, "output");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003593 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003594 COMPILE_EXTS_GOTO(ctx, action_p->output.exts, action->output_exts, &action->output, LYEXT_PAR_OUTPUT, ret, cleanup);
Radek Krejciec4da802019-05-02 13:02:41 +02003595 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003596 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003597 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003598 }
Radek Krejci327de162019-06-14 12:52:07 +02003599 lysc_update_path(ctx, NULL, NULL);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003600
Radek Krejci327de162019-06-14 12:52:07 +02003601 lysc_update_path(ctx, NULL, NULL);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003602cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003603 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003604 return ret;
3605}
3606
3607/**
Radek Krejci43981a32019-04-12 09:44:11 +02003608 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003609 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003610 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003611 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3612 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3613 * @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 +02003614 * Zero means no uses, non-zero value with no status bit set mean the default status.
3615 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3616 */
3617static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003618lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003619 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3620{
3621 LY_ERR ret = LY_SUCCESS;
3622 struct lysp_node *child_p;
3623 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003624 int opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003625
Radek Krejci327de162019-06-14 12:52:07 +02003626 lysc_update_path(ctx, parent, notif_p->name);
3627
Radek Krejcifc11bd72019-04-11 16:00:05 +02003628 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3629 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3630 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3631 notif_p->name, notif)) {
3632 return LY_EVALID;
3633 }
3634
Radek Krejciec4da802019-05-02 13:02:41 +02003635 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003636 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3637 "Notification \"%s\" is placed inside %s.", notif_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003638 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003639 return LY_EVALID;
3640 }
3641
3642 notif->nodetype = LYS_NOTIF;
3643 notif->module = ctx->mod;
3644 notif->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003645 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003646 notif->sp = notif_p;
3647 }
3648 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3649
3650 /* status - it is not inherited by specification, but it does not make sense to have
3651 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3652 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3653
3654 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3655 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3656 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003657 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003658 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003659 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003660
Radek Krejciec4da802019-05-02 13:02:41 +02003661 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003662 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003663 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003664 }
3665
Radek Krejci327de162019-06-14 12:52:07 +02003666 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003667cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003668 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003669 return ret;
3670}
3671
3672/**
Radek Krejcia3045382018-11-22 14:30:31 +01003673 * @brief Compile parsed container node information.
3674 * @param[in] ctx Compile context
3675 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003676 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3677 * is enriched with the container-specific information.
3678 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3679 */
Radek Krejci19a96102018-11-15 13:38:09 +01003680static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003681lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003682{
3683 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3684 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3685 struct lysp_node *child_p;
3686 unsigned int u;
3687 LY_ERR ret = LY_SUCCESS;
3688
Radek Krejcife909632019-02-12 15:34:42 +01003689 if (cont_p->presence) {
3690 cont->flags |= LYS_PRESENCE;
3691 }
3692
Radek Krejci19a96102018-11-15 13:38:09 +01003693 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003694 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003695 }
3696
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003697 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02003698 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3699 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003700
3701done:
3702 return ret;
3703}
3704
Radek Krejci33f72892019-02-21 10:36:58 +01003705/*
3706 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3707 * @param[in] ctx Compile context.
3708 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3709 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003710 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3711 * @return LY_ERR value.
3712 */
3713static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003714lys_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 +01003715{
Radek Krejcia1911222019-07-22 17:24:50 +02003716 unsigned int u;
Radek Krejci33f72892019-02-21 10:36:58 +01003717 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3718
Radek Krejciec4da802019-05-02 13:02:41 +02003719 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 +01003720 leaf->units ? NULL : &leaf->units));
3721 if (leaf->nodetype == LYS_LEAFLIST) {
3722 if (llist->type->dflt && !llist->dflts && !llist->min) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003723 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM);
3724 llist->dflts_mods[0] = llist->type->dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003725 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
Radek Krejcia1911222019-07-22 17:24:50 +02003726 llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]);
3727 llist->dflts[0]->realtype = llist->type->dflt->realtype;
3728 llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3729 llist->dflts[0]->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003730 LY_ARRAY_INCREMENT(llist->dflts);
3731 }
3732 } else {
3733 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003734 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003735 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3736 leaf->dflt->realtype = leaf->type->dflt->realtype;
3737 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
3738 leaf->dflt->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003739 }
3740 }
3741 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3742 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3743 ly_set_add(&ctx->unres, leaf, 0);
3744 } else if (leaf->type->basetype == LY_TYPE_UNION) {
3745 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3746 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3747 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3748 ly_set_add(&ctx->unres, leaf, 0);
3749 }
3750 }
3751 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003752 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3753 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3754 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3755 return LY_EVALID;
3756 }
3757 }
3758
Radek Krejci33f72892019-02-21 10:36:58 +01003759 return LY_SUCCESS;
3760}
3761
Radek Krejcia3045382018-11-22 14:30:31 +01003762/**
3763 * @brief Compile parsed leaf node information.
3764 * @param[in] ctx Compile context
3765 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003766 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3767 * is enriched with the leaf-specific information.
3768 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3769 */
Radek Krejci19a96102018-11-15 13:38:09 +01003770static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003771lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003772{
3773 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3774 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3775 unsigned int u;
3776 LY_ERR ret = LY_SUCCESS;
3777
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003778 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003779 if (leaf_p->units) {
3780 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3781 leaf->flags |= LYS_SET_UNITS;
3782 }
Radek Krejcia1911222019-07-22 17:24:50 +02003783
3784 /* the dflt member is just filled to avoid getting the default value from the type */
3785 leaf->dflt = (void*)leaf_p->dflt;
3786 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
Radek Krejci812a5bf2019-09-09 09:18:05 +02003787 if (ret) {
3788 leaf->dflt = NULL;
3789 return ret;
3790 }
Radek Krejcia1911222019-07-22 17:24:50 +02003791
Radek Krejciccd20f12019-02-15 14:12:27 +01003792 if (leaf_p->dflt) {
Radek Krejcia1911222019-07-22 17:24:50 +02003793 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003794 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02003795 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3796 leaf->dflt->realtype = leaf->type;
3797 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt),
3798 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003799 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003800 leaf->dflt->realtype->refcount++;
3801 if (err) {
3802 ly_err_print(err);
3803 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3804 "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg);
3805 ly_err_free(err);
3806 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003807 if (ret == LY_EINCOMPLETE) {
Radek Krejci1c0c3442019-07-23 16:08:47 +02003808 /* postpone default compilation when the tree is complete */
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003809 LY_CHECK_RET(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod));
Radek Krejci1c0c3442019-07-23 16:08:47 +02003810
3811 /* but in general result is so far ok */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003812 ret = LY_SUCCESS;
3813 }
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003814 LY_CHECK_RET(ret);
Radek Krejci76b3e962018-12-14 17:01:25 +01003815 leaf->flags |= LYS_SET_DFLT;
3816 }
Radek Krejci43699232018-11-23 14:59:46 +01003817
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003818
Radek Krejci19a96102018-11-15 13:38:09 +01003819done:
3820 return ret;
3821}
3822
Radek Krejcia3045382018-11-22 14:30:31 +01003823/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003824 * @brief Compile parsed leaf-list node information.
3825 * @param[in] ctx Compile context
3826 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003827 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3828 * is enriched with the leaf-list-specific information.
3829 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3830 */
3831static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003832lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003833{
3834 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3835 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Radek Krejcia1911222019-07-22 17:24:50 +02003836 unsigned int u, v;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003837 LY_ERR ret = LY_SUCCESS;
3838
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003839 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003840 if (llist_p->units) {
3841 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3842 llist->flags |= LYS_SET_UNITS;
3843 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003844
Radek Krejcia1911222019-07-22 17:24:50 +02003845 /* the dflts member is just filled to avoid getting the default value from the type */
3846 llist->dflts = (void*)llist_p->dflts;
3847 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003848 if (llist_p->dflts) {
Radek Krejcia1911222019-07-22 17:24:50 +02003849 llist->dflts = NULL; /* reset the temporary llist_p->dflts */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003850 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003851 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
3852 LY_ARRAY_FOR(llist_p->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02003853 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003854 LY_ARRAY_INCREMENT(llist->dflts_mods);
3855 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003856 LY_ARRAY_INCREMENT(llist->dflts);
Radek Krejcia1911222019-07-22 17:24:50 +02003857 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
3858 llist->dflts[u]->realtype = llist->type;
3859 ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]),
3860 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003861 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003862 llist->dflts[u]->realtype->refcount++;
3863 if (err) {
3864 ly_err_print(err);
3865 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3866 "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg);
3867 ly_err_free(err);
3868 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003869 if (ret == LY_EINCOMPLETE) {
3870 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003871 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), done);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003872
3873 /* but in general result is so far ok */
3874 ret = LY_SUCCESS;
3875 }
Radek Krejcia1911222019-07-22 17:24:50 +02003876 LY_CHECK_GOTO(ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003877 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003878 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003879 }
Radek Krejcia1911222019-07-22 17:24:50 +02003880 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
3881 /* configuration data values must be unique - so check the default values */
3882 LY_ARRAY_FOR(llist->dflts, u) {
3883 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
3884 if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) {
3885 int dynamic = 0;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003886 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 +02003887 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3888 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
3889 if (dynamic) {
3890 free((char*)val);
3891 }
3892 return LY_EVALID;
3893 }
3894 }
3895 }
3896 }
3897
3898 /* 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 +01003899
3900 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003901 if (llist->min) {
3902 llist->flags |= LYS_MAND_TRUE;
3903 }
Radek Krejcib7408632018-11-28 17:12:11 +01003904 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003905
Radek Krejci0e5d8382018-11-28 16:37:53 +01003906done:
3907 return ret;
3908}
3909
3910/**
Radek Krejci7af64242019-02-18 13:07:53 +01003911 * @brief Compile information about list's uniques.
3912 * @param[in] ctx Compile context.
3913 * @param[in] context_module Module where the prefixes are going to be resolved.
3914 * @param[in] uniques Sized array list of unique statements.
3915 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3916 * @return LY_ERR value.
3917 */
3918static LY_ERR
3919lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3920{
3921 LY_ERR ret = LY_SUCCESS;
3922 struct lysc_node_leaf **key, ***unique;
3923 const char *keystr, *delim;
3924 size_t len;
3925 unsigned int v;
3926 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003927 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003928
3929 for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) {
3930 config = -1;
3931 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3932 keystr = uniques[v];
3933 while (keystr) {
3934 delim = strpbrk(keystr, " \t\n");
3935 if (delim) {
3936 len = delim - keystr;
3937 while (isspace(*delim)) {
3938 ++delim;
3939 }
3940 } else {
3941 len = strlen(keystr);
3942 }
3943
3944 /* unique node must be present */
3945 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003946 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3947 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003948 if (ret != LY_SUCCESS) {
3949 if (ret == LY_EDENIED) {
3950 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003951 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003952 len, keystr, lys_nodetype2str((*key)->nodetype));
3953 }
3954 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003955 } else if (flags) {
3956 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3957 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
3958 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
3959 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003960 }
3961
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003962
Radek Krejci7af64242019-02-18 13:07:53 +01003963 /* all referenced leafs must be of the same config type */
3964 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3965 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3966 "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]);
3967 return LY_EVALID;
3968 } else if ((*key)->flags & LYS_CONFIG_W) {
3969 config = 1;
3970 } else { /* LYS_CONFIG_R */
3971 config = 0;
3972 }
3973
3974 /* check status */
3975 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3976 (*key)->flags, (*key)->module, (*key)->name));
3977
3978 /* mark leaf as unique */
3979 (*key)->flags |= LYS_UNIQUE;
3980
3981 /* next unique value in line */
3982 keystr = delim;
3983 }
3984 /* next unique definition */
3985 }
3986
3987 return LY_SUCCESS;
3988}
3989
3990/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003991 * @brief Compile parsed list node information.
3992 * @param[in] ctx Compile context
3993 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003994 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3995 * is enriched with the list-specific information.
3996 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3997 */
3998static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003999lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004000{
4001 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
4002 struct lysc_node_list *list = (struct lysc_node_list*)node;
4003 struct lysp_node *child_p;
Radek Krejci0fe9b512019-07-26 17:51:05 +02004004 struct lysc_node_leaf *key, *prev_key = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004005 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01004006 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004007 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004008 LY_ERR ret = LY_SUCCESS;
4009
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004010 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01004011 if (list->min) {
4012 list->flags |= LYS_MAND_TRUE;
4013 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004014 list->max = list_p->max ? list_p->max : (uint32_t)-1;
4015
4016 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004017 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004018 }
4019
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004020 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004021
4022 /* keys */
4023 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
4024 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
4025 return LY_EVALID;
4026 }
4027
4028 /* find all the keys (must be direct children) */
4029 keystr = list_p->key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02004030 if (!keystr) {
4031 /* keyless list */
4032 list->flags |= LYS_KEYLESS;
4033 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004034 while (keystr) {
4035 delim = strpbrk(keystr, " \t\n");
4036 if (delim) {
4037 len = delim - keystr;
4038 while (isspace(*delim)) {
4039 ++delim;
4040 }
4041 } else {
4042 len = strlen(keystr);
4043 }
4044
4045 /* key node must be present */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004046 key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
4047 if (!(key)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004048 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4049 "The list's key \"%.*s\" not found.", len, keystr);
4050 return LY_EVALID;
4051 }
4052 /* keys must be unique */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004053 if (key->flags & LYS_KEY) {
4054 /* the node was already marked as a key */
4055 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4056 "Duplicated key identifier \"%.*s\".", len, keystr);
4057 return LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004058 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02004059
4060 lysc_update_path(ctx, (struct lysc_node*)list, key->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004061 /* key must have the same config flag as the list itself */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004062 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004063 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
4064 return LY_EVALID;
4065 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004066 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004067 /* YANG 1.0 denies key to be of empty type */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004068 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004069 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004070 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004071 return LY_EVALID;
4072 }
4073 } else {
4074 /* when and if-feature are illegal on list keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004075 if (key->when) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004076 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004077 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004078 return LY_EVALID;
4079 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02004080 if (key->iffeatures) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004081 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004082 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004083 return LY_EVALID;
4084 }
4085 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004086
4087 /* check status */
4088 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0fe9b512019-07-26 17:51:05 +02004089 key->flags, key->module, key->name));
Radek Krejci76b3e962018-12-14 17:01:25 +01004090
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004091 /* ignore default values of the key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004092 if (key->dflt) {
4093 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
4094 lysc_type_free(ctx->ctx, key->dflt->realtype);
4095 free(key->dflt);
4096 key->dflt = NULL;
4097 key->dflt_mod = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004098 }
4099 /* mark leaf as key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004100 key->flags |= LYS_KEY;
4101
4102 /* move it to the correct position */
4103 if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) {
4104 /* fix links in closest previous siblings of the key */
4105 if (key->next) {
4106 key->next->prev = key->prev;
4107 } else {
4108 /* last child */
4109 list->child->prev = key->prev;
4110 }
4111 if (key->prev->next) {
4112 key->prev->next = key->next;
4113 }
4114 /* fix links in the key */
4115 if (prev_key) {
4116 key->prev = (struct lysc_node*)prev_key;
4117 key->next = prev_key->next;
4118 } else {
4119 key->prev = list->child->prev;
4120 key->next = list->child;
4121 }
4122 /* fix links in closes future siblings of the key */
4123 if (prev_key) {
4124 if (prev_key->next) {
4125 prev_key->next->prev = (struct lysc_node*)key;
4126 } else {
4127 list->child->prev = (struct lysc_node*)key;
4128 }
4129 prev_key->next = (struct lysc_node*)key;
4130 } else {
4131 list->child->prev = (struct lysc_node*)key;
4132 }
4133 /* fix links in parent */
4134 if (!key->prev->next) {
4135 list->child = (struct lysc_node*)key;
4136 }
4137 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004138
4139 /* next key value */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004140 prev_key = key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004141 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02004142 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004143 }
4144
4145 /* uniques */
4146 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01004147 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004148 }
4149
Radek Krejciec4da802019-05-02 13:02:41 +02004150 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
4151 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004152
4153done:
4154 return ret;
4155}
4156
Radek Krejcib56c7502019-02-13 14:19:54 +01004157/**
4158 * @brief Do some checks and set the default choice's case.
4159 *
4160 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
4161 *
4162 * @param[in] ctx Compile context.
4163 * @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,
4164 * not the reference to the imported module.
4165 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
4166 * @return LY_ERR value.
4167 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004168static LY_ERR
4169lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
4170{
4171 struct lysc_node *iter, *node = (struct lysc_node*)ch;
4172 const char *prefix = NULL, *name;
4173 size_t prefix_len = 0;
4174
4175 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
4176 name = strchr(dflt, ':');
4177 if (name) {
4178 prefix = dflt;
4179 prefix_len = name - prefix;
4180 ++name;
4181 } else {
4182 name = dflt;
4183 }
Radek Krejci7f9b6512019-09-18 13:11:09 +02004184 if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004185 /* prefixed default case make sense only for the prefix of the schema itself */
4186 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4187 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
4188 prefix_len, prefix);
4189 return LY_EVALID;
4190 }
4191 ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
4192 if (!ch->dflt) {
4193 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4194 "Default case \"%s\" not found.", dflt);
4195 return LY_EVALID;
4196 }
4197 /* no mandatory nodes directly under the default case */
4198 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01004199 if (iter->parent != (struct lysc_node*)ch->dflt) {
4200 break;
4201 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004202 if (iter->flags & LYS_MAND_TRUE) {
4203 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4204 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
4205 return LY_EVALID;
4206 }
4207 }
Radek Krejci01342af2019-01-03 15:18:08 +01004208 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004209 return LY_SUCCESS;
4210}
4211
Radek Krejciccd20f12019-02-15 14:12:27 +01004212static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004213lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01004214{
4215 struct lys_module *mod;
4216 const char *prefix = NULL, *name;
4217 size_t prefix_len = 0;
4218 struct lysc_node_case *cs;
4219 struct lysc_node *node;
4220
4221 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
4222 name = strchr(dflt, ':');
4223 if (name) {
4224 prefix = dflt;
4225 prefix_len = name - prefix;
4226 ++name;
4227 } else {
4228 name = dflt;
4229 }
4230 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
4231 if (prefix) {
4232 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
4233 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004234 "Invalid deviation adding \"default\" property \"%s\" of choice. "
4235 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01004236 return LY_EVALID;
4237 }
4238 } else {
4239 mod = ctx->mod;
4240 }
4241 /* get the default case */
4242 cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
4243 if (!cs) {
4244 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004245 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01004246 return LY_EVALID;
4247 }
4248
4249 /* check that there is no mandatory node */
4250 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01004251 if (node->parent != (struct lysc_node*)cs) {
4252 break;
4253 }
Radek Krejciccd20f12019-02-15 14:12:27 +01004254 if (node->flags & LYS_MAND_TRUE) {
4255 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004256 "Invalid deviation adding \"default\" property \"%s\" of choice - "
4257 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01004258 return LY_EVALID;
4259 }
4260 }
4261
4262 /* set the default case in choice */
4263 ch->dflt = cs;
4264 cs->flags |= LYS_SET_DFLT;
4265
4266 return LY_SUCCESS;
4267}
4268
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004269/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004270 * @brief Compile parsed choice node information.
4271 * @param[in] ctx Compile context
4272 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004273 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01004274 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01004275 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4276 */
4277static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004278lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004279{
4280 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
4281 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
4282 struct lysp_node *child_p, *case_child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004283 LY_ERR ret = LY_SUCCESS;
4284
Radek Krejci056d0a82018-12-06 16:57:25 +01004285 LY_LIST_FOR(ch_p->child, child_p) {
4286 if (child_p->nodetype == LYS_CASE) {
4287 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004288 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004289 }
4290 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004291 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004292 }
4293 }
4294
4295 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01004296 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004297 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01004298 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004299
Radek Krejci9800fb82018-12-13 14:26:23 +01004300 return ret;
4301}
4302
4303/**
4304 * @brief Compile parsed anydata or anyxml node information.
4305 * @param[in] ctx Compile context
4306 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01004307 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4308 * is enriched with the any-specific information.
4309 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4310 */
4311static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004312lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004313{
4314 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
4315 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
4316 unsigned int u;
4317 LY_ERR ret = LY_SUCCESS;
4318
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004319 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Radek Krejci9800fb82018-12-13 14:26:23 +01004320
4321 if (any->flags & LYS_CONFIG_W) {
4322 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
Radek Krejcid6b76452019-09-03 17:03:03 +02004323 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML));
Radek Krejci9800fb82018-12-13 14:26:23 +01004324 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004325done:
4326 return ret;
4327}
4328
Radek Krejcib56c7502019-02-13 14:19:54 +01004329/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004330 * @brief Connect the node into the siblings list and check its name uniqueness.
4331 *
4332 * @param[in] ctx Compile context
4333 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4334 * the choice itself is expected instead of a specific case node.
4335 * @param[in] node Schema node to connect into the list.
4336 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
4337 */
4338static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004339lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004340{
4341 struct lysc_node **children;
4342
4343 if (node->nodetype == LYS_CASE) {
4344 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
4345 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004346 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01004347 }
4348 if (children) {
4349 if (!(*children)) {
4350 /* first child */
4351 *children = node;
4352 } else if (*children != node) {
4353 /* by the condition in previous branch we cover the choice/case children
4354 * - the children list is shared by the choice and the the first case, in addition
4355 * the first child of each case must be referenced from the case node. So the node is
4356 * actually always already inserted in case it is the first children - so here such
4357 * a situation actually corresponds to the first branch */
4358 /* insert at the end of the parent's children list */
4359 (*children)->prev->next = node;
4360 node->prev = (*children)->prev;
4361 (*children)->prev = node;
4362
4363 /* check the name uniqueness */
4364 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
4365 lysc_node_notifs(parent), node->name, node)) {
4366 return LY_EEXIST;
4367 }
4368 }
4369 }
4370 return LY_SUCCESS;
4371}
4372
Radek Krejci95710c92019-02-11 15:49:55 +01004373/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004374 * @brief Prepare the case structure in choice node for the new data node.
4375 *
4376 * 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
4377 * created in the choice when the first child was processed.
4378 *
4379 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004380 * @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,
4381 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004382 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4383 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4384 * @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,
4385 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004386 */
Radek Krejci056d0a82018-12-06 16:57:25 +01004387static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02004388lys_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 +01004389{
4390 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02004391 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01004392 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01004393 unsigned int u;
4394 LY_ERR ret;
4395
Radek Krejci95710c92019-02-11 15:49:55 +01004396#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01004397 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01004398 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01004399 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
4400 return NULL; \
4401 } \
4402 }
4403
Radek Krejci95710c92019-02-11 15:49:55 +01004404 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
4405 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004406
4407 /* we have to add an implicit case node into the parent choice */
4408 cs = calloc(1, sizeof(struct lysc_node_case));
4409 DUP_STRING(ctx->ctx, child->name, cs->name);
4410 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01004411 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004412 if (ch->cases && (node_p == ch->cases->prev->sp)) {
4413 /* the case is already present since the child is not its first children */
4414 return (struct lysc_node_case*)ch->cases->prev;
4415 }
Radek Krejci95710c92019-02-11 15:49:55 +01004416 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004417
4418 /* explicit parent case is not present (this is its first child) */
4419 cs = calloc(1, sizeof(struct lysc_node_case));
4420 DUP_STRING(ctx->ctx, node_p->name, cs->name);
4421 cs->flags = LYS_STATUS_MASK & node_p->flags;
4422 cs->sp = node_p;
4423
Radek Krejcib1b59152019-01-07 13:21:56 +01004424 /* 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 +02004425 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01004426
4427 if (node_p->when) {
4428 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02004429 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004430 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01004431 (*when)->context = lysc_xpath_context(ch->parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004432 }
Radek Krejciec4da802019-05-02 13:02:41 +02004433 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01004434 } else {
4435 LOGINT(ctx->ctx);
4436 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01004437 }
4438 cs->module = ctx->mod;
4439 cs->prev = (struct lysc_node*)cs;
4440 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02004441 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01004442 cs->parent = (struct lysc_node*)ch;
4443 cs->child = child;
4444
4445 return cs;
4446error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02004447 if (cs) {
4448 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
4449 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004450 return NULL;
4451
4452#undef UNIQUE_CHECK
4453}
4454
Radek Krejcib56c7502019-02-13 14:19:54 +01004455/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004456 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004457 *
4458 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004459 * @param[in] node Target node where the config is supposed to be changed.
4460 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004461 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4462 * 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 +01004463 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4464 * @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 +01004465 * @return LY_ERR value.
4466 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004467static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004468lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci327de162019-06-14 12:52:07 +02004469 int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004470{
4471 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004472 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004473
4474 if (config == (node->flags & LYS_CONFIG_MASK)) {
4475 /* nothing to do */
4476 return LY_SUCCESS;
4477 }
4478
4479 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004480 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004481 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4482 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004483 "Invalid %s of config - configuration node cannot be child of any state data node.",
4484 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004485 return LY_EVALID;
4486 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004487 node->flags |= LYS_SET_CONFIG;
4488 } else {
4489 if (node->flags & LYS_SET_CONFIG) {
4490 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4491 /* setting config flags, but have node with explicit config true */
4492 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004493 "Invalid %s of config - configuration node cannot be child of any state data node.",
4494 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004495 return LY_EVALID;
4496 }
4497 /* do not change config on nodes where the config is explicitely set, this does not apply to
4498 * nodes, which are being changed explicitly (targets of refine or deviation) */
4499 return LY_SUCCESS;
4500 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004501 }
4502 node->flags &= ~LYS_CONFIG_MASK;
4503 node->flags |= config;
4504
4505 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004506 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004507 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004508 }
4509
Radek Krejci76b3e962018-12-14 17:01:25 +01004510 return LY_SUCCESS;
4511}
4512
Radek Krejcib56c7502019-02-13 14:19:54 +01004513/**
4514 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4515 *
4516 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4517 * the flag to such parents from a mandatory children.
4518 *
4519 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4520 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4521 * (mandatory children was removed).
4522 */
Radek Krejcife909632019-02-12 15:34:42 +01004523void
4524lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4525{
4526 struct lysc_node *iter;
4527
4528 if (add) { /* set flag */
4529 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4530 parent = parent->parent) {
4531 parent->flags |= LYS_MAND_TRUE;
4532 }
4533 } else { /* unset flag */
4534 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004535 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004536 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004537 /* there is another mandatory node */
4538 return;
4539 }
4540 }
4541 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4542 parent->flags &= ~LYS_MAND_TRUE;
4543 }
4544 }
4545}
4546
Radek Krejci056d0a82018-12-06 16:57:25 +01004547/**
Radek Krejci3641f562019-02-13 15:38:40 +01004548 * @brief Internal sorting process for the lys_compile_augment_sort().
4549 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4550 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4551 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4552 */
4553static void
4554lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4555{
4556 unsigned int v;
4557 size_t len;
4558
4559 len = strlen(aug_p->nodeid);
4560 LY_ARRAY_FOR(result, v) {
4561 if (strlen(result[v]->nodeid) <= len) {
4562 continue;
4563 }
4564 if (v < LY_ARRAY_SIZE(result)) {
4565 /* move the rest of array */
4566 memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result);
4567 break;
4568 }
4569 }
4570 result[v] = aug_p;
4571 LY_ARRAY_INCREMENT(result);
4572}
4573
4574/**
4575 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4576 *
4577 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4578 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4579 *
4580 * @param[in] ctx Compile context.
4581 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4582 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4583 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4584 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4585 * @return LY_ERR value.
4586 */
4587LY_ERR
4588lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4589{
4590 struct lysp_augment **result = NULL;
4591 unsigned int u, v;
4592 size_t count = 0;
4593
4594 assert(augments);
4595
4596 /* get count of the augments in module and all its submodules */
4597 if (aug_p) {
4598 count += LY_ARRAY_SIZE(aug_p);
4599 }
4600 LY_ARRAY_FOR(inc_p, u) {
4601 if (inc_p[u].submodule->augments) {
4602 count += LY_ARRAY_SIZE(inc_p[u].submodule->augments);
4603 }
4604 }
4605
4606 if (!count) {
4607 *augments = NULL;
4608 return LY_SUCCESS;
4609 }
4610 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4611
4612 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4613 * together, so there can be even /z/y betwwen them. */
4614 LY_ARRAY_FOR(aug_p, u) {
4615 lys_compile_augment_sort_(&aug_p[u], result);
4616 }
4617 LY_ARRAY_FOR(inc_p, u) {
4618 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4619 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4620 }
4621 }
4622
4623 *augments = result;
4624 return LY_SUCCESS;
4625}
4626
4627/**
4628 * @brief Compile the parsed augment connecting it into its target.
4629 *
4630 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4631 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4632 * are already implemented and compiled.
4633 *
4634 * @param[in] ctx Compile context.
4635 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004636 * @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
4637 * children in case of the augmenting uses data.
4638 * @return LY_SUCCESS on success.
4639 * @return LY_EVALID on failure.
4640 */
4641LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004642lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004643{
4644 LY_ERR ret = LY_SUCCESS;
4645 struct lysp_node *node_p, *case_node_p;
4646 struct lysc_node *target; /* target target of the augment */
4647 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004648 struct lysc_when **when, *when_shared;
4649 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004650 uint16_t flags = 0;
4651 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02004652 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004653
Radek Krejci327de162019-06-14 12:52:07 +02004654 lysc_update_path(ctx, NULL, "{augment}");
4655 lysc_update_path(ctx, NULL, aug_p->nodeid);
4656
Radek Krejci7af64242019-02-18 13:07:53 +01004657 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004658 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004659 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004660 if (ret != LY_SUCCESS) {
4661 if (ret == LY_EDENIED) {
4662 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4663 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4664 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4665 }
4666 return LY_EVALID;
4667 }
4668
4669 /* check for mandatory nodes
4670 * - new cases augmenting some choice can have mandatory nodes
4671 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4672 */
Radek Krejci733988a2019-02-15 15:12:44 +01004673 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004674 allow_mandatory = 1;
4675 }
4676
4677 when_shared = NULL;
4678 LY_LIST_FOR(aug_p->child, node_p) {
4679 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4680 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
4681 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004682 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4683 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004684 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004685 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4686 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004687 return LY_EVALID;
4688 }
4689
4690 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004691 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004692 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004693 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004694 } else {
4695 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004696 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004697 }
4698 }
Radek Krejciec4da802019-05-02 13:02:41 +02004699 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004700
Radek Krejcife13da42019-02-15 14:51:01 +01004701 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4702 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004703 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004704 /* 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 +01004705 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 +01004706 } else if (target->nodetype == LYS_CHOICE) {
4707 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4708 node = ((struct lysc_node_choice*)target)->cases->prev;
4709 } else {
4710 /* the compiled node is the last child of the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004711 node = lysc_node_children(target, flags)->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004712 }
4713
Radek Krejci733988a2019-02-15 15:12:44 +01004714 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004715 node->flags &= ~LYS_MAND_TRUE;
4716 lys_compile_mandatory_parents(target, 0);
4717 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004718 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004719 return LY_EVALID;
4720 }
4721
4722 /* pass augment's when to all the children */
4723 if (aug_p->when) {
4724 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4725 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004726 ret = lys_compile_when(ctx, aug_p->when, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004727 LY_CHECK_GOTO(ret, error);
4728 (*when)->context = lysc_xpath_context(target);
4729 when_shared = *when;
4730 } else {
4731 ++when_shared->refcount;
4732 (*when) = when_shared;
4733 }
4734 }
4735 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004736
Radek Krejciec4da802019-05-02 13:02:41 +02004737 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004738 switch (target->nodetype) {
4739 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004740 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004741 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004742 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004743 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004744 break;
4745 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004746 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004747 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004748 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004749 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004750 break;
4751 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004752 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004753 if (aug_p->actions) {
4754 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004755 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4756 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004757 return LY_EVALID;
4758 }
4759 if (aug_p->notifs) {
4760 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004761 "Invalid augment of %s node which is not allowed to contain Notification node \"%s\".",
4762 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004763 return LY_EVALID;
4764 }
4765 }
Radek Krejci3641f562019-02-13 15:38:40 +01004766
Radek Krejci327de162019-06-14 12:52:07 +02004767 lysc_update_path(ctx, NULL, NULL);
4768 lysc_update_path(ctx, NULL, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004769error:
Radek Krejciec4da802019-05-02 13:02:41 +02004770 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004771 return ret;
4772}
4773
4774/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004775 * @brief Apply refined or deviated mandatory flag to the target node.
4776 *
4777 * @param[in] ctx Compile context.
4778 * @param[in] node Target node where the mandatory property is supposed to be changed.
4779 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004780 * @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 +01004781 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4782 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4783 * 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 +01004784 * @return LY_ERR value.
4785 */
4786static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004787lys_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 +01004788{
4789 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4790 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004791 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4792 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004793 return LY_EVALID;
4794 }
4795
4796 if (mandatory_flag & LYS_MAND_TRUE) {
4797 /* check if node has default value */
4798 if (node->nodetype & LYS_LEAF) {
4799 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004800 if (refine_flag) {
4801 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004802 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004803 return LY_EVALID;
4804 }
Radek Krejcia1911222019-07-22 17:24:50 +02004805 } else if (((struct lysc_node_leaf*)node)->dflt) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004806 /* remove the default value taken from the leaf's type */
Radek Krejcia1911222019-07-22 17:24:50 +02004807 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejci474f9b82019-07-24 11:36:37 +02004808
4809 /* update the list of incomplete default values if needed */
4810 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
4811
Radek Krejcia1911222019-07-22 17:24:50 +02004812 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4813 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4814 free(leaf->dflt);
4815 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004816 leaf->dflt_mod = NULL;
Radek Krejcif1421c22019-02-19 13:05:20 +01004817 }
4818 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004819 if (refine_flag) {
4820 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004821 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004822 return LY_EVALID;
4823 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004824 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004825 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004826 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 +01004827 return LY_EVALID;
4828 }
4829
4830 node->flags &= ~LYS_MAND_FALSE;
4831 node->flags |= LYS_MAND_TRUE;
4832 lys_compile_mandatory_parents(node->parent, 1);
4833 } else {
4834 /* make mandatory false */
4835 node->flags &= ~LYS_MAND_TRUE;
4836 node->flags |= LYS_MAND_FALSE;
4837 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcia1911222019-07-22 17:24:50 +02004838 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 +01004839 /* get the type's default value if any */
Radek Krejcia1911222019-07-22 17:24:50 +02004840 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004841 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02004842 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4843 leaf->dflt->realtype = leaf->type->dflt->realtype;
4844 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
4845 leaf->dflt->realtype->refcount++;
Radek Krejcif1421c22019-02-19 13:05:20 +01004846 }
4847 }
4848 return LY_SUCCESS;
4849}
4850
4851/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004852 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4853 * If present, also apply uses's modificators.
4854 *
4855 * @param[in] ctx Compile context
4856 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004857 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4858 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4859 * the compile context.
4860 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4861 */
4862static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004863lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent)
Radek Krejcie86bf772018-12-14 11:39:53 +01004864{
4865 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004866 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004867 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004868 struct lysc_node_container context_node_fake =
4869 {.nodetype = LYS_CONTAINER,
4870 .module = ctx->mod,
4871 .flags = parent ? parent->flags : 0,
4872 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004873 .prev = (struct lysc_node*)&context_node_fake,
4874 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004875 struct lysp_grp *grp = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01004876 unsigned int u, v, grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004877 int found;
4878 const char *id, *name, *prefix;
4879 size_t prefix_len, name_len;
4880 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004881 struct lysp_refine *rfn;
Radek Krejcia1911222019-07-22 17:24:50 +02004882 LY_ERR ret = LY_EVALID, rc;
Radek Krejcif2271f12019-01-07 16:42:23 +01004883 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004884 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004885 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004886 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004887 struct lysp_augment **augments = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004888 unsigned int actions_index, notifs_index;
4889 struct lysc_notif **notifs = NULL;
4890 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004891
4892 /* search for the grouping definition */
4893 found = 0;
4894 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004895 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004896 if (prefix) {
4897 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4898 if (!mod) {
4899 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004900 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004901 return LY_EVALID;
4902 }
4903 } else {
4904 mod = ctx->mod_def;
4905 }
4906 if (mod == ctx->mod_def) {
4907 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004908 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004909 LY_ARRAY_FOR(grp, u) {
4910 if (!strcmp(grp[u].name, name)) {
4911 grp = &grp[u];
4912 found = 1;
4913 break;
4914 }
4915 }
4916 }
4917 }
4918 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004919 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004920 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004921 if (grp) {
4922 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
4923 if (!strcmp(grp[u].name, name)) {
4924 grp = &grp[u];
4925 found = 1;
4926 }
4927 }
4928 }
4929 if (!found && mod->parsed->includes) {
4930 /* ... and all the submodules */
4931 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
4932 grp = mod->parsed->includes[u].submodule->groupings;
4933 if (grp) {
4934 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
4935 if (!strcmp(grp[v].name, name)) {
4936 grp = &grp[v];
4937 found = 1;
4938 }
4939 }
4940 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004941 }
4942 }
4943 }
4944 if (!found) {
4945 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4946 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4947 return LY_EVALID;
4948 }
4949
4950 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4951 grp_stack_count = ctx->groupings.count;
4952 ly_set_add(&ctx->groupings, (void*)grp, 0);
4953 if (grp_stack_count == ctx->groupings.count) {
4954 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4955 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4956 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4957 return LY_EVALID;
4958 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004959 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4960 /* remember that the grouping is instantiated to avoid its standalone validation */
4961 grp->flags |= LYS_USED_GRP;
4962 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004963
4964 /* switch context's mod_def */
4965 mod_old = ctx->mod_def;
4966 ctx->mod_def = mod;
4967
4968 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004969 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 +01004970
Radek Krejcifc11bd72019-04-11 16:00:05 +02004971 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004972 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004973 /* 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 +02004974 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 +01004975
4976 /* some preparation for applying refines */
4977 if (grp->data == node_p) {
4978 /* remember the first child */
Radek Krejci684faf22019-05-27 14:31:16 +02004979 if (parent) {
4980 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
4981 } else if (ctx->mod->compiled->data) {
4982 child = ctx->mod->compiled->data;
4983 } else {
4984 child = NULL;
4985 }
4986 context_node_fake.child = child ? child->prev : NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004987 }
4988 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004989 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004990 LY_LIST_FOR(context_node_fake.child, child) {
4991 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004992
Radek Krejcifc11bd72019-04-11 16:00:05 +02004993 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004994 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004995 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004996 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004997 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, when), cleanup);
Radek Krejcib56c7502019-02-13 14:19:54 +01004998 (*when)->context = lysc_xpath_context(parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004999 when_shared = *when;
5000 } else {
5001 ++when_shared->refcount;
5002 (*when) = when_shared;
5003 }
5004 }
Radek Krejci01342af2019-01-03 15:18:08 +01005005 }
5006 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005007 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01005008 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005009 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejciec4da802019-05-02 13:02:41 +02005010 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 +01005011 }
5012
Radek Krejcifc11bd72019-04-11 16:00:05 +02005013 /* compile actions */
5014 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
5015 if (actions) {
5016 actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02005017 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005018 if (*actions && (uses_p->augments || uses_p->refines)) {
5019 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
5020 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup);
5021 LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index;
5022 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
5023 }
5024 }
5025
5026 /* compile notifications */
5027 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
5028 if (notifs) {
5029 notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02005030 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005031 if (*notifs && (uses_p->augments || uses_p->refines)) {
5032 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
5033 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup);
5034 LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index;
5035 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
5036 }
5037 }
5038
5039
Radek Krejci3641f562019-02-13 15:38:40 +01005040 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005041 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01005042 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02005043 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01005044 }
Radek Krejci12fb9142019-01-08 09:45:30 +01005045
Radek Krejcif0089082019-01-07 16:42:01 +01005046 /* reload previous context's mod_def */
5047 ctx->mod_def = mod_old;
Radek Krejci327de162019-06-14 12:52:07 +02005048 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01005049
Radek Krejci76b3e962018-12-14 17:01:25 +01005050 /* apply refine */
5051 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02005052 lysc_update_path(ctx, NULL, rfn->nodeid);
5053
Radek Krejci7af64242019-02-18 13:07:53 +01005054 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 +01005055 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02005056 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01005057 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01005058
5059 /* default value */
5060 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01005061 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01005062 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005063 "Invalid refine of default - %s cannot hold %d default values.",
5064 lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005065 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01005066 }
5067 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
5068 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005069 "Invalid refine of default - %s cannot hold default value(s).",
5070 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005071 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01005072 }
5073 if (node->nodetype == LYS_LEAF) {
Radek Krejcia1911222019-07-22 17:24:50 +02005074 struct ly_err_item *err = NULL;
5075 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
5076 if (leaf->dflt) {
5077 /* remove the previous default value */
Radek Krejci474f9b82019-07-24 11:36:37 +02005078 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02005079 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5080 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5081 } else {
5082 /* prepare a new one */
5083 leaf->dflt = calloc(1, sizeof *leaf->dflt);
5084 leaf->dflt->realtype = leaf->type;
5085 }
5086 /* parse the new one */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005087 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005088 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]),
5089 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005090 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005091 leaf->dflt->realtype->refcount++;
5092 if (err) {
5093 ly_err_print(err);
5094 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5095 "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg);
5096 ly_err_free(err);
5097 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02005098 if (rc == LY_EINCOMPLETE) {
5099 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02005100 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02005101
5102 /* but in general result is so far ok */
5103 rc = LY_SUCCESS;
5104 }
Radek Krejcia1911222019-07-22 17:24:50 +02005105 LY_CHECK_GOTO(rc, cleanup);
5106 leaf->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01005107 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejcia1911222019-07-22 17:24:50 +02005108 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
5109
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005110 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01005111 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5112 "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 +02005113 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01005114 }
Radek Krejcia1911222019-07-22 17:24:50 +02005115
5116 /* remove previous set of default values */
5117 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci474f9b82019-07-24 11:36:37 +02005118 lysc_incomplete_dflts_remove(ctx, llist->dflts[u]);
Radek Krejcia1911222019-07-22 17:24:50 +02005119 llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]);
5120 lysc_type_free(ctx->ctx, llist->dflts[u]->realtype);
5121 free(llist->dflts[u]);
Radek Krejci76b3e962018-12-14 17:01:25 +01005122 }
Radek Krejcia1911222019-07-22 17:24:50 +02005123 LY_ARRAY_FREE(llist->dflts);
5124 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005125 LY_ARRAY_FREE(llist->dflts_mods);
5126 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005127
5128 /* create the new set of the default values */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005129 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02005130 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005131 LY_ARRAY_FOR(rfn->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005132 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005133 LY_ARRAY_INCREMENT(llist->dflts_mods);
5134 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005135 LY_ARRAY_INCREMENT(llist->dflts);
5136 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
5137 llist->dflts[u]->realtype = llist->type;
Radek Krejci1c0c3442019-07-23 16:08:47 +02005138 rc = llist->type->plugin->store(ctx->ctx, llist->type, rfn->dflts[u], strlen(rfn->dflts[u]),
Radek Krejcia1911222019-07-22 17:24:50 +02005139 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005140 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005141 llist->dflts[u]->realtype->refcount++;
5142 if (err) {
5143 ly_err_print(err);
5144 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5145 "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg);
5146 ly_err_free(err);
5147 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02005148 if (rc == LY_EINCOMPLETE) {
5149 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02005150 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02005151
5152 /* but in general result is so far ok */
5153 rc = LY_SUCCESS;
5154 }
5155 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005156 }
Radek Krejcia1911222019-07-22 17:24:50 +02005157 llist->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01005158 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01005159 if (((struct lysc_node_choice*)node)->dflt) {
5160 /* unset LYS_SET_DFLT from the current default case */
5161 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
5162 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005163 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 +01005164 }
5165 }
5166
Radek Krejci12fb9142019-01-08 09:45:30 +01005167 /* description */
5168 if (rfn->dsc) {
5169 FREE_STRING(ctx->ctx, node->dsc);
5170 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
5171 }
5172
5173 /* reference */
5174 if (rfn->ref) {
5175 FREE_STRING(ctx->ctx, node->ref);
5176 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
5177 }
Radek Krejci76b3e962018-12-14 17:01:25 +01005178
5179 /* config */
5180 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005181 if (!flags) {
Radek Krejci327de162019-06-14 12:52:07 +02005182 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005183 } else {
5184 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
5185 flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path);
5186 }
Radek Krejci76b3e962018-12-14 17:01:25 +01005187 }
5188
5189 /* mandatory */
5190 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci327de162019-06-14 12:52:07 +02005191 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005192 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01005193
5194 /* presence */
5195 if (rfn->presence) {
5196 if (node->nodetype != LYS_CONTAINER) {
5197 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005198 "Invalid refine of presence statement - %s cannot hold the presence statement.",
5199 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005200 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01005201 }
5202 node->flags |= LYS_PRESENCE;
5203 }
Radek Krejci9a564c92019-01-07 14:53:57 +01005204
5205 /* must */
5206 if (rfn->musts) {
5207 switch (node->nodetype) {
5208 case LYS_LEAF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005209 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 +01005210 break;
5211 case LYS_LEAFLIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005212 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 +01005213 break;
5214 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005215 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 +01005216 break;
5217 case LYS_CONTAINER:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005218 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 +01005219 break;
5220 case LYS_ANYXML:
5221 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005222 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 +01005223 break;
5224 default:
5225 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005226 "Invalid refine of must statement - %s cannot hold any must statement.",
5227 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005228 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01005229 }
5230 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005231
5232 /* min/max-elements */
5233 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5234 switch (node->nodetype) {
5235 case LYS_LEAFLIST:
5236 if (rfn->flags & LYS_SET_MAX) {
5237 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5238 }
5239 if (rfn->flags & LYS_SET_MIN) {
5240 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005241 if (rfn->min) {
5242 node->flags |= LYS_MAND_TRUE;
5243 lys_compile_mandatory_parents(node->parent, 1);
5244 } else {
5245 node->flags &= ~LYS_MAND_TRUE;
5246 lys_compile_mandatory_parents(node->parent, 0);
5247 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005248 }
5249 break;
5250 case LYS_LIST:
5251 if (rfn->flags & LYS_SET_MAX) {
5252 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5253 }
5254 if (rfn->flags & LYS_SET_MIN) {
5255 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005256 if (rfn->min) {
5257 node->flags |= LYS_MAND_TRUE;
5258 lys_compile_mandatory_parents(node->parent, 1);
5259 } else {
5260 node->flags &= ~LYS_MAND_TRUE;
5261 lys_compile_mandatory_parents(node->parent, 0);
5262 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005263 }
5264 break;
5265 default:
5266 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005267 "Invalid refine of %s statement - %s cannot hold this statement.",
5268 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005269 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01005270 }
5271 }
Radek Krejcif0089082019-01-07 16:42:01 +01005272
5273 /* if-feature */
5274 if (rfn->iffeatures) {
5275 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02005276 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01005277 }
Radek Krejci327de162019-06-14 12:52:07 +02005278
5279 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01005280 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005281
Radek Krejcif2271f12019-01-07 16:42:23 +01005282 /* do some additional checks of the changed nodes when all the refines are applied */
5283 for (u = 0; u < refined.count; ++u) {
5284 node = (struct lysc_node*)refined.objs[u];
5285 rfn = &uses_p->refines[u];
Radek Krejci327de162019-06-14 12:52:07 +02005286 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01005287
5288 /* check possible conflict with default value (default added, mandatory left true) */
5289 if ((node->flags & LYS_MAND_TRUE) &&
5290 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
5291 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
5292 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005293 "Invalid refine of default - the node is mandatory.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005294 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005295 }
5296
5297 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5298 if (node->nodetype == LYS_LIST) {
5299 min = ((struct lysc_node_list*)node)->min;
5300 max = ((struct lysc_node_list*)node)->max;
5301 } else {
5302 min = ((struct lysc_node_leaflist*)node)->min;
5303 max = ((struct lysc_node_leaflist*)node)->max;
5304 }
5305 if (min > max) {
5306 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005307 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
5308 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005309 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005310 }
5311 }
5312 }
5313
Radek Krejci327de162019-06-14 12:52:07 +02005314 lysc_update_path(ctx, NULL, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005315 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005316
5317cleanup:
5318 /* fix connection of the children nodes from fake context node back into the parent */
5319 if (context_node_fake.child) {
5320 context_node_fake.child->prev = child;
5321 }
5322 LY_LIST_FOR(context_node_fake.child, child) {
5323 child->parent = parent;
5324 }
5325
5326 if (uses_p->augments || uses_p->refines) {
5327 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005328 if (context_node_fake.actions) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005329 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
5330 LY_ARRAY_FREE(context_node_fake.actions);
5331 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005332 if (context_node_fake.notifs) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005333 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
5334 LY_ARRAY_FREE(context_node_fake.notifs);
5335 }
5336 }
5337
Radek Krejcie86bf772018-12-14 11:39:53 +01005338 /* reload previous context's mod_def */
5339 ctx->mod_def = mod_old;
5340 /* remove the grouping from the stack for circular groupings dependency check */
5341 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5342 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01005343 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01005344 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005345
5346 return ret;
5347}
5348
Radek Krejci327de162019-06-14 12:52:07 +02005349static int
5350lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5351{
5352 struct lysp_node *iter;
5353 int len = 0;
5354
5355 *path = NULL;
5356 for (iter = node; iter && len >= 0; iter = iter->parent) {
5357 char *s = *path;
5358 char *id;
5359
5360 switch (iter->nodetype) {
5361 case LYS_USES:
5362 asprintf(&id, "{uses='%s'}", iter->name);
5363 break;
5364 case LYS_GROUPING:
5365 asprintf(&id, "{grouping='%s'}", iter->name);
5366 break;
5367 case LYS_AUGMENT:
5368 asprintf(&id, "{augment='%s'}", iter->name);
5369 break;
5370 default:
5371 id = strdup(iter->name);
5372 break;
5373 }
5374
5375 if (!iter->parent) {
5376 /* print prefix */
5377 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5378 } else {
5379 /* prefix is the same as in parent */
5380 len = asprintf(path, "/%s%s", id, s ? s : "");
5381 }
5382 free(s);
5383 free(id);
5384 }
5385
5386 if (len < 0) {
5387 free(*path);
5388 *path = NULL;
5389 } else if (len == 0) {
5390 *path = strdup("/");
5391 len = 1;
5392 }
5393 return len;
5394}
5395
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005396/**
5397 * @brief Validate groupings that were defined but not directly used in the schema itself.
5398 *
5399 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5400 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5401 */
5402static LY_ERR
5403lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5404{
5405 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005406 char *path;
5407 int len;
5408
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005409 struct lysp_node_uses fake_uses = {
5410 .parent = node_p,
5411 .nodetype = LYS_USES,
5412 .flags = 0, .next = NULL,
5413 .name = grp->name,
5414 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5415 .refines = NULL, .augments = NULL
5416 };
5417 struct lysc_node_container fake_container = {
5418 .nodetype = LYS_CONTAINER,
5419 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5420 .module = ctx->mod,
5421 .sp = NULL, .parent = NULL, .next = NULL,
5422 .prev = (struct lysc_node*)&fake_container,
5423 .name = "fake",
5424 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5425 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5426 };
5427
5428 if (grp->parent) {
5429 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5430 }
Radek Krejci327de162019-06-14 12:52:07 +02005431
5432 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5433 if (len < 0) {
5434 LOGMEM(ctx->ctx);
5435 return LY_EMEM;
5436 }
5437 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
5438 ctx->path_len = (uint16_t)len;
5439 free(path);
5440
5441 lysc_update_path(ctx, NULL, "{grouping}");
5442 lysc_update_path(ctx, NULL, grp->name);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005443 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container);
Radek Krejci327de162019-06-14 12:52:07 +02005444 lysc_update_path(ctx, NULL, NULL);
5445 lysc_update_path(ctx, NULL, NULL);
5446
5447 ctx->path_len = 1;
5448 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005449
5450 /* cleanup */
5451 lysc_node_container_free(ctx->ctx, &fake_container);
5452
5453 return ret;
5454}
Radek Krejcife909632019-02-12 15:34:42 +01005455
Radek Krejcie86bf772018-12-14 11:39:53 +01005456/**
Radek Krejcia3045382018-11-22 14:30:31 +01005457 * @brief Compile parsed schema node information.
5458 * @param[in] ctx Compile context
5459 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005460 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5461 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5462 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005463 * @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).
5464 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005465 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5466 */
Radek Krejci19a96102018-11-15 13:38:09 +01005467static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005468lys_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 +01005469{
5470 LY_ERR ret = LY_EVALID;
Radek Krejci056d0a82018-12-06 16:57:25 +01005471 struct lysc_node *node;
5472 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01005473 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01005474 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02005475 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01005476
Radek Krejci327de162019-06-14 12:52:07 +02005477 if (node_p->nodetype != LYS_USES) {
5478 lysc_update_path(ctx, parent, node_p->name);
5479 } else {
5480 lysc_update_path(ctx, NULL, "{uses}");
5481 lysc_update_path(ctx, NULL, node_p->name);
5482 }
5483
Radek Krejci19a96102018-11-15 13:38:09 +01005484 switch (node_p->nodetype) {
5485 case LYS_CONTAINER:
5486 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
5487 node_compile_spec = lys_compile_node_container;
5488 break;
5489 case LYS_LEAF:
5490 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
5491 node_compile_spec = lys_compile_node_leaf;
5492 break;
5493 case LYS_LIST:
5494 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005495 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005496 break;
5497 case LYS_LEAFLIST:
5498 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005499 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005500 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005501 case LYS_CHOICE:
5502 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005503 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005504 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005505 case LYS_ANYXML:
5506 case LYS_ANYDATA:
5507 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005508 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005509 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005510 case LYS_USES:
Radek Krejci327de162019-06-14 12:52:07 +02005511 ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent);
5512 lysc_update_path(ctx, NULL, NULL);
5513 lysc_update_path(ctx, NULL, NULL);
5514 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005515 default:
5516 LOGINT(ctx->ctx);
5517 return LY_EINT;
5518 }
5519 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5520 node->nodetype = node_p->nodetype;
5521 node->module = ctx->mod;
5522 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005523 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005524
5525 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02005526 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005527 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005528 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02005529 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5530 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005531 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005532 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005533 node->flags |= LYS_CONFIG_R;
5534 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005535 /* config not explicitely set, inherit it from parent */
5536 if (parent) {
5537 node->flags |= parent->flags & LYS_CONFIG_MASK;
5538 } else {
5539 /* default is config true */
5540 node->flags |= LYS_CONFIG_W;
5541 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005542 } else {
5543 /* config set explicitely */
5544 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01005545 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005546 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5547 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5548 "Configuration node cannot be child of any state data node.");
5549 goto error;
5550 }
Radek Krejci19a96102018-11-15 13:38:09 +01005551
Radek Krejcia6d57732018-11-29 13:40:37 +01005552 /* *list ordering */
5553 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5554 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005555 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02005556 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5557 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005558 node->flags &= ~LYS_ORDBY_MASK;
5559 node->flags |= LYS_ORDBY_SYSTEM;
5560 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5561 /* default ordering is system */
5562 node->flags |= LYS_ORDBY_SYSTEM;
5563 }
5564 }
5565
Radek Krejci19a96102018-11-15 13:38:09 +01005566 /* status - it is not inherited by specification, but it does not make sense to have
5567 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01005568 if (!parent || parent->nodetype != LYS_CHOICE) {
5569 /* in case of choice/case's children, postpone the check to the moment we know if
5570 * 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 +01005571 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 +01005572 }
5573
Radek Krejciec4da802019-05-02 13:02:41 +02005574 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005575 node->sp = node_p;
5576 }
5577 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01005578 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
5579 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01005580 if (node_p->when) {
5581 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02005582 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01005583 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01005584 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +01005585 }
Radek Krejciec4da802019-05-02 13:02:41 +02005586 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005587
5588 /* nodetype-specific part */
Radek Krejciec4da802019-05-02 13:02:41 +02005589 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005590
Radek Krejci0935f412019-08-20 16:15:18 +02005591 COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error);
5592
Radek Krejcife909632019-02-12 15:34:42 +01005593 /* inherit LYS_MAND_TRUE in parent containers */
5594 if (node->flags & LYS_MAND_TRUE) {
5595 lys_compile_mandatory_parents(parent, 1);
5596 }
5597
Radek Krejci327de162019-06-14 12:52:07 +02005598 lysc_update_path(ctx, NULL, NULL);
5599
Radek Krejci19a96102018-11-15 13:38:09 +01005600 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01005601 if (parent) {
5602 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci327de162019-06-14 12:52:07 +02005603 if (node_p->parent->nodetype == LYS_CASE) {
5604 lysc_update_path(ctx, parent, node_p->parent->name);
5605 } else {
5606 lysc_update_path(ctx, parent, node->name);
5607 }
Radek Krejciec4da802019-05-02 13:02:41 +02005608 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01005609 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01005610 if (uses_status) {
5611
5612 }
Radek Krejci056d0a82018-12-06 16:57:25 +01005613 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01005614 * it directly gets the same status flags as the choice;
5615 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005616 LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01005617 node->parent = (struct lysc_node*)cs;
Radek Krejci327de162019-06-14 12:52:07 +02005618 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005619 } else { /* other than choice */
Radek Krejci327de162019-06-14 12:52:07 +02005620 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005621 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01005622 }
Radek Krejciec4da802019-05-02 13:02:41 +02005623 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 +02005624
5625 if (parent->nodetype == LYS_CHOICE) {
5626 lysc_update_path(ctx, NULL, NULL);
5627 }
Radek Krejci19a96102018-11-15 13:38:09 +01005628 } else {
5629 /* top-level element */
5630 if (!ctx->mod->compiled->data) {
5631 ctx->mod->compiled->data = node;
5632 } else {
5633 /* insert at the end of the module's top-level nodes list */
5634 ctx->mod->compiled->data->prev->next = node;
5635 node->prev = ctx->mod->compiled->data->prev;
5636 ctx->mod->compiled->data->prev = node;
5637 }
Radek Krejci327de162019-06-14 12:52:07 +02005638 lysc_update_path(ctx, parent, node->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01005639 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
5640 ctx->mod->compiled->notifs, node->name, node)) {
5641 return LY_EVALID;
5642 }
Radek Krejci19a96102018-11-15 13:38:09 +01005643 }
Radek Krejci327de162019-06-14 12:52:07 +02005644 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005645
5646 return LY_SUCCESS;
5647
5648error:
5649 lysc_node_free(ctx->ctx, node);
5650 return ret;
5651}
5652
Radek Krejciccd20f12019-02-15 14:12:27 +01005653static void
5654lysc_disconnect(struct lysc_node *node)
5655{
Radek Krejci0a048dd2019-02-18 16:01:43 +01005656 struct lysc_node *parent, *child, *prev = NULL, *next;
5657 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005658 int remove_cs = 0;
5659
5660 parent = node->parent;
5661
5662 /* parent's first child */
5663 if (!parent) {
5664 return;
5665 }
5666 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005667 cs = (struct lysc_node_case*)node;
5668 } else if (parent->nodetype == LYS_CASE) {
5669 /* disconnecting some node in a case */
5670 cs = (struct lysc_node_case*)parent;
5671 parent = cs->parent;
5672 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
5673 if (child == node) {
5674 if (cs->child == child) {
5675 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5676 /* case with a single child -> remove also the case */
5677 child->parent = NULL;
5678 remove_cs = 1;
5679 } else {
5680 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005681 }
5682 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005683 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005684 }
5685 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005686 if (!remove_cs) {
5687 cs = NULL;
5688 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005689 } else if (lysc_node_children(parent, node->flags) == node) {
5690 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005691 }
5692
5693 if (cs) {
5694 if (remove_cs) {
5695 /* cs has only one child which is being also removed */
5696 lysc_disconnect((struct lysc_node*)cs);
5697 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5698 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005699 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5700 /* default case removed */
5701 ((struct lysc_node_choice*)parent)->dflt = NULL;
5702 }
5703 if (((struct lysc_node_choice*)parent)->cases == cs) {
5704 /* first case removed */
5705 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5706 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005707 if (cs->child) {
5708 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5709 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5710 prev = cs->child->prev;
5711 } /* else all the children are under a single case */
5712 LY_LIST_FOR_SAFE(cs->child, next, child) {
5713 if (child->parent != (struct lysc_node*)cs) {
5714 break;
5715 }
5716 lysc_node_free(node->module->ctx, child);
5717 }
5718 if (prev) {
5719 if (prev->next) {
5720 prev->next = child;
5721 }
5722 if (child) {
5723 child->prev = prev;
5724 } else {
5725 /* link from the first child under the cases */
5726 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5727 }
5728 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005729 }
5730 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005731 }
5732
5733 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005734 if (node->prev->next) {
5735 node->prev->next = node->next;
5736 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005737 if (node->next) {
5738 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005739 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005740 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005741 if (child) {
5742 child->prev = node->prev;
5743 }
5744 } else if (((struct lysc_node_choice*)parent)->cases) {
5745 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005746 }
5747}
5748
5749LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005750lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
Radek Krejciccd20f12019-02-15 14:12:27 +01005751{
Radek Krejcia1911222019-07-22 17:24:50 +02005752 LY_ERR ret = LY_EVALID, rc;
Radek Krejciccd20f12019-02-15 14:12:27 +01005753 struct ly_set devs_p = {0};
5754 struct ly_set targets = {0};
5755 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005756 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005757 struct lysc_action *rpcs;
5758 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005759 struct lysp_deviation *dev;
5760 struct lysp_deviate *d, **dp_new;
5761 struct lysp_deviate_add *d_add;
5762 struct lysp_deviate_del *d_del;
5763 struct lysp_deviate_rpl *d_rpl;
Radek Krejci7af64242019-02-18 13:07:53 +01005764 unsigned int u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005765 struct lysc_deviation {
5766 const char *nodeid;
5767 struct lysc_node *target; /* target node of the deviation */
5768 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005769 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005770 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5771 } **devs = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005772 int i, changed_type;
Radek Krejciccd20f12019-02-15 14:12:27 +01005773 size_t prefix_len, name_len;
Radek Krejcia1911222019-07-22 17:24:50 +02005774 const char *prefix, *name, *nodeid, *dflt;
Radek Krejciccd20f12019-02-15 14:12:27 +01005775 struct lys_module *mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005776 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005777 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005778
5779 /* get all deviations from the module and all its submodules ... */
5780 LY_ARRAY_FOR(mod_p->deviations, u) {
5781 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5782 }
5783 LY_ARRAY_FOR(mod_p->includes, v) {
5784 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5785 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5786 }
5787 }
5788 if (!devs_p.count) {
5789 /* nothing to do */
5790 return LY_SUCCESS;
5791 }
5792
Radek Krejci327de162019-06-14 12:52:07 +02005793 lysc_update_path(ctx, NULL, "{deviation}");
5794
Radek Krejciccd20f12019-02-15 14:12:27 +01005795 /* ... and group them by the target node */
5796 devs = calloc(devs_p.count, sizeof *devs);
5797 for (u = 0; u < devs_p.count; ++u) {
5798 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02005799 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005800
5801 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005802 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5803 (const struct lysc_node**)&target, &flags), cleanup);
Radek Krejcif538ce52019-03-05 10:46:14 +01005804 if (target->nodetype == LYS_ACTION) {
5805 /* move the target pointer to input/output to make them different from the action and
5806 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5807 * back to the RPC/action node due to a better compatibility and decision code in this function.
5808 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5809 if (flags & LYSC_OPT_RPC_INPUT) {
5810 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5811 flags |= LYSC_OPT_INTERNAL;
5812 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5813 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5814 flags |= LYSC_OPT_INTERNAL;
5815 }
5816 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005817 /* insert into the set of targets with duplicity detection */
5818 i = ly_set_add(&targets, target, 0);
5819 if (!devs[i]) {
5820 /* new record */
5821 devs[i] = calloc(1, sizeof **devs);
5822 devs[i]->target = target;
5823 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005824 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005825 }
5826 /* add deviates into the deviation's list of deviates */
5827 for (d = dev->deviates; d; d = d->next) {
5828 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5829 *dp_new = d;
5830 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5831 devs[i]->not_supported = 1;
5832 }
5833 }
Radek Krejci327de162019-06-14 12:52:07 +02005834
5835 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01005836 }
5837
5838 /* MACROS for deviates checking */
5839#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5840 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
Radek Krejci327de162019-06-14 12:52:07 +02005841 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 +01005842 goto cleanup; \
5843 }
5844
5845#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5846 if (LY_ARRAY_SIZE(ARRAY) > MAX) { \
Radek Krejci327de162019-06-14 12:52:07 +02005847 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%u) %s properties.", \
5848 lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005849 goto cleanup; \
5850 }
5851
Radek Krejcia1911222019-07-22 17:24:50 +02005852
Radek Krejciccd20f12019-02-15 14:12:27 +01005853#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejcia1911222019-07-22 17:24:50 +02005854 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
5855 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5856 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5857 PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5858 goto cleanup; \
5859 }
5860
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005861#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005862 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejcia1911222019-07-22 17:24:50 +02005863 int dynamic_ = 0; const char *val_; \
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005864 val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \
5865 lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005866 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejcia1911222019-07-22 17:24:50 +02005867 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5868 if (dynamic_) {free((void*)val_);} \
Radek Krejciccd20f12019-02-15 14:12:27 +01005869 goto cleanup; \
5870 }
5871
Radek Krejci551b12c2019-02-19 16:11:21 +01005872#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5873 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5874 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005875 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5876 PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005877 goto cleanup; \
5878 }
5879
Radek Krejciccd20f12019-02-15 14:12:27 +01005880#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5881 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
Radek Krejci327de162019-06-14 12:52:07 +02005882 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005883 goto cleanup; \
5884 }
5885
Radek Krejci551b12c2019-02-19 16:11:21 +01005886#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5887 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5888 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005889 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d_rpl->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005890 goto cleanup; \
5891 }
5892
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005893#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5894 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5895 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5896 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5897 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 +01005898 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005899 if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005900 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005901 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5902 PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005903 goto cleanup; \
5904 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005905 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5906 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5907 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5908 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
5909 (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005910 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005911 if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
5912 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5913 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005914 }
5915
5916 /* apply deviations */
5917 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005918 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target;
5919 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target;
5920 struct ly_err_item *err = NULL;
5921
5922 dflt = NULL;
5923 changed_type = 0;
5924
Radek Krejci327de162019-06-14 12:52:07 +02005925 lysc_update_path(ctx, NULL, devs[u]->nodeid);
5926
Radek Krejcif538ce52019-03-05 10:46:14 +01005927 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5928 /* fix the target pointer in case of RPC's/action's input/output */
5929 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5930 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5931 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5932 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5933 }
5934 }
5935
Radek Krejciccd20f12019-02-15 14:12:27 +01005936 /* not-supported */
5937 if (devs[u]->not_supported) {
5938 if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) {
5939 LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.",
5940 LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid);
5941 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005942
5943#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5944 if (devs[u]->target->parent) { \
5945 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
5946 } else { \
5947 ARRAY = devs[u]->target->module->compiled->ARRAY; \
5948 } \
5949 LY_ARRAY_FOR(ARRAY, x) { \
5950 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
5951 } \
5952 if (x < LY_ARRAY_SIZE(ARRAY)) { \
5953 FREEFUNC(ctx->ctx, &ARRAY[x]); \
5954 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \
5955 LY_ARRAY_DECREMENT(ARRAY); \
5956 }
5957
Radek Krejcif538ce52019-03-05 10:46:14 +01005958 if (devs[u]->target->nodetype == LYS_ACTION) {
5959 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5960 /* remove RPC's/action's input */
5961 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
5962 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005963 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
5964 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005965 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5966 /* remove RPC's/action's output */
5967 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
5968 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005969 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
5970 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005971 } else {
5972 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005973 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005974 }
5975 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005976 /* remove Notification */
5977 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005978 } else {
5979 /* remove the target node */
5980 lysc_disconnect(devs[u]->target);
5981 lysc_node_free(ctx->ctx, devs[u]->target);
5982 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005983
Radek Krejci474f9b82019-07-24 11:36:37 +02005984 /* mark the context for later re-compilation of objects that could reference the curently removed node */
5985 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
Radek Krejciccd20f12019-02-15 14:12:27 +01005986 continue;
5987 }
5988
5989 /* list of deviates (not-supported is not present in the list) */
5990 LY_ARRAY_FOR(devs[u]->deviates, v) {
5991 d = devs[u]->deviates[v];
5992
5993 switch (d->mod) {
5994 case LYS_DEV_ADD:
5995 d_add = (struct lysp_deviate_add*)d;
5996 /* [units-stmt] */
5997 if (d_add->units) {
5998 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5999 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
6000
6001 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6002 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6003 }
6004
6005 /* *must-stmt */
6006 if (d_add->musts) {
6007 switch (devs[u]->target->nodetype) {
6008 case LYS_CONTAINER:
6009 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02006010 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
6011 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006012 break;
6013 case LYS_LEAF:
6014 case LYS_LEAFLIST:
6015 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02006016 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
6017 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006018 break;
6019 case LYS_NOTIF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02006020 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
6021 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006022 break;
6023 case LYS_ACTION:
6024 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02006025 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
6026 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006027 break;
6028 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02006029 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
6030 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006031 break;
6032 }
6033 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01006034 default:
6035 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006036 lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006037 goto cleanup;
6038 }
6039 }
6040
6041 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01006042 if (d_add->uniques) {
6043 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
6044 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
6045 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006046
6047 /* *default-stmt */
6048 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006049 switch (devs[u]->target->nodetype) {
6050 case LYS_LEAF:
6051 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006052 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 +02006053 if (leaf->dflt) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006054 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02006055 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006056 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6057 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6058 } else {
6059 /* prepare new default value storage */
6060 leaf->dflt = calloc(1, sizeof *leaf->dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01006061 }
Radek Krejcia1911222019-07-22 17:24:50 +02006062 dflt = d_add->dflts[0];
6063 /* parsing is done at the end after possible replace of the leaf's type */
6064
Radek Krejci551b12c2019-02-19 16:11:21 +01006065 /* mark the new default values as leaf's own */
6066 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006067 break;
6068 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006069 if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006070 /* first, remove the default value taken from the type */
Radek Krejcia1911222019-07-22 17:24:50 +02006071 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006072 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006073 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6074 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6075 free(llist->dflts[x]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006076 }
Radek Krejcia1911222019-07-22 17:24:50 +02006077 LY_ARRAY_FREE(llist->dflts);
6078 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006079 LY_ARRAY_FREE(llist->dflts_mods);
6080 llist->dflts_mods = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01006081 }
6082 /* add new default value(s) */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006083 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006084 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
6085 for (x = y = LY_ARRAY_SIZE(llist->dflts);
Radek Krejciccd20f12019-02-15 14:12:27 +01006086 x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006087 LY_ARRAY_INCREMENT(llist->dflts_mods);
6088 llist->dflts_mods[x] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006089 LY_ARRAY_INCREMENT(llist->dflts);
6090 llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]);
6091 llist->dflts[x]->realtype = llist->type;
6092 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 +02006093 LY_TYPE_OPTS_INCOMPLETE_DATA |LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, lys_resolve_prefix,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006094 (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006095 llist->dflts[x]->realtype->refcount++;
6096 if (err) {
6097 ly_err_print(err);
6098 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6099 "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).",
6100 d_add->dflts[x - y], err->msg);
6101 ly_err_free(err);
6102 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006103 if (rc == LY_EINCOMPLETE) {
6104 /* postpone default compilation when the tree is complete */
6105 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6106
6107 /* but in general result is so far ok */
6108 rc = LY_SUCCESS;
6109 }
Radek Krejcia1911222019-07-22 17:24:50 +02006110 LY_CHECK_GOTO(rc, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006111 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006112 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01006113 devs[u]->target->flags |= LYS_SET_DFLT;
6114 break;
6115 case LYS_CHOICE:
6116 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
6117 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
6118 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
6119 * to allow making the default case even the augmented case from the deviating module */
Radek Krejci327de162019-06-14 12:52:07 +02006120 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 +01006121 goto cleanup;
6122 }
6123 break;
6124 default:
6125 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006126 lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006127 goto cleanup;
6128 }
6129 }
6130
6131 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006132 if (d_add->flags & LYS_CONFIG_MASK) {
6133 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006134 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006135 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
6136 goto cleanup;
6137 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006138 if (devs[u]->flags) {
Radek Krejci327de162019-06-14 12:52:07 +02006139 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
6140 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006141 }
Radek Krejci93dcc392019-02-19 10:43:38 +01006142 if (devs[u]->target->flags & LYS_SET_CONFIG) {
6143 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006144 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
6145 devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
Radek Krejci93dcc392019-02-19 10:43:38 +01006146 goto cleanup;
6147 }
Radek Krejci327de162019-06-14 12:52:07 +02006148 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006149 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006150
6151 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006152 if (d_add->flags & LYS_MAND_MASK) {
6153 if (devs[u]->target->flags & LYS_MAND_MASK) {
6154 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006155 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
6156 devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
Radek Krejcif1421c22019-02-19 13:05:20 +01006157 goto cleanup;
6158 }
Radek Krejci327de162019-06-14 12:52:07 +02006159 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006160 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006161
6162 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006163 if (d_add->flags & LYS_SET_MIN) {
6164 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6165 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6166 /* change value */
6167 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
6168 } else if (devs[u]->target->nodetype == LYS_LIST) {
6169 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6170 /* change value */
6171 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
6172 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006173 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006174 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
6175 goto cleanup;
6176 }
6177 if (d_add->min) {
6178 devs[u]->target->flags |= LYS_MAND_TRUE;
6179 }
6180 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006181
6182 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006183 if (d_add->flags & LYS_SET_MAX) {
6184 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6185 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6186 /* change value */
6187 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
6188 } else if (devs[u]->target->nodetype == LYS_LIST) {
6189 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6190 /* change value */
6191 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
6192 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006193 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006194 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
6195 goto cleanup;
6196 }
6197 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006198
6199 break;
6200 case LYS_DEV_DELETE:
6201 d_del = (struct lysp_deviate_del*)d;
6202
6203 /* [units-stmt] */
6204 if (d_del->units) {
6205 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Radek Krejcia1911222019-07-22 17:24:50 +02006206 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units);
6207 if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) {
6208 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6209 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
6210 d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6211 goto cleanup;
6212 }
6213 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6214 ((struct lysc_node_leaf*)devs[u]->target)->units = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01006215 }
6216
6217 /* *must-stmt */
6218 if (d_del->musts) {
6219 switch (devs[u]->target->nodetype) {
6220 case LYS_CONTAINER:
6221 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006222 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006223 break;
6224 case LYS_LEAF:
6225 case LYS_LEAFLIST:
6226 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006227 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006228 break;
6229 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02006230 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006231 break;
6232 case LYS_ACTION:
6233 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
6234 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6235 break;
6236 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
6237 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6238 break;
6239 }
6240 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01006241 default:
6242 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006243 lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006244 goto cleanup;
6245 }
6246 }
6247
6248 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01006249 if (d_del->uniques) {
6250 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
6251 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
6252 LY_ARRAY_FOR(d_del->uniques, x) {
6253 LY_ARRAY_FOR(list->uniques, z) {
6254 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
6255 nodeid = strpbrk(name, " \t\n");
6256 if (nodeid) {
Radek Krejci7f9b6512019-09-18 13:11:09 +02006257 if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) {
Radek Krejci7af64242019-02-18 13:07:53 +01006258 break;
6259 }
6260 while (isspace(*nodeid)) {
6261 ++nodeid;
6262 }
6263 } else {
6264 if (strcmp(name, list->uniques[z][y]->name)) {
6265 break;
6266 }
6267 }
6268 }
6269 if (!name) {
6270 /* complete match - remove the unique */
6271 LY_ARRAY_DECREMENT(list->uniques);
6272 LY_ARRAY_FREE(list->uniques[z]);
6273 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques));
6274 --z;
6275 break;
6276 }
6277 }
6278 if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) {
6279 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006280 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
6281 d_del->uniques[x]);
Radek Krejci7af64242019-02-18 13:07:53 +01006282 goto cleanup;
6283 }
6284 }
6285 if (!LY_ARRAY_SIZE(list->uniques)) {
6286 LY_ARRAY_FREE(list->uniques);
6287 list->uniques = NULL;
6288 }
6289 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006290
6291 /* *default-stmt */
6292 if (d_del->dflts) {
6293 switch (devs[u]->target->nodetype) {
6294 case LYS_LEAF:
6295 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6296 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6297 dflt, "deleting", "default", d_del->dflts[0]);
6298
Radek Krejcia1911222019-07-22 17:24:50 +02006299 /* check that the values matches */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006300 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &i);
Radek Krejcia1911222019-07-22 17:24:50 +02006301 if (strcmp(dflt, d_del->dflts[0])) {
6302 if (i) {
6303 free((char*)dflt);
6304 }
6305 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6306 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
6307 d_del->dflts[0], dflt);
6308 goto cleanup;
6309 }
6310 if (i) {
6311 free((char*)dflt);
6312 }
6313 dflt = NULL;
6314
Radek Krejci474f9b82019-07-24 11:36:37 +02006315 /* update the list of incomplete default values if needed */
6316 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6317
Radek Krejcia1911222019-07-22 17:24:50 +02006318 /* remove the default specification */
6319 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6320 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6321 free(leaf->dflt);
6322 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006323 leaf->dflt_mod = NULL;
Radek Krejci551b12c2019-02-19 16:11:21 +01006324 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006325 break;
6326 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006327 DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]);
6328 LY_ARRAY_FOR(d_del->dflts, x) {
6329 LY_ARRAY_FOR(llist->dflts, y) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006330 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 +02006331 if (!strcmp(dflt, d_del->dflts[x])) {
6332 if (i) {
6333 free((char*)dflt);
6334 }
6335 dflt = NULL;
6336 break;
6337 }
6338 if (i) {
6339 free((char*)dflt);
6340 }
6341 dflt = NULL;
6342 }
6343 if (y == LY_ARRAY_SIZE(llist->dflts)) {
6344 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
6345 "which does not match any of the target's property values.", d_del->dflts[x]);
6346 goto cleanup;
6347 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006348
6349 /* update the list of incomplete default values if needed */
6350 lysc_incomplete_dflts_remove(ctx, llist->dflts[y]);
6351
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006352 LY_ARRAY_DECREMENT(llist->dflts_mods);
Radek Krejcia1911222019-07-22 17:24:50 +02006353 LY_ARRAY_DECREMENT(llist->dflts);
6354 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
6355 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
6356 free(llist->dflts[y]);
6357 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_SIZE(llist->dflts) - y) * (sizeof *llist->dflts));
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006358 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 +02006359 }
6360 if (!LY_ARRAY_SIZE(llist->dflts)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006361 LY_ARRAY_FREE(llist->dflts_mods);
6362 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006363 LY_ARRAY_FREE(llist->dflts);
6364 llist->dflts = NULL;
6365 llist->flags &= ~LYS_SET_DFLT;
Radek Krejci551b12c2019-02-19 16:11:21 +01006366 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006367 break;
6368 case LYS_CHOICE:
6369 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6370 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
6371 nodeid = d_del->dflts[0];
Radek Krejcib4a4a272019-06-10 12:44:52 +02006372 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006373 if (prefix) {
6374 /* use module prefixes from the deviation module to match the module of the default case */
6375 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
6376 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006377 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6378 "The prefix does not match any imported module of the deviation module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006379 goto cleanup;
6380 }
6381 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
6382 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006383 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6384 "The prefix does not match the default case's module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006385 goto cleanup;
6386 }
6387 }
6388 /* else {
6389 * strictly, the default prefix would point to the deviation module, but the value should actually
6390 * match the default string in the original module (usually unprefixed), so in this case we do not check
6391 * the module of the default case, just matching its name */
6392 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
6393 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006394 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
6395 d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01006396 goto cleanup;
6397 }
6398 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
6399 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
6400 break;
6401 default:
6402 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006403 lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006404 goto cleanup;
6405 }
6406 }
6407
6408 break;
6409 case LYS_DEV_REPLACE:
6410 d_rpl = (struct lysp_deviate_rpl*)d;
6411
6412 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01006413 if (d_rpl->type) {
6414 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6415 /* type is mandatory, so checking for its presence is not necessary */
6416 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
Radek Krejcia1911222019-07-22 17:24:50 +02006417
6418 if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) {
6419 /* the target has default from the previous type - remove it */
6420 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006421 /* update the list of incomplete default values if needed */
6422 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6423
Radek Krejcia1911222019-07-22 17:24:50 +02006424 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6425 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6426 free(leaf->dflt);
6427 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006428 leaf->dflt_mod = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006429 } else { /* LYS_LEAFLIST */
6430 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006431 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006432 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6433 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6434 free(llist->dflts[x]);
6435 }
6436 LY_ARRAY_FREE(llist->dflts);
6437 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006438 LY_ARRAY_FREE(llist->dflts_mods);
6439 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006440 }
6441 }
6442 if (!leaf->dflt) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006443 /* there is no default value, do not set changed_type after type compilation
6444 * which is used to recompile the default value */
Radek Krejcia1911222019-07-22 17:24:50 +02006445 changed_type = -1;
6446 }
Radek Krejciec4da802019-05-02 13:02:41 +02006447 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 +02006448 changed_type++;
Radek Krejci33f72892019-02-21 10:36:58 +01006449 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006450
6451 /* [units-stmt] */
6452 if (d_rpl->units) {
6453 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6454 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
6455 units, "replacing", "units", d_rpl->units);
6456
6457 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6458 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6459 }
6460
6461 /* [default-stmt] */
6462 if (d_rpl->dflt) {
6463 switch (devs[u]->target->nodetype) {
6464 case LYS_LEAF:
6465 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6466 dflt, "replacing", "default", d_rpl->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006467 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02006468 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006469 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6470 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6471 dflt = d_rpl->dflt;
6472 /* parsing is done at the end after possible replace of the leaf's type */
Radek Krejciccd20f12019-02-15 14:12:27 +01006473 break;
6474 case LYS_CHOICE:
6475 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
Radek Krejci327de162019-06-14 12:52:07 +02006476 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 +01006477 goto cleanup;
6478 }
6479 break;
6480 default:
6481 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006482 lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006483 goto cleanup;
6484 }
6485 }
6486
6487 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006488 if (d_rpl->flags & LYS_CONFIG_MASK) {
6489 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006490 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006491 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
6492 goto cleanup;
6493 }
6494 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
Radek Krejci327de162019-06-14 12:52:07 +02006495 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejci93dcc392019-02-19 10:43:38 +01006496 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
6497 goto cleanup;
6498 }
Radek Krejci327de162019-06-14 12:52:07 +02006499 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006500 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006501
6502 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006503 if (d_rpl->flags & LYS_MAND_MASK) {
6504 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
Radek Krejci327de162019-06-14 12:52:07 +02006505 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejcif1421c22019-02-19 13:05:20 +01006506 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6507 goto cleanup;
6508 }
Radek Krejci327de162019-06-14 12:52:07 +02006509 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006510 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006511
6512 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006513 if (d_rpl->flags & LYS_SET_MIN) {
6514 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6515 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6516 /* change value */
6517 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
6518 } else if (devs[u]->target->nodetype == LYS_LIST) {
6519 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6520 /* change value */
6521 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
6522 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006523 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006524 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
6525 goto cleanup;
6526 }
6527 if (d_rpl->min) {
6528 devs[u]->target->flags |= LYS_MAND_TRUE;
6529 }
6530 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006531
6532 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006533 if (d_rpl->flags & LYS_SET_MAX) {
6534 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6535 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6536 /* change value */
6537 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6538 } else if (devs[u]->target->nodetype == LYS_LIST) {
6539 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6540 /* change value */
6541 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6542 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006543 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006544 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
6545 goto cleanup;
6546 }
6547 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006548
6549 break;
6550 default:
6551 LOGINT(ctx->ctx);
6552 goto cleanup;
6553 }
6554 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006555
Radek Krejci33f72892019-02-21 10:36:58 +01006556 /* final check when all deviations of a single target node are applied */
6557
Radek Krejci551b12c2019-02-19 16:11:21 +01006558 /* check min-max compatibility */
6559 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6560 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
6561 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
6562 } else if (devs[u]->target->nodetype == LYS_LIST) {
6563 min = ((struct lysc_node_list*)devs[u]->target)->min;
6564 max = ((struct lysc_node_list*)devs[u]->target)->max;
6565 } else {
6566 min = max = 0;
6567 }
6568 if (min > max) {
6569 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 +02006570 "after deviation: min value %u is bigger than max value %u.", min, max);
Radek Krejci551b12c2019-02-19 16:11:21 +01006571 goto cleanup;
6572 }
6573
Radek Krejcia1911222019-07-22 17:24:50 +02006574 if (dflt) {
6575 /* parse added/changed default value after possible change of the type */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006576 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006577 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006578 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6579 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006580 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006581 leaf->dflt->realtype->refcount++;
6582 if (err) {
6583 ly_err_print(err);
6584 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6585 "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg);
6586 ly_err_free(err);
6587 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006588 if (rc == LY_EINCOMPLETE) {
6589 /* postpone default compilation when the tree is complete */
6590 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6591
6592 /* but in general result is so far ok */
6593 rc = LY_SUCCESS;
6594 }
Radek Krejcia1911222019-07-22 17:24:50 +02006595 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006596 } else if (changed_type) {
Radek Krejcia1911222019-07-22 17:24:50 +02006597 /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */
6598 int dynamic;
6599 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006600 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &dynamic);
Radek Krejci474f9b82019-07-24 11:36:37 +02006601
6602 /* update the list of incomplete default values if needed */
6603 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6604
6605 /* remove the previous default */
Radek Krejcia1911222019-07-22 17:24:50 +02006606 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6607 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6608 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006609 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6610 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006611 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006612 leaf->dflt->realtype->refcount++;
6613 if (err) {
6614 ly_err_print(err);
6615 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6616 "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg);
6617 ly_err_free(err);
6618 }
6619 if (dynamic) {
6620 free((void*)dflt);
6621 }
Radek Krejcia1911222019-07-22 17:24:50 +02006622 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006623 if (rc == LY_EINCOMPLETE) {
6624 /* postpone default compilation when the tree is complete */
6625 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6626
6627 /* but in general result is so far ok */
6628 rc = LY_SUCCESS;
6629 }
6630 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006631 } else { /* LYS_LEAFLIST */
6632 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006633 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 +02006634 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6635 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6636 llist->dflts[x]->realtype = llist->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006637 rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt),
6638 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006639 lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL,
6640 llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006641 llist->dflts[x]->realtype->refcount++;
6642 if (err) {
6643 ly_err_print(err);
6644 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6645 "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).",
6646 dflt, err->msg);
6647 ly_err_free(err);
6648 }
6649 if (dynamic) {
6650 free((void*)dflt);
6651 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006652 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006653 if (rc == LY_EINCOMPLETE) {
6654 /* postpone default compilation when the tree is complete */
6655 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6656
6657 /* but in general result is so far ok */
6658 rc = LY_SUCCESS;
6659 }
Radek Krejcia1911222019-07-22 17:24:50 +02006660 LY_CHECK_GOTO(rc, cleanup);
6661 }
6662 }
6663 }
6664
Radek Krejci551b12c2019-02-19 16:11:21 +01006665 /* check mandatory - default compatibility */
6666 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
6667 && (devs[u]->target->flags & LYS_SET_DFLT)
6668 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6669 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006670 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(devs[u]->target->nodetype));
Radek Krejci551b12c2019-02-19 16:11:21 +01006671 goto cleanup;
6672 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
6673 && ((struct lysc_node_choice*)devs[u]->target)->dflt
6674 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
Radek Krejci327de162019-06-14 12:52:07 +02006675 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 +01006676 goto cleanup;
6677 }
6678 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6679 /* mandatory node under a default case */
6680 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006681 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6682 lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
Radek Krejci551b12c2019-02-19 16:11:21 +01006683 goto cleanup;
6684 }
Radek Krejci33f72892019-02-21 10:36:58 +01006685
Radek Krejci327de162019-06-14 12:52:07 +02006686 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006687 }
6688
Radek Krejci327de162019-06-14 12:52:07 +02006689 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006690 ret = LY_SUCCESS;
6691
6692cleanup:
6693 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6694 LY_ARRAY_FREE(devs[u]->deviates);
6695 free(devs[u]);
6696 }
6697 free(devs);
6698 ly_set_erase(&targets, NULL);
6699 ly_set_erase(&devs_p, NULL);
6700
6701 return ret;
6702}
6703
Radek Krejcib56c7502019-02-13 14:19:54 +01006704/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006705 * @brief Compile the given YANG submodule into the main module.
6706 * @param[in] ctx Compile context
6707 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006708 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6709 */
6710LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006711lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006712{
6713 unsigned int u;
6714 LY_ERR ret = LY_SUCCESS;
6715 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006716 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006717 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006718 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006719
Radek Krejci0af46292019-01-11 16:02:31 +01006720 if (!mainmod->mod->off_features) {
6721 /* features are compiled directly into the compiled module structure,
6722 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6723 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci0935f412019-08-20 16:15:18 +02006724 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features);
6725 LY_CHECK_GOTO(ret, error);
6726 }
6727 if (!mainmod->mod->off_extensions) {
6728 /* extensions are compiled directly into the compiled module structure, compilation is finished in the main module (lys_compile()). */
6729 ret = lys_extension_precompile(ctx, NULL, NULL, submod->extensions, &mainmod->extensions);
Radek Krejci0af46292019-01-11 16:02:31 +01006730 LY_CHECK_GOTO(ret, error);
6731 }
6732
Radek Krejci327de162019-06-14 12:52:07 +02006733 lysc_update_path(ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006734 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, u, lys_compile_identity, ret, error);
Radek Krejci327de162019-06-14 12:52:07 +02006735 lysc_update_path(ctx, NULL, NULL);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006736
Radek Krejci474f9b82019-07-24 11:36:37 +02006737 /* data nodes */
6738 LY_LIST_FOR(submod->data, node_p) {
6739 ret = lys_compile_node(ctx, node_p, NULL, 0);
6740 LY_CHECK_GOTO(ret, error);
6741 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006742
Radek Krejciec4da802019-05-02 13:02:41 +02006743 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6744 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006745
Radek Krejcid05cbd92018-12-05 14:26:40 +01006746error:
6747 return ret;
6748}
6749
Radek Krejci335332a2019-09-05 13:03:35 +02006750static void *
6751lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
6752{
6753 for (unsigned int u = 0; substmts[u].stmt; ++u) {
6754 if (substmts[u].stmt == stmt) {
6755 return substmts[u].storage;
6756 }
6757 }
6758 return NULL;
6759}
6760
6761LY_ERR
6762lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
6763{
6764 LY_ERR ret = LY_EVALID, r;
6765 unsigned int u;
6766 struct lysp_stmt *stmt;
6767 void *parsed = NULL, **compiled = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006768
6769 /* check for invalid substatements */
6770 for (stmt = ext->child; stmt; stmt = stmt->next) {
Radek Krejcif56e2a42019-09-09 14:15:25 +02006771 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
6772 continue;
6773 }
Radek Krejci335332a2019-09-05 13:03:35 +02006774 for (u = 0; substmts[u].stmt; ++u) {
6775 if (substmts[u].stmt == stmt->kw) {
6776 break;
6777 }
6778 }
6779 if (!substmts[u].stmt) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006780 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
6781 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006782 goto cleanup;
6783 }
Radek Krejci335332a2019-09-05 13:03:35 +02006784 }
6785
Radek Krejciad5963b2019-09-06 16:03:05 +02006786 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
6787
Radek Krejci335332a2019-09-05 13:03:35 +02006788 /* keep order of the processing the same as the order in the defined substmts,
6789 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
6790 for (u = 0; substmts[u].stmt; ++u) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006791 int stmt_present = 0;
6792
Radek Krejci335332a2019-09-05 13:03:35 +02006793 for (stmt = ext->child; stmt; stmt = stmt->next) {
6794 if (substmts[u].stmt != stmt->kw) {
6795 continue;
6796 }
6797
Radek Krejciad5963b2019-09-06 16:03:05 +02006798 stmt_present = 1;
Radek Krejci335332a2019-09-05 13:03:35 +02006799 if (substmts[u].storage) {
6800 switch (stmt->kw) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006801 case LY_STMT_STATUS:
6802 assert(substmts[u].cardinality < LY_STMT_CARD_SOME);
6803 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
6804 break;
6805 case LY_STMT_UNITS: {
6806 const char **units;
6807
6808 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6809 /* single item */
6810 if (*((const char **)substmts[u].storage)) {
6811 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6812 goto cleanup;
6813 }
6814 units = (const char **)substmts[u].storage;
6815 } else {
6816 /* sized array */
6817 const char ***units_array = (const char ***)substmts[u].storage;
6818 LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
6819 }
6820 *units = lydict_insert(ctx->ctx, stmt->arg, 0);
6821 break;
6822 }
Radek Krejci335332a2019-09-05 13:03:35 +02006823 case LY_STMT_TYPE: {
6824 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
6825 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
6826
6827 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6828 /* single item */
Radek Krejciad5963b2019-09-06 16:03:05 +02006829 if (*(struct lysc_type**)substmts[u].storage) {
Radek Krejci335332a2019-09-05 13:03:35 +02006830 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6831 goto cleanup;
6832 }
6833 compiled = substmts[u].storage;
6834 } else {
6835 /* sized array */
6836 struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL;
6837 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
6838 compiled = (void*)type;
6839 }
6840
Radek Krejciad5963b2019-09-06 16:03:05 +02006841 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup);
Radek Krejci335332a2019-09-05 13:03:35 +02006842 LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL,
6843 flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type**)compiled,
Radek Krejci38d85362019-09-05 16:26:38 +02006844 units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup);
6845 lysp_type_free(ctx->ctx, parsed);
6846 free(parsed);
Radek Krejci335332a2019-09-05 13:03:35 +02006847 break;
6848 }
Radek Krejciad5963b2019-09-06 16:03:05 +02006849 case LY_STMT_IF_FEATURE: {
6850 struct lysc_iffeature *iff = NULL;
6851
6852 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6853 /* single item */
6854 if (((struct lysc_iffeature*)substmts[u].storage)->features) {
6855 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6856 goto cleanup;
6857 }
6858 iff = (struct lysc_iffeature*)substmts[u].storage;
6859 } else {
6860 /* sized array */
6861 struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage;
6862 LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup);
6863 }
6864 LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup);
6865 break;
6866 }
6867 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
6868 * also note that in many statements their extensions are not taken into account */
Radek Krejci335332a2019-09-05 13:03:35 +02006869 default:
Radek Krejciad5963b2019-09-06 16:03:05 +02006870 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Statement \"%s\" is not supported as an extension (found in \"%s%s%s\") substatement.",
6871 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006872 goto cleanup;
6873 }
6874 }
Radek Krejci335332a2019-09-05 13:03:35 +02006875 }
Radek Krejci335332a2019-09-05 13:03:35 +02006876
Radek Krejciad5963b2019-09-06 16:03:05 +02006877 if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) {
6878 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
6879 ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
6880 goto cleanup;
6881 }
Radek Krejci335332a2019-09-05 13:03:35 +02006882 }
6883
6884 ret = LY_SUCCESS;
6885
6886cleanup:
Radek Krejci335332a2019-09-05 13:03:35 +02006887 return ret;
6888}
6889
Radek Krejci19a96102018-11-15 13:38:09 +01006890LY_ERR
6891lys_compile(struct lys_module *mod, int options)
6892{
6893 struct lysc_ctx ctx = {0};
6894 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01006895 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01006896 struct lysp_module *sp;
6897 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01006898 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02006899 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01006900 struct lys_module *m;
Radek Krejcicdfecd92018-11-26 11:27:32 +01006901 unsigned int u, v;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006902 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01006903
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006904 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01006905
6906 if (!mod->implemented) {
6907 /* just imported modules are not compiled */
6908 return LY_SUCCESS;
6909 }
6910
Radek Krejci19a96102018-11-15 13:38:09 +01006911 sp = mod->parsed;
6912
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006913 ctx.ctx = mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01006914 ctx.mod = mod;
Radek Krejcie86bf772018-12-14 11:39:53 +01006915 ctx.mod_def = mod;
Radek Krejciec4da802019-05-02 13:02:41 +02006916 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02006917 ctx.path_len = 1;
6918 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01006919
6920 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006921 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
6922 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01006923
Radek Krejciec4da802019-05-02 13:02:41 +02006924 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006925 LY_ARRAY_FOR(sp->includes, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006926 ret = lys_compile_submodule(&ctx, &sp->includes[u]);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006927 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6928 }
Radek Krejci0935f412019-08-20 16:15:18 +02006929
6930 /* features */
Radek Krejci0af46292019-01-11 16:02:31 +01006931 if (mod->off_features) {
6932 /* there is already precompiled array of features */
6933 mod_c->features = mod->off_features;
6934 mod->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01006935 } else {
6936 /* features are compiled directly into the compiled module structure,
6937 * 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 +02006938 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006939 LY_CHECK_GOTO(ret, error);
6940 }
6941 /* finish feature compilation, not only for the main module, but also for the submodules.
6942 * Due to possible forward references, it must be done when all the features (including submodules)
6943 * are present. */
6944 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006945 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006946 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6947 }
Radek Krejci327de162019-06-14 12:52:07 +02006948 lysc_update_path(&ctx, NULL, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01006949 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02006950 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01006951 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006952 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006953 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6954 }
Radek Krejci327de162019-06-14 12:52:07 +02006955 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01006956 }
Radek Krejci327de162019-06-14 12:52:07 +02006957 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01006958
Radek Krejci0935f412019-08-20 16:15:18 +02006959 /* extensions */
6960 /* 2-steps: a) prepare compiled structures and ... */
6961 if (mod->off_extensions) {
6962 /* there is already precompiled array of extension definitions */
6963 mod_c->extensions = mod->off_extensions;
6964 mod->off_extensions = NULL;
6965 } else {
6966 /* extension definitions are compiled directly into the compiled module structure */
6967 ret = lys_extension_precompile(&ctx, NULL, NULL, sp->extensions, &mod_c->extensions);
Radek Krejcibfb2e142019-09-09 14:47:44 +02006968 LY_CHECK_GOTO(ret, error);
Radek Krejci0935f412019-08-20 16:15:18 +02006969 }
6970 /* ... b) connect the extension definitions with the appropriate extension plugins */
6971 lys_compile_extension_plugins(mod_c->extensions);
6972
6973 /* identities */
Radek Krejci327de162019-06-14 12:52:07 +02006974 lysc_update_path(&ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006975 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006976 if (sp->identities) {
6977 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
6978 }
Radek Krejci327de162019-06-14 12:52:07 +02006979 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01006980
Radek Krejci95710c92019-02-11 15:49:55 +01006981 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01006982 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02006983 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, node_p, NULL, 0), error);
Radek Krejci19a96102018-11-15 13:38:09 +01006984 }
Radek Krejci95710c92019-02-11 15:49:55 +01006985
Radek Krejciec4da802019-05-02 13:02:41 +02006986 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6987 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01006988
Radek Krejci95710c92019-02-11 15:49:55 +01006989 /* augments - sort first to cover augments augmenting other augments */
Radek Krejcid3acfce2019-09-09 14:48:50 +02006990 LY_CHECK_GOTO(ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments), error);
Radek Krejci95710c92019-02-11 15:49:55 +01006991 LY_ARRAY_FOR(augments, u) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02006992 LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error);
Radek Krejci95710c92019-02-11 15:49:55 +01006993 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006994
Radek Krejci474f9b82019-07-24 11:36:37 +02006995 /* deviations TODO cover deviations from submodules */
Radek Krejcid3acfce2019-09-09 14:48:50 +02006996 LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error);
Radek Krejci19a96102018-11-15 13:38:09 +01006997
Radek Krejci0935f412019-08-20 16:15:18 +02006998 /* extension instances TODO cover extension instances from submodules */
6999 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007000
Radek Krejcia3045382018-11-22 14:30:31 +01007001 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01007002 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
7003 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
7004 * 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 +01007005 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01007006 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01007007 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
7008 if (type->basetype == LY_TYPE_LEAFREF) {
7009 /* validate the path */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007010 LY_CHECK_GOTO(ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), (struct lysc_type_leafref*)type), error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01007011 } else if (type->basetype == LY_TYPE_UNION) {
7012 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
7013 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7014 /* validate the path */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007015 LY_CHECK_GOTO(ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
7016 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]),
7017 error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01007018 }
7019 }
Radek Krejcia3045382018-11-22 14:30:31 +01007020 }
7021 }
7022 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01007023 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01007024 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01007025 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
7026 if (type->basetype == LY_TYPE_LEAFREF) {
7027 /* store pointer to the real type */
7028 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
7029 typeiter->basetype == LY_TYPE_LEAFREF;
7030 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
7031 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01007032 } else if (type->basetype == LY_TYPE_UNION) {
7033 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
7034 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7035 /* store pointer to the real type */
7036 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
7037 typeiter->basetype == LY_TYPE_LEAFREF;
7038 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
7039 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
7040 }
7041 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01007042 }
7043 }
7044 }
Radek Krejciec4da802019-05-02 13:02:41 +02007045
Radek Krejci474f9b82019-07-24 11:36:37 +02007046 /* finish incomplete default values compilation */
7047 for (u = 0; u < ctx.dflts.count; ++u) {
7048 struct ly_err_item *err = NULL;
7049 struct lysc_incomplete_dflt *r = ctx.dflts.objs[u];
Radek Krejci950f6a52019-09-12 17:15:32 +02007050 ret = r->dflt->realtype->plugin->store(ctx.ctx, r->dflt->realtype, r->dflt->original, strlen(r->dflt->original),
Radek Krejci474f9b82019-07-24 11:36:37 +02007051 LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix,
7052 (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err);
7053 if (err) {
7054 ly_err_print(err);
7055 ctx.path[0] = '\0';
Michal Vasko03ff5a72019-09-11 13:49:33 +02007056 lysc_path(r->context_node, LYSC_PATH_LOG, ctx.path, LYSC_CTX_BUFSIZE);
Radek Krejci474f9b82019-07-24 11:36:37 +02007057 LOGVAL(ctx.ctx, LY_VLOG_STR, ctx.path, LYVE_SEMANTICS,
7058 "Invalid default - value does not fit the type (%s).", err->msg);
7059 ly_err_free(err);
7060 }
7061 LY_CHECK_GOTO(ret, error);
7062 }
7063
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007064 /* validate non-instantiated groupings from the parsed schema,
7065 * without it we would accept even the schemas with invalid grouping specification */
7066 ctx.options |= LYSC_OPT_GROUPING;
7067 LY_ARRAY_FOR(sp->groupings, u) {
7068 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007069 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007070 }
7071 }
7072 LY_LIST_FOR(sp->data, node_p) {
7073 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
7074 LY_ARRAY_FOR(grps, u) {
7075 if (!(grps[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007076 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &grps[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007077 }
7078 }
7079 }
7080
Radek Krejci474f9b82019-07-24 11:36:37 +02007081 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
7082 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
7083 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
7084 }
7085
Radek Krejci1c0c3442019-07-23 16:08:47 +02007086 ly_set_erase(&ctx.dflts, free);
Radek Krejcia3045382018-11-22 14:30:31 +01007087 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007088 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007089 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007090 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01007091
Radek Krejciec4da802019-05-02 13:02:41 +02007092 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejci19a96102018-11-15 13:38:09 +01007093 lysp_module_free(mod->parsed);
7094 ((struct lys_module*)mod)->parsed = NULL;
7095 }
7096
Radek Krejciec4da802019-05-02 13:02:41 +02007097 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01007098 /* remove flag of the modules implemented by dependency */
7099 for (u = 0; u < ctx.ctx->list.count; ++u) {
7100 m = ctx.ctx->list.objs[u];
7101 if (m->implemented == 2) {
7102 m->implemented = 1;
7103 }
7104 }
7105 }
7106
Radek Krejci19a96102018-11-15 13:38:09 +01007107 ((struct lys_module*)mod)->compiled = mod_c;
7108 return LY_SUCCESS;
7109
7110error:
Radek Krejci95710c92019-02-11 15:49:55 +01007111 lys_feature_precompile_revert(&ctx, mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02007112 ly_set_erase(&ctx.dflts, free);
Radek Krejcia3045382018-11-22 14:30:31 +01007113 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007114 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007115 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007116 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01007117 lysc_module_free(mod_c, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007118 mod->compiled = NULL;
7119
7120 /* revert compilation of modules implemented by dependency */
7121 for (u = 0; u < ctx.ctx->list.count; ++u) {
7122 m = ctx.ctx->list.objs[u];
7123 if (m->implemented == 2) {
7124 /* revert features list to the precompiled state */
7125 lys_feature_precompile_revert(&ctx, m);
7126 /* mark module as imported-only / not-implemented */
7127 m->implemented = 0;
7128 /* free the compiled version of the module */
7129 lysc_module_free(m->compiled, NULL);
7130 m->compiled = NULL;
7131 }
7132 }
7133
Radek Krejci19a96102018-11-15 13:38:09 +01007134 return ret;
7135}