blob: bc6c8052e8be3f7f9e8d5ea410ad4d5692458f6b [file] [log] [blame]
Radek Krejci19a96102018-11-15 13:38:09 +01001/**
2 * @file tree_schema.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema tree implementation
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#include "common.h"
16
Radek Krejcie7b95092019-05-15 11:03:07 +020017#include <assert.h>
Radek Krejci19a96102018-11-15 13:38:09 +010018#include <ctype.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include <stddef.h>
20#include <stdint.h>
Radek Krejci19a96102018-11-15 13:38:09 +010021#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdlib.h>
23#include <string.h>
Radek Krejci19a96102018-11-15 13:38:09 +010024
Radek Krejcie7b95092019-05-15 11:03:07 +020025#include "dict.h"
26#include "log.h"
Radek Krejci0935f412019-08-20 16:15:18 +020027#include "plugins_exts.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "set.h"
29#include "plugins_types.h"
Radek Krejci0935f412019-08-20 16:15:18 +020030#include "plugins_exts_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020031#include "tree.h"
32#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010033#include "tree_schema_internal.h"
34#include "xpath.h"
35
36/**
37 * @brief Duplicate string into dictionary
38 * @param[in] CTX libyang context of the dictionary.
39 * @param[in] ORIG String to duplicate.
40 * @param[out] DUP Where to store the result.
41 */
42#define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);}
43
Radek Krejciec4da802019-05-02 13:02:41 +020044#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010045 if (ARRAY_P) { \
46 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010047 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
Radek Krejci19a96102018-11-15 13:38:09 +010048 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
49 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020050 RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010051 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
52 } \
53 }
54
Radek Krejciec4da802019-05-02 13:02:41 +020055#define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010056 if (ARRAY_P) { \
57 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
58 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
59 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
60 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020061 RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010062 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
63 } \
64 }
65
Radek Krejci0935f412019-08-20 16:15:18 +020066#define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \
67 if (EXTS_P) { \
68 LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_SIZE(EXTS_P), RET, GOTO); \
69 for (uint32_t __exts_iter = 0, __array_offset = LY_ARRAY_SIZE(EXT_C); __exts_iter < LY_ARRAY_SIZE(EXTS_P); ++__exts_iter) { \
70 LY_ARRAY_INCREMENT(EXT_C); \
71 RET = lys_compile_ext(CTX, &(EXTS_P)[__exts_iter], &(EXT_C)[__exts_iter + __array_offset], PARENT, PARENT_TYPE); \
72 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
73 } \
74 }
75
Radek Krejciec4da802019-05-02 13:02:41 +020076#define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejcid05cbd92018-12-05 14:26:40 +010077 if (ARRAY_P) { \
78 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
79 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
80 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
81 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020082 RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejci19a96102018-11-15 13:38:09 +010083 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
84 } \
85 }
86
Radek Krejciec4da802019-05-02 13:02:41 +020087#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010088 if (MEMBER_P) { \
89 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
90 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
Radek Krejciec4da802019-05-02 13:02:41 +020091 RET = FUNC(CTX, MEMBER_P, MEMBER_C); \
Radek Krejci19a96102018-11-15 13:38:09 +010092 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
93 }
94
Radek Krejciec4da802019-05-02 13:02:41 +020095#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \
Radek Krejci00b874b2019-02-12 10:54:50 +010096 if (MEMBER_P) { \
97 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
98 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
99 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +0200100 RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100101 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
102 }
103
Radek Krejcid05cbd92018-12-05 14:26:40 +0100104#define COMPILE_CHECK_UNIQUENESS(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
105 if (ARRAY) { \
Radek Krejci0af46292019-01-11 16:02:31 +0100106 for (unsigned int u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \
107 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100108 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
109 return LY_EVALID; \
110 } \
111 } \
112 }
113
Radek Krejci19a96102018-11-15 13:38:09 +0100114static struct lysc_ext_instance *
115lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
116{
Radek Krejcie7b95092019-05-15 11:03:07 +0200117 /* TODO - extensions */
Radek Krejci19a96102018-11-15 13:38:09 +0100118 (void) ctx;
119 (void) orig;
120 return NULL;
121}
122
Radek Krejcib56c7502019-02-13 14:19:54 +0100123/**
Radek Krejci474f9b82019-07-24 11:36:37 +0200124 * @brief Add record into the compile context's list of incomplete default values.
125 * @param[in] ctx Compile context with the incomplete default values list.
126 * @param[in] context_node Context schema node to store in the record.
127 * @param[in] dflt Incomplete default value to store in the record.
128 * @param[in] dflt_mod Module of the default value definition to store in the record.
129 * @return LY_EMEM in case of memory allocation failure.
130 * @return LY_SUCCESS
131 */
132static LY_ERR
133lysc_incomplete_dflts_add(struct lysc_ctx *ctx, struct lysc_node *context_node, struct lyd_value *dflt, struct lys_module *dflt_mod)
134{
135 struct lysc_incomplete_dflt *r;
136 r = malloc(sizeof *r);
137 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
138 r->context_node = context_node;
139 r->dflt = dflt;
140 r->dflt_mod = dflt_mod;
141 ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST);
142
143 return LY_SUCCESS;
144}
145
146/**
147 * @brief Remove record of the given default value from the compile context's list of incomplete default values.
148 * @param[in] ctx Compile context with the incomplete default values list.
149 * @param[in] dflt Incomplete default values identifying the record to remove.
150 */
151static void
152lysc_incomplete_dflts_remove(struct lysc_ctx *ctx, struct lyd_value *dflt)
153{
154 unsigned int u;
155 struct lysc_incomplete_dflt *r;
156
157 for (u = 0; u < ctx->dflts.count; ++u) {
158 r = (struct lysc_incomplete_dflt*)ctx->dflts.objs[u];
159 if (r->dflt == dflt) {
160 free(ctx->dflts.objs[u]);
161 memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs);
162 --ctx->dflts.count;
163 return;
164 }
165 }
166}
167
Radek Krejci0e59c312019-08-15 15:34:15 +0200168void
Radek Krejci327de162019-06-14 12:52:07 +0200169lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name)
170{
171 int len;
172 int nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
173
174 if (!name) {
175 /* removing last path segment */
176 if (ctx->path[ctx->path_len - 1] == '}') {
177 for (; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len);
178 if (ctx->path[ctx->path_len] == '=') {
179 ctx->path[ctx->path_len++] = '}';
180 } else {
181 /* not a top-level special tag, remove also preceiding '/' */
182 goto remove_nodelevel;
183 }
184 } else {
185remove_nodelevel:
186 for (; ctx->path[ctx->path_len] != '/' ; --ctx->path_len);
187 if (ctx->path_len == 0) {
188 /* top-level (last segment) */
Radek Krejciacc79042019-07-25 14:14:57 +0200189 ctx->path_len = 1;
Radek Krejci327de162019-06-14 12:52:07 +0200190 }
191 }
192 /* set new terminating NULL-byte */
193 ctx->path[ctx->path_len] = '\0';
194 } else {
195 if (ctx->path_len > 1) {
196 if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') {
197 /* extension of the special tag */
198 nextlevel = 2;
199 --ctx->path_len;
200 } else {
201 /* there is already some path, so add next level */
202 nextlevel = 1;
203 }
204 } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
205
206 if (nextlevel != 2) {
207 if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) {
208 /* module not changed, print the name unprefixed */
Radek Krejci70ee9152019-07-25 11:27:27 +0200209 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name);
Radek Krejci327de162019-06-14 12:52:07 +0200210 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200211 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s:%s", nextlevel ? "/" : "", ctx->mod->name, name);
Radek Krejci327de162019-06-14 12:52:07 +0200212 }
213 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200214 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
Radek Krejci327de162019-06-14 12:52:07 +0200215 }
Radek Krejciacc79042019-07-25 14:14:57 +0200216 if (len >= LYSC_CTX_BUFSIZE - ctx->path_len) {
217 /* output truncated */
218 ctx->path_len = LYSC_CTX_BUFSIZE - 1;
219 } else {
220 ctx->path_len += len;
221 }
Radek Krejci327de162019-06-14 12:52:07 +0200222 }
223}
224
225/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100226 * @brief Duplicate the compiled pattern structure.
227 *
228 * Instead of duplicating memory, the reference counter in the @p orig is increased.
229 *
230 * @param[in] orig The pattern structure to duplicate.
231 * @return The duplicated structure to use.
232 */
Radek Krejci19a96102018-11-15 13:38:09 +0100233static struct lysc_pattern*
234lysc_pattern_dup(struct lysc_pattern *orig)
235{
236 ++orig->refcount;
237 return orig;
238}
239
Radek Krejcib56c7502019-02-13 14:19:54 +0100240/**
241 * @brief Duplicate the array of compiled patterns.
242 *
243 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
244 *
245 * @param[in] ctx Libyang context for logging.
246 * @param[in] orig The patterns sized array to duplicate.
247 * @return New sized array as a copy of @p orig.
248 * @return NULL in case of memory allocation error.
249 */
Radek Krejci19a96102018-11-15 13:38:09 +0100250static struct lysc_pattern**
251lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
252{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100253 struct lysc_pattern **dup = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100254 unsigned int u;
255
Radek Krejcib56c7502019-02-13 14:19:54 +0100256 assert(orig);
257
Radek Krejci19a96102018-11-15 13:38:09 +0100258 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL);
259 LY_ARRAY_FOR(orig, u) {
260 dup[u] = lysc_pattern_dup(orig[u]);
261 LY_ARRAY_INCREMENT(dup);
262 }
263 return dup;
264}
265
Radek Krejcib56c7502019-02-13 14:19:54 +0100266/**
267 * @brief Duplicate compiled range structure.
268 *
269 * @param[in] ctx Libyang context for logging.
270 * @param[in] orig The range structure to be duplicated.
271 * @return New compiled range structure as a copy of @p orig.
272 * @return NULL in case of memory allocation error.
273 */
Radek Krejci19a96102018-11-15 13:38:09 +0100274struct lysc_range*
275lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
276{
277 struct lysc_range *dup;
278 LY_ERR ret;
279
Radek Krejcib56c7502019-02-13 14:19:54 +0100280 assert(orig);
281
Radek Krejci19a96102018-11-15 13:38:09 +0100282 dup = calloc(1, sizeof *dup);
283 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
284 if (orig->parts) {
285 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup);
286 LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts);
287 memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts);
288 }
289 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
290 DUP_STRING(ctx, orig->emsg, dup->emsg);
291 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
292
293 return dup;
294cleanup:
295 free(dup);
296 (void) ret; /* set but not used due to the return type */
297 return NULL;
298}
299
Radek Krejcib56c7502019-02-13 14:19:54 +0100300/**
301 * @brief Stack for processing if-feature expressions.
302 */
Radek Krejci19a96102018-11-15 13:38:09 +0100303struct iff_stack {
Radek Krejcib56c7502019-02-13 14:19:54 +0100304 int size; /**< number of items in the stack */
305 int index; /**< first empty item */
306 uint8_t *stack;/**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100307};
308
Radek Krejcib56c7502019-02-13 14:19:54 +0100309/**
310 * @brief Add @ref ifftokens into the stack.
311 * @param[in] stack The if-feature stack to use.
312 * @param[in] value One of the @ref ifftokens to store in the stack.
313 * @return LY_EMEM in case of memory allocation error
314 * @return LY_ESUCCESS if the value successfully stored.
315 */
Radek Krejci19a96102018-11-15 13:38:09 +0100316static LY_ERR
317iff_stack_push(struct iff_stack *stack, uint8_t value)
318{
319 if (stack->index == stack->size) {
320 stack->size += 4;
321 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
322 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
323 }
324 stack->stack[stack->index++] = value;
325 return LY_SUCCESS;
326}
327
Radek Krejcib56c7502019-02-13 14:19:54 +0100328/**
329 * @brief Get (and remove) the last item form the stack.
330 * @param[in] stack The if-feature stack to use.
331 * @return The value from the top of the stack.
332 */
Radek Krejci19a96102018-11-15 13:38:09 +0100333static uint8_t
334iff_stack_pop(struct iff_stack *stack)
335{
Radek Krejcib56c7502019-02-13 14:19:54 +0100336 assert(stack && stack->index);
337
Radek Krejci19a96102018-11-15 13:38:09 +0100338 stack->index--;
339 return stack->stack[stack->index];
340}
341
Radek Krejcib56c7502019-02-13 14:19:54 +0100342/**
343 * @brief Clean up the stack.
344 * @param[in] stack The if-feature stack to use.
345 */
Radek Krejci19a96102018-11-15 13:38:09 +0100346static void
347iff_stack_clean(struct iff_stack *stack)
348{
349 stack->size = 0;
350 free(stack->stack);
351}
352
Radek Krejcib56c7502019-02-13 14:19:54 +0100353/**
354 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
355 * (libyang format of the if-feature expression).
356 * @param[in,out] list The 2bits array to modify.
357 * @param[in] op The operand (@ref ifftokens) to store.
358 * @param[in] pos Position (0-based) where to store the given @p op.
359 */
Radek Krejci19a96102018-11-15 13:38:09 +0100360static void
361iff_setop(uint8_t *list, uint8_t op, int pos)
362{
363 uint8_t *item;
364 uint8_t mask = 3;
365
366 assert(pos >= 0);
367 assert(op <= 3); /* max 2 bits */
368
369 item = &list[pos / 4];
370 mask = mask << 2 * (pos % 4);
371 *item = (*item) & ~mask;
372 *item = (*item) | (op << 2 * (pos % 4));
373}
374
Radek Krejcib56c7502019-02-13 14:19:54 +0100375#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
376#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100377
Radek Krejci0af46292019-01-11 16:02:31 +0100378/**
379 * @brief Find a feature of the given name and referenced in the given module.
380 *
381 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
382 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
383 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
384 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
385 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
386 * assigned till that time will be still valid.
387 *
388 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
389 * @param[in] name Name of the feature including possible prefix.
390 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
391 * @return Pointer to the feature structure if found, NULL otherwise.
392 */
Radek Krejci19a96102018-11-15 13:38:09 +0100393static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100394lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100395{
396 size_t i;
Radek Krejci0af46292019-01-11 16:02:31 +0100397 struct lysc_feature *f, *flist;
Radek Krejci19a96102018-11-15 13:38:09 +0100398
399 for (i = 0; i < len; ++i) {
400 if (name[i] == ':') {
401 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100402 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100403 LY_CHECK_RET(!mod, NULL);
404
405 name = &name[i + 1];
406 len = len - i - 1;
407 }
408 }
409
410 /* we have the correct module, get the feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100411 if (mod->implemented) {
412 /* module is implemented so there is already the compiled schema */
413 flist = mod->compiled->features;
414 } else {
415 flist = mod->off_features;
416 }
417 LY_ARRAY_FOR(flist, i) {
418 f = &flist[i];
Radek Krejci19a96102018-11-15 13:38:09 +0100419 if (!strncmp(f->name, name, len) && f->name[len] == '\0') {
420 return f;
421 }
422 }
423
424 return NULL;
425}
426
427static LY_ERR
Radek Krejci0935f412019-08-20 16:15:18 +0200428lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent, LYEXT_PARENT parent_type)
Radek Krejci19a96102018-11-15 13:38:09 +0100429{
430 const char *name;
431 unsigned int u;
432 const struct lys_module *mod;
Radek Krejci0935f412019-08-20 16:15:18 +0200433 struct lysc_ext *elist = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100434
435 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
436 ext->insubstmt = ext_p->insubstmt;
437 ext->insubstmt_index = ext_p->insubstmt_index;
Radek Krejci0935f412019-08-20 16:15:18 +0200438 ext->parent = parent;
439 ext->parent_type = parent_type;
Radek Krejci19a96102018-11-15 13:38:09 +0100440
441 /* get module where the extension definition should be placed */
442 for (u = 0; ext_p->name[u] != ':'; ++u);
Radek Krejcie86bf772018-12-14 11:39:53 +0100443 mod = lys_module_find_prefix(ctx->mod_def, ext_p->name, u);
Radek Krejci19a96102018-11-15 13:38:09 +0100444 LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
445 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name),
446 LY_EVALID);
447 LY_CHECK_ERR_RET(!mod->parsed->extensions,
448 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
449 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100450 ext_p->name, mod->name),
Radek Krejci19a96102018-11-15 13:38:09 +0100451 LY_EVALID);
452 name = &ext_p->name[u + 1];
Radek Krejci0935f412019-08-20 16:15:18 +0200453
Radek Krejci19a96102018-11-15 13:38:09 +0100454 /* find the extension definition there */
Radek Krejci0935f412019-08-20 16:15:18 +0200455 if (mod->off_extensions) {
456 elist = mod->off_extensions;
457 } else {
458 elist = mod->compiled->extensions;
459 }
460 LY_ARRAY_FOR(elist, u) {
461 if (!strcmp(name, elist[u].name)) {
462 ext->def = &elist[u];
Radek Krejci19a96102018-11-15 13:38:09 +0100463 break;
464 }
465 }
Radek Krejci0935f412019-08-20 16:15:18 +0200466 LY_CHECK_ERR_RET(!ext->def,
467 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
468 "Extension definition of extension instance \"%s\" not found.", ext_p->name),
Radek Krejci19a96102018-11-15 13:38:09 +0100469 LY_EVALID);
Radek Krejci0935f412019-08-20 16:15:18 +0200470
471 if (ext->def->plugin && ext->def->plugin->compile) {
472 LY_CHECK_RET(ext->def->plugin->compile(ctx, ext_p, ext),LY_EVALID);
473 }
474
475 return LY_SUCCESS;
476}
477
Radek Krejcid6b76452019-09-03 17:03:03 +0200478LY_ERR
479lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
480{
481 unsigned int u;
482 struct lysp_stmt *stmt;
483
484 for (stmt = ext->child; stmt; stmt = stmt->next) {
485 const char *s = stmt->stmt;
486 enum ly_stmt kw = lysp_match_kw(NULL, &s);
487 if (!kw || *s != '\0') {
488 /* TODO handle other extension instances */
489 goto invalid_stmt;
490 }
491 for (u = 0; substmts[u].stmt; ++u) {
492 if (substmts[u].stmt == kw) {
493 break;
494 }
495 }
496 if (!substmts[u].stmt) {
497invalid_stmt:
498 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s\" extension instance.",
499 ly_stmt2str(kw), ext->name);
500 return LY_EVALID;
501 }
502
503 /* TODO parse stmt to lysp */
504 /* TODO compile lysp to lysc */
505 }
506 return LY_SUCCESS;
507}
508
Radek Krejci0935f412019-08-20 16:15:18 +0200509/**
510 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
511 */
512static LY_ERR
513lys_compile_extension(struct lysc_ctx *ctx, struct lysp_ext *ext_p, struct lysc_ext *ext)
514{
515 LY_ERR ret = LY_SUCCESS;
516
517 DUP_STRING(ctx->ctx, ext_p->name, ext->name);
518 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
519 ext->module = ctx->mod_def;
520 COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext->exts, ext, LYEXT_PAR_EXT, ret, done);
521
522done:
523 return ret;
524}
525
526/**
527 * @brief Link the extensions definitions with the available extension plugins.
528 *
529 * This is done only in the compiled (implemented) module. Extensions of a non-implemented modules
530 * are not connected with even available extension plugins.
531 *
532 * @param[in] extensions List of extensions to be processed ([sized array](@ref sizedarrays)).
533 */
534static void
535lys_compile_extension_plugins(struct lysc_ext *extensions)
536{
537 unsigned int u;
538
539 LY_ARRAY_FOR(extensions, u) {
540 extensions[u].plugin = lyext_get_plugin(&extensions[u]);
541 }
542}
543
544LY_ERR
545lys_extension_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
546 struct lysp_ext *extensions_p, struct lysc_ext **extensions)
547{
548 unsigned int offset = 0, u;
549 struct lysc_ctx context = {0};
550
551 assert(ctx_sc || ctx);
552
553 if (!ctx_sc) {
554 context.ctx = ctx;
555 context.mod = module;
556 context.path_len = 1;
557 context.path[0] = '/';
558 ctx_sc = &context;
559 }
560
561 if (!extensions_p) {
562 return LY_SUCCESS;
563 }
564 if (*extensions) {
565 offset = LY_ARRAY_SIZE(*extensions);
566 }
567
568 lysc_update_path(ctx_sc, NULL, "{extension}");
569 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *extensions, LY_ARRAY_SIZE(extensions_p), LY_EMEM);
570 LY_ARRAY_FOR(extensions_p, u) {
571 lysc_update_path(ctx_sc, NULL, extensions_p[u].name);
572 LY_ARRAY_INCREMENT(*extensions);
573 COMPILE_CHECK_UNIQUENESS(ctx_sc, *extensions, name, &(*extensions)[offset + u], "extension", extensions_p[u].name);
574 LY_CHECK_RET(lys_compile_extension(ctx_sc, &extensions_p[u], &(*extensions)[offset + u]));
575 lysc_update_path(ctx_sc, NULL, NULL);
576 }
577 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100578
579 return LY_SUCCESS;
580}
581
Radek Krejcib56c7502019-02-13 14:19:54 +0100582/**
583 * @brief Compile information from the if-feature statement
584 * @param[in] ctx Compile context.
585 * @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 +0100586 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
587 * @return LY_ERR value.
588 */
Radek Krejci19a96102018-11-15 13:38:09 +0100589static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200590lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff)
Radek Krejci19a96102018-11-15 13:38:09 +0100591{
592 const char *c = *value;
593 int r, rc = EXIT_FAILURE;
594 int i, j, last_not, checkversion = 0;
595 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
596 uint8_t op;
597 struct iff_stack stack = {0, 0, NULL};
598 struct lysc_feature *f;
599
600 assert(c);
601
602 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
603 for (i = j = last_not = 0; c[i]; i++) {
604 if (c[i] == '(') {
605 j++;
606 checkversion = 1;
607 continue;
608 } else if (c[i] == ')') {
609 j--;
610 continue;
611 } else if (isspace(c[i])) {
612 checkversion = 1;
613 continue;
614 }
615
616 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 +0200617 int sp;
618 for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++);
619 if (c[i + r + sp] == '\0') {
Radek Krejci19a96102018-11-15 13:38:09 +0100620 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
621 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
622 return LY_EVALID;
623 } else if (!isspace(c[i + r])) {
624 /* feature name starting with the not/and/or */
625 last_not = 0;
626 f_size++;
627 } else if (c[i] == 'n') { /* not operation */
628 if (last_not) {
629 /* double not */
630 expr_size = expr_size - 2;
631 last_not = 0;
632 } else {
633 last_not = 1;
634 }
635 } else { /* and, or */
Radek Krejci6788abc2019-06-14 13:56:49 +0200636 if (f_exp != f_size) {
637 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
638 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]);
639 return LY_EVALID;
640 }
Radek Krejci19a96102018-11-15 13:38:09 +0100641 f_exp++;
Radek Krejci6788abc2019-06-14 13:56:49 +0200642
Radek Krejci19a96102018-11-15 13:38:09 +0100643 /* not a not operation */
644 last_not = 0;
645 }
646 i += r;
647 } else {
648 f_size++;
649 last_not = 0;
650 }
651 expr_size++;
652
653 while (!isspace(c[i])) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200654 if (!c[i] || c[i] == ')' || c[i] == '(') {
Radek Krejci19a96102018-11-15 13:38:09 +0100655 i--;
656 break;
657 }
658 i++;
659 }
660 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200661 if (j) {
Radek Krejci19a96102018-11-15 13:38:09 +0100662 /* not matching count of ( and ) */
663 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
664 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
665 return LY_EVALID;
666 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200667 if (f_exp != f_size) {
668 /* features do not match the needed arguments for the logical operations */
669 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
670 "Invalid value \"%s\" of if-feature - number of features in expression does not match "
671 "the required number of operands for the operations.", *value);
672 return LY_EVALID;
673 }
Radek Krejci19a96102018-11-15 13:38:09 +0100674
675 if (checkversion || expr_size > 1) {
676 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100677 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100678 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
679 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
680 return LY_EVALID;
681 }
682 }
683
684 /* allocate the memory */
685 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
686 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
687 stack.stack = malloc(expr_size * sizeof *stack.stack);
688 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
689
690 stack.size = expr_size;
691 f_size--; expr_size--; /* used as indexes from now */
692
693 for (i--; i >= 0; i--) {
694 if (c[i] == ')') {
695 /* push it on stack */
696 iff_stack_push(&stack, LYS_IFF_RP);
697 continue;
698 } else if (c[i] == '(') {
699 /* pop from the stack into result all operators until ) */
700 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
701 iff_setop(iff->expr, op, expr_size--);
702 }
703 continue;
704 } else if (isspace(c[i])) {
705 continue;
706 }
707
708 /* end of operator or operand -> find beginning and get what is it */
709 j = i + 1;
710 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
711 i--;
712 }
713 i++; /* go back by one step */
714
715 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
716 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
717 /* double not */
718 iff_stack_pop(&stack);
719 } else {
720 /* not has the highest priority, so do not pop from the stack
721 * as in case of AND and OR */
722 iff_stack_push(&stack, LYS_IFF_NOT);
723 }
724 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
725 /* as for OR - pop from the stack all operators with the same or higher
726 * priority and store them to the result, then push the AND to the stack */
727 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
728 op = iff_stack_pop(&stack);
729 iff_setop(iff->expr, op, expr_size--);
730 }
731 iff_stack_push(&stack, LYS_IFF_AND);
732 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
733 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
734 op = iff_stack_pop(&stack);
735 iff_setop(iff->expr, op, expr_size--);
736 }
737 iff_stack_push(&stack, LYS_IFF_OR);
738 } else {
739 /* feature name, length is j - i */
740
741 /* add it to the expression */
742 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
743
744 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100745 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
746 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
747 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
748 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100749 iff->features[f_size] = f;
750 LY_ARRAY_INCREMENT(iff->features);
751 f_size--;
752 }
753 }
754 while (stack.index) {
755 op = iff_stack_pop(&stack);
756 iff_setop(iff->expr, op, expr_size--);
757 }
758
759 if (++expr_size || ++f_size) {
760 /* not all expected operators and operands found */
761 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
762 "Invalid value \"%s\" of if-feature - processing error.", *value);
763 rc = LY_EINT;
764 } else {
765 rc = LY_SUCCESS;
766 }
767
768error:
769 /* cleanup */
770 iff_stack_clean(&stack);
771
772 return rc;
773}
774
Radek Krejcib56c7502019-02-13 14:19:54 +0100775/**
776 * @brief Compile information from the when statement
777 * @param[in] ctx Compile context.
778 * @param[in] when_p The parsed when statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100779 * @param[out] when Pointer where to store pointer to the created compiled when structure.
780 * @return LY_ERR value.
781 */
Radek Krejci19a96102018-11-15 13:38:09 +0100782static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200783lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, struct lysc_when **when)
Radek Krejci19a96102018-11-15 13:38:09 +0100784{
Radek Krejci19a96102018-11-15 13:38:09 +0100785 LY_ERR ret = LY_SUCCESS;
786
Radek Krejci00b874b2019-02-12 10:54:50 +0100787 *when = calloc(1, sizeof **when);
788 (*when)->refcount = 1;
789 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond);
790 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
791 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
792 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci0935f412019-08-20 16:15:18 +0200793 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100794
795done:
796 return ret;
797}
798
Radek Krejcib56c7502019-02-13 14:19:54 +0100799/**
800 * @brief Compile information from the must statement
801 * @param[in] ctx Compile context.
802 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100803 * @param[in,out] must Prepared (empty) compiled must structure to fill.
804 * @return LY_ERR value.
805 */
Radek Krejci19a96102018-11-15 13:38:09 +0100806static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200807lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100808{
Radek Krejci19a96102018-11-15 13:38:09 +0100809 LY_ERR ret = LY_SUCCESS;
810
811 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg);
812 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci462cf252019-07-24 09:49:08 +0200813 must->module = ctx->mod_def;
Radek Krejci19a96102018-11-15 13:38:09 +0100814 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
815 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100816 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
817 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejci0935f412019-08-20 16:15:18 +0200818 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100819
820done:
821 return ret;
822}
823
Radek Krejcib56c7502019-02-13 14:19:54 +0100824/**
825 * @brief Compile information from the import statement
826 * @param[in] ctx Compile context.
827 * @param[in] imp_p The parsed import statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100828 * @param[in,out] imp Prepared (empty) compiled import structure to fill.
829 * @return LY_ERR value.
830 */
Radek Krejci19a96102018-11-15 13:38:09 +0100831static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200832lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, struct lysc_import *imp)
Radek Krejci19a96102018-11-15 13:38:09 +0100833{
Radek Krejci19a96102018-11-15 13:38:09 +0100834 struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100835 LY_ERR ret = LY_SUCCESS;
836
837 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
Radek Krejci0935f412019-08-20 16:15:18 +0200838 COMPILE_EXTS_GOTO(ctx, imp_p->exts, imp->exts, imp, LYEXT_PAR_IMPORT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100839 imp->module = imp_p->module;
840
Radek Krejci7f2a5362018-11-28 13:05:37 +0100841 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
842 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100843 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100844 if (!imp->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100845 /* try to use filepath if present */
846 if (imp->module->filepath) {
847 mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath,
848 !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
Radek Krejci19a96102018-11-15 13:38:09 +0100849 if (mod != imp->module) {
850 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100851 imp->module->filepath, imp->module->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100852 mod = NULL;
853 }
854 }
855 if (!mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100856 if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, &mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100857 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 +0100858 imp->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100859 return LY_ENOTFOUND;
860 }
861 }
Radek Krejci19a96102018-11-15 13:38:09 +0100862 }
863
864done:
865 return ret;
866}
867
Radek Krejcib56c7502019-02-13 14:19:54 +0100868/**
869 * @brief Compile information from the identity statement
870 *
871 * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases().
872 *
873 * @param[in] ctx Compile context.
874 * @param[in] ident_p The parsed identity statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100875 * @param[in] idents List of so far compiled identities to check the name uniqueness.
876 * @param[in,out] ident Prepared (empty) compiled identity structure to fill.
877 * @return LY_ERR value.
878 */
Radek Krejci19a96102018-11-15 13:38:09 +0100879static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200880lys_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 +0100881{
882 unsigned int u;
883 LY_ERR ret = LY_SUCCESS;
884
Radek Krejci327de162019-06-14 12:52:07 +0200885 lysc_update_path(ctx, NULL, ident_p->name);
886
Radek Krejcid05cbd92018-12-05 14:26:40 +0100887 COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100888 DUP_STRING(ctx->ctx, ident_p->name, ident->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200889 DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc);
890 DUP_STRING(ctx->ctx, ident_p->ref, ident->ref);
Radek Krejci693262f2019-04-29 15:23:20 +0200891 ident->module = ctx->mod;
Radek Krejciec4da802019-05-02 13:02:41 +0200892 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100893 /* 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 +0200894 COMPILE_EXTS_GOTO(ctx, ident_p->exts, ident->exts, ident, LYEXT_PAR_IDENT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100895 ident->flags = ident_p->flags;
896
Radek Krejci327de162019-06-14 12:52:07 +0200897 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100898done:
899 return ret;
900}
901
Radek Krejcib56c7502019-02-13 14:19:54 +0100902/**
903 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
904 *
905 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
906 *
907 * @param[in] ctx Compile context for logging.
908 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
909 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
910 * @return LY_SUCCESS if everything is ok.
911 * @return LY_EVALID if the identity is derived from itself.
912 */
Radek Krejci38222632019-02-12 16:55:05 +0100913static LY_ERR
914lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
915{
916 LY_ERR ret = LY_EVALID;
917 unsigned int u, v;
918 struct ly_set recursion = {0};
919 struct lysc_ident *drv;
920
921 if (!derived) {
922 return LY_SUCCESS;
923 }
924
925 for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) {
926 if (ident == derived[u]) {
927 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
928 "Identity \"%s\" is indirectly derived from itself.", ident->name);
929 goto cleanup;
930 }
931 ly_set_add(&recursion, derived[u], 0);
932 }
933
934 for (v = 0; v < recursion.count; ++v) {
935 drv = recursion.objs[v];
936 if (!drv->derived) {
937 continue;
938 }
939 for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) {
940 if (ident == drv->derived[u]) {
941 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
942 "Identity \"%s\" is indirectly derived from itself.", ident->name);
943 goto cleanup;
944 }
945 ly_set_add(&recursion, drv->derived[u], 0);
946 }
947 }
948 ret = LY_SUCCESS;
949
950cleanup:
951 ly_set_erase(&recursion, NULL);
952 return ret;
953}
954
Radek Krejcia3045382018-11-22 14:30:31 +0100955/**
956 * @brief Find and process the referenced base identities from another identity or identityref
957 *
Radek Krejciaca74032019-06-04 08:53:06 +0200958 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +0100959 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
960 * to distinguish these two use cases.
961 *
962 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
963 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
964 * @param[in] ident Referencing identity to work with.
965 * @param[in] bases Array of bases of identityref to fill in.
966 * @return LY_ERR value.
967 */
Radek Krejci19a96102018-11-15 13:38:09 +0100968static LY_ERR
Radek Krejci555cb5b2018-11-16 14:54:33 +0100969lys_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 +0100970{
Radek Krejci555cb5b2018-11-16 14:54:33 +0100971 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +0100972 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +0100973 struct lys_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100974 struct lysc_ident **idref;
975
976 assert(ident || bases);
977
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100978 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100979 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
980 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
981 return LY_EVALID;
982 }
983
984 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
985 s = strchr(bases_p[u], ':');
986 if (s) {
987 /* prefixed identity */
988 name = &s[1];
Radek Krejcie86bf772018-12-14 11:39:53 +0100989 mod = lys_module_find_prefix(ctx->mod_def, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100990 } else {
991 name = bases_p[u];
Radek Krejcie86bf772018-12-14 11:39:53 +0100992 mod = ctx->mod_def;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100993 }
994 if (!mod) {
995 if (ident) {
996 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
997 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
998 } else {
999 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1000 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
1001 }
1002 return LY_EVALID;
1003 }
1004 idref = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001005 if (mod->compiled && mod->compiled->identities) {
1006 for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) {
1007 if (!strcmp(name, mod->compiled->identities[v].name)) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001008 if (ident) {
Radek Krejci38222632019-02-12 16:55:05 +01001009 if (ident == &mod->compiled->identities[v]) {
1010 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1011 "Identity \"%s\" is derived from itself.", ident->name);
1012 return LY_EVALID;
1013 }
1014 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->compiled->identities[v], ident->derived));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001015 /* we have match! store the backlink */
Radek Krejcie86bf772018-12-14 11:39:53 +01001016 LY_ARRAY_NEW_RET(ctx->ctx, mod->compiled->identities[v].derived, idref, LY_EMEM);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001017 *idref = ident;
1018 } else {
1019 /* we have match! store the found identity */
1020 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01001021 *idref = &mod->compiled->identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +01001022 }
1023 break;
1024 }
1025 }
1026 }
1027 if (!idref || !(*idref)) {
1028 if (ident) {
1029 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1030 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
1031 } else {
1032 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1033 "Unable to find base (%s) of identityref.", bases_p[u]);
1034 }
1035 return LY_EVALID;
1036 }
1037 }
1038 return LY_SUCCESS;
1039}
1040
Radek Krejcia3045382018-11-22 14:30:31 +01001041/**
1042 * @brief For the given array of identities, set the backlinks from all their base identities.
1043 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1044 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
1045 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
1046 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1047 */
Radek Krejci555cb5b2018-11-16 14:54:33 +01001048static LY_ERR
1049lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
1050{
1051 unsigned int i;
Radek Krejci19a96102018-11-15 13:38:09 +01001052
1053 for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) {
1054 if (!idents_p[i].bases) {
1055 continue;
1056 }
Radek Krejci327de162019-06-14 12:52:07 +02001057 lysc_update_path(ctx, NULL, idents[i].name);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001058 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL));
Radek Krejci327de162019-06-14 12:52:07 +02001059 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001060 }
1061 return LY_SUCCESS;
1062}
1063
Radek Krejci0af46292019-01-11 16:02:31 +01001064LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02001065lys_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 +01001066{
1067 unsigned int offset = 0, u;
1068 struct lysc_ctx context = {0};
1069
Radek Krejci327de162019-06-14 12:52:07 +02001070 assert(ctx_sc || ctx);
1071
1072 if (!ctx_sc) {
1073 context.ctx = ctx;
1074 context.mod = module;
1075 context.path_len = 1;
1076 context.path[0] = '/';
1077 ctx_sc = &context;
1078 }
Radek Krejci0af46292019-01-11 16:02:31 +01001079
1080 if (!features_p) {
1081 return LY_SUCCESS;
1082 }
1083 if (*features) {
1084 offset = LY_ARRAY_SIZE(*features);
1085 }
1086
Radek Krejci327de162019-06-14 12:52:07 +02001087 lysc_update_path(ctx_sc, NULL, "{feature}");
1088 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM);
Radek Krejci0af46292019-01-11 16:02:31 +01001089 LY_ARRAY_FOR(features_p, u) {
Radek Krejci327de162019-06-14 12:52:07 +02001090 lysc_update_path(ctx_sc, NULL, features_p[u].name);
1091
Radek Krejci0af46292019-01-11 16:02:31 +01001092 LY_ARRAY_INCREMENT(*features);
Radek Krejci327de162019-06-14 12:52:07 +02001093 COMPILE_CHECK_UNIQUENESS(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
1094 DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name);
1095 DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc);
1096 DUP_STRING(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref);
Radek Krejci0af46292019-01-11 16:02:31 +01001097 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci327de162019-06-14 12:52:07 +02001098 (*features)[offset + u].module = ctx_sc->mod;
1099
1100 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001101 }
Radek Krejci327de162019-06-14 12:52:07 +02001102 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001103
1104 return LY_SUCCESS;
1105}
1106
Radek Krejcia3045382018-11-22 14:30:31 +01001107/**
Radek Krejci09a1fc52019-02-13 10:55:17 +01001108 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +01001109 *
1110 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
1111 *
Radek Krejci09a1fc52019-02-13 10:55:17 +01001112 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +01001113 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
1114 * being currently processed).
1115 * @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 +01001116 * @return LY_SUCCESS if everything is ok.
1117 * @return LY_EVALID if the feature references indirectly itself.
1118 */
1119static LY_ERR
1120lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
1121{
1122 LY_ERR ret = LY_EVALID;
1123 unsigned int u, v;
1124 struct ly_set recursion = {0};
1125 struct lysc_feature *drv;
1126
1127 if (!depfeatures) {
1128 return LY_SUCCESS;
1129 }
1130
1131 for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) {
1132 if (feature == depfeatures[u]) {
1133 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1134 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1135 goto cleanup;
1136 }
1137 ly_set_add(&recursion, depfeatures[u], 0);
1138 }
1139
1140 for (v = 0; v < recursion.count; ++v) {
1141 drv = recursion.objs[v];
1142 if (!drv->depfeatures) {
1143 continue;
1144 }
1145 for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) {
1146 if (feature == drv->depfeatures[u]) {
1147 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1148 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1149 goto cleanup;
1150 }
1151 ly_set_add(&recursion, drv->depfeatures[u], 0);
1152 }
1153 }
1154 ret = LY_SUCCESS;
1155
1156cleanup:
1157 ly_set_erase(&recursion, NULL);
1158 return ret;
1159}
1160
1161/**
Radek Krejci0af46292019-01-11 16:02:31 +01001162 * @brief Create pre-compiled features array.
1163 *
1164 * See lys_feature_precompile() for more details.
1165 *
Radek Krejcia3045382018-11-22 14:30:31 +01001166 * @param[in] ctx Compile context.
1167 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +01001168 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +01001169 * @return LY_ERR value.
1170 */
Radek Krejci19a96102018-11-15 13:38:09 +01001171static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001172lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +01001173{
Radek Krejci0af46292019-01-11 16:02:31 +01001174 unsigned int u, v, x;
1175 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +01001176 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01001177
Radek Krejci327de162019-06-14 12:52:07 +02001178
Radek Krejci0af46292019-01-11 16:02:31 +01001179 /* find the preprecompiled feature */
1180 LY_ARRAY_FOR(features, x) {
1181 if (strcmp(features[x].name, feature_p->name)) {
1182 continue;
1183 }
1184 feature = &features[x];
Radek Krejci327de162019-06-14 12:52:07 +02001185 lysc_update_path(ctx, NULL, "{feature}");
1186 lysc_update_path(ctx, NULL, feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001187
Radek Krejci0af46292019-01-11 16:02:31 +01001188 /* finish compilation started in lys_feature_precompile() */
Radek Krejci0935f412019-08-20 16:15:18 +02001189 COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02001190 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001191 if (feature->iffeatures) {
1192 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
1193 if (feature->iffeatures[u].features) {
1194 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001195 /* check for circular dependency - direct reference first,... */
1196 if (feature == feature->iffeatures[u].features[v]) {
1197 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1198 "Feature \"%s\" is referenced from itself.", feature->name);
1199 return LY_EVALID;
1200 }
1201 /* ... and indirect circular reference */
1202 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1203
Radek Krejci0af46292019-01-11 16:02:31 +01001204 /* add itself into the dependants list */
1205 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1206 *df = feature;
1207 }
Radek Krejci19a96102018-11-15 13:38:09 +01001208 }
Radek Krejci19a96102018-11-15 13:38:09 +01001209 }
1210 }
Radek Krejci327de162019-06-14 12:52:07 +02001211 lysc_update_path(ctx, NULL, NULL);
1212 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001213 done:
1214 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01001215 }
Radek Krejci0af46292019-01-11 16:02:31 +01001216
1217 LOGINT(ctx->ctx);
1218 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +01001219}
1220
Radek Krejcib56c7502019-02-13 14:19:54 +01001221/**
1222 * @brief Revert compiled list of features back to the precompiled state.
1223 *
1224 * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status.
1225 * The features are supposed to be stored again as off_features in ::lys_module structure.
1226 *
1227 * @param[in] ctx Compilation context.
1228 * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema
1229 * and supposed to hold the off_features list.
1230 */
Radek Krejci95710c92019-02-11 15:49:55 +01001231static void
1232lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1233{
1234 unsigned int u, v;
1235
1236 /* keep the off_features list until the complete lys_module is freed */
1237 mod->off_features = mod->compiled->features;
1238 mod->compiled->features = NULL;
1239
1240 /* in the off_features list, remove all the parts (from finished compiling process)
1241 * which may points into the data being freed here */
1242 LY_ARRAY_FOR(mod->off_features, u) {
1243 LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) {
1244 lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]);
1245 }
1246 LY_ARRAY_FREE(mod->off_features[u].iffeatures);
1247 mod->off_features[u].iffeatures = NULL;
1248
1249 LY_ARRAY_FOR(mod->off_features[u].exts, v) {
1250 lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]);
1251 }
1252 LY_ARRAY_FREE(mod->off_features[u].exts);
1253 mod->off_features[u].exts = NULL;
1254 }
1255}
1256
Radek Krejcia3045382018-11-22 14:30:31 +01001257/**
1258 * @brief Validate and normalize numeric value from a range definition.
1259 * @param[in] ctx Compile context.
1260 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
1261 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
1262 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
1263 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
1264 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1265 * @param[in] value String value of the range boundary.
1266 * @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.
1267 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1268 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1269 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001270LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001271range_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 +01001272{
Radek Krejci6cba4292018-11-15 17:33:29 +01001273 size_t fraction = 0, size;
1274
Radek Krejci19a96102018-11-15 13:38:09 +01001275 *len = 0;
1276
1277 assert(value);
1278 /* parse value */
1279 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1280 return LY_EVALID;
1281 }
1282
1283 if ((value[*len] == '-') || (value[*len] == '+')) {
1284 ++(*len);
1285 }
1286
1287 while (isdigit(value[*len])) {
1288 ++(*len);
1289 }
1290
1291 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001292 if (basetype == LY_TYPE_DEC64) {
1293 goto decimal;
1294 } else {
1295 *valcopy = strndup(value, *len);
1296 return LY_SUCCESS;
1297 }
Radek Krejci19a96102018-11-15 13:38:09 +01001298 }
1299 fraction = *len;
1300
1301 ++(*len);
1302 while (isdigit(value[*len])) {
1303 ++(*len);
1304 }
1305
Radek Krejci6cba4292018-11-15 17:33:29 +01001306 if (basetype == LY_TYPE_DEC64) {
1307decimal:
1308 assert(frdigits);
Radek Krejci943177f2019-06-14 16:32:43 +02001309 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001310 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1311 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1312 *len, value, frdigits);
1313 return LY_EINVAL;
1314 }
1315 if (fraction) {
1316 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1317 } else {
1318 size = (*len) + frdigits + 1;
1319 }
1320 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001321 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1322
Radek Krejci6cba4292018-11-15 17:33:29 +01001323 (*valcopy)[size - 1] = '\0';
1324 if (fraction) {
1325 memcpy(&(*valcopy)[0], &value[0], fraction);
1326 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1327 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1328 } else {
1329 memcpy(&(*valcopy)[0], &value[0], *len);
1330 memset(&(*valcopy)[*len], '0', frdigits);
1331 }
Radek Krejci19a96102018-11-15 13:38:09 +01001332 }
1333 return LY_SUCCESS;
1334}
1335
Radek Krejcia3045382018-11-22 14:30:31 +01001336/**
1337 * @brief Check that values in range are in ascendant order.
1338 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001339 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1340 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001341 * @param[in] value Current value to check.
1342 * @param[in] prev_value The last seen value.
1343 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1344 */
Radek Krejci19a96102018-11-15 13:38:09 +01001345static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001346range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001347{
1348 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001349 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001350 return LY_EEXIST;
1351 }
1352 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001353 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001354 return LY_EEXIST;
1355 }
1356 }
1357 return LY_SUCCESS;
1358}
1359
Radek Krejcia3045382018-11-22 14:30:31 +01001360/**
1361 * @brief Set min/max value of the range part.
1362 * @param[in] ctx Compile context.
1363 * @param[in] part Range part structure to fill.
1364 * @param[in] max Flag to distinguish if storing min or max value.
1365 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1366 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1367 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1368 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1369 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001370 * @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 +01001371 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1372 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1373 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1374 * frdigits value), LY_EMEM.
1375 */
Radek Krejci19a96102018-11-15 13:38:09 +01001376static LY_ERR
1377range_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 +01001378 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001379{
1380 LY_ERR ret = LY_SUCCESS;
1381 char *valcopy = NULL;
1382 size_t len;
1383
1384 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001385 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001386 LY_CHECK_GOTO(ret, finalize);
1387 }
1388 if (!valcopy && base_range) {
1389 if (max) {
1390 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
1391 } else {
1392 part->min_64 = base_range->parts[0].min_64;
1393 }
1394 if (!first) {
1395 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1396 }
1397 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001398 }
1399
1400 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001401 case LY_TYPE_INT8: /* range */
1402 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001403 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 +01001404 } else if (max) {
1405 part->max_64 = INT64_C(127);
1406 } else {
1407 part->min_64 = INT64_C(-128);
1408 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001409 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001410 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001411 }
1412 break;
1413 case LY_TYPE_INT16: /* range */
1414 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001415 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 +01001416 } else if (max) {
1417 part->max_64 = INT64_C(32767);
1418 } else {
1419 part->min_64 = INT64_C(-32768);
1420 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001421 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001422 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001423 }
1424 break;
1425 case LY_TYPE_INT32: /* range */
1426 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001427 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 +01001428 } else if (max) {
1429 part->max_64 = INT64_C(2147483647);
1430 } else {
1431 part->min_64 = INT64_C(-2147483648);
1432 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001433 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001434 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001435 }
1436 break;
1437 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001438 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001439 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001440 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001441 max ? &part->max_64 : &part->min_64);
1442 } else if (max) {
1443 part->max_64 = INT64_C(9223372036854775807);
1444 } else {
1445 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1446 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001447 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001448 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001449 }
1450 break;
1451 case LY_TYPE_UINT8: /* range */
1452 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001453 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 +01001454 } else if (max) {
1455 part->max_u64 = UINT64_C(255);
1456 } else {
1457 part->min_u64 = UINT64_C(0);
1458 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001459 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001460 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001461 }
1462 break;
1463 case LY_TYPE_UINT16: /* range */
1464 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001465 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 +01001466 } else if (max) {
1467 part->max_u64 = UINT64_C(65535);
1468 } else {
1469 part->min_u64 = UINT64_C(0);
1470 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001471 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001472 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001473 }
1474 break;
1475 case LY_TYPE_UINT32: /* range */
1476 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001477 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 +01001478 } else if (max) {
1479 part->max_u64 = UINT64_C(4294967295);
1480 } else {
1481 part->min_u64 = UINT64_C(0);
1482 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001483 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001484 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001485 }
1486 break;
1487 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001488 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001489 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001490 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001491 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 +01001492 } else if (max) {
1493 part->max_u64 = UINT64_C(18446744073709551615);
1494 } else {
1495 part->min_u64 = UINT64_C(0);
1496 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001497 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001498 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001499 }
1500 break;
1501 default:
1502 LOGINT(ctx->ctx);
1503 ret = LY_EINT;
1504 }
1505
Radek Krejci5969f272018-11-23 10:03:58 +01001506finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001507 if (ret == LY_EDENIED) {
1508 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1509 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1510 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1511 } else if (ret == LY_EVALID) {
1512 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1513 "Invalid %s restriction - invalid value \"%s\".",
1514 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1515 } else if (ret == LY_EEXIST) {
1516 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1517 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001518 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001519 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001520 } else if (!ret && value) {
1521 *value = *value + len;
1522 }
1523 free(valcopy);
1524 return ret;
1525}
1526
Radek Krejcia3045382018-11-22 14:30:31 +01001527/**
1528 * @brief Compile the parsed range restriction.
1529 * @param[in] ctx Compile context.
1530 * @param[in] range_p Parsed range structure to compile.
1531 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1532 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1533 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1534 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1535 * range restriction must be more restrictive than the base_range.
1536 * @param[in,out] range Pointer to the created current range structure.
1537 * @return LY_ERR value.
1538 */
Radek Krejci19a96102018-11-15 13:38:09 +01001539static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001540lys_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 +01001541 struct lysc_range *base_range, struct lysc_range **range)
1542{
1543 LY_ERR ret = LY_EVALID;
1544 const char *expr;
1545 struct lysc_range_part *parts = NULL, *part;
1546 int range_expected = 0, uns;
1547 unsigned int parts_done = 0, u, v;
1548
1549 assert(range);
1550 assert(range_p);
1551
1552 expr = range_p->arg;
1553 while(1) {
1554 if (isspace(*expr)) {
1555 ++expr;
1556 } else if (*expr == '\0') {
1557 if (range_expected) {
1558 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1559 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1560 length_restr ? "length" : "range", range_p->arg);
1561 goto cleanup;
1562 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
1563 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1564 "Invalid %s restriction - unexpected end of the expression (%s).",
1565 length_restr ? "length" : "range", range_p->arg);
1566 goto cleanup;
1567 }
1568 parts_done++;
1569 break;
1570 } else if (!strncmp(expr, "min", 3)) {
1571 if (parts) {
1572 /* min cannot be used elsewhere than in the first part */
1573 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1574 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1575 expr - range_p->arg, range_p->arg);
1576 goto cleanup;
1577 }
1578 expr += 3;
1579
1580 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001581 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 +01001582 part->max_64 = part->min_64;
1583 } else if (*expr == '|') {
1584 if (!parts || range_expected) {
1585 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1586 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1587 goto cleanup;
1588 }
1589 expr++;
1590 parts_done++;
1591 /* process next part of the expression */
1592 } else if (!strncmp(expr, "..", 2)) {
1593 expr += 2;
1594 while (isspace(*expr)) {
1595 expr++;
1596 }
1597 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
1598 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1599 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1600 goto cleanup;
1601 }
1602 /* continue expecting the upper boundary */
1603 range_expected = 1;
1604 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1605 /* number */
1606 if (range_expected) {
1607 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001608 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 +01001609 range_expected = 0;
1610 } else {
1611 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1612 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 +01001613 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001614 part->max_64 = part->min_64;
1615 }
1616
1617 /* continue with possible another expression part */
1618 } else if (!strncmp(expr, "max", 3)) {
1619 expr += 3;
1620 while (isspace(*expr)) {
1621 expr++;
1622 }
1623 if (*expr != '\0') {
1624 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1625 length_restr ? "length" : "range", expr);
1626 goto cleanup;
1627 }
1628 if (range_expected) {
1629 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001630 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 +01001631 range_expected = 0;
1632 } else {
1633 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1634 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 +01001635 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001636 part->min_64 = part->max_64;
1637 }
1638 } else {
1639 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1640 length_restr ? "length" : "range", expr);
1641 goto cleanup;
1642 }
1643 }
1644
1645 /* check with the previous range/length restriction */
1646 if (base_range) {
1647 switch (basetype) {
1648 case LY_TYPE_BINARY:
1649 case LY_TYPE_UINT8:
1650 case LY_TYPE_UINT16:
1651 case LY_TYPE_UINT32:
1652 case LY_TYPE_UINT64:
1653 case LY_TYPE_STRING:
1654 uns = 1;
1655 break;
1656 case LY_TYPE_DEC64:
1657 case LY_TYPE_INT8:
1658 case LY_TYPE_INT16:
1659 case LY_TYPE_INT32:
1660 case LY_TYPE_INT64:
1661 uns = 0;
1662 break;
1663 default:
1664 LOGINT(ctx->ctx);
1665 ret = LY_EINT;
1666 goto cleanup;
1667 }
1668 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1669 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1670 goto baseerror;
1671 }
1672 /* current lower bound is not lower than the base */
1673 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1674 /* base has single value */
1675 if (base_range->parts[v].min_64 == parts[u].min_64) {
1676 /* both lower bounds are the same */
1677 if (parts[u].min_64 != parts[u].max_64) {
1678 /* current continues with a range */
1679 goto baseerror;
1680 } else {
1681 /* equal single values, move both forward */
1682 ++v;
1683 continue;
1684 }
1685 } else {
1686 /* base is single value lower than current range, so the
1687 * value from base range is removed in the current,
1688 * move only base and repeat checking */
1689 ++v;
1690 --u;
1691 continue;
1692 }
1693 } else {
1694 /* base is the range */
1695 if (parts[u].min_64 == parts[u].max_64) {
1696 /* current is a single value */
1697 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1698 /* current is behind the base range, so base range is omitted,
1699 * move the base and keep the current for further check */
1700 ++v;
1701 --u;
1702 } /* else it is within the base range, so move the current, but keep the base */
1703 continue;
1704 } else {
1705 /* both are ranges - check the higher bound, the lower was already checked */
1706 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1707 /* higher bound is higher than the current higher bound */
1708 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1709 /* but the current lower bound is also higher, so the base range is omitted,
1710 * continue with the same current, but move the base */
1711 --u;
1712 ++v;
1713 continue;
1714 }
1715 /* current range starts within the base range but end behind it */
1716 goto baseerror;
1717 } else {
1718 /* current range is smaller than the base,
1719 * move current, but stay with the base */
1720 continue;
1721 }
1722 }
1723 }
1724 }
1725 if (u != parts_done) {
1726baseerror:
1727 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1728 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1729 length_restr ? "length" : "range", range_p->arg);
1730 goto cleanup;
1731 }
1732 }
1733
1734 if (!(*range)) {
1735 *range = calloc(1, sizeof **range);
1736 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1737 }
1738
Radek Krejcic8b31002019-01-08 10:24:45 +01001739 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001740 if (range_p->eapptag) {
1741 lydict_remove(ctx->ctx, (*range)->eapptag);
1742 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1743 }
1744 if (range_p->emsg) {
1745 lydict_remove(ctx->ctx, (*range)->emsg);
1746 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1747 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001748 if (range_p->dsc) {
1749 lydict_remove(ctx->ctx, (*range)->dsc);
1750 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1751 }
1752 if (range_p->ref) {
1753 lydict_remove(ctx->ctx, (*range)->ref);
1754 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1755 }
Radek Krejci19a96102018-11-15 13:38:09 +01001756 /* extensions are taken only from the last range by the caller */
1757
1758 (*range)->parts = parts;
1759 parts = NULL;
1760 ret = LY_SUCCESS;
1761cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001762 LY_ARRAY_FREE(parts);
1763
1764 return ret;
1765}
1766
1767/**
1768 * @brief Checks pattern syntax.
1769 *
Radek Krejcia3045382018-11-22 14:30:31 +01001770 * @param[in] ctx Compile context.
Radek Krejci19a96102018-11-15 13:38:09 +01001771 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001772 * @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 +01001773 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001774 */
1775static LY_ERR
Radek Krejci54579462019-04-30 12:47:06 +02001776lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre2_code **code)
Radek Krejci19a96102018-11-15 13:38:09 +01001777{
Radek Krejci54579462019-04-30 12:47:06 +02001778 int idx, idx2, start, end, count;
Radek Krejci19a96102018-11-15 13:38:09 +01001779 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001780 int err_code;
1781 const char *orig_ptr;
1782 PCRE2_SIZE err_offset;
1783 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001784#define URANGE_LEN 19
1785 char *ublock2urange[][2] = {
1786 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1787 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1788 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1789 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1790 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1791 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1792 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1793 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1794 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1795 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1796 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1797 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1798 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1799 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1800 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1801 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1802 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1803 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1804 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1805 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1806 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1807 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1808 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1809 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1810 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1811 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1812 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1813 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1814 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1815 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1816 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1817 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1818 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1819 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1820 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1821 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1822 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1823 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1824 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1825 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1826 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1827 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1828 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1829 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1830 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1831 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1832 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1833 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1834 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1835 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1836 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1837 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1838 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1839 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1840 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1841 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1842 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1843 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1844 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1845 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1846 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1847 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1848 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1849 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1850 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1851 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1852 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1853 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1854 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1855 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1856 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1857 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1858 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1859 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1860 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1861 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1862 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1863 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1864 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1865 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1866 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1867 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1868 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1869 {NULL, NULL}
1870 };
1871
1872 /* adjust the expression to a Perl equivalent
1873 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1874
1875 /* we need to replace all "$" with "\$", count them now */
1876 for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$"));
1877
1878 perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char));
1879 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM);
1880 perl_regex[0] = '\0';
1881
1882 ptr = perl_regex;
1883
Radek Krejci19a96102018-11-15 13:38:09 +01001884 for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) {
1885 if (orig_ptr[0] == '$') {
1886 ptr += sprintf(ptr, "\\$");
1887 } else if (orig_ptr[0] == '^') {
1888 ptr += sprintf(ptr, "\\^");
1889 } else {
1890 ptr[0] = orig_ptr[0];
1891 ++ptr;
1892 }
1893 }
Radek Krejci5819f7c2019-05-31 14:53:29 +02001894 ptr[0] = '\0';
1895 ++ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01001896
1897 /* substitute Unicode Character Blocks with exact Character Ranges */
1898 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1899 start = ptr - perl_regex;
1900
1901 ptr = strchr(ptr, '}');
1902 if (!ptr) {
1903 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1904 pattern, perl_regex + start + 2, "unterminated character property");
1905 free(perl_regex);
1906 return LY_EVALID;
1907 }
1908 end = (ptr - perl_regex) + 1;
1909
1910 /* need more space */
1911 if (end - start < URANGE_LEN) {
1912 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1913 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM);
1914 }
1915
1916 /* find our range */
1917 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1918 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1919 break;
1920 }
1921 }
1922 if (!ublock2urange[idx][0]) {
1923 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1924 pattern, perl_regex + start + 5, "unknown block name");
1925 free(perl_regex);
1926 return LY_EVALID;
1927 }
1928
1929 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1930 for (idx2 = 0, count = 0; idx2 < start; ++idx2) {
1931 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1932 ++count;
1933 }
1934 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1935 --count;
1936 }
1937 }
1938 if (count) {
1939 /* skip brackets */
1940 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1941 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1942 } else {
1943 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1944 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1945 }
1946 }
1947
1948 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02001949 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
1950 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02001951 &err_code, &err_offset, NULL);
1952 if (!code_local) {
1953 PCRE2_UCHAR err_msg[256] = {0};
1954 pcre2_get_error_message(err_code, err_msg, 256);
Radek Krejci19a96102018-11-15 13:38:09 +01001955 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
1956 free(perl_regex);
1957 return LY_EVALID;
1958 }
1959 free(perl_regex);
1960
Radek Krejci54579462019-04-30 12:47:06 +02001961 if (code) {
1962 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001963 } else {
Radek Krejci54579462019-04-30 12:47:06 +02001964 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01001965 }
1966
1967 return LY_SUCCESS;
1968
1969#undef URANGE_LEN
1970}
1971
Radek Krejcia3045382018-11-22 14:30:31 +01001972/**
1973 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1974 * @param[in] ctx Compile context.
1975 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01001976 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1977 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1978 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1979 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1980 */
Radek Krejci19a96102018-11-15 13:38:09 +01001981static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001982lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci19a96102018-11-15 13:38:09 +01001983 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1984{
1985 struct lysc_pattern **pattern;
Radek Krejci0935f412019-08-20 16:15:18 +02001986 unsigned int u;
Radek Krejci19a96102018-11-15 13:38:09 +01001987 LY_ERR ret = LY_SUCCESS;
1988
1989 /* first, copy the patterns from the base type */
1990 if (base_patterns) {
1991 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1992 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1993 }
1994
1995 LY_ARRAY_FOR(patterns_p, u) {
1996 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1997 *pattern = calloc(1, sizeof **pattern);
1998 ++(*pattern)->refcount;
1999
Radek Krejci54579462019-04-30 12:47:06 +02002000 ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01002001 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01002002
2003 if (patterns_p[u].arg[0] == 0x15) {
2004 (*pattern)->inverted = 1;
2005 }
Radek Krejci54579462019-04-30 12:47:06 +02002006 DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +01002007 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
2008 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01002009 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
2010 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejci0935f412019-08-20 16:15:18 +02002011 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002012 }
2013done:
2014 return ret;
2015}
2016
Radek Krejcia3045382018-11-22 14:30:31 +01002017/**
2018 * @brief map of the possible restrictions combination for the specific built-in type.
2019 */
Radek Krejci19a96102018-11-15 13:38:09 +01002020static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
2021 0 /* LY_TYPE_UNKNOWN */,
2022 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01002023 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
2024 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
2025 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
2026 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
2027 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01002028 LYS_SET_BIT /* LY_TYPE_BITS */,
2029 0 /* LY_TYPE_BOOL */,
2030 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
2031 0 /* LY_TYPE_EMPTY */,
2032 LYS_SET_ENUM /* LY_TYPE_ENUM */,
2033 LYS_SET_BASE /* LY_TYPE_IDENT */,
2034 LYS_SET_REQINST /* LY_TYPE_INST */,
2035 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01002036 LYS_SET_TYPE /* LY_TYPE_UNION */,
2037 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002038 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002039 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01002040 LYS_SET_RANGE /* LY_TYPE_INT64 */
2041};
2042
2043/**
2044 * @brief stringification of the YANG built-in data types
2045 */
2046const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
2047 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
2048 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01002049};
2050
Radek Krejcia3045382018-11-22 14:30:31 +01002051/**
2052 * @brief Compile parsed type's enum structures (for enumeration and bits types).
2053 * @param[in] ctx Compile context.
2054 * @param[in] enums_p Array of the parsed enum structures to compile.
2055 * @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 +01002056 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
2057 * @param[out] enums Newly created array of the compiled enums information for the current type.
2058 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2059 */
Radek Krejci19a96102018-11-15 13:38:09 +01002060static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002061lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci693262f2019-04-29 15:23:20 +02002062 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01002063{
2064 LY_ERR ret = LY_SUCCESS;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02002065 unsigned int u, v, match = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002066 int32_t value = 0;
2067 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02002068 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01002069
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002070 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002071 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
2072 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
2073 return LY_EVALID;
2074 }
2075
2076 LY_ARRAY_FOR(enums_p, u) {
2077 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
2078 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01002079 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
2080 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci693262f2019-04-29 15:23:20 +02002081 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01002082 if (base_enums) {
2083 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
2084 LY_ARRAY_FOR(base_enums, v) {
2085 if (!strcmp(e->name, base_enums[v].name)) {
2086 break;
2087 }
2088 }
2089 if (v == LY_ARRAY_SIZE(base_enums)) {
2090 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2091 "Invalid %s - derived type adds new item \"%s\".",
2092 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
2093 return LY_EVALID;
2094 }
2095 match = v;
2096 }
2097
2098 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02002099 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01002100 if (enums_p[u].flags & LYS_SET_VALUE) {
2101 e->value = (int32_t)enums_p[u].value;
2102 if (!u || e->value >= value) {
2103 value = e->value + 1;
2104 }
2105 /* check collision with other values */
2106 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
2107 if (e->value == (*enums)[v].value) {
2108 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2109 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
2110 e->value, e->name, (*enums)[v].name);
2111 return LY_EVALID;
2112 }
2113 }
2114 } else if (base_enums) {
2115 /* inherit the assigned value */
2116 e->value = base_enums[match].value;
2117 if (!u || e->value >= value) {
2118 value = e->value + 1;
2119 }
2120 } else {
2121 /* assign value automatically */
2122 if (u && value == INT32_MIN) {
2123 /* counter overflow */
2124 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2125 "Invalid enumeration - it is not possible to auto-assign enum value for "
2126 "\"%s\" since the highest value is already 2147483647.", e->name);
2127 return LY_EVALID;
2128 }
2129 e->value = value++;
2130 }
2131 } else { /* LY_TYPE_BITS */
2132 if (enums_p[u].flags & LYS_SET_VALUE) {
2133 e->value = (int32_t)enums_p[u].value;
2134 if (!u || (uint32_t)e->value >= position) {
2135 position = (uint32_t)e->value + 1;
2136 }
2137 /* check collision with other values */
2138 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
2139 if (e->value == (*enums)[v].value) {
2140 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2141 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
2142 (uint32_t)e->value, e->name, (*enums)[v].name);
2143 return LY_EVALID;
2144 }
2145 }
2146 } else if (base_enums) {
2147 /* inherit the assigned value */
2148 e->value = base_enums[match].value;
2149 if (!u || (uint32_t)e->value >= position) {
2150 position = (uint32_t)e->value + 1;
2151 }
2152 } else {
2153 /* assign value automatically */
2154 if (u && position == 0) {
2155 /* counter overflow */
2156 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2157 "Invalid bits - it is not possible to auto-assign bit position for "
2158 "\"%s\" since the highest value is already 4294967295.", e->name);
2159 return LY_EVALID;
2160 }
2161 e->value = position++;
2162 }
2163 }
2164
2165 if (base_enums) {
2166 /* the assigned values must not change from the derived type */
2167 if (e->value != base_enums[match].value) {
2168 if (basetype == LY_TYPE_ENUM) {
2169 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2170 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
2171 e->name, base_enums[match].value, e->value);
2172 } else {
2173 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2174 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
2175 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
2176 }
2177 return LY_EVALID;
2178 }
2179 }
2180
Radek Krejciec4da802019-05-02 13:02:41 +02002181 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002182 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 +01002183
2184 if (basetype == LY_TYPE_BITS) {
2185 /* keep bits ordered by position */
2186 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
2187 if (v != u) {
2188 memcpy(&storage, e, sizeof *e);
2189 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
2190 memcpy(&(*enums)[v], &storage, sizeof storage);
2191 }
2192 }
2193 }
2194
2195done:
2196 return ret;
2197}
2198
Radek Krejcia3045382018-11-22 14:30:31 +01002199#define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \
2200 for ((NODE) = (NODE)->parent; \
2201 (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \
2202 (NODE) = (NODE)->parent); \
2203 if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \
2204 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \
2205 TERM; \
2206 }
2207
2208/**
2209 * @brief Validate the predicate(s) from the leafref path.
2210 * @param[in] ctx Compile context
2211 * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s).
2212 * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated.
Radek Krejci4ce68932018-11-28 12:53:36 +01002213 * @param[in] start_node Path context node (where the path is instantiated).
Radek Krejcia3045382018-11-22 14:30:31 +01002214 * @param[in] context_node Predicate context node (where the predicate is placed).
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002215 * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes.
Radek Krejcia3045382018-11-22 14:30:31 +01002216 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2217 */
2218static LY_ERR
Radek Krejci4ce68932018-11-28 12:53:36 +01002219lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate, const struct lysc_node *start_node,
Radek Krejcibade82a2018-12-05 10:13:48 +01002220 const struct lysc_node_list *context_node, const struct lys_module *path_context)
Radek Krejcia3045382018-11-22 14:30:31 +01002221{
2222 LY_ERR ret = LY_EVALID;
2223 const struct lys_module *mod;
2224 const struct lysc_node *src_node, *dst_node;
2225 const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix;
2226 size_t src_len, src_prefix_len, dst_len, dst_prefix_len;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002227 unsigned int dest_parent_times, c;
Radek Krejcia3045382018-11-22 14:30:31 +01002228 const char *start, *end, *pke_end;
2229 struct ly_set keys = {0};
2230 int i;
2231
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002232 assert(path_context);
2233
Radek Krejcia3045382018-11-22 14:30:31 +01002234 while (**predicate == '[') {
2235 start = (*predicate)++;
2236
2237 while (isspace(**predicate)) {
2238 ++(*predicate);
2239 }
Radek Krejcib4a4a272019-06-10 12:44:52 +02002240 LY_CHECK_GOTO(ly_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup);
Radek Krejcia3045382018-11-22 14:30:31 +01002241 while (isspace(**predicate)) {
2242 ++(*predicate);
2243 }
2244 if (**predicate != '=') {
2245 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002246 "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.",
2247 *predicate - start + 1, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002248 goto cleanup;
2249 }
2250 ++(*predicate);
2251 while (isspace(**predicate)) {
2252 ++(*predicate);
2253 }
2254
2255 if ((end = pke_end = strchr(*predicate, ']')) == NULL) {
2256 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2257 "Invalid leafref path predicate \"%s\" - missing predicate termination.", start);
2258 goto cleanup;
2259 }
2260 --pke_end;
2261 while (isspace(*pke_end)) {
2262 --pke_end;
2263 }
Radek Krejci7adf4ff2018-12-05 08:55:00 +01002264 ++pke_end;
Radek Krejcia3045382018-11-22 14:30:31 +01002265 /* localize path-key-expr */
2266 pke_start = path_key_expr = *predicate;
2267 /* move after the current predicate */
2268 *predicate = end + 1;
2269
2270 /* source (must be leaf or leaf-list) */
2271 if (src_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002272 mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002273 if (!mod) {
2274 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2275 "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002276 *predicate - start, start, src_prefix_len, src_prefix, path_context->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002277 goto cleanup;
2278 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002279 if (!mod->implemented) {
2280 /* make the module implemented */
2281 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2282 }
Radek Krejcia3045382018-11-22 14:30:31 +01002283 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002284 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002285 }
Radek Krejcibade82a2018-12-05 10:13:48 +01002286 src_node = NULL;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002287 if (!(context_node->flags & LYS_KEYLESS)) {
2288 struct lysc_node *key;
2289 for (key = context_node->child; key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY); key = key->next) {
2290 if (!strncmp(src, key->name, src_len) && key->name[src_len] == '\0') {
2291 src_node = key;
Radek Krejcibade82a2018-12-05 10:13:48 +01002292 break;
2293 }
2294 }
2295 }
Radek Krejcia3045382018-11-22 14:30:31 +01002296 if (!src_node) {
2297 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002298 "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002299 *predicate - start, start, src_len, src, mod->name);
Radek Krejcia3045382018-11-22 14:30:31 +01002300 goto cleanup;
2301 }
Radek Krejcia3045382018-11-22 14:30:31 +01002302
2303 /* check that there is only one predicate for the */
Radek Krejcibade82a2018-12-05 10:13:48 +01002304 c = keys.count;
Radek Krejcia3045382018-11-22 14:30:31 +01002305 i = ly_set_add(&keys, (void*)src_node, 0);
2306 LY_CHECK_GOTO(i == -1, cleanup);
Radek Krejcibade82a2018-12-05 10:13:48 +01002307 if (keys.count == c) { /* node was already present in the set */
Radek Krejcia3045382018-11-22 14:30:31 +01002308 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002309 "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".",
Radek Krejcia3045382018-11-22 14:30:31 +01002310 *predicate - start, start, src_node->name);
2311 goto cleanup;
2312 }
2313
2314 /* destination */
2315 dest_parent_times = 0;
Radek Krejci4ce68932018-11-28 12:53:36 +01002316 dst_node = start_node;
Radek Krejcia3045382018-11-22 14:30:31 +01002317
2318 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002319 if (strncmp(path_key_expr, "current", 7)) {
2320error_current_function_invocation:
Radek Krejcia3045382018-11-22 14:30:31 +01002321 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2322 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
2323 *predicate - start, start);
2324 goto cleanup;
2325 }
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002326 for (path_key_expr += 7; isspace(*path_key_expr); ++path_key_expr);
2327 if (*path_key_expr != '(') {
2328 goto error_current_function_invocation;
Radek Krejcia3045382018-11-22 14:30:31 +01002329 }
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002330 for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr);
2331 if (*path_key_expr != ')') {
2332 goto error_current_function_invocation;
2333 }
2334 for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr);
Radek Krejcia3045382018-11-22 14:30:31 +01002335
2336 if (*path_key_expr != '/') {
2337 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2338 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
2339 *predicate - start, start);
2340 goto cleanup;
2341 }
2342 ++path_key_expr;
2343 while (isspace(*path_key_expr)) {
2344 ++path_key_expr;
2345 }
2346
2347 /* rel-path-keyexpr:
2348 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
2349 while (!strncmp(path_key_expr, "..", 2)) {
2350 ++dest_parent_times;
2351 path_key_expr += 2;
2352 while (isspace(*path_key_expr)) {
2353 ++path_key_expr;
2354 }
2355 if (*path_key_expr != '/') {
2356 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2357 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
2358 *predicate - start, start);
2359 goto cleanup;
2360 }
2361 ++path_key_expr;
2362 while (isspace(*path_key_expr)) {
2363 ++path_key_expr;
2364 }
2365
2366 /* path is supposed to be evaluated in data tree, so we have to skip
2367 * all schema nodes that cannot be instantiated in data tree */
2368 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
2369 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
2370 *predicate - start, start);
2371 }
2372 if (!dest_parent_times) {
2373 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2374 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
2375 *predicate - start, start);
2376 goto cleanup;
2377 }
2378 if (path_key_expr == pke_end) {
2379 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2380 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
2381 *predicate - start, start);
2382 goto cleanup;
2383 }
2384
2385 while(path_key_expr != pke_end) {
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002386 for (;*path_key_expr == '/' || isspace(*path_key_expr); ++path_key_expr);
Radek Krejcib4a4a272019-06-10 12:44:52 +02002387 if (ly_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +01002388 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcibade82a2018-12-05 10:13:48 +01002389 "Invalid node identifier in leafref path predicate - character %d (of %.*s).",
2390 path_key_expr - start + 1, *predicate - start, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002391 goto cleanup;
2392 }
2393
2394 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002395 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002396 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002397 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002398 }
2399 if (!mod) {
2400 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002401 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002402 *predicate - start, start, dst_len, dst);
2403 goto cleanup;
2404 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002405 if (!mod->implemented) {
2406 /* make the module implemented */
2407 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2408 }
Radek Krejcia3045382018-11-22 14:30:31 +01002409
2410 dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
2411 if (!dst_node) {
2412 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002413 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002414 *predicate - start, start, path_key_expr - pke_start, pke_start);
2415 goto cleanup;
2416 }
2417 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002418 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 +01002419 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002420 "Invalid leafref path predicate \"%.*s\" - rel-path-keyexpr \"%.*s\" refers %s instead of leaf.",
Radek Krejcia3045382018-11-22 14:30:31 +01002421 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
2422 goto cleanup;
2423 }
2424 }
2425
2426 ret = LY_SUCCESS;
2427cleanup:
2428 ly_set_erase(&keys, NULL);
2429 return ret;
2430}
2431
2432/**
2433 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2434 *
2435 * path-arg = absolute-path / relative-path
2436 * absolute-path = 1*("/" (node-identifier *path-predicate))
2437 * relative-path = 1*(".." "/") descendant-path
2438 *
2439 * @param[in,out] path Path to parse.
2440 * @param[out] prefix Prefix of the token, NULL if there is not any.
2441 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2442 * @param[out] name Name of the token.
2443 * @param[out] nam_len Length of the name.
2444 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2445 * must not be changed between consecutive calls. -1 if the
2446 * path is absolute.
2447 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2448 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2449 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002450LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002451lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2452 int *parent_times, int *has_predicate)
2453{
2454 int par_times = 0;
2455
2456 assert(path && *path);
2457 assert(parent_times);
2458 assert(prefix);
2459 assert(prefix_len);
2460 assert(name);
2461 assert(name_len);
2462 assert(has_predicate);
2463
2464 *prefix = NULL;
2465 *prefix_len = 0;
2466 *name = NULL;
2467 *name_len = 0;
2468 *has_predicate = 0;
2469
2470 if (!*parent_times) {
2471 if (!strncmp(*path, "..", 2)) {
2472 *path += 2;
2473 ++par_times;
2474 while (!strncmp(*path, "/..", 3)) {
2475 *path += 3;
2476 ++par_times;
2477 }
2478 }
2479 if (par_times) {
2480 *parent_times = par_times;
2481 } else {
2482 *parent_times = -1;
2483 }
2484 }
2485
2486 if (**path != '/') {
2487 return LY_EINVAL;
2488 }
2489 /* skip '/' */
2490 ++(*path);
2491
2492 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002493 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002494
2495 if ((**path == '/' && (*path)[1]) || !**path) {
2496 /* path continues by another token or this is the last token */
2497 return LY_SUCCESS;
2498 } else if ((*path)[0] != '[') {
2499 /* unexpected character */
2500 return LY_EINVAL;
2501 } else {
2502 /* predicate starting with [ */
2503 *has_predicate = 1;
2504 return LY_SUCCESS;
2505 }
2506}
2507
2508/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002509 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2510 *
2511 * The set of features used for target must be a subset of features used for the leafref.
2512 * This is not a perfect, we should compare the truth tables but it could require too much resources
2513 * and RFC 7950 does not require it explicitely, so we simplify that.
2514 *
2515 * @param[in] refnode The leafref node.
2516 * @param[in] target Tha target node of the leafref.
2517 * @return LY_SUCCESS or LY_EVALID;
2518 */
2519static LY_ERR
2520lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2521{
2522 LY_ERR ret = LY_EVALID;
2523 const struct lysc_node *iter;
Radek Krejci58d171e2018-11-23 13:50:55 +01002524 unsigned int u, v, count;
2525 struct ly_set features = {0};
2526
2527 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002528 if (iter->iffeatures) {
2529 LY_ARRAY_FOR(iter->iffeatures, u) {
2530 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2531 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002532 }
2533 }
2534 }
2535 }
2536
2537 /* we should have, in features set, a superset of features applicable to the target node.
2538 * So when adding features applicable to the target into the features set, we should not be
2539 * able to actually add any new feature, otherwise it is not a subset of features applicable
2540 * to the leafref itself. */
2541 count = features.count;
2542 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002543 if (iter->iffeatures) {
2544 LY_ARRAY_FOR(iter->iffeatures, u) {
2545 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2546 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002547 /* new feature was added (or LY_EMEM) */
2548 goto cleanup;
2549 }
2550 }
2551 }
2552 }
2553 }
2554 ret = LY_SUCCESS;
2555
2556cleanup:
2557 ly_set_erase(&features, NULL);
2558 return ret;
2559}
2560
2561/**
Radek Krejcia3045382018-11-22 14:30:31 +01002562 * @brief Validate the leafref path.
2563 * @param[in] ctx Compile context
2564 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01002565 * @param[in] leafref Leafref to validate.
Radek Krejcia3045382018-11-22 14:30:31 +01002566 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2567 */
2568static LY_ERR
Radek Krejci412ddfa2018-11-23 11:44:11 +01002569lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref)
Radek Krejcia3045382018-11-22 14:30:31 +01002570{
2571 const struct lysc_node *node = NULL, *parent = NULL;
2572 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002573 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01002574 const char *id, *prefix, *name;
2575 size_t prefix_len, name_len;
2576 int parent_times = 0, has_predicate;
2577 unsigned int iter, u;
2578 LY_ERR ret = LY_SUCCESS;
2579
2580 assert(ctx);
2581 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01002582 assert(leafref);
2583
Radek Krejci1c0c3442019-07-23 16:08:47 +02002584 ctx->path[0] = '\0';
2585 lysc_path(startnode, LY_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
2586 ctx->path_len = strlen(ctx->path);
Radek Krejci327de162019-06-14 12:52:07 +02002587
Radek Krejcia3045382018-11-22 14:30:31 +01002588 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002589 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01002590 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
2591 if (!iter) { /* first iteration */
2592 /* precess ".." in relative paths */
2593 if (parent_times > 0) {
2594 /* move from the context node */
2595 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
2596 /* path is supposed to be evaluated in data tree, so we have to skip
2597 * all schema nodes that cannot be instantiated in data tree */
2598 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002599 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002600 }
2601 }
2602 }
2603
2604 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01002605 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002606 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002607 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002608 }
2609 if (!mod) {
2610 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002611 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
2612 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002613 return LY_EVALID;
2614 }
Radek Krejci3d929562019-02-21 11:25:55 +01002615 if (!mod->implemented) {
2616 /* make the module implemented */
2617 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2618 }
Radek Krejcia3045382018-11-22 14:30:31 +01002619
2620 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
2621 if (!node) {
2622 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002623 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002624 return LY_EVALID;
2625 }
2626 parent = node;
2627
2628 if (has_predicate) {
2629 /* we have predicate, so the current result must be list */
2630 if (node->nodetype != LYS_LIST) {
2631 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2632 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002633 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002634 return LY_EVALID;
2635 }
2636
Radek Krejcibade82a2018-12-05 10:13:48 +01002637 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context),
2638 LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01002639 }
2640
2641 ++iter;
2642 }
2643 if (ret) {
2644 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002645 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002646 return LY_EVALID;
2647 }
2648
2649 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2650 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2651 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002652 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002653 return LY_EVALID;
2654 }
2655
2656 /* check status */
2657 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2658 return LY_EVALID;
2659 }
2660
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002661 /* check config */
2662 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2663 if (node->flags & LYS_CONFIG_R) {
2664 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2665 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2666 leafref->path);
2667 return LY_EVALID;
2668 }
2669 }
2670
Radek Krejci412ddfa2018-11-23 11:44:11 +01002671 /* store the target's type and check for circular chain of leafrefs */
2672 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2673 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2674 if (type == (struct lysc_type*)leafref) {
2675 /* circular chain detected */
2676 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2677 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2678 return LY_EVALID;
2679 }
2680 }
2681
Radek Krejci58d171e2018-11-23 13:50:55 +01002682 /* check if leafref and its target are under common if-features */
2683 if (lys_compile_leafref_features_validate(startnode, node)) {
2684 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2685 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2686 leafref->path);
2687 return LY_EVALID;
2688 }
2689
Radek Krejci327de162019-06-14 12:52:07 +02002690 ctx->path_len = 1;
2691 ctx->path[1] = '\0';
Radek Krejcia3045382018-11-22 14:30:31 +01002692 return LY_SUCCESS;
2693}
2694
Radek Krejcicdfecd92018-11-26 11:27:32 +01002695static 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 +02002696 struct lysp_type *type_p, struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002697/**
2698 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2699 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002700 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2701 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2702 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2703 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002704 * @param[in] type_p Parsed type to compile.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002705 * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path)
Radek Krejcia3045382018-11-22 14:30:31 +01002706 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002707 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2708 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2709 * @param[out] type Newly created type structure with the filled information about the type.
2710 * @return LY_ERR value.
2711 */
Radek Krejci19a96102018-11-15 13:38:09 +01002712static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002713lys_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 +02002714 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002715 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002716{
2717 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002718 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002719 struct lysc_type_bin *bin;
2720 struct lysc_type_num *num;
2721 struct lysc_type_str *str;
2722 struct lysc_type_bits *bits;
2723 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002724 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002725 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002726 struct lysc_type_union *un, *un_aux;
2727 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002728
2729 switch (basetype) {
2730 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002731 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002732
2733 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002734 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002735 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2736 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002737 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002738 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 +01002739 }
2740 }
2741
2742 if (tpdfname) {
2743 type_p->compiled = *type;
2744 *type = calloc(1, sizeof(struct lysc_type_bin));
2745 }
2746 break;
2747 case LY_TYPE_BITS:
2748 /* RFC 7950 9.7 - bits */
2749 bits = (struct lysc_type_bits*)(*type);
2750 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002751 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002752 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2753 (struct lysc_type_bitenum_item**)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002754 }
2755
Radek Krejci555cb5b2018-11-16 14:54:33 +01002756 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002757 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002758 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002759 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002760 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002761 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002762 free(*type);
2763 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002764 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002765 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002766 }
2767
2768 if (tpdfname) {
2769 type_p->compiled = *type;
2770 *type = calloc(1, sizeof(struct lysc_type_bits));
2771 }
2772 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002773 case LY_TYPE_DEC64:
2774 dec = (struct lysc_type_dec*)(*type);
2775
2776 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002777 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002778 if (!type_p->fraction_digits) {
2779 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002780 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002781 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002782 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002783 free(*type);
2784 *type = NULL;
2785 }
2786 return LY_EVALID;
2787 }
2788 } else if (type_p->fraction_digits) {
2789 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002790 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002791 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2792 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002793 tpdfname);
2794 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002795 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2796 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002797 free(*type);
2798 *type = NULL;
2799 }
2800 return LY_EVALID;
2801 }
2802 dec->fraction_digits = type_p->fraction_digits;
2803
2804 /* RFC 7950 9.2.4 - range */
2805 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002806 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2807 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002808 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002809 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 +01002810 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002811 }
2812
2813 if (tpdfname) {
2814 type_p->compiled = *type;
2815 *type = calloc(1, sizeof(struct lysc_type_dec));
2816 }
2817 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002818 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002819 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002820
2821 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002822 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002823 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2824 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002825 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002826 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 +01002827 }
2828 } else if (base && ((struct lysc_type_str*)base)->length) {
2829 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2830 }
2831
2832 /* RFC 7950 9.4.5 - pattern */
2833 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002834 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002835 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002836 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2837 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2838 }
2839
2840 if (tpdfname) {
2841 type_p->compiled = *type;
2842 *type = calloc(1, sizeof(struct lysc_type_str));
2843 }
2844 break;
2845 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002846 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002847
2848 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002849 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002850 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002851 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002852 }
2853
Radek Krejci555cb5b2018-11-16 14:54:33 +01002854 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002855 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002856 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002857 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002858 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002859 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002860 free(*type);
2861 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002862 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002863 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002864 }
2865
2866 if (tpdfname) {
2867 type_p->compiled = *type;
2868 *type = calloc(1, sizeof(struct lysc_type_enum));
2869 }
2870 break;
2871 case LY_TYPE_INT8:
2872 case LY_TYPE_UINT8:
2873 case LY_TYPE_INT16:
2874 case LY_TYPE_UINT16:
2875 case LY_TYPE_INT32:
2876 case LY_TYPE_UINT32:
2877 case LY_TYPE_INT64:
2878 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002879 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002880
2881 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002882 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002883 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
2884 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002885 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002886 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 +01002887 }
2888 }
2889
2890 if (tpdfname) {
2891 type_p->compiled = *type;
2892 *type = calloc(1, sizeof(struct lysc_type_num));
2893 }
2894 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002895 case LY_TYPE_IDENT:
2896 idref = (struct lysc_type_identityref*)(*type);
2897
2898 /* RFC 7950 9.10.2 - base */
2899 if (type_p->bases) {
2900 if (base) {
2901 /* only the directly derived identityrefs can contain base specification */
2902 if (tpdfname) {
2903 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002904 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002905 tpdfname);
2906 } else {
2907 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002908 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002909 free(*type);
2910 *type = NULL;
2911 }
2912 return LY_EVALID;
2913 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002914 LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002915 }
2916
2917 if (!base && !type_p->flags) {
2918 /* type derived from identityref built-in type must contain at least one base */
2919 if (tpdfname) {
2920 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2921 } else {
2922 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2923 free(*type);
2924 *type = NULL;
2925 }
2926 return LY_EVALID;
2927 }
2928
2929 if (tpdfname) {
2930 type_p->compiled = *type;
2931 *type = calloc(1, sizeof(struct lysc_type_identityref));
2932 }
2933 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002934 case LY_TYPE_LEAFREF:
2935 /* RFC 7950 9.9.3 - require-instance */
2936 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002937 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002938 if (tpdfname) {
2939 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2940 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2941 } else {
2942 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2943 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2944 free(*type);
2945 *type = NULL;
2946 }
2947 return LY_EVALID;
2948 }
Radek Krejcia3045382018-11-22 14:30:31 +01002949 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002950 } else if (base) {
2951 /* inherit */
2952 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002953 } else {
2954 /* default is true */
2955 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
2956 }
2957 if (type_p->path) {
2958 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002959 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002960 } else if (base) {
2961 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002962 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002963 } else if (tpdfname) {
2964 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2965 return LY_EVALID;
2966 } else {
2967 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2968 free(*type);
2969 *type = NULL;
2970 return LY_EVALID;
2971 }
2972 if (tpdfname) {
2973 type_p->compiled = *type;
2974 *type = calloc(1, sizeof(struct lysc_type_leafref));
2975 }
2976 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002977 case LY_TYPE_INST:
2978 /* RFC 7950 9.9.3 - require-instance */
2979 if (type_p->flags & LYS_SET_REQINST) {
2980 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2981 } else {
2982 /* default is true */
2983 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2984 }
2985
2986 if (tpdfname) {
2987 type_p->compiled = *type;
2988 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2989 }
2990 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002991 case LY_TYPE_UNION:
2992 un = (struct lysc_type_union*)(*type);
2993
2994 /* RFC 7950 7.4 - type */
2995 if (type_p->types) {
2996 if (base) {
2997 /* only the directly derived union can contain types specification */
2998 if (tpdfname) {
2999 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
3000 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
3001 tpdfname);
3002 } else {
3003 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
3004 "Invalid type substatement for the type not directly derived from union built-in type.");
3005 free(*type);
3006 *type = NULL;
3007 }
3008 return LY_EVALID;
3009 }
3010 /* compile the type */
3011 additional = 0;
3012 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
3013 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Radek Krejciec4da802019-05-02 13:02:41 +02003014 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 +01003015 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
3016 /* add space for additional types from the union subtype */
3017 un_aux = (struct lysc_type_union *)un->types[u + additional];
3018 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)));
3019 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
3020 un->types = (void*)((uint32_t*)(p) + 1);
3021
3022 /* copy subtypes of the subtype union */
3023 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
3024 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
3025 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
3026 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
3027 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
3028 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
3029 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
3030 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
3031 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
3032 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
3033 /* TODO extensions */
3034
3035 } else {
3036 un->types[u + additional] = un_aux->types[v];
3037 ++un_aux->types[v]->refcount;
3038 }
3039 ++additional;
3040 LY_ARRAY_INCREMENT(un->types);
3041 }
3042 /* compensate u increment in main loop */
3043 --additional;
3044
3045 /* free the replaced union subtype */
3046 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
3047 } else {
3048 LY_ARRAY_INCREMENT(un->types);
3049 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01003050 }
3051 }
3052
3053 if (!base && !type_p->flags) {
3054 /* type derived from union built-in type must contain at least one type */
3055 if (tpdfname) {
3056 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
3057 } else {
3058 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
3059 free(*type);
3060 *type = NULL;
3061 }
3062 return LY_EVALID;
3063 }
3064
3065 if (tpdfname) {
3066 type_p->compiled = *type;
3067 *type = calloc(1, sizeof(struct lysc_type_union));
3068 }
3069 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01003070 case LY_TYPE_BOOL:
3071 case LY_TYPE_EMPTY:
3072 case LY_TYPE_UNKNOWN: /* just to complete switch */
3073 break;
3074 }
3075 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
3076done:
3077 return ret;
3078}
3079
Radek Krejcia3045382018-11-22 14:30:31 +01003080/**
3081 * @brief Compile information about the leaf/leaf-list's type.
3082 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01003083 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
3084 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
3085 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
3086 * @param[in] context_name Name of the context node or referencing typedef for logging.
3087 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01003088 * @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 +01003089 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01003090 * @return LY_ERR value.
3091 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01003092static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01003093lys_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 +02003094 struct lysp_type *type_p, struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01003095{
3096 LY_ERR ret = LY_SUCCESS;
3097 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003098 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01003099 struct type_context {
3100 const struct lysp_tpdf *tpdf;
3101 struct lysp_node *node;
3102 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003103 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01003104 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01003105 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003106 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01003107 const char *dflt = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02003108 struct lys_module *dflt_mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01003109
3110 (*type) = NULL;
3111
3112 tctx = calloc(1, sizeof *tctx);
3113 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01003114 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01003115 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
3116 ret == LY_SUCCESS;
3117 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
3118 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
3119 if (basetype) {
3120 break;
3121 }
3122
3123 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003124 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
3125 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01003126 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
3127
Radek Krejcicdfecd92018-11-26 11:27:32 +01003128 if (units && !*units) {
3129 /* inherit units */
3130 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
3131 }
Radek Krejci01342af2019-01-03 15:18:08 +01003132 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003133 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01003134 dflt = tctx->tpdf->dflt;
Radek Krejcia1911222019-07-22 17:24:50 +02003135 dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003136 }
Radek Krejci01342af2019-01-03 15:18:08 +01003137 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003138 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
3139 break;
3140 }
3141
Radek Krejci19a96102018-11-15 13:38:09 +01003142 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003143 /* it is not necessary to continue, the rest of the chain was already compiled,
3144 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01003145 basetype = tctx->tpdf->type.compiled->basetype;
3146 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01003147 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003148 dummyloops = 1;
3149 goto preparenext;
3150 } else {
3151 tctx = NULL;
3152 break;
3153 }
Radek Krejci19a96102018-11-15 13:38:09 +01003154 }
3155
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003156 /* circular typedef reference detection */
3157 for (u = 0; u < tpdf_chain.count; u++) {
3158 /* local part */
3159 tctx_iter = (struct type_context*)tpdf_chain.objs[u];
3160 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3161 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3162 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3163 free(tctx);
3164 ret = LY_EVALID;
3165 goto cleanup;
3166 }
3167 }
3168 for (u = 0; u < ctx->tpdf_chain.count; u++) {
3169 /* global part for unions corner case */
3170 tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u];
3171 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3172 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3173 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3174 free(tctx);
3175 ret = LY_EVALID;
3176 goto cleanup;
3177 }
3178 }
3179
Radek Krejci19a96102018-11-15 13:38:09 +01003180 /* store information for the following processing */
3181 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3182
Radek Krejcicdfecd92018-11-26 11:27:32 +01003183preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01003184 /* prepare next loop */
3185 tctx_prev = tctx;
3186 tctx = calloc(1, sizeof *tctx);
3187 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
3188 }
3189 free(tctx);
3190
3191 /* allocate type according to the basetype */
3192 switch (basetype) {
3193 case LY_TYPE_BINARY:
3194 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01003195 break;
3196 case LY_TYPE_BITS:
3197 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01003198 break;
3199 case LY_TYPE_BOOL:
3200 case LY_TYPE_EMPTY:
3201 *type = calloc(1, sizeof(struct lysc_type));
3202 break;
3203 case LY_TYPE_DEC64:
3204 *type = calloc(1, sizeof(struct lysc_type_dec));
3205 break;
3206 case LY_TYPE_ENUM:
3207 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01003208 break;
3209 case LY_TYPE_IDENT:
3210 *type = calloc(1, sizeof(struct lysc_type_identityref));
3211 break;
3212 case LY_TYPE_INST:
3213 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3214 break;
3215 case LY_TYPE_LEAFREF:
3216 *type = calloc(1, sizeof(struct lysc_type_leafref));
3217 break;
3218 case LY_TYPE_STRING:
3219 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01003220 break;
3221 case LY_TYPE_UNION:
3222 *type = calloc(1, sizeof(struct lysc_type_union));
3223 break;
3224 case LY_TYPE_INT8:
3225 case LY_TYPE_UINT8:
3226 case LY_TYPE_INT16:
3227 case LY_TYPE_UINT16:
3228 case LY_TYPE_INT32:
3229 case LY_TYPE_UINT32:
3230 case LY_TYPE_INT64:
3231 case LY_TYPE_UINT64:
3232 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003233 break;
3234 case LY_TYPE_UNKNOWN:
3235 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3236 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3237 ret = LY_EVALID;
3238 goto cleanup;
3239 }
3240 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003241 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003242 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3243 ly_data_type2str[basetype]);
3244 free(*type);
3245 (*type) = NULL;
3246 ret = LY_EVALID;
3247 goto cleanup;
3248 }
3249
3250 /* get restrictions from the referred typedefs */
3251 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
3252 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003253
3254 /* remember the typedef context for circular check */
3255 ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3256
Radek Krejci43699232018-11-23 14:59:46 +01003257 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003258 base = tctx->tpdf->type.compiled;
3259 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003260 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003261 /* no change, just use the type information from the base */
3262 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
3263 ++base->refcount;
3264 continue;
3265 }
3266
3267 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003268 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3269 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3270 tctx->tpdf->name, ly_data_type2str[basetype]);
3271 ret = LY_EVALID;
3272 goto cleanup;
3273 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3274 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3275 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3276 tctx->tpdf->name, tctx->tpdf->dflt);
3277 ret = LY_EVALID;
3278 goto cleanup;
3279 }
3280
Radek Krejci19a96102018-11-15 13:38:09 +01003281 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003282 /* TODO user type plugins */
3283 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003284 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003285 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 +01003286 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003287 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003288 LY_CHECK_GOTO(ret, cleanup);
3289 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003290 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003291 /* remove the processed typedef contexts from the stack for circular check */
3292 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003293
Radek Krejcic5c27e52018-11-15 14:38:11 +01003294 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003295 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003296 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003297 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003298 /* TODO user type plugins */
3299 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003300 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003301 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 +01003302 LY_CHECK_GOTO(ret, cleanup);
3303 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01003304 /* no specific restriction in leaf's type definition, copy from the base */
3305 free(*type);
3306 (*type) = base;
3307 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003308 }
Radek Krejcia1911222019-07-22 17:24:50 +02003309 if (dflt && !(*type)->dflt) {
3310 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003311 (*type)->dflt_mod = dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003312 (*type)->dflt = calloc(1, sizeof *(*type)->dflt);
3313 (*type)->dflt->realtype = (*type);
3314 ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt),
3315 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003316 lys_resolve_prefix, (void*)(*type)->dflt_mod, LYD_XML, NULL, NULL, (*type)->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003317 if (err) {
3318 ly_err_print(err);
3319 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3320 "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg);
3321 ly_err_free(err);
3322 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003323 if (ret == LY_EINCOMPLETE) {
3324 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003325 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, NULL, (*type)->dflt, (*type)->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003326
3327 /* but in general result is so far ok */
3328 ret = LY_SUCCESS;
3329 }
Radek Krejcia1911222019-07-22 17:24:50 +02003330 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci01342af2019-01-03 15:18:08 +01003331 }
Radek Krejci19a96102018-11-15 13:38:09 +01003332
Radek Krejci0935f412019-08-20 16:15:18 +02003333 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003334
3335cleanup:
3336 ly_set_erase(&tpdf_chain, free);
3337 return ret;
3338}
3339
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003340/**
3341 * @brief Compile status information of the given node.
3342 *
3343 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3344 * has the status correctly set during the compilation.
3345 *
3346 * @param[in] ctx Compile context
3347 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3348 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3349 * the compatibility with the parent's status value.
3350 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3351 * @return LY_ERR value.
3352 */
3353static LY_ERR
3354lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3355{
3356 /* status - it is not inherited by specification, but it does not make sense to have
3357 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3358 if (!((*node_flags) & LYS_STATUS_MASK)) {
3359 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3360 if ((parent_flags & 0x3) != 0x3) {
3361 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3362 * combination of bits (0x3) which marks the uses_status value */
3363 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3364 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3365 }
3366 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3367 } else {
3368 (*node_flags) |= LYS_STATUS_CURR;
3369 }
3370 } else if (parent_flags & LYS_STATUS_MASK) {
3371 /* check status compatibility with the parent */
3372 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3373 if ((*node_flags) & LYS_STATUS_CURR) {
3374 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3375 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3376 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3377 } else { /* LYS_STATUS_DEPRC */
3378 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3379 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3380 }
3381 return LY_EVALID;
3382 }
3383 }
3384 return LY_SUCCESS;
3385}
3386
Radek Krejci8cce8532019-03-05 11:27:45 +01003387/**
3388 * @brief Check uniqness of the node/action/notification name.
3389 *
3390 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3391 * structures, but they share the namespace so we need to check their name collisions.
3392 *
3393 * @param[in] ctx Compile context.
3394 * @param[in] children List (linked list) of data nodes to go through.
3395 * @param[in] actions List (sized array) of actions or RPCs to go through.
3396 * @param[in] notifs List (sized array) of Notifications to go through.
3397 * @param[in] name Name of the item to find in the given lists.
3398 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3399 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3400 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3401 */
3402static LY_ERR
3403lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children,
3404 const struct lysc_action *actions, const struct lysc_notif *notifs,
3405 const char *name, void *exclude)
3406{
3407 const struct lysc_node *iter;
3408 unsigned int u;
3409
3410 LY_LIST_FOR(children, iter) {
3411 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3412 goto error;
3413 }
3414 }
3415 LY_ARRAY_FOR(actions, u) {
3416 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3417 goto error;
3418 }
3419 }
3420 LY_ARRAY_FOR(notifs, u) {
3421 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3422 goto error;
3423 }
3424 }
3425 return LY_SUCCESS;
3426error:
3427 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification");
3428 return LY_EEXIST;
3429}
3430
Radek Krejciec4da802019-05-02 13:02:41 +02003431static 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 +01003432
Radek Krejcia3045382018-11-22 14:30:31 +01003433/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003434 * @brief Compile parsed RPC/action schema node information.
3435 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003436 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003437 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003438 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3439 * @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).
3440 * Zero means no uses, non-zero value with no status bit set mean the default status.
3441 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3442 */
3443static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003444lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003445 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3446{
3447 LY_ERR ret = LY_SUCCESS;
3448 struct lysp_node *child_p;
3449 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003450 int opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003451
Radek Krejci327de162019-06-14 12:52:07 +02003452 lysc_update_path(ctx, parent, action_p->name);
3453
Radek Krejci8cce8532019-03-05 11:27:45 +01003454 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3455 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3456 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3457 action_p->name, action)) {
3458 return LY_EVALID;
3459 }
3460
Radek Krejciec4da802019-05-02 13:02:41 +02003461 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003462 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003463 "Action \"%s\" is placed inside %s.", action_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003464 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003465 return LY_EVALID;
3466 }
3467
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003468 action->nodetype = LYS_ACTION;
3469 action->module = ctx->mod;
3470 action->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003471 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003472 action->sp = action_p;
3473 }
3474 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3475
3476 /* status - it is not inherited by specification, but it does not make sense to have
3477 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3478 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3479
3480 DUP_STRING(ctx->ctx, action_p->name, action->name);
3481 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3482 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003483 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003484 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003485
3486 /* input */
Radek Krejci327de162019-06-14 12:52:07 +02003487 lysc_update_path(ctx, (struct lysc_node*)action, "input");
Radek Krejciec4da802019-05-02 13:02:41 +02003488 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003489 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 +02003490 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003491 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003492 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003493 }
Radek Krejci327de162019-06-14 12:52:07 +02003494 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003495 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003496
3497 /* output */
Radek Krejci327de162019-06-14 12:52:07 +02003498 lysc_update_path(ctx, (struct lysc_node*)action, "output");
Radek Krejciec4da802019-05-02 13:02:41 +02003499 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003500 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 +02003501 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003502 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003503 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003504 }
Radek Krejci327de162019-06-14 12:52:07 +02003505 lysc_update_path(ctx, NULL, NULL);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003506
Radek Krejci327de162019-06-14 12:52:07 +02003507 lysc_update_path(ctx, NULL, NULL);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003508cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003509 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003510 return ret;
3511}
3512
3513/**
Radek Krejci43981a32019-04-12 09:44:11 +02003514 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003515 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003516 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003517 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3518 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3519 * @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 +02003520 * Zero means no uses, non-zero value with no status bit set mean the default status.
3521 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3522 */
3523static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003524lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003525 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3526{
3527 LY_ERR ret = LY_SUCCESS;
3528 struct lysp_node *child_p;
3529 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003530 int opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003531
Radek Krejci327de162019-06-14 12:52:07 +02003532 lysc_update_path(ctx, parent, notif_p->name);
3533
Radek Krejcifc11bd72019-04-11 16:00:05 +02003534 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3535 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3536 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3537 notif_p->name, notif)) {
3538 return LY_EVALID;
3539 }
3540
Radek Krejciec4da802019-05-02 13:02:41 +02003541 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003542 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3543 "Notification \"%s\" is placed inside %s.", notif_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003544 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003545 return LY_EVALID;
3546 }
3547
3548 notif->nodetype = LYS_NOTIF;
3549 notif->module = ctx->mod;
3550 notif->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003551 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003552 notif->sp = notif_p;
3553 }
3554 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3555
3556 /* status - it is not inherited by specification, but it does not make sense to have
3557 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3558 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3559
3560 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3561 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3562 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003563 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejciec4da802019-05-02 13:02:41 +02003564 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003565 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003566
Radek Krejciec4da802019-05-02 13:02:41 +02003567 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003568 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003569 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003570 }
3571
Radek Krejci327de162019-06-14 12:52:07 +02003572 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003573cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003574 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003575 return ret;
3576}
3577
3578/**
Radek Krejcia3045382018-11-22 14:30:31 +01003579 * @brief Compile parsed container node information.
3580 * @param[in] ctx Compile context
3581 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003582 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3583 * is enriched with the container-specific information.
3584 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3585 */
Radek Krejci19a96102018-11-15 13:38:09 +01003586static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003587lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003588{
3589 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3590 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3591 struct lysp_node *child_p;
3592 unsigned int u;
3593 LY_ERR ret = LY_SUCCESS;
3594
Radek Krejcife909632019-02-12 15:34:42 +01003595 if (cont_p->presence) {
3596 cont->flags |= LYS_PRESENCE;
3597 }
3598
Radek Krejci19a96102018-11-15 13:38:09 +01003599 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003600 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003601 }
3602
Radek Krejciec4da802019-05-02 13:02:41 +02003603 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
3604 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3605 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003606
3607done:
3608 return ret;
3609}
3610
Radek Krejci33f72892019-02-21 10:36:58 +01003611/*
3612 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3613 * @param[in] ctx Compile context.
3614 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3615 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003616 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3617 * @return LY_ERR value.
3618 */
3619static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003620lys_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 +01003621{
Radek Krejcia1911222019-07-22 17:24:50 +02003622 unsigned int u;
Radek Krejci33f72892019-02-21 10:36:58 +01003623 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3624
Radek Krejciec4da802019-05-02 13:02:41 +02003625 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 +01003626 leaf->units ? NULL : &leaf->units));
3627 if (leaf->nodetype == LYS_LEAFLIST) {
3628 if (llist->type->dflt && !llist->dflts && !llist->min) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003629 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM);
3630 llist->dflts_mods[0] = llist->type->dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003631 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
Radek Krejcia1911222019-07-22 17:24:50 +02003632 llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]);
3633 llist->dflts[0]->realtype = llist->type->dflt->realtype;
3634 llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3635 llist->dflts[0]->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003636 LY_ARRAY_INCREMENT(llist->dflts);
3637 }
3638 } else {
3639 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003640 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003641 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3642 leaf->dflt->realtype = leaf->type->dflt->realtype;
3643 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
3644 leaf->dflt->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003645 }
3646 }
3647 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3648 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3649 ly_set_add(&ctx->unres, leaf, 0);
3650 } else if (leaf->type->basetype == LY_TYPE_UNION) {
3651 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3652 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3653 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3654 ly_set_add(&ctx->unres, leaf, 0);
3655 }
3656 }
3657 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003658 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3659 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3660 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3661 return LY_EVALID;
3662 }
3663 }
3664
Radek Krejci33f72892019-02-21 10:36:58 +01003665 return LY_SUCCESS;
3666}
3667
Radek Krejcia3045382018-11-22 14:30:31 +01003668/**
3669 * @brief Compile parsed leaf node information.
3670 * @param[in] ctx Compile context
3671 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003672 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3673 * is enriched with the leaf-specific information.
3674 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3675 */
Radek Krejci19a96102018-11-15 13:38:09 +01003676static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003677lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003678{
3679 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3680 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3681 unsigned int u;
3682 LY_ERR ret = LY_SUCCESS;
3683
Radek Krejciec4da802019-05-02 13:02:41 +02003684 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003685 if (leaf_p->units) {
3686 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3687 leaf->flags |= LYS_SET_UNITS;
3688 }
Radek Krejcia1911222019-07-22 17:24:50 +02003689
3690 /* the dflt member is just filled to avoid getting the default value from the type */
3691 leaf->dflt = (void*)leaf_p->dflt;
3692 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
3693
Radek Krejciccd20f12019-02-15 14:12:27 +01003694 if (leaf_p->dflt) {
Radek Krejcia1911222019-07-22 17:24:50 +02003695 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003696 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02003697 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3698 leaf->dflt->realtype = leaf->type;
3699 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt),
3700 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003701 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003702 leaf->dflt->realtype->refcount++;
3703 if (err) {
3704 ly_err_print(err);
3705 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3706 "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg);
3707 ly_err_free(err);
3708 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003709 if (ret == LY_EINCOMPLETE) {
Radek Krejci1c0c3442019-07-23 16:08:47 +02003710 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003711 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), done);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003712
3713 /* but in general result is so far ok */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003714 ret = LY_SUCCESS;
3715 }
Radek Krejcia1911222019-07-22 17:24:50 +02003716 LY_CHECK_GOTO(ret, done);
Radek Krejci76b3e962018-12-14 17:01:25 +01003717 leaf->flags |= LYS_SET_DFLT;
3718 }
Radek Krejci43699232018-11-23 14:59:46 +01003719
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003720
Radek Krejci19a96102018-11-15 13:38:09 +01003721done:
3722 return ret;
3723}
3724
Radek Krejcia3045382018-11-22 14:30:31 +01003725/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003726 * @brief Compile parsed leaf-list node information.
3727 * @param[in] ctx Compile context
3728 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003729 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3730 * is enriched with the leaf-list-specific information.
3731 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3732 */
3733static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003734lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003735{
3736 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3737 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Radek Krejcia1911222019-07-22 17:24:50 +02003738 unsigned int u, v;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003739 LY_ERR ret = LY_SUCCESS;
3740
Radek Krejciec4da802019-05-02 13:02:41 +02003741 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003742 if (llist_p->units) {
3743 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3744 llist->flags |= LYS_SET_UNITS;
3745 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003746
Radek Krejcia1911222019-07-22 17:24:50 +02003747 /* the dflts member is just filled to avoid getting the default value from the type */
3748 llist->dflts = (void*)llist_p->dflts;
3749 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003750 if (llist_p->dflts) {
Radek Krejcia1911222019-07-22 17:24:50 +02003751 llist->dflts = NULL; /* reset the temporary llist_p->dflts */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003752 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003753 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
3754 LY_ARRAY_FOR(llist_p->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02003755 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003756 LY_ARRAY_INCREMENT(llist->dflts_mods);
3757 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003758 LY_ARRAY_INCREMENT(llist->dflts);
Radek Krejcia1911222019-07-22 17:24:50 +02003759 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
3760 llist->dflts[u]->realtype = llist->type;
3761 ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]),
3762 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003763 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003764 llist->dflts[u]->realtype->refcount++;
3765 if (err) {
3766 ly_err_print(err);
3767 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3768 "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg);
3769 ly_err_free(err);
3770 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003771 if (ret == LY_EINCOMPLETE) {
3772 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003773 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), done);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003774
3775 /* but in general result is so far ok */
3776 ret = LY_SUCCESS;
3777 }
Radek Krejcia1911222019-07-22 17:24:50 +02003778 LY_CHECK_GOTO(ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003779 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003780 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003781 }
Radek Krejcia1911222019-07-22 17:24:50 +02003782 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
3783 /* configuration data values must be unique - so check the default values */
3784 LY_ARRAY_FOR(llist->dflts, u) {
3785 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
3786 if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) {
3787 int dynamic = 0;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003788 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 +02003789 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3790 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
3791 if (dynamic) {
3792 free((char*)val);
3793 }
3794 return LY_EVALID;
3795 }
3796 }
3797 }
3798 }
3799
3800 /* 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 +01003801
3802 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003803 if (llist->min) {
3804 llist->flags |= LYS_MAND_TRUE;
3805 }
Radek Krejcib7408632018-11-28 17:12:11 +01003806 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003807
Radek Krejci0e5d8382018-11-28 16:37:53 +01003808done:
3809 return ret;
3810}
3811
3812/**
Radek Krejci7af64242019-02-18 13:07:53 +01003813 * @brief Compile information about list's uniques.
3814 * @param[in] ctx Compile context.
3815 * @param[in] context_module Module where the prefixes are going to be resolved.
3816 * @param[in] uniques Sized array list of unique statements.
3817 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3818 * @return LY_ERR value.
3819 */
3820static LY_ERR
3821lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3822{
3823 LY_ERR ret = LY_SUCCESS;
3824 struct lysc_node_leaf **key, ***unique;
3825 const char *keystr, *delim;
3826 size_t len;
3827 unsigned int v;
3828 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003829 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003830
3831 for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) {
3832 config = -1;
3833 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3834 keystr = uniques[v];
3835 while (keystr) {
3836 delim = strpbrk(keystr, " \t\n");
3837 if (delim) {
3838 len = delim - keystr;
3839 while (isspace(*delim)) {
3840 ++delim;
3841 }
3842 } else {
3843 len = strlen(keystr);
3844 }
3845
3846 /* unique node must be present */
3847 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003848 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3849 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003850 if (ret != LY_SUCCESS) {
3851 if (ret == LY_EDENIED) {
3852 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003853 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003854 len, keystr, lys_nodetype2str((*key)->nodetype));
3855 }
3856 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003857 } else if (flags) {
3858 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3859 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
3860 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
3861 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003862 }
3863
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003864
Radek Krejci7af64242019-02-18 13:07:53 +01003865 /* all referenced leafs must be of the same config type */
3866 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3867 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3868 "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]);
3869 return LY_EVALID;
3870 } else if ((*key)->flags & LYS_CONFIG_W) {
3871 config = 1;
3872 } else { /* LYS_CONFIG_R */
3873 config = 0;
3874 }
3875
3876 /* check status */
3877 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3878 (*key)->flags, (*key)->module, (*key)->name));
3879
3880 /* mark leaf as unique */
3881 (*key)->flags |= LYS_UNIQUE;
3882
3883 /* next unique value in line */
3884 keystr = delim;
3885 }
3886 /* next unique definition */
3887 }
3888
3889 return LY_SUCCESS;
3890}
3891
3892/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003893 * @brief Compile parsed list node information.
3894 * @param[in] ctx Compile context
3895 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003896 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3897 * is enriched with the list-specific information.
3898 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3899 */
3900static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003901lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003902{
3903 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3904 struct lysc_node_list *list = (struct lysc_node_list*)node;
3905 struct lysp_node *child_p;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003906 struct lysc_node_leaf *key, *prev_key = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003907 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003908 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003909 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003910 LY_ERR ret = LY_SUCCESS;
3911
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003912 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003913 if (list->min) {
3914 list->flags |= LYS_MAND_TRUE;
3915 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003916 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3917
3918 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003919 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003920 }
3921
Radek Krejciec4da802019-05-02 13:02:41 +02003922 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003923
3924 /* keys */
3925 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3926 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3927 return LY_EVALID;
3928 }
3929
3930 /* find all the keys (must be direct children) */
3931 keystr = list_p->key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003932 if (!keystr) {
3933 /* keyless list */
3934 list->flags |= LYS_KEYLESS;
3935 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003936 while (keystr) {
3937 delim = strpbrk(keystr, " \t\n");
3938 if (delim) {
3939 len = delim - keystr;
3940 while (isspace(*delim)) {
3941 ++delim;
3942 }
3943 } else {
3944 len = strlen(keystr);
3945 }
3946
3947 /* key node must be present */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003948 key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
3949 if (!(key)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003950 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3951 "The list's key \"%.*s\" not found.", len, keystr);
3952 return LY_EVALID;
3953 }
3954 /* keys must be unique */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003955 if (key->flags & LYS_KEY) {
3956 /* the node was already marked as a key */
3957 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3958 "Duplicated key identifier \"%.*s\".", len, keystr);
3959 return LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003960 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003961
3962 lysc_update_path(ctx, (struct lysc_node*)list, key->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003963 /* key must have the same config flag as the list itself */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003964 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003965 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3966 return LY_EVALID;
3967 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003968 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003969 /* YANG 1.0 denies key to be of empty type */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003970 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003971 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003972 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003973 return LY_EVALID;
3974 }
3975 } else {
3976 /* when and if-feature are illegal on list keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003977 if (key->when) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003978 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003979 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003980 return LY_EVALID;
3981 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003982 if (key->iffeatures) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003983 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003984 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003985 return LY_EVALID;
3986 }
3987 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003988
3989 /* check status */
3990 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0fe9b512019-07-26 17:51:05 +02003991 key->flags, key->module, key->name));
Radek Krejci76b3e962018-12-14 17:01:25 +01003992
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003993 /* ignore default values of the key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003994 if (key->dflt) {
3995 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3996 lysc_type_free(ctx->ctx, key->dflt->realtype);
3997 free(key->dflt);
3998 key->dflt = NULL;
3999 key->dflt_mod = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004000 }
4001 /* mark leaf as key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004002 key->flags |= LYS_KEY;
4003
4004 /* move it to the correct position */
4005 if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) {
4006 /* fix links in closest previous siblings of the key */
4007 if (key->next) {
4008 key->next->prev = key->prev;
4009 } else {
4010 /* last child */
4011 list->child->prev = key->prev;
4012 }
4013 if (key->prev->next) {
4014 key->prev->next = key->next;
4015 }
4016 /* fix links in the key */
4017 if (prev_key) {
4018 key->prev = (struct lysc_node*)prev_key;
4019 key->next = prev_key->next;
4020 } else {
4021 key->prev = list->child->prev;
4022 key->next = list->child;
4023 }
4024 /* fix links in closes future siblings of the key */
4025 if (prev_key) {
4026 if (prev_key->next) {
4027 prev_key->next->prev = (struct lysc_node*)key;
4028 } else {
4029 list->child->prev = (struct lysc_node*)key;
4030 }
4031 prev_key->next = (struct lysc_node*)key;
4032 } else {
4033 list->child->prev = (struct lysc_node*)key;
4034 }
4035 /* fix links in parent */
4036 if (!key->prev->next) {
4037 list->child = (struct lysc_node*)key;
4038 }
4039 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004040
4041 /* next key value */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004042 prev_key = key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004043 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02004044 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004045 }
4046
4047 /* uniques */
4048 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01004049 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004050 }
4051
Radek Krejciec4da802019-05-02 13:02:41 +02004052 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
4053 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004054
4055done:
4056 return ret;
4057}
4058
Radek Krejcib56c7502019-02-13 14:19:54 +01004059/**
4060 * @brief Do some checks and set the default choice's case.
4061 *
4062 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
4063 *
4064 * @param[in] ctx Compile context.
4065 * @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,
4066 * not the reference to the imported module.
4067 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
4068 * @return LY_ERR value.
4069 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004070static LY_ERR
4071lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
4072{
4073 struct lysc_node *iter, *node = (struct lysc_node*)ch;
4074 const char *prefix = NULL, *name;
4075 size_t prefix_len = 0;
4076
4077 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
4078 name = strchr(dflt, ':');
4079 if (name) {
4080 prefix = dflt;
4081 prefix_len = name - prefix;
4082 ++name;
4083 } else {
4084 name = dflt;
4085 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004086 if (prefix && (strncmp(prefix, node->module->prefix, prefix_len) || node->module->prefix[prefix_len] != '\0')) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004087 /* prefixed default case make sense only for the prefix of the schema itself */
4088 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4089 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
4090 prefix_len, prefix);
4091 return LY_EVALID;
4092 }
4093 ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
4094 if (!ch->dflt) {
4095 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4096 "Default case \"%s\" not found.", dflt);
4097 return LY_EVALID;
4098 }
4099 /* no mandatory nodes directly under the default case */
4100 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01004101 if (iter->parent != (struct lysc_node*)ch->dflt) {
4102 break;
4103 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004104 if (iter->flags & LYS_MAND_TRUE) {
4105 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4106 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
4107 return LY_EVALID;
4108 }
4109 }
Radek Krejci01342af2019-01-03 15:18:08 +01004110 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004111 return LY_SUCCESS;
4112}
4113
Radek Krejciccd20f12019-02-15 14:12:27 +01004114static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004115lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01004116{
4117 struct lys_module *mod;
4118 const char *prefix = NULL, *name;
4119 size_t prefix_len = 0;
4120 struct lysc_node_case *cs;
4121 struct lysc_node *node;
4122
4123 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
4124 name = strchr(dflt, ':');
4125 if (name) {
4126 prefix = dflt;
4127 prefix_len = name - prefix;
4128 ++name;
4129 } else {
4130 name = dflt;
4131 }
4132 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
4133 if (prefix) {
4134 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
4135 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004136 "Invalid deviation adding \"default\" property \"%s\" of choice. "
4137 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01004138 return LY_EVALID;
4139 }
4140 } else {
4141 mod = ctx->mod;
4142 }
4143 /* get the default case */
4144 cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
4145 if (!cs) {
4146 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004147 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01004148 return LY_EVALID;
4149 }
4150
4151 /* check that there is no mandatory node */
4152 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01004153 if (node->parent != (struct lysc_node*)cs) {
4154 break;
4155 }
Radek Krejciccd20f12019-02-15 14:12:27 +01004156 if (node->flags & LYS_MAND_TRUE) {
4157 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004158 "Invalid deviation adding \"default\" property \"%s\" of choice - "
4159 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01004160 return LY_EVALID;
4161 }
4162 }
4163
4164 /* set the default case in choice */
4165 ch->dflt = cs;
4166 cs->flags |= LYS_SET_DFLT;
4167
4168 return LY_SUCCESS;
4169}
4170
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004171/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004172 * @brief Compile parsed choice node information.
4173 * @param[in] ctx Compile context
4174 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004175 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01004176 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01004177 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4178 */
4179static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004180lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004181{
4182 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
4183 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
4184 struct lysp_node *child_p, *case_child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004185 LY_ERR ret = LY_SUCCESS;
4186
Radek Krejci056d0a82018-12-06 16:57:25 +01004187 LY_LIST_FOR(ch_p->child, child_p) {
4188 if (child_p->nodetype == LYS_CASE) {
4189 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004190 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004191 }
4192 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004193 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004194 }
4195 }
4196
4197 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01004198 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004199 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01004200 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004201
Radek Krejci9800fb82018-12-13 14:26:23 +01004202 return ret;
4203}
4204
4205/**
4206 * @brief Compile parsed anydata or anyxml node information.
4207 * @param[in] ctx Compile context
4208 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01004209 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4210 * is enriched with the any-specific information.
4211 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4212 */
4213static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004214lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004215{
4216 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
4217 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
4218 unsigned int u;
4219 LY_ERR ret = LY_SUCCESS;
4220
Radek Krejciec4da802019-05-02 13:02:41 +02004221 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Radek Krejci9800fb82018-12-13 14:26:23 +01004222
4223 if (any->flags & LYS_CONFIG_W) {
4224 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
Radek Krejcid6b76452019-09-03 17:03:03 +02004225 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML));
Radek Krejci9800fb82018-12-13 14:26:23 +01004226 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004227done:
4228 return ret;
4229}
4230
Radek Krejcib56c7502019-02-13 14:19:54 +01004231/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004232 * @brief Connect the node into the siblings list and check its name uniqueness.
4233 *
4234 * @param[in] ctx Compile context
4235 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4236 * the choice itself is expected instead of a specific case node.
4237 * @param[in] node Schema node to connect into the list.
4238 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
4239 */
4240static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004241lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004242{
4243 struct lysc_node **children;
4244
4245 if (node->nodetype == LYS_CASE) {
4246 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
4247 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004248 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01004249 }
4250 if (children) {
4251 if (!(*children)) {
4252 /* first child */
4253 *children = node;
4254 } else if (*children != node) {
4255 /* by the condition in previous branch we cover the choice/case children
4256 * - the children list is shared by the choice and the the first case, in addition
4257 * the first child of each case must be referenced from the case node. So the node is
4258 * actually always already inserted in case it is the first children - so here such
4259 * a situation actually corresponds to the first branch */
4260 /* insert at the end of the parent's children list */
4261 (*children)->prev->next = node;
4262 node->prev = (*children)->prev;
4263 (*children)->prev = node;
4264
4265 /* check the name uniqueness */
4266 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
4267 lysc_node_notifs(parent), node->name, node)) {
4268 return LY_EEXIST;
4269 }
4270 }
4271 }
4272 return LY_SUCCESS;
4273}
4274
Radek Krejci95710c92019-02-11 15:49:55 +01004275/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004276 * @brief Get the XPath context node for the given schema node.
4277 * @param[in] start The schema node where the XPath expression appears.
4278 * @return The context node to evaluate XPath expression in given schema node.
4279 * @return NULL in case the context node is the root node.
4280 */
4281static struct lysc_node *
4282lysc_xpath_context(struct lysc_node *start)
4283{
4284 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF));
4285 start = start->parent);
4286 return start;
4287}
4288
4289/**
4290 * @brief Prepare the case structure in choice node for the new data node.
4291 *
4292 * 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
4293 * created in the choice when the first child was processed.
4294 *
4295 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004296 * @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,
4297 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004298 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4299 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4300 * @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,
4301 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004302 */
Radek Krejci056d0a82018-12-06 16:57:25 +01004303static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02004304lys_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 +01004305{
4306 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02004307 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01004308 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01004309 unsigned int u;
4310 LY_ERR ret;
4311
Radek Krejci95710c92019-02-11 15:49:55 +01004312#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01004313 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01004314 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01004315 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
4316 return NULL; \
4317 } \
4318 }
4319
Radek Krejci95710c92019-02-11 15:49:55 +01004320 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
4321 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004322
4323 /* we have to add an implicit case node into the parent choice */
4324 cs = calloc(1, sizeof(struct lysc_node_case));
4325 DUP_STRING(ctx->ctx, child->name, cs->name);
4326 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01004327 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004328 if (ch->cases && (node_p == ch->cases->prev->sp)) {
4329 /* the case is already present since the child is not its first children */
4330 return (struct lysc_node_case*)ch->cases->prev;
4331 }
Radek Krejci95710c92019-02-11 15:49:55 +01004332 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004333
4334 /* explicit parent case is not present (this is its first child) */
4335 cs = calloc(1, sizeof(struct lysc_node_case));
4336 DUP_STRING(ctx->ctx, node_p->name, cs->name);
4337 cs->flags = LYS_STATUS_MASK & node_p->flags;
4338 cs->sp = node_p;
4339
Radek Krejcib1b59152019-01-07 13:21:56 +01004340 /* 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 +02004341 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01004342
4343 if (node_p->when) {
4344 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02004345 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004346 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01004347 (*when)->context = lysc_xpath_context(ch->parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004348 }
Radek Krejciec4da802019-05-02 13:02:41 +02004349 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01004350 } else {
4351 LOGINT(ctx->ctx);
4352 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01004353 }
4354 cs->module = ctx->mod;
4355 cs->prev = (struct lysc_node*)cs;
4356 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02004357 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01004358 cs->parent = (struct lysc_node*)ch;
4359 cs->child = child;
4360
4361 return cs;
4362error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02004363 if (cs) {
4364 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
4365 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004366 return NULL;
4367
4368#undef UNIQUE_CHECK
4369}
4370
Radek Krejcib56c7502019-02-13 14:19:54 +01004371/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004372 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004373 *
4374 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004375 * @param[in] node Target node where the config is supposed to be changed.
4376 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004377 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4378 * 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 +01004379 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4380 * @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 +01004381 * @return LY_ERR value.
4382 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004383static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004384lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci327de162019-06-14 12:52:07 +02004385 int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004386{
4387 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004388 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004389
4390 if (config == (node->flags & LYS_CONFIG_MASK)) {
4391 /* nothing to do */
4392 return LY_SUCCESS;
4393 }
4394
4395 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004396 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004397 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4398 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004399 "Invalid %s of config - configuration node cannot be child of any state data node.",
4400 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004401 return LY_EVALID;
4402 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004403 node->flags |= LYS_SET_CONFIG;
4404 } else {
4405 if (node->flags & LYS_SET_CONFIG) {
4406 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4407 /* setting config flags, but have node with explicit config true */
4408 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004409 "Invalid %s of config - configuration node cannot be child of any state data node.",
4410 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004411 return LY_EVALID;
4412 }
4413 /* do not change config on nodes where the config is explicitely set, this does not apply to
4414 * nodes, which are being changed explicitly (targets of refine or deviation) */
4415 return LY_SUCCESS;
4416 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004417 }
4418 node->flags &= ~LYS_CONFIG_MASK;
4419 node->flags |= config;
4420
4421 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004422 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004423 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004424 }
4425
Radek Krejci76b3e962018-12-14 17:01:25 +01004426 return LY_SUCCESS;
4427}
4428
Radek Krejcib56c7502019-02-13 14:19:54 +01004429/**
4430 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4431 *
4432 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4433 * the flag to such parents from a mandatory children.
4434 *
4435 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4436 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4437 * (mandatory children was removed).
4438 */
Radek Krejcife909632019-02-12 15:34:42 +01004439void
4440lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4441{
4442 struct lysc_node *iter;
4443
4444 if (add) { /* set flag */
4445 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4446 parent = parent->parent) {
4447 parent->flags |= LYS_MAND_TRUE;
4448 }
4449 } else { /* unset flag */
4450 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004451 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004452 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004453 /* there is another mandatory node */
4454 return;
4455 }
4456 }
4457 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4458 parent->flags &= ~LYS_MAND_TRUE;
4459 }
4460 }
4461}
4462
Radek Krejci056d0a82018-12-06 16:57:25 +01004463/**
Radek Krejci3641f562019-02-13 15:38:40 +01004464 * @brief Internal sorting process for the lys_compile_augment_sort().
4465 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4466 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4467 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4468 */
4469static void
4470lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4471{
4472 unsigned int v;
4473 size_t len;
4474
4475 len = strlen(aug_p->nodeid);
4476 LY_ARRAY_FOR(result, v) {
4477 if (strlen(result[v]->nodeid) <= len) {
4478 continue;
4479 }
4480 if (v < LY_ARRAY_SIZE(result)) {
4481 /* move the rest of array */
4482 memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result);
4483 break;
4484 }
4485 }
4486 result[v] = aug_p;
4487 LY_ARRAY_INCREMENT(result);
4488}
4489
4490/**
4491 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4492 *
4493 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4494 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4495 *
4496 * @param[in] ctx Compile context.
4497 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4498 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4499 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4500 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4501 * @return LY_ERR value.
4502 */
4503LY_ERR
4504lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4505{
4506 struct lysp_augment **result = NULL;
4507 unsigned int u, v;
4508 size_t count = 0;
4509
4510 assert(augments);
4511
4512 /* get count of the augments in module and all its submodules */
4513 if (aug_p) {
4514 count += LY_ARRAY_SIZE(aug_p);
4515 }
4516 LY_ARRAY_FOR(inc_p, u) {
4517 if (inc_p[u].submodule->augments) {
4518 count += LY_ARRAY_SIZE(inc_p[u].submodule->augments);
4519 }
4520 }
4521
4522 if (!count) {
4523 *augments = NULL;
4524 return LY_SUCCESS;
4525 }
4526 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4527
4528 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4529 * together, so there can be even /z/y betwwen them. */
4530 LY_ARRAY_FOR(aug_p, u) {
4531 lys_compile_augment_sort_(&aug_p[u], result);
4532 }
4533 LY_ARRAY_FOR(inc_p, u) {
4534 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4535 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4536 }
4537 }
4538
4539 *augments = result;
4540 return LY_SUCCESS;
4541}
4542
4543/**
4544 * @brief Compile the parsed augment connecting it into its target.
4545 *
4546 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4547 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4548 * are already implemented and compiled.
4549 *
4550 * @param[in] ctx Compile context.
4551 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004552 * @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
4553 * children in case of the augmenting uses data.
4554 * @return LY_SUCCESS on success.
4555 * @return LY_EVALID on failure.
4556 */
4557LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004558lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004559{
4560 LY_ERR ret = LY_SUCCESS;
4561 struct lysp_node *node_p, *case_node_p;
4562 struct lysc_node *target; /* target target of the augment */
4563 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004564 struct lysc_when **when, *when_shared;
4565 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004566 uint16_t flags = 0;
4567 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02004568 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004569
Radek Krejci327de162019-06-14 12:52:07 +02004570 lysc_update_path(ctx, NULL, "{augment}");
4571 lysc_update_path(ctx, NULL, aug_p->nodeid);
4572
Radek Krejci7af64242019-02-18 13:07:53 +01004573 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004574 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004575 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004576 if (ret != LY_SUCCESS) {
4577 if (ret == LY_EDENIED) {
4578 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4579 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4580 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4581 }
4582 return LY_EVALID;
4583 }
4584
4585 /* check for mandatory nodes
4586 * - new cases augmenting some choice can have mandatory nodes
4587 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4588 */
Radek Krejci733988a2019-02-15 15:12:44 +01004589 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004590 allow_mandatory = 1;
4591 }
4592
4593 when_shared = NULL;
4594 LY_LIST_FOR(aug_p->child, node_p) {
4595 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4596 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
4597 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004598 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4599 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004600 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004601 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4602 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004603 return LY_EVALID;
4604 }
4605
4606 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004607 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004608 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004609 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004610 } else {
4611 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004612 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004613 }
4614 }
Radek Krejciec4da802019-05-02 13:02:41 +02004615 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004616
Radek Krejcife13da42019-02-15 14:51:01 +01004617 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4618 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004619 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004620 /* 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 +01004621 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 +01004622 } else if (target->nodetype == LYS_CHOICE) {
4623 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4624 node = ((struct lysc_node_choice*)target)->cases->prev;
4625 } else {
4626 /* the compiled node is the last child of the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004627 node = lysc_node_children(target, flags)->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004628 }
4629
Radek Krejci733988a2019-02-15 15:12:44 +01004630 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004631 node->flags &= ~LYS_MAND_TRUE;
4632 lys_compile_mandatory_parents(target, 0);
4633 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004634 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004635 return LY_EVALID;
4636 }
4637
4638 /* pass augment's when to all the children */
4639 if (aug_p->when) {
4640 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4641 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004642 ret = lys_compile_when(ctx, aug_p->when, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004643 LY_CHECK_GOTO(ret, error);
4644 (*when)->context = lysc_xpath_context(target);
4645 when_shared = *when;
4646 } else {
4647 ++when_shared->refcount;
4648 (*when) = when_shared;
4649 }
4650 }
4651 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004652
Radek Krejciec4da802019-05-02 13:02:41 +02004653 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004654 switch (target->nodetype) {
4655 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004656 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004657 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004658 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004659 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004660 break;
4661 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004662 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004663 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004664 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004665 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004666 break;
4667 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004668 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004669 if (aug_p->actions) {
4670 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004671 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4672 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004673 return LY_EVALID;
4674 }
4675 if (aug_p->notifs) {
4676 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004677 "Invalid augment of %s node which is not allowed to contain Notification node \"%s\".",
4678 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004679 return LY_EVALID;
4680 }
4681 }
Radek Krejci3641f562019-02-13 15:38:40 +01004682
Radek Krejci327de162019-06-14 12:52:07 +02004683 lysc_update_path(ctx, NULL, NULL);
4684 lysc_update_path(ctx, NULL, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004685error:
Radek Krejciec4da802019-05-02 13:02:41 +02004686 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004687 return ret;
4688}
4689
4690/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004691 * @brief Apply refined or deviated mandatory flag to the target node.
4692 *
4693 * @param[in] ctx Compile context.
4694 * @param[in] node Target node where the mandatory property is supposed to be changed.
4695 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004696 * @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 +01004697 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4698 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4699 * 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 +01004700 * @return LY_ERR value.
4701 */
4702static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004703lys_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 +01004704{
4705 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4706 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004707 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4708 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004709 return LY_EVALID;
4710 }
4711
4712 if (mandatory_flag & LYS_MAND_TRUE) {
4713 /* check if node has default value */
4714 if (node->nodetype & LYS_LEAF) {
4715 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004716 if (refine_flag) {
4717 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004718 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004719 return LY_EVALID;
4720 }
Radek Krejcia1911222019-07-22 17:24:50 +02004721 } else if (((struct lysc_node_leaf*)node)->dflt) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004722 /* remove the default value taken from the leaf's type */
Radek Krejcia1911222019-07-22 17:24:50 +02004723 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejci474f9b82019-07-24 11:36:37 +02004724
4725 /* update the list of incomplete default values if needed */
4726 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
4727
Radek Krejcia1911222019-07-22 17:24:50 +02004728 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4729 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4730 free(leaf->dflt);
4731 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004732 leaf->dflt_mod = NULL;
Radek Krejcif1421c22019-02-19 13:05:20 +01004733 }
4734 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004735 if (refine_flag) {
4736 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004737 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004738 return LY_EVALID;
4739 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004740 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004741 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004742 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 +01004743 return LY_EVALID;
4744 }
4745
4746 node->flags &= ~LYS_MAND_FALSE;
4747 node->flags |= LYS_MAND_TRUE;
4748 lys_compile_mandatory_parents(node->parent, 1);
4749 } else {
4750 /* make mandatory false */
4751 node->flags &= ~LYS_MAND_TRUE;
4752 node->flags |= LYS_MAND_FALSE;
4753 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcia1911222019-07-22 17:24:50 +02004754 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 +01004755 /* get the type's default value if any */
Radek Krejcia1911222019-07-22 17:24:50 +02004756 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004757 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02004758 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4759 leaf->dflt->realtype = leaf->type->dflt->realtype;
4760 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
4761 leaf->dflt->realtype->refcount++;
Radek Krejcif1421c22019-02-19 13:05:20 +01004762 }
4763 }
4764 return LY_SUCCESS;
4765}
4766
4767/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004768 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4769 * If present, also apply uses's modificators.
4770 *
4771 * @param[in] ctx Compile context
4772 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004773 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4774 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4775 * the compile context.
4776 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4777 */
4778static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004779lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent)
Radek Krejcie86bf772018-12-14 11:39:53 +01004780{
4781 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004782 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004783 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004784 struct lysc_node_container context_node_fake =
4785 {.nodetype = LYS_CONTAINER,
4786 .module = ctx->mod,
4787 .flags = parent ? parent->flags : 0,
4788 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004789 .prev = (struct lysc_node*)&context_node_fake,
4790 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004791 struct lysp_grp *grp = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01004792 unsigned int u, v, grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004793 int found;
4794 const char *id, *name, *prefix;
4795 size_t prefix_len, name_len;
4796 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004797 struct lysp_refine *rfn;
Radek Krejcia1911222019-07-22 17:24:50 +02004798 LY_ERR ret = LY_EVALID, rc;
Radek Krejcif2271f12019-01-07 16:42:23 +01004799 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004800 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004801 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004802 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004803 struct lysp_augment **augments = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004804 unsigned int actions_index, notifs_index;
4805 struct lysc_notif **notifs = NULL;
4806 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004807
4808 /* search for the grouping definition */
4809 found = 0;
4810 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004811 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004812 if (prefix) {
4813 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4814 if (!mod) {
4815 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004816 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004817 return LY_EVALID;
4818 }
4819 } else {
4820 mod = ctx->mod_def;
4821 }
4822 if (mod == ctx->mod_def) {
4823 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004824 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004825 LY_ARRAY_FOR(grp, u) {
4826 if (!strcmp(grp[u].name, name)) {
4827 grp = &grp[u];
4828 found = 1;
4829 break;
4830 }
4831 }
4832 }
4833 }
4834 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004835 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004836 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004837 if (grp) {
4838 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
4839 if (!strcmp(grp[u].name, name)) {
4840 grp = &grp[u];
4841 found = 1;
4842 }
4843 }
4844 }
4845 if (!found && mod->parsed->includes) {
4846 /* ... and all the submodules */
4847 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
4848 grp = mod->parsed->includes[u].submodule->groupings;
4849 if (grp) {
4850 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
4851 if (!strcmp(grp[v].name, name)) {
4852 grp = &grp[v];
4853 found = 1;
4854 }
4855 }
4856 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004857 }
4858 }
4859 }
4860 if (!found) {
4861 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4862 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4863 return LY_EVALID;
4864 }
4865
4866 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4867 grp_stack_count = ctx->groupings.count;
4868 ly_set_add(&ctx->groupings, (void*)grp, 0);
4869 if (grp_stack_count == ctx->groupings.count) {
4870 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4871 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4872 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4873 return LY_EVALID;
4874 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004875 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4876 /* remember that the grouping is instantiated to avoid its standalone validation */
4877 grp->flags |= LYS_USED_GRP;
4878 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004879
4880 /* switch context's mod_def */
4881 mod_old = ctx->mod_def;
4882 ctx->mod_def = mod;
4883
4884 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004885 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 +01004886
Radek Krejcifc11bd72019-04-11 16:00:05 +02004887 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004888 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004889 /* 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 +02004890 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 +01004891
4892 /* some preparation for applying refines */
4893 if (grp->data == node_p) {
4894 /* remember the first child */
Radek Krejci684faf22019-05-27 14:31:16 +02004895 if (parent) {
4896 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
4897 } else if (ctx->mod->compiled->data) {
4898 child = ctx->mod->compiled->data;
4899 } else {
4900 child = NULL;
4901 }
4902 context_node_fake.child = child ? child->prev : NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004903 }
4904 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004905 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004906 LY_LIST_FOR(context_node_fake.child, child) {
4907 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004908
Radek Krejcifc11bd72019-04-11 16:00:05 +02004909 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004910 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004911 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004912 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004913 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, when), cleanup);
Radek Krejcib56c7502019-02-13 14:19:54 +01004914 (*when)->context = lysc_xpath_context(parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004915 when_shared = *when;
4916 } else {
4917 ++when_shared->refcount;
4918 (*when) = when_shared;
4919 }
4920 }
Radek Krejci01342af2019-01-03 15:18:08 +01004921 }
4922 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004923 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01004924 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004925 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejciec4da802019-05-02 13:02:41 +02004926 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 +01004927 }
4928
Radek Krejcifc11bd72019-04-11 16:00:05 +02004929 /* compile actions */
4930 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4931 if (actions) {
4932 actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004933 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004934 if (*actions && (uses_p->augments || uses_p->refines)) {
4935 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
4936 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup);
4937 LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index;
4938 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4939 }
4940 }
4941
4942 /* compile notifications */
4943 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4944 if (notifs) {
4945 notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004946 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004947 if (*notifs && (uses_p->augments || uses_p->refines)) {
4948 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
4949 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup);
4950 LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index;
4951 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4952 }
4953 }
4954
4955
Radek Krejci3641f562019-02-13 15:38:40 +01004956 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004957 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004958 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02004959 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004960 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004961
Radek Krejcif0089082019-01-07 16:42:01 +01004962 /* reload previous context's mod_def */
4963 ctx->mod_def = mod_old;
Radek Krejci327de162019-06-14 12:52:07 +02004964 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01004965
Radek Krejci76b3e962018-12-14 17:01:25 +01004966 /* apply refine */
4967 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02004968 lysc_update_path(ctx, NULL, rfn->nodeid);
4969
Radek Krejci7af64242019-02-18 13:07:53 +01004970 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 +01004971 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004972 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01004973 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004974
4975 /* default value */
4976 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01004977 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004978 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004979 "Invalid refine of default - %s cannot hold %d default values.",
4980 lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004981 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004982 }
4983 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4984 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004985 "Invalid refine of default - %s cannot hold default value(s).",
4986 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004987 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004988 }
4989 if (node->nodetype == LYS_LEAF) {
Radek Krejcia1911222019-07-22 17:24:50 +02004990 struct ly_err_item *err = NULL;
4991 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
4992 if (leaf->dflt) {
4993 /* remove the previous default value */
Radek Krejci474f9b82019-07-24 11:36:37 +02004994 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02004995 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4996 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4997 } else {
4998 /* prepare a new one */
4999 leaf->dflt = calloc(1, sizeof *leaf->dflt);
5000 leaf->dflt->realtype = leaf->type;
5001 }
5002 /* parse the new one */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005003 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005004 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]),
5005 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005006 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005007 leaf->dflt->realtype->refcount++;
5008 if (err) {
5009 ly_err_print(err);
5010 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5011 "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg);
5012 ly_err_free(err);
5013 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02005014 if (rc == LY_EINCOMPLETE) {
5015 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02005016 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02005017
5018 /* but in general result is so far ok */
5019 rc = LY_SUCCESS;
5020 }
Radek Krejcia1911222019-07-22 17:24:50 +02005021 LY_CHECK_GOTO(rc, cleanup);
5022 leaf->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01005023 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejcia1911222019-07-22 17:24:50 +02005024 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
5025
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005026 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01005027 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5028 "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 +02005029 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01005030 }
Radek Krejcia1911222019-07-22 17:24:50 +02005031
5032 /* remove previous set of default values */
5033 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci474f9b82019-07-24 11:36:37 +02005034 lysc_incomplete_dflts_remove(ctx, llist->dflts[u]);
Radek Krejcia1911222019-07-22 17:24:50 +02005035 llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]);
5036 lysc_type_free(ctx->ctx, llist->dflts[u]->realtype);
5037 free(llist->dflts[u]);
Radek Krejci76b3e962018-12-14 17:01:25 +01005038 }
Radek Krejcia1911222019-07-22 17:24:50 +02005039 LY_ARRAY_FREE(llist->dflts);
5040 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005041 LY_ARRAY_FREE(llist->dflts_mods);
5042 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005043
5044 /* create the new set of the default values */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005045 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02005046 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005047 LY_ARRAY_FOR(rfn->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005048 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005049 LY_ARRAY_INCREMENT(llist->dflts_mods);
5050 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005051 LY_ARRAY_INCREMENT(llist->dflts);
5052 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
5053 llist->dflts[u]->realtype = llist->type;
Radek Krejci1c0c3442019-07-23 16:08:47 +02005054 rc = llist->type->plugin->store(ctx->ctx, llist->type, rfn->dflts[u], strlen(rfn->dflts[u]),
Radek Krejcia1911222019-07-22 17:24:50 +02005055 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005056 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005057 llist->dflts[u]->realtype->refcount++;
5058 if (err) {
5059 ly_err_print(err);
5060 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5061 "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg);
5062 ly_err_free(err);
5063 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02005064 if (rc == LY_EINCOMPLETE) {
5065 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02005066 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02005067
5068 /* but in general result is so far ok */
5069 rc = LY_SUCCESS;
5070 }
5071 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005072 }
Radek Krejcia1911222019-07-22 17:24:50 +02005073 llist->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01005074 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01005075 if (((struct lysc_node_choice*)node)->dflt) {
5076 /* unset LYS_SET_DFLT from the current default case */
5077 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
5078 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005079 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 +01005080 }
5081 }
5082
Radek Krejci12fb9142019-01-08 09:45:30 +01005083 /* description */
5084 if (rfn->dsc) {
5085 FREE_STRING(ctx->ctx, node->dsc);
5086 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
5087 }
5088
5089 /* reference */
5090 if (rfn->ref) {
5091 FREE_STRING(ctx->ctx, node->ref);
5092 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
5093 }
Radek Krejci76b3e962018-12-14 17:01:25 +01005094
5095 /* config */
5096 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005097 if (!flags) {
Radek Krejci327de162019-06-14 12:52:07 +02005098 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005099 } else {
5100 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
5101 flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path);
5102 }
Radek Krejci76b3e962018-12-14 17:01:25 +01005103 }
5104
5105 /* mandatory */
5106 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci327de162019-06-14 12:52:07 +02005107 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005108 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01005109
5110 /* presence */
5111 if (rfn->presence) {
5112 if (node->nodetype != LYS_CONTAINER) {
5113 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005114 "Invalid refine of presence statement - %s cannot hold the presence statement.",
5115 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005116 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01005117 }
5118 node->flags |= LYS_PRESENCE;
5119 }
Radek Krejci9a564c92019-01-07 14:53:57 +01005120
5121 /* must */
5122 if (rfn->musts) {
5123 switch (node->nodetype) {
5124 case LYS_LEAF:
Radek Krejciec4da802019-05-02 13:02:41 +02005125 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 +01005126 break;
5127 case LYS_LEAFLIST:
Radek Krejciec4da802019-05-02 13:02:41 +02005128 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 +01005129 break;
5130 case LYS_LIST:
Radek Krejciec4da802019-05-02 13:02:41 +02005131 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 +01005132 break;
5133 case LYS_CONTAINER:
Radek Krejciec4da802019-05-02 13:02:41 +02005134 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 +01005135 break;
5136 case LYS_ANYXML:
5137 case LYS_ANYDATA:
Radek Krejciec4da802019-05-02 13:02:41 +02005138 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 +01005139 break;
5140 default:
5141 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005142 "Invalid refine of must statement - %s cannot hold any must statement.",
5143 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005144 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01005145 }
5146 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005147
5148 /* min/max-elements */
5149 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5150 switch (node->nodetype) {
5151 case LYS_LEAFLIST:
5152 if (rfn->flags & LYS_SET_MAX) {
5153 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5154 }
5155 if (rfn->flags & LYS_SET_MIN) {
5156 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005157 if (rfn->min) {
5158 node->flags |= LYS_MAND_TRUE;
5159 lys_compile_mandatory_parents(node->parent, 1);
5160 } else {
5161 node->flags &= ~LYS_MAND_TRUE;
5162 lys_compile_mandatory_parents(node->parent, 0);
5163 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005164 }
5165 break;
5166 case LYS_LIST:
5167 if (rfn->flags & LYS_SET_MAX) {
5168 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5169 }
5170 if (rfn->flags & LYS_SET_MIN) {
5171 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005172 if (rfn->min) {
5173 node->flags |= LYS_MAND_TRUE;
5174 lys_compile_mandatory_parents(node->parent, 1);
5175 } else {
5176 node->flags &= ~LYS_MAND_TRUE;
5177 lys_compile_mandatory_parents(node->parent, 0);
5178 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005179 }
5180 break;
5181 default:
5182 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005183 "Invalid refine of %s statement - %s cannot hold this statement.",
5184 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005185 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01005186 }
5187 }
Radek Krejcif0089082019-01-07 16:42:01 +01005188
5189 /* if-feature */
5190 if (rfn->iffeatures) {
5191 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02005192 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01005193 }
Radek Krejci327de162019-06-14 12:52:07 +02005194
5195 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01005196 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005197
Radek Krejcif2271f12019-01-07 16:42:23 +01005198 /* do some additional checks of the changed nodes when all the refines are applied */
5199 for (u = 0; u < refined.count; ++u) {
5200 node = (struct lysc_node*)refined.objs[u];
5201 rfn = &uses_p->refines[u];
Radek Krejci327de162019-06-14 12:52:07 +02005202 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01005203
5204 /* check possible conflict with default value (default added, mandatory left true) */
5205 if ((node->flags & LYS_MAND_TRUE) &&
5206 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
5207 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
5208 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005209 "Invalid refine of default - the node is mandatory.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005210 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005211 }
5212
5213 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5214 if (node->nodetype == LYS_LIST) {
5215 min = ((struct lysc_node_list*)node)->min;
5216 max = ((struct lysc_node_list*)node)->max;
5217 } else {
5218 min = ((struct lysc_node_leaflist*)node)->min;
5219 max = ((struct lysc_node_leaflist*)node)->max;
5220 }
5221 if (min > max) {
5222 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005223 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
5224 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005225 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005226 }
5227 }
5228 }
5229
Radek Krejci327de162019-06-14 12:52:07 +02005230 lysc_update_path(ctx, NULL, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005231 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005232
5233cleanup:
5234 /* fix connection of the children nodes from fake context node back into the parent */
5235 if (context_node_fake.child) {
5236 context_node_fake.child->prev = child;
5237 }
5238 LY_LIST_FOR(context_node_fake.child, child) {
5239 child->parent = parent;
5240 }
5241
5242 if (uses_p->augments || uses_p->refines) {
5243 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005244 if (context_node_fake.actions) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005245 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
5246 LY_ARRAY_FREE(context_node_fake.actions);
5247 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005248 if (context_node_fake.notifs) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005249 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
5250 LY_ARRAY_FREE(context_node_fake.notifs);
5251 }
5252 }
5253
Radek Krejcie86bf772018-12-14 11:39:53 +01005254 /* reload previous context's mod_def */
5255 ctx->mod_def = mod_old;
5256 /* remove the grouping from the stack for circular groupings dependency check */
5257 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5258 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01005259 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01005260 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005261
5262 return ret;
5263}
5264
Radek Krejci327de162019-06-14 12:52:07 +02005265static int
5266lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5267{
5268 struct lysp_node *iter;
5269 int len = 0;
5270
5271 *path = NULL;
5272 for (iter = node; iter && len >= 0; iter = iter->parent) {
5273 char *s = *path;
5274 char *id;
5275
5276 switch (iter->nodetype) {
5277 case LYS_USES:
5278 asprintf(&id, "{uses='%s'}", iter->name);
5279 break;
5280 case LYS_GROUPING:
5281 asprintf(&id, "{grouping='%s'}", iter->name);
5282 break;
5283 case LYS_AUGMENT:
5284 asprintf(&id, "{augment='%s'}", iter->name);
5285 break;
5286 default:
5287 id = strdup(iter->name);
5288 break;
5289 }
5290
5291 if (!iter->parent) {
5292 /* print prefix */
5293 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5294 } else {
5295 /* prefix is the same as in parent */
5296 len = asprintf(path, "/%s%s", id, s ? s : "");
5297 }
5298 free(s);
5299 free(id);
5300 }
5301
5302 if (len < 0) {
5303 free(*path);
5304 *path = NULL;
5305 } else if (len == 0) {
5306 *path = strdup("/");
5307 len = 1;
5308 }
5309 return len;
5310}
5311
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005312/**
5313 * @brief Validate groupings that were defined but not directly used in the schema itself.
5314 *
5315 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5316 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5317 */
5318static LY_ERR
5319lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5320{
5321 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005322 char *path;
5323 int len;
5324
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005325 struct lysp_node_uses fake_uses = {
5326 .parent = node_p,
5327 .nodetype = LYS_USES,
5328 .flags = 0, .next = NULL,
5329 .name = grp->name,
5330 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5331 .refines = NULL, .augments = NULL
5332 };
5333 struct lysc_node_container fake_container = {
5334 .nodetype = LYS_CONTAINER,
5335 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5336 .module = ctx->mod,
5337 .sp = NULL, .parent = NULL, .next = NULL,
5338 .prev = (struct lysc_node*)&fake_container,
5339 .name = "fake",
5340 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5341 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5342 };
5343
5344 if (grp->parent) {
5345 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5346 }
Radek Krejci327de162019-06-14 12:52:07 +02005347
5348 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5349 if (len < 0) {
5350 LOGMEM(ctx->ctx);
5351 return LY_EMEM;
5352 }
5353 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
5354 ctx->path_len = (uint16_t)len;
5355 free(path);
5356
5357 lysc_update_path(ctx, NULL, "{grouping}");
5358 lysc_update_path(ctx, NULL, grp->name);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005359 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container);
Radek Krejci327de162019-06-14 12:52:07 +02005360 lysc_update_path(ctx, NULL, NULL);
5361 lysc_update_path(ctx, NULL, NULL);
5362
5363 ctx->path_len = 1;
5364 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005365
5366 /* cleanup */
5367 lysc_node_container_free(ctx->ctx, &fake_container);
5368
5369 return ret;
5370}
Radek Krejcife909632019-02-12 15:34:42 +01005371
Radek Krejcie86bf772018-12-14 11:39:53 +01005372/**
Radek Krejcia3045382018-11-22 14:30:31 +01005373 * @brief Compile parsed schema node information.
5374 * @param[in] ctx Compile context
5375 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005376 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5377 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5378 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005379 * @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).
5380 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005381 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5382 */
Radek Krejci19a96102018-11-15 13:38:09 +01005383static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005384lys_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 +01005385{
5386 LY_ERR ret = LY_EVALID;
Radek Krejci056d0a82018-12-06 16:57:25 +01005387 struct lysc_node *node;
5388 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01005389 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01005390 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02005391 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01005392
Radek Krejci327de162019-06-14 12:52:07 +02005393 if (node_p->nodetype != LYS_USES) {
5394 lysc_update_path(ctx, parent, node_p->name);
5395 } else {
5396 lysc_update_path(ctx, NULL, "{uses}");
5397 lysc_update_path(ctx, NULL, node_p->name);
5398 }
5399
Radek Krejci19a96102018-11-15 13:38:09 +01005400 switch (node_p->nodetype) {
5401 case LYS_CONTAINER:
5402 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
5403 node_compile_spec = lys_compile_node_container;
5404 break;
5405 case LYS_LEAF:
5406 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
5407 node_compile_spec = lys_compile_node_leaf;
5408 break;
5409 case LYS_LIST:
5410 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005411 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005412 break;
5413 case LYS_LEAFLIST:
5414 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005415 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005416 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005417 case LYS_CHOICE:
5418 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005419 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005420 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005421 case LYS_ANYXML:
5422 case LYS_ANYDATA:
5423 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005424 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005425 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005426 case LYS_USES:
Radek Krejci327de162019-06-14 12:52:07 +02005427 ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent);
5428 lysc_update_path(ctx, NULL, NULL);
5429 lysc_update_path(ctx, NULL, NULL);
5430 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005431 default:
5432 LOGINT(ctx->ctx);
5433 return LY_EINT;
5434 }
5435 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5436 node->nodetype = node_p->nodetype;
5437 node->module = ctx->mod;
5438 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005439 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005440
5441 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02005442 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005443 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005444 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02005445 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5446 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005447 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005448 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005449 node->flags |= LYS_CONFIG_R;
5450 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005451 /* config not explicitely set, inherit it from parent */
5452 if (parent) {
5453 node->flags |= parent->flags & LYS_CONFIG_MASK;
5454 } else {
5455 /* default is config true */
5456 node->flags |= LYS_CONFIG_W;
5457 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005458 } else {
5459 /* config set explicitely */
5460 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01005461 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005462 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5463 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5464 "Configuration node cannot be child of any state data node.");
5465 goto error;
5466 }
Radek Krejci19a96102018-11-15 13:38:09 +01005467
Radek Krejcia6d57732018-11-29 13:40:37 +01005468 /* *list ordering */
5469 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5470 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005471 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02005472 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5473 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005474 node->flags &= ~LYS_ORDBY_MASK;
5475 node->flags |= LYS_ORDBY_SYSTEM;
5476 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5477 /* default ordering is system */
5478 node->flags |= LYS_ORDBY_SYSTEM;
5479 }
5480 }
5481
Radek Krejci19a96102018-11-15 13:38:09 +01005482 /* status - it is not inherited by specification, but it does not make sense to have
5483 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01005484 if (!parent || parent->nodetype != LYS_CHOICE) {
5485 /* in case of choice/case's children, postpone the check to the moment we know if
5486 * 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 +01005487 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 +01005488 }
5489
Radek Krejciec4da802019-05-02 13:02:41 +02005490 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005491 node->sp = node_p;
5492 }
5493 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01005494 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
5495 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01005496 if (node_p->when) {
5497 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02005498 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01005499 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01005500 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +01005501 }
Radek Krejciec4da802019-05-02 13:02:41 +02005502 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005503
5504 /* nodetype-specific part */
Radek Krejciec4da802019-05-02 13:02:41 +02005505 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005506
Radek Krejci0935f412019-08-20 16:15:18 +02005507 COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error);
5508
Radek Krejcife909632019-02-12 15:34:42 +01005509 /* inherit LYS_MAND_TRUE in parent containers */
5510 if (node->flags & LYS_MAND_TRUE) {
5511 lys_compile_mandatory_parents(parent, 1);
5512 }
5513
Radek Krejci327de162019-06-14 12:52:07 +02005514 lysc_update_path(ctx, NULL, NULL);
5515
Radek Krejci19a96102018-11-15 13:38:09 +01005516 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01005517 if (parent) {
5518 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci327de162019-06-14 12:52:07 +02005519 if (node_p->parent->nodetype == LYS_CASE) {
5520 lysc_update_path(ctx, parent, node_p->parent->name);
5521 } else {
5522 lysc_update_path(ctx, parent, node->name);
5523 }
Radek Krejciec4da802019-05-02 13:02:41 +02005524 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01005525 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01005526 if (uses_status) {
5527
5528 }
Radek Krejci056d0a82018-12-06 16:57:25 +01005529 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01005530 * it directly gets the same status flags as the choice;
5531 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005532 LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01005533 node->parent = (struct lysc_node*)cs;
Radek Krejci327de162019-06-14 12:52:07 +02005534 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005535 } else { /* other than choice */
Radek Krejci327de162019-06-14 12:52:07 +02005536 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005537 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01005538 }
Radek Krejciec4da802019-05-02 13:02:41 +02005539 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 +02005540
5541 if (parent->nodetype == LYS_CHOICE) {
5542 lysc_update_path(ctx, NULL, NULL);
5543 }
Radek Krejci19a96102018-11-15 13:38:09 +01005544 } else {
5545 /* top-level element */
5546 if (!ctx->mod->compiled->data) {
5547 ctx->mod->compiled->data = node;
5548 } else {
5549 /* insert at the end of the module's top-level nodes list */
5550 ctx->mod->compiled->data->prev->next = node;
5551 node->prev = ctx->mod->compiled->data->prev;
5552 ctx->mod->compiled->data->prev = node;
5553 }
Radek Krejci327de162019-06-14 12:52:07 +02005554 lysc_update_path(ctx, parent, node->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01005555 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
5556 ctx->mod->compiled->notifs, node->name, node)) {
5557 return LY_EVALID;
5558 }
Radek Krejci19a96102018-11-15 13:38:09 +01005559 }
Radek Krejci327de162019-06-14 12:52:07 +02005560 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005561
5562 return LY_SUCCESS;
5563
5564error:
5565 lysc_node_free(ctx->ctx, node);
5566 return ret;
5567}
5568
Radek Krejciccd20f12019-02-15 14:12:27 +01005569static void
5570lysc_disconnect(struct lysc_node *node)
5571{
Radek Krejci0a048dd2019-02-18 16:01:43 +01005572 struct lysc_node *parent, *child, *prev = NULL, *next;
5573 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005574 int remove_cs = 0;
5575
5576 parent = node->parent;
5577
5578 /* parent's first child */
5579 if (!parent) {
5580 return;
5581 }
5582 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005583 cs = (struct lysc_node_case*)node;
5584 } else if (parent->nodetype == LYS_CASE) {
5585 /* disconnecting some node in a case */
5586 cs = (struct lysc_node_case*)parent;
5587 parent = cs->parent;
5588 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
5589 if (child == node) {
5590 if (cs->child == child) {
5591 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5592 /* case with a single child -> remove also the case */
5593 child->parent = NULL;
5594 remove_cs = 1;
5595 } else {
5596 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005597 }
5598 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005599 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005600 }
5601 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005602 if (!remove_cs) {
5603 cs = NULL;
5604 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005605 } else if (lysc_node_children(parent, node->flags) == node) {
5606 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005607 }
5608
5609 if (cs) {
5610 if (remove_cs) {
5611 /* cs has only one child which is being also removed */
5612 lysc_disconnect((struct lysc_node*)cs);
5613 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5614 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005615 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5616 /* default case removed */
5617 ((struct lysc_node_choice*)parent)->dflt = NULL;
5618 }
5619 if (((struct lysc_node_choice*)parent)->cases == cs) {
5620 /* first case removed */
5621 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5622 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005623 if (cs->child) {
5624 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5625 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5626 prev = cs->child->prev;
5627 } /* else all the children are under a single case */
5628 LY_LIST_FOR_SAFE(cs->child, next, child) {
5629 if (child->parent != (struct lysc_node*)cs) {
5630 break;
5631 }
5632 lysc_node_free(node->module->ctx, child);
5633 }
5634 if (prev) {
5635 if (prev->next) {
5636 prev->next = child;
5637 }
5638 if (child) {
5639 child->prev = prev;
5640 } else {
5641 /* link from the first child under the cases */
5642 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5643 }
5644 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005645 }
5646 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005647 }
5648
5649 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005650 if (node->prev->next) {
5651 node->prev->next = node->next;
5652 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005653 if (node->next) {
5654 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005655 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005656 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005657 if (child) {
5658 child->prev = node->prev;
5659 }
5660 } else if (((struct lysc_node_choice*)parent)->cases) {
5661 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005662 }
5663}
5664
5665LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005666lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
Radek Krejciccd20f12019-02-15 14:12:27 +01005667{
Radek Krejcia1911222019-07-22 17:24:50 +02005668 LY_ERR ret = LY_EVALID, rc;
Radek Krejciccd20f12019-02-15 14:12:27 +01005669 struct ly_set devs_p = {0};
5670 struct ly_set targets = {0};
5671 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005672 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005673 struct lysc_action *rpcs;
5674 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005675 struct lysp_deviation *dev;
5676 struct lysp_deviate *d, **dp_new;
5677 struct lysp_deviate_add *d_add;
5678 struct lysp_deviate_del *d_del;
5679 struct lysp_deviate_rpl *d_rpl;
Radek Krejci7af64242019-02-18 13:07:53 +01005680 unsigned int u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005681 struct lysc_deviation {
5682 const char *nodeid;
5683 struct lysc_node *target; /* target node of the deviation */
5684 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005685 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005686 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5687 } **devs = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005688 int i, changed_type;
Radek Krejciccd20f12019-02-15 14:12:27 +01005689 size_t prefix_len, name_len;
Radek Krejcia1911222019-07-22 17:24:50 +02005690 const char *prefix, *name, *nodeid, *dflt;
Radek Krejciccd20f12019-02-15 14:12:27 +01005691 struct lys_module *mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005692 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005693 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005694
5695 /* get all deviations from the module and all its submodules ... */
5696 LY_ARRAY_FOR(mod_p->deviations, u) {
5697 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5698 }
5699 LY_ARRAY_FOR(mod_p->includes, v) {
5700 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5701 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5702 }
5703 }
5704 if (!devs_p.count) {
5705 /* nothing to do */
5706 return LY_SUCCESS;
5707 }
5708
Radek Krejci327de162019-06-14 12:52:07 +02005709 lysc_update_path(ctx, NULL, "{deviation}");
5710
Radek Krejciccd20f12019-02-15 14:12:27 +01005711 /* ... and group them by the target node */
5712 devs = calloc(devs_p.count, sizeof *devs);
5713 for (u = 0; u < devs_p.count; ++u) {
5714 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02005715 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005716
5717 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005718 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5719 (const struct lysc_node**)&target, &flags), cleanup);
Radek Krejcif538ce52019-03-05 10:46:14 +01005720 if (target->nodetype == LYS_ACTION) {
5721 /* move the target pointer to input/output to make them different from the action and
5722 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5723 * back to the RPC/action node due to a better compatibility and decision code in this function.
5724 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5725 if (flags & LYSC_OPT_RPC_INPUT) {
5726 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5727 flags |= LYSC_OPT_INTERNAL;
5728 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5729 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5730 flags |= LYSC_OPT_INTERNAL;
5731 }
5732 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005733 /* insert into the set of targets with duplicity detection */
5734 i = ly_set_add(&targets, target, 0);
5735 if (!devs[i]) {
5736 /* new record */
5737 devs[i] = calloc(1, sizeof **devs);
5738 devs[i]->target = target;
5739 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005740 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005741 }
5742 /* add deviates into the deviation's list of deviates */
5743 for (d = dev->deviates; d; d = d->next) {
5744 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5745 *dp_new = d;
5746 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5747 devs[i]->not_supported = 1;
5748 }
5749 }
Radek Krejci327de162019-06-14 12:52:07 +02005750
5751 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01005752 }
5753
5754 /* MACROS for deviates checking */
5755#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5756 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
Radek Krejci327de162019-06-14 12:52:07 +02005757 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 +01005758 goto cleanup; \
5759 }
5760
5761#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5762 if (LY_ARRAY_SIZE(ARRAY) > MAX) { \
Radek Krejci327de162019-06-14 12:52:07 +02005763 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%u) %s properties.", \
5764 lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005765 goto cleanup; \
5766 }
5767
Radek Krejcia1911222019-07-22 17:24:50 +02005768
Radek Krejciccd20f12019-02-15 14:12:27 +01005769#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejcia1911222019-07-22 17:24:50 +02005770 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
5771 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5772 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5773 PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5774 goto cleanup; \
5775 }
5776
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005777#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005778 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejcia1911222019-07-22 17:24:50 +02005779 int dynamic_ = 0; const char *val_; \
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005780 val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \
5781 lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005782 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejcia1911222019-07-22 17:24:50 +02005783 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5784 if (dynamic_) {free((void*)val_);} \
Radek Krejciccd20f12019-02-15 14:12:27 +01005785 goto cleanup; \
5786 }
5787
Radek Krejci551b12c2019-02-19 16:11:21 +01005788#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5789 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5790 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005791 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5792 PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005793 goto cleanup; \
5794 }
5795
Radek Krejciccd20f12019-02-15 14:12:27 +01005796#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5797 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
Radek Krejci327de162019-06-14 12:52:07 +02005798 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005799 goto cleanup; \
5800 }
5801
Radek Krejci551b12c2019-02-19 16:11:21 +01005802#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5803 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5804 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005805 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d_rpl->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005806 goto cleanup; \
5807 }
5808
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005809#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5810 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5811 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5812 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5813 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 +01005814 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005815 if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005816 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005817 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5818 PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005819 goto cleanup; \
5820 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005821 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5822 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5823 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5824 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
5825 (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005826 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005827 if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
5828 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5829 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005830 }
5831
5832 /* apply deviations */
5833 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005834 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target;
5835 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target;
5836 struct ly_err_item *err = NULL;
5837
5838 dflt = NULL;
5839 changed_type = 0;
5840
Radek Krejci327de162019-06-14 12:52:07 +02005841 lysc_update_path(ctx, NULL, devs[u]->nodeid);
5842
Radek Krejcif538ce52019-03-05 10:46:14 +01005843 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5844 /* fix the target pointer in case of RPC's/action's input/output */
5845 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5846 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5847 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5848 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5849 }
5850 }
5851
Radek Krejciccd20f12019-02-15 14:12:27 +01005852 /* not-supported */
5853 if (devs[u]->not_supported) {
5854 if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) {
5855 LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.",
5856 LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid);
5857 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005858
5859#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5860 if (devs[u]->target->parent) { \
5861 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
5862 } else { \
5863 ARRAY = devs[u]->target->module->compiled->ARRAY; \
5864 } \
5865 LY_ARRAY_FOR(ARRAY, x) { \
5866 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
5867 } \
5868 if (x < LY_ARRAY_SIZE(ARRAY)) { \
5869 FREEFUNC(ctx->ctx, &ARRAY[x]); \
5870 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \
5871 LY_ARRAY_DECREMENT(ARRAY); \
5872 }
5873
Radek Krejcif538ce52019-03-05 10:46:14 +01005874 if (devs[u]->target->nodetype == LYS_ACTION) {
5875 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5876 /* remove RPC's/action's input */
5877 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
5878 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005879 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
5880 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005881 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5882 /* remove RPC's/action's output */
5883 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
5884 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005885 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
5886 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005887 } else {
5888 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005889 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005890 }
5891 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005892 /* remove Notification */
5893 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005894 } else {
5895 /* remove the target node */
5896 lysc_disconnect(devs[u]->target);
5897 lysc_node_free(ctx->ctx, devs[u]->target);
5898 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005899
Radek Krejci474f9b82019-07-24 11:36:37 +02005900 /* mark the context for later re-compilation of objects that could reference the curently removed node */
5901 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
Radek Krejciccd20f12019-02-15 14:12:27 +01005902 continue;
5903 }
5904
5905 /* list of deviates (not-supported is not present in the list) */
5906 LY_ARRAY_FOR(devs[u]->deviates, v) {
5907 d = devs[u]->deviates[v];
5908
5909 switch (d->mod) {
5910 case LYS_DEV_ADD:
5911 d_add = (struct lysp_deviate_add*)d;
5912 /* [units-stmt] */
5913 if (d_add->units) {
5914 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5915 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
5916
5917 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5918 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5919 }
5920
5921 /* *must-stmt */
5922 if (d_add->musts) {
5923 switch (devs[u]->target->nodetype) {
5924 case LYS_CONTAINER:
5925 case LYS_LIST:
5926 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005927 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005928 break;
5929 case LYS_LEAF:
5930 case LYS_LEAFLIST:
5931 case LYS_ANYDATA:
5932 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005933 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005934 break;
5935 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005936 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005937 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005938 break;
5939 case LYS_ACTION:
5940 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5941 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005942 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005943 break;
5944 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5945 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005946 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005947 break;
5948 }
5949 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005950 default:
5951 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005952 lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005953 goto cleanup;
5954 }
5955 }
5956
5957 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005958 if (d_add->uniques) {
5959 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5960 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
5961 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005962
5963 /* *default-stmt */
5964 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005965 switch (devs[u]->target->nodetype) {
5966 case LYS_LEAF:
5967 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005968 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 +02005969 if (leaf->dflt) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005970 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02005971 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02005972 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5973 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5974 } else {
5975 /* prepare new default value storage */
5976 leaf->dflt = calloc(1, sizeof *leaf->dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01005977 }
Radek Krejcia1911222019-07-22 17:24:50 +02005978 dflt = d_add->dflts[0];
5979 /* parsing is done at the end after possible replace of the leaf's type */
5980
Radek Krejci551b12c2019-02-19 16:11:21 +01005981 /* mark the new default values as leaf's own */
5982 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005983 break;
5984 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02005985 if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005986 /* first, remove the default value taken from the type */
Radek Krejcia1911222019-07-22 17:24:50 +02005987 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02005988 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02005989 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
5990 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
5991 free(llist->dflts[x]);
Radek Krejciccd20f12019-02-15 14:12:27 +01005992 }
Radek Krejcia1911222019-07-22 17:24:50 +02005993 LY_ARRAY_FREE(llist->dflts);
5994 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005995 LY_ARRAY_FREE(llist->dflts_mods);
5996 llist->dflts_mods = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005997 }
5998 /* add new default value(s) */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005999 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006000 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
6001 for (x = y = LY_ARRAY_SIZE(llist->dflts);
Radek Krejciccd20f12019-02-15 14:12:27 +01006002 x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006003 LY_ARRAY_INCREMENT(llist->dflts_mods);
6004 llist->dflts_mods[x] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006005 LY_ARRAY_INCREMENT(llist->dflts);
6006 llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]);
6007 llist->dflts[x]->realtype = llist->type;
6008 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 +02006009 LY_TYPE_OPTS_INCOMPLETE_DATA |LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, lys_resolve_prefix,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006010 (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006011 llist->dflts[x]->realtype->refcount++;
6012 if (err) {
6013 ly_err_print(err);
6014 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6015 "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).",
6016 d_add->dflts[x - y], err->msg);
6017 ly_err_free(err);
6018 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006019 if (rc == LY_EINCOMPLETE) {
6020 /* postpone default compilation when the tree is complete */
6021 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6022
6023 /* but in general result is so far ok */
6024 rc = LY_SUCCESS;
6025 }
Radek Krejcia1911222019-07-22 17:24:50 +02006026 LY_CHECK_GOTO(rc, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006027 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006028 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01006029 devs[u]->target->flags |= LYS_SET_DFLT;
6030 break;
6031 case LYS_CHOICE:
6032 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
6033 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
6034 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
6035 * to allow making the default case even the augmented case from the deviating module */
Radek Krejci327de162019-06-14 12:52:07 +02006036 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 +01006037 goto cleanup;
6038 }
6039 break;
6040 default:
6041 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006042 lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006043 goto cleanup;
6044 }
6045 }
6046
6047 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006048 if (d_add->flags & LYS_CONFIG_MASK) {
6049 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006050 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006051 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
6052 goto cleanup;
6053 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006054 if (devs[u]->flags) {
Radek Krejci327de162019-06-14 12:52:07 +02006055 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
6056 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006057 }
Radek Krejci93dcc392019-02-19 10:43:38 +01006058 if (devs[u]->target->flags & LYS_SET_CONFIG) {
6059 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006060 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
6061 devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
Radek Krejci93dcc392019-02-19 10:43:38 +01006062 goto cleanup;
6063 }
Radek Krejci327de162019-06-14 12:52:07 +02006064 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006065 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006066
6067 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006068 if (d_add->flags & LYS_MAND_MASK) {
6069 if (devs[u]->target->flags & LYS_MAND_MASK) {
6070 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006071 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
6072 devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
Radek Krejcif1421c22019-02-19 13:05:20 +01006073 goto cleanup;
6074 }
Radek Krejci327de162019-06-14 12:52:07 +02006075 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006076 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006077
6078 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006079 if (d_add->flags & LYS_SET_MIN) {
6080 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6081 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6082 /* change value */
6083 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
6084 } else if (devs[u]->target->nodetype == LYS_LIST) {
6085 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6086 /* change value */
6087 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
6088 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006089 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006090 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
6091 goto cleanup;
6092 }
6093 if (d_add->min) {
6094 devs[u]->target->flags |= LYS_MAND_TRUE;
6095 }
6096 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006097
6098 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006099 if (d_add->flags & LYS_SET_MAX) {
6100 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6101 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6102 /* change value */
6103 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
6104 } else if (devs[u]->target->nodetype == LYS_LIST) {
6105 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6106 /* change value */
6107 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
6108 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006109 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006110 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
6111 goto cleanup;
6112 }
6113 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006114
6115 break;
6116 case LYS_DEV_DELETE:
6117 d_del = (struct lysp_deviate_del*)d;
6118
6119 /* [units-stmt] */
6120 if (d_del->units) {
6121 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Radek Krejcia1911222019-07-22 17:24:50 +02006122 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units);
6123 if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) {
6124 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6125 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
6126 d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6127 goto cleanup;
6128 }
6129 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6130 ((struct lysc_node_leaf*)devs[u]->target)->units = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01006131 }
6132
6133 /* *must-stmt */
6134 if (d_del->musts) {
6135 switch (devs[u]->target->nodetype) {
6136 case LYS_CONTAINER:
6137 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006138 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006139 break;
6140 case LYS_LEAF:
6141 case LYS_LEAFLIST:
6142 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006143 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006144 break;
6145 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02006146 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006147 break;
6148 case LYS_ACTION:
6149 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
6150 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6151 break;
6152 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
6153 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6154 break;
6155 }
6156 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01006157 default:
6158 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006159 lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006160 goto cleanup;
6161 }
6162 }
6163
6164 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01006165 if (d_del->uniques) {
6166 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
6167 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
6168 LY_ARRAY_FOR(d_del->uniques, x) {
6169 LY_ARRAY_FOR(list->uniques, z) {
6170 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
6171 nodeid = strpbrk(name, " \t\n");
6172 if (nodeid) {
6173 if (strncmp(name, list->uniques[z][y]->name, nodeid - name)
6174 || list->uniques[z][y]->name[nodeid - name] != '\0') {
6175 break;
6176 }
6177 while (isspace(*nodeid)) {
6178 ++nodeid;
6179 }
6180 } else {
6181 if (strcmp(name, list->uniques[z][y]->name)) {
6182 break;
6183 }
6184 }
6185 }
6186 if (!name) {
6187 /* complete match - remove the unique */
6188 LY_ARRAY_DECREMENT(list->uniques);
6189 LY_ARRAY_FREE(list->uniques[z]);
6190 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques));
6191 --z;
6192 break;
6193 }
6194 }
6195 if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) {
6196 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006197 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
6198 d_del->uniques[x]);
Radek Krejci7af64242019-02-18 13:07:53 +01006199 goto cleanup;
6200 }
6201 }
6202 if (!LY_ARRAY_SIZE(list->uniques)) {
6203 LY_ARRAY_FREE(list->uniques);
6204 list->uniques = NULL;
6205 }
6206 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006207
6208 /* *default-stmt */
6209 if (d_del->dflts) {
6210 switch (devs[u]->target->nodetype) {
6211 case LYS_LEAF:
6212 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6213 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6214 dflt, "deleting", "default", d_del->dflts[0]);
6215
Radek Krejcia1911222019-07-22 17:24:50 +02006216 /* check that the values matches */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006217 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &i);
Radek Krejcia1911222019-07-22 17:24:50 +02006218 if (strcmp(dflt, d_del->dflts[0])) {
6219 if (i) {
6220 free((char*)dflt);
6221 }
6222 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6223 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
6224 d_del->dflts[0], dflt);
6225 goto cleanup;
6226 }
6227 if (i) {
6228 free((char*)dflt);
6229 }
6230 dflt = NULL;
6231
Radek Krejci474f9b82019-07-24 11:36:37 +02006232 /* update the list of incomplete default values if needed */
6233 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6234
Radek Krejcia1911222019-07-22 17:24:50 +02006235 /* remove the default specification */
6236 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6237 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6238 free(leaf->dflt);
6239 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006240 leaf->dflt_mod = NULL;
Radek Krejci551b12c2019-02-19 16:11:21 +01006241 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006242 break;
6243 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006244 DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]);
6245 LY_ARRAY_FOR(d_del->dflts, x) {
6246 LY_ARRAY_FOR(llist->dflts, y) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006247 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 +02006248 if (!strcmp(dflt, d_del->dflts[x])) {
6249 if (i) {
6250 free((char*)dflt);
6251 }
6252 dflt = NULL;
6253 break;
6254 }
6255 if (i) {
6256 free((char*)dflt);
6257 }
6258 dflt = NULL;
6259 }
6260 if (y == LY_ARRAY_SIZE(llist->dflts)) {
6261 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
6262 "which does not match any of the target's property values.", d_del->dflts[x]);
6263 goto cleanup;
6264 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006265
6266 /* update the list of incomplete default values if needed */
6267 lysc_incomplete_dflts_remove(ctx, llist->dflts[y]);
6268
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006269 LY_ARRAY_DECREMENT(llist->dflts_mods);
Radek Krejcia1911222019-07-22 17:24:50 +02006270 LY_ARRAY_DECREMENT(llist->dflts);
6271 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
6272 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
6273 free(llist->dflts[y]);
6274 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_SIZE(llist->dflts) - y) * (sizeof *llist->dflts));
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006275 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 +02006276 }
6277 if (!LY_ARRAY_SIZE(llist->dflts)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006278 LY_ARRAY_FREE(llist->dflts_mods);
6279 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006280 LY_ARRAY_FREE(llist->dflts);
6281 llist->dflts = NULL;
6282 llist->flags &= ~LYS_SET_DFLT;
Radek Krejci551b12c2019-02-19 16:11:21 +01006283 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006284 break;
6285 case LYS_CHOICE:
6286 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6287 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
6288 nodeid = d_del->dflts[0];
Radek Krejcib4a4a272019-06-10 12:44:52 +02006289 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006290 if (prefix) {
6291 /* use module prefixes from the deviation module to match the module of the default case */
6292 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
6293 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006294 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6295 "The prefix does not match any imported module of the deviation module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006296 goto cleanup;
6297 }
6298 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
6299 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006300 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6301 "The prefix does not match the default case's module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006302 goto cleanup;
6303 }
6304 }
6305 /* else {
6306 * strictly, the default prefix would point to the deviation module, but the value should actually
6307 * match the default string in the original module (usually unprefixed), so in this case we do not check
6308 * the module of the default case, just matching its name */
6309 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
6310 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006311 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
6312 d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01006313 goto cleanup;
6314 }
6315 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
6316 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
6317 break;
6318 default:
6319 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006320 lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006321 goto cleanup;
6322 }
6323 }
6324
6325 break;
6326 case LYS_DEV_REPLACE:
6327 d_rpl = (struct lysp_deviate_rpl*)d;
6328
6329 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01006330 if (d_rpl->type) {
6331 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6332 /* type is mandatory, so checking for its presence is not necessary */
6333 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
Radek Krejcia1911222019-07-22 17:24:50 +02006334
6335 if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) {
6336 /* the target has default from the previous type - remove it */
6337 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006338 /* update the list of incomplete default values if needed */
6339 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6340
Radek Krejcia1911222019-07-22 17:24:50 +02006341 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6342 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6343 free(leaf->dflt);
6344 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006345 leaf->dflt_mod = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006346 } else { /* LYS_LEAFLIST */
6347 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006348 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006349 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6350 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6351 free(llist->dflts[x]);
6352 }
6353 LY_ARRAY_FREE(llist->dflts);
6354 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006355 LY_ARRAY_FREE(llist->dflts_mods);
6356 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006357 }
6358 }
6359 if (!leaf->dflt) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006360 /* there is no default value, do not set changed_type after type compilation
6361 * which is used to recompile the default value */
Radek Krejcia1911222019-07-22 17:24:50 +02006362 changed_type = -1;
6363 }
Radek Krejciec4da802019-05-02 13:02:41 +02006364 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 +02006365 changed_type++;
Radek Krejci33f72892019-02-21 10:36:58 +01006366 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006367
6368 /* [units-stmt] */
6369 if (d_rpl->units) {
6370 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6371 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
6372 units, "replacing", "units", d_rpl->units);
6373
6374 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6375 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6376 }
6377
6378 /* [default-stmt] */
6379 if (d_rpl->dflt) {
6380 switch (devs[u]->target->nodetype) {
6381 case LYS_LEAF:
6382 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6383 dflt, "replacing", "default", d_rpl->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006384 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02006385 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006386 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6387 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6388 dflt = d_rpl->dflt;
6389 /* parsing is done at the end after possible replace of the leaf's type */
Radek Krejciccd20f12019-02-15 14:12:27 +01006390 break;
6391 case LYS_CHOICE:
6392 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
Radek Krejci327de162019-06-14 12:52:07 +02006393 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 +01006394 goto cleanup;
6395 }
6396 break;
6397 default:
6398 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006399 lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006400 goto cleanup;
6401 }
6402 }
6403
6404 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006405 if (d_rpl->flags & LYS_CONFIG_MASK) {
6406 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006407 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006408 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
6409 goto cleanup;
6410 }
6411 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
Radek Krejci327de162019-06-14 12:52:07 +02006412 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejci93dcc392019-02-19 10:43:38 +01006413 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
6414 goto cleanup;
6415 }
Radek Krejci327de162019-06-14 12:52:07 +02006416 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006417 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006418
6419 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006420 if (d_rpl->flags & LYS_MAND_MASK) {
6421 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
Radek Krejci327de162019-06-14 12:52:07 +02006422 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejcif1421c22019-02-19 13:05:20 +01006423 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6424 goto cleanup;
6425 }
Radek Krejci327de162019-06-14 12:52:07 +02006426 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006427 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006428
6429 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006430 if (d_rpl->flags & LYS_SET_MIN) {
6431 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6432 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6433 /* change value */
6434 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
6435 } else if (devs[u]->target->nodetype == LYS_LIST) {
6436 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6437 /* change value */
6438 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
6439 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006440 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006441 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
6442 goto cleanup;
6443 }
6444 if (d_rpl->min) {
6445 devs[u]->target->flags |= LYS_MAND_TRUE;
6446 }
6447 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006448
6449 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006450 if (d_rpl->flags & LYS_SET_MAX) {
6451 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6452 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6453 /* change value */
6454 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6455 } else if (devs[u]->target->nodetype == LYS_LIST) {
6456 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6457 /* change value */
6458 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6459 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006460 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006461 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
6462 goto cleanup;
6463 }
6464 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006465
6466 break;
6467 default:
6468 LOGINT(ctx->ctx);
6469 goto cleanup;
6470 }
6471 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006472
Radek Krejci33f72892019-02-21 10:36:58 +01006473 /* final check when all deviations of a single target node are applied */
6474
Radek Krejci551b12c2019-02-19 16:11:21 +01006475 /* check min-max compatibility */
6476 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6477 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
6478 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
6479 } else if (devs[u]->target->nodetype == LYS_LIST) {
6480 min = ((struct lysc_node_list*)devs[u]->target)->min;
6481 max = ((struct lysc_node_list*)devs[u]->target)->max;
6482 } else {
6483 min = max = 0;
6484 }
6485 if (min > max) {
6486 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 +02006487 "after deviation: min value %u is bigger than max value %u.", min, max);
Radek Krejci551b12c2019-02-19 16:11:21 +01006488 goto cleanup;
6489 }
6490
Radek Krejcia1911222019-07-22 17:24:50 +02006491 if (dflt) {
6492 /* parse added/changed default value after possible change of the type */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006493 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006494 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006495 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6496 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006497 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006498 leaf->dflt->realtype->refcount++;
6499 if (err) {
6500 ly_err_print(err);
6501 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6502 "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg);
6503 ly_err_free(err);
6504 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006505 if (rc == LY_EINCOMPLETE) {
6506 /* postpone default compilation when the tree is complete */
6507 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6508
6509 /* but in general result is so far ok */
6510 rc = LY_SUCCESS;
6511 }
Radek Krejcia1911222019-07-22 17:24:50 +02006512 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006513 } else if (changed_type) {
Radek Krejcia1911222019-07-22 17:24:50 +02006514 /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */
6515 int dynamic;
6516 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006517 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &dynamic);
Radek Krejci474f9b82019-07-24 11:36:37 +02006518
6519 /* update the list of incomplete default values if needed */
6520 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6521
6522 /* remove the previous default */
Radek Krejcia1911222019-07-22 17:24:50 +02006523 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6524 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6525 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006526 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6527 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006528 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006529 leaf->dflt->realtype->refcount++;
6530 if (err) {
6531 ly_err_print(err);
6532 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6533 "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg);
6534 ly_err_free(err);
6535 }
6536 if (dynamic) {
6537 free((void*)dflt);
6538 }
Radek Krejcia1911222019-07-22 17:24:50 +02006539 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006540 if (rc == LY_EINCOMPLETE) {
6541 /* postpone default compilation when the tree is complete */
6542 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6543
6544 /* but in general result is so far ok */
6545 rc = LY_SUCCESS;
6546 }
6547 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006548 } else { /* LYS_LEAFLIST */
6549 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006550 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 +02006551 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6552 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6553 llist->dflts[x]->realtype = llist->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006554 rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt),
6555 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006556 lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL,
6557 llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006558 llist->dflts[x]->realtype->refcount++;
6559 if (err) {
6560 ly_err_print(err);
6561 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6562 "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).",
6563 dflt, err->msg);
6564 ly_err_free(err);
6565 }
6566 if (dynamic) {
6567 free((void*)dflt);
6568 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006569 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006570 if (rc == LY_EINCOMPLETE) {
6571 /* postpone default compilation when the tree is complete */
6572 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6573
6574 /* but in general result is so far ok */
6575 rc = LY_SUCCESS;
6576 }
Radek Krejcia1911222019-07-22 17:24:50 +02006577 LY_CHECK_GOTO(rc, cleanup);
6578 }
6579 }
6580 }
6581
Radek Krejci551b12c2019-02-19 16:11:21 +01006582 /* check mandatory - default compatibility */
6583 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
6584 && (devs[u]->target->flags & LYS_SET_DFLT)
6585 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6586 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006587 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(devs[u]->target->nodetype));
Radek Krejci551b12c2019-02-19 16:11:21 +01006588 goto cleanup;
6589 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
6590 && ((struct lysc_node_choice*)devs[u]->target)->dflt
6591 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
Radek Krejci327de162019-06-14 12:52:07 +02006592 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 +01006593 goto cleanup;
6594 }
6595 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6596 /* mandatory node under a default case */
6597 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006598 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6599 lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
Radek Krejci551b12c2019-02-19 16:11:21 +01006600 goto cleanup;
6601 }
Radek Krejci33f72892019-02-21 10:36:58 +01006602
Radek Krejci327de162019-06-14 12:52:07 +02006603 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006604 }
6605
Radek Krejci327de162019-06-14 12:52:07 +02006606 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006607 ret = LY_SUCCESS;
6608
6609cleanup:
6610 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6611 LY_ARRAY_FREE(devs[u]->deviates);
6612 free(devs[u]);
6613 }
6614 free(devs);
6615 ly_set_erase(&targets, NULL);
6616 ly_set_erase(&devs_p, NULL);
6617
6618 return ret;
6619}
6620
Radek Krejcib56c7502019-02-13 14:19:54 +01006621/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006622 * @brief Compile the given YANG submodule into the main module.
6623 * @param[in] ctx Compile context
6624 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006625 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6626 */
6627LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006628lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006629{
6630 unsigned int u;
6631 LY_ERR ret = LY_SUCCESS;
6632 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006633 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006634 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006635 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006636
Radek Krejci0af46292019-01-11 16:02:31 +01006637 if (!mainmod->mod->off_features) {
6638 /* features are compiled directly into the compiled module structure,
6639 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6640 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci0935f412019-08-20 16:15:18 +02006641 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features);
6642 LY_CHECK_GOTO(ret, error);
6643 }
6644 if (!mainmod->mod->off_extensions) {
6645 /* extensions are compiled directly into the compiled module structure, compilation is finished in the main module (lys_compile()). */
6646 ret = lys_extension_precompile(ctx, NULL, NULL, submod->extensions, &mainmod->extensions);
Radek Krejci0af46292019-01-11 16:02:31 +01006647 LY_CHECK_GOTO(ret, error);
6648 }
6649
Radek Krejci327de162019-06-14 12:52:07 +02006650 lysc_update_path(ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006651 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, u, lys_compile_identity, ret, error);
Radek Krejci327de162019-06-14 12:52:07 +02006652 lysc_update_path(ctx, NULL, NULL);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006653
Radek Krejci474f9b82019-07-24 11:36:37 +02006654 /* data nodes */
6655 LY_LIST_FOR(submod->data, node_p) {
6656 ret = lys_compile_node(ctx, node_p, NULL, 0);
6657 LY_CHECK_GOTO(ret, error);
6658 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006659
Radek Krejciec4da802019-05-02 13:02:41 +02006660 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6661 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006662
Radek Krejcid05cbd92018-12-05 14:26:40 +01006663error:
6664 return ret;
6665}
6666
Radek Krejci19a96102018-11-15 13:38:09 +01006667LY_ERR
6668lys_compile(struct lys_module *mod, int options)
6669{
6670 struct lysc_ctx ctx = {0};
6671 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01006672 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01006673 struct lysp_module *sp;
6674 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01006675 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02006676 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01006677 struct lys_module *m;
Radek Krejcicdfecd92018-11-26 11:27:32 +01006678 unsigned int u, v;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006679 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01006680
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006681 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01006682
6683 if (!mod->implemented) {
6684 /* just imported modules are not compiled */
6685 return LY_SUCCESS;
6686 }
6687
Radek Krejci19a96102018-11-15 13:38:09 +01006688 sp = mod->parsed;
6689
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006690 ctx.ctx = mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01006691 ctx.mod = mod;
Radek Krejcie86bf772018-12-14 11:39:53 +01006692 ctx.mod_def = mod;
Radek Krejciec4da802019-05-02 13:02:41 +02006693 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02006694 ctx.path_len = 1;
6695 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01006696
6697 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006698 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
6699 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01006700
Radek Krejciec4da802019-05-02 13:02:41 +02006701 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006702 LY_ARRAY_FOR(sp->includes, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006703 ret = lys_compile_submodule(&ctx, &sp->includes[u]);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006704 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6705 }
Radek Krejci0935f412019-08-20 16:15:18 +02006706
6707 /* features */
Radek Krejci0af46292019-01-11 16:02:31 +01006708 if (mod->off_features) {
6709 /* there is already precompiled array of features */
6710 mod_c->features = mod->off_features;
6711 mod->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01006712 } else {
6713 /* features are compiled directly into the compiled module structure,
6714 * 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 +02006715 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006716 LY_CHECK_GOTO(ret, error);
6717 }
6718 /* finish feature compilation, not only for the main module, but also for the submodules.
6719 * Due to possible forward references, it must be done when all the features (including submodules)
6720 * are present. */
6721 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006722 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006723 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6724 }
Radek Krejci327de162019-06-14 12:52:07 +02006725 lysc_update_path(&ctx, NULL, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01006726 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02006727 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01006728 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006729 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006730 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6731 }
Radek Krejci327de162019-06-14 12:52:07 +02006732 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01006733 }
Radek Krejci327de162019-06-14 12:52:07 +02006734 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01006735
Radek Krejci0935f412019-08-20 16:15:18 +02006736 /* extensions */
6737 /* 2-steps: a) prepare compiled structures and ... */
6738 if (mod->off_extensions) {
6739 /* there is already precompiled array of extension definitions */
6740 mod_c->extensions = mod->off_extensions;
6741 mod->off_extensions = NULL;
6742 } else {
6743 /* extension definitions are compiled directly into the compiled module structure */
6744 ret = lys_extension_precompile(&ctx, NULL, NULL, sp->extensions, &mod_c->extensions);
6745 }
6746 /* ... b) connect the extension definitions with the appropriate extension plugins */
6747 lys_compile_extension_plugins(mod_c->extensions);
6748
6749 /* identities */
Radek Krejci327de162019-06-14 12:52:07 +02006750 lysc_update_path(&ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006751 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006752 if (sp->identities) {
6753 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
6754 }
Radek Krejci327de162019-06-14 12:52:07 +02006755 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01006756
Radek Krejci95710c92019-02-11 15:49:55 +01006757 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01006758 LY_LIST_FOR(sp->data, node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02006759 ret = lys_compile_node(&ctx, node_p, NULL, 0);
Radek Krejci19a96102018-11-15 13:38:09 +01006760 LY_CHECK_GOTO(ret, error);
6761 }
Radek Krejci95710c92019-02-11 15:49:55 +01006762
Radek Krejciec4da802019-05-02 13:02:41 +02006763 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6764 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01006765
Radek Krejci95710c92019-02-11 15:49:55 +01006766 /* augments - sort first to cover augments augmenting other augments */
Radek Krejci3641f562019-02-13 15:38:40 +01006767 ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments);
Radek Krejci95710c92019-02-11 15:49:55 +01006768 LY_CHECK_GOTO(ret, error);
6769 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006770 ret = lys_compile_augment(&ctx, augments[u], NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006771 LY_CHECK_GOTO(ret, error);
6772 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006773
Radek Krejci474f9b82019-07-24 11:36:37 +02006774 /* deviations TODO cover deviations from submodules */
Radek Krejciec4da802019-05-02 13:02:41 +02006775 ret = lys_compile_deviations(&ctx, sp);
Radek Krejciccd20f12019-02-15 14:12:27 +01006776 LY_CHECK_GOTO(ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006777
Radek Krejci0935f412019-08-20 16:15:18 +02006778 /* extension instances TODO cover extension instances from submodules */
6779 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006780
Radek Krejcia3045382018-11-22 14:30:31 +01006781 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01006782 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
6783 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
6784 * 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 +01006785 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01006786 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01006787 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
6788 if (type->basetype == LY_TYPE_LEAFREF) {
6789 /* validate the path */
Radek Krejci412ddfa2018-11-23 11:44:11 +01006790 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), (struct lysc_type_leafref*)type);
Radek Krejcia3045382018-11-22 14:30:31 +01006791 LY_CHECK_GOTO(ret, error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01006792 } else if (type->basetype == LY_TYPE_UNION) {
6793 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
6794 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
6795 /* validate the path */
6796 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
6797 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]);
6798 LY_CHECK_GOTO(ret, error);
6799 }
6800 }
Radek Krejcia3045382018-11-22 14:30:31 +01006801 }
6802 }
6803 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01006804 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01006805 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01006806 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
6807 if (type->basetype == LY_TYPE_LEAFREF) {
6808 /* store pointer to the real type */
6809 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
6810 typeiter->basetype == LY_TYPE_LEAFREF;
6811 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
6812 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01006813 } else if (type->basetype == LY_TYPE_UNION) {
6814 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
6815 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
6816 /* store pointer to the real type */
6817 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
6818 typeiter->basetype == LY_TYPE_LEAFREF;
6819 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
6820 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
6821 }
6822 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01006823 }
6824 }
6825 }
Radek Krejciec4da802019-05-02 13:02:41 +02006826
Radek Krejci474f9b82019-07-24 11:36:37 +02006827 /* finish incomplete default values compilation */
6828 for (u = 0; u < ctx.dflts.count; ++u) {
6829 struct ly_err_item *err = NULL;
6830 struct lysc_incomplete_dflt *r = ctx.dflts.objs[u];
6831 ret = r->dflt->realtype->plugin->store(ctx.ctx, r->dflt->realtype, r->dflt->canonized, strlen(r->dflt->canonized),
6832 LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix,
6833 (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err);
6834 if (err) {
6835 ly_err_print(err);
6836 ctx.path[0] = '\0';
6837 lysc_path(r->context_node, LY_PATH_LOG, ctx.path, LYSC_CTX_BUFSIZE);
6838 LOGVAL(ctx.ctx, LY_VLOG_STR, ctx.path, LYVE_SEMANTICS,
6839 "Invalid default - value does not fit the type (%s).", err->msg);
6840 ly_err_free(err);
6841 }
6842 LY_CHECK_GOTO(ret, error);
6843 }
6844
Radek Krejcif2de0ed2019-05-02 14:13:18 +02006845 /* validate non-instantiated groupings from the parsed schema,
6846 * without it we would accept even the schemas with invalid grouping specification */
6847 ctx.options |= LYSC_OPT_GROUPING;
6848 LY_ARRAY_FOR(sp->groupings, u) {
6849 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
6850 LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u])) != LY_SUCCESS, error);
6851 }
6852 }
6853 LY_LIST_FOR(sp->data, node_p) {
6854 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
6855 LY_ARRAY_FOR(grps, u) {
6856 if (!(grps[u].flags & LYS_USED_GRP)) {
6857 LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &grps[u])) != LY_SUCCESS, error);
6858 }
6859 }
6860 }
6861
Radek Krejci474f9b82019-07-24 11:36:37 +02006862 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
6863 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
6864 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
6865 }
6866
Radek Krejci1c0c3442019-07-23 16:08:47 +02006867 ly_set_erase(&ctx.dflts, free);
Radek Krejcia3045382018-11-22 14:30:31 +01006868 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01006869 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02006870 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006871 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01006872
Radek Krejciec4da802019-05-02 13:02:41 +02006873 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejci19a96102018-11-15 13:38:09 +01006874 lysp_module_free(mod->parsed);
6875 ((struct lys_module*)mod)->parsed = NULL;
6876 }
6877
Radek Krejciec4da802019-05-02 13:02:41 +02006878 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01006879 /* remove flag of the modules implemented by dependency */
6880 for (u = 0; u < ctx.ctx->list.count; ++u) {
6881 m = ctx.ctx->list.objs[u];
6882 if (m->implemented == 2) {
6883 m->implemented = 1;
6884 }
6885 }
6886 }
6887
Radek Krejci19a96102018-11-15 13:38:09 +01006888 ((struct lys_module*)mod)->compiled = mod_c;
6889 return LY_SUCCESS;
6890
6891error:
Radek Krejci95710c92019-02-11 15:49:55 +01006892 lys_feature_precompile_revert(&ctx, mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02006893 ly_set_erase(&ctx.dflts, free);
Radek Krejcia3045382018-11-22 14:30:31 +01006894 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01006895 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02006896 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006897 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01006898 lysc_module_free(mod_c, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006899 mod->compiled = NULL;
6900
6901 /* revert compilation of modules implemented by dependency */
6902 for (u = 0; u < ctx.ctx->list.count; ++u) {
6903 m = ctx.ctx->list.objs[u];
6904 if (m->implemented == 2) {
6905 /* revert features list to the precompiled state */
6906 lys_feature_precompile_revert(&ctx, m);
6907 /* mark module as imported-only / not-implemented */
6908 m->implemented = 0;
6909 /* free the compiled version of the module */
6910 lysc_module_free(m->compiled, NULL);
6911 m->compiled = NULL;
6912 }
6913 }
6914
Radek Krejci19a96102018-11-15 13:38:09 +01006915 return ret;
6916}