blob: d79c81bd2469f2caf8e03fbb1955ace5ea492e9a [file] [log] [blame]
Radek Krejci19a96102018-11-15 13:38:09 +01001/**
2 * @file tree_schema.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema tree implementation
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#include "common.h"
16
Radek Krejcie7b95092019-05-15 11:03:07 +020017#include <assert.h>
Radek Krejci19a96102018-11-15 13:38:09 +010018#include <ctype.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include <stddef.h>
20#include <stdint.h>
Radek Krejci19a96102018-11-15 13:38:09 +010021#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdlib.h>
23#include <string.h>
Radek Krejci19a96102018-11-15 13:38:09 +010024
Radek Krejcie7b95092019-05-15 11:03:07 +020025#include "dict.h"
26#include "log.h"
Radek Krejci0935f412019-08-20 16:15:18 +020027#include "plugins_exts.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "set.h"
29#include "plugins_types.h"
Radek Krejci0935f412019-08-20 16:15:18 +020030#include "plugins_exts_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020031#include "tree.h"
32#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010033#include "tree_schema_internal.h"
34#include "xpath.h"
35
36/**
37 * @brief Duplicate string into dictionary
38 * @param[in] CTX libyang context of the dictionary.
39 * @param[in] ORIG String to duplicate.
40 * @param[out] DUP Where to store the result.
41 */
42#define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);}
43
Radek Krejciec4da802019-05-02 13:02:41 +020044#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010045 if (ARRAY_P) { \
46 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010047 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
Radek Krejci19a96102018-11-15 13:38:09 +010048 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
49 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020050 RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010051 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
52 } \
53 }
54
Radek Krejciec4da802019-05-02 13:02:41 +020055#define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010056 if (ARRAY_P) { \
57 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
58 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
59 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
60 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020061 RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010062 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
63 } \
64 }
65
Radek Krejci0935f412019-08-20 16:15:18 +020066#define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \
67 if (EXTS_P) { \
68 LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_SIZE(EXTS_P), RET, GOTO); \
69 for (uint32_t __exts_iter = 0, __array_offset = LY_ARRAY_SIZE(EXT_C); __exts_iter < LY_ARRAY_SIZE(EXTS_P); ++__exts_iter) { \
70 LY_ARRAY_INCREMENT(EXT_C); \
71 RET = lys_compile_ext(CTX, &(EXTS_P)[__exts_iter], &(EXT_C)[__exts_iter + __array_offset], PARENT, PARENT_TYPE); \
72 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
73 } \
74 }
75
Radek Krejciec4da802019-05-02 13:02:41 +020076#define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejcid05cbd92018-12-05 14:26:40 +010077 if (ARRAY_P) { \
78 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
79 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
80 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
81 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020082 RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejci19a96102018-11-15 13:38:09 +010083 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
84 } \
85 }
86
Radek Krejciec4da802019-05-02 13:02:41 +020087#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010088 if (MEMBER_P) { \
89 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
90 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
Radek Krejciec4da802019-05-02 13:02:41 +020091 RET = FUNC(CTX, MEMBER_P, MEMBER_C); \
Radek Krejci19a96102018-11-15 13:38:09 +010092 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
93 }
94
Radek Krejciec4da802019-05-02 13:02:41 +020095#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \
Radek Krejci00b874b2019-02-12 10:54:50 +010096 if (MEMBER_P) { \
97 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
98 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
99 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +0200100 RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100101 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
102 }
103
Radek Krejcid05cbd92018-12-05 14:26:40 +0100104#define COMPILE_CHECK_UNIQUENESS(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
105 if (ARRAY) { \
Radek Krejci0af46292019-01-11 16:02:31 +0100106 for (unsigned int u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \
107 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100108 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
109 return LY_EVALID; \
110 } \
111 } \
112 }
113
Radek Krejci19a96102018-11-15 13:38:09 +0100114static struct lysc_ext_instance *
115lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
116{
Radek Krejcie7b95092019-05-15 11:03:07 +0200117 /* TODO - extensions */
Radek Krejci19a96102018-11-15 13:38:09 +0100118 (void) ctx;
119 (void) orig;
120 return NULL;
121}
122
Radek Krejcib56c7502019-02-13 14:19:54 +0100123/**
Radek Krejci474f9b82019-07-24 11:36:37 +0200124 * @brief Add record into the compile context's list of incomplete default values.
125 * @param[in] ctx Compile context with the incomplete default values list.
126 * @param[in] context_node Context schema node to store in the record.
127 * @param[in] dflt Incomplete default value to store in the record.
128 * @param[in] dflt_mod Module of the default value definition to store in the record.
129 * @return LY_EMEM in case of memory allocation failure.
130 * @return LY_SUCCESS
131 */
132static LY_ERR
133lysc_incomplete_dflts_add(struct lysc_ctx *ctx, struct lysc_node *context_node, struct lyd_value *dflt, struct lys_module *dflt_mod)
134{
135 struct lysc_incomplete_dflt *r;
136 r = malloc(sizeof *r);
137 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
138 r->context_node = context_node;
139 r->dflt = dflt;
140 r->dflt_mod = dflt_mod;
141 ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST);
142
143 return LY_SUCCESS;
144}
145
146/**
147 * @brief Remove record of the given default value from the compile context's list of incomplete default values.
148 * @param[in] ctx Compile context with the incomplete default values list.
149 * @param[in] dflt Incomplete default values identifying the record to remove.
150 */
151static void
152lysc_incomplete_dflts_remove(struct lysc_ctx *ctx, struct lyd_value *dflt)
153{
154 unsigned int u;
155 struct lysc_incomplete_dflt *r;
156
157 for (u = 0; u < ctx->dflts.count; ++u) {
158 r = (struct lysc_incomplete_dflt*)ctx->dflts.objs[u];
159 if (r->dflt == dflt) {
160 free(ctx->dflts.objs[u]);
161 memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs);
162 --ctx->dflts.count;
163 return;
164 }
165 }
166}
167
Radek Krejci0e59c312019-08-15 15:34:15 +0200168void
Radek Krejci327de162019-06-14 12:52:07 +0200169lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name)
170{
171 int len;
172 int nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
173
174 if (!name) {
175 /* removing last path segment */
176 if (ctx->path[ctx->path_len - 1] == '}') {
177 for (; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len);
178 if (ctx->path[ctx->path_len] == '=') {
179 ctx->path[ctx->path_len++] = '}';
180 } else {
181 /* not a top-level special tag, remove also preceiding '/' */
182 goto remove_nodelevel;
183 }
184 } else {
185remove_nodelevel:
186 for (; ctx->path[ctx->path_len] != '/' ; --ctx->path_len);
187 if (ctx->path_len == 0) {
188 /* top-level (last segment) */
Radek Krejciacc79042019-07-25 14:14:57 +0200189 ctx->path_len = 1;
Radek Krejci327de162019-06-14 12:52:07 +0200190 }
191 }
192 /* set new terminating NULL-byte */
193 ctx->path[ctx->path_len] = '\0';
194 } else {
195 if (ctx->path_len > 1) {
196 if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') {
197 /* extension of the special tag */
198 nextlevel = 2;
199 --ctx->path_len;
200 } else {
201 /* there is already some path, so add next level */
202 nextlevel = 1;
203 }
204 } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
205
206 if (nextlevel != 2) {
207 if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) {
208 /* module not changed, print the name unprefixed */
Radek Krejci70ee9152019-07-25 11:27:27 +0200209 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name);
Radek Krejci327de162019-06-14 12:52:07 +0200210 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200211 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s:%s", nextlevel ? "/" : "", ctx->mod->name, name);
Radek Krejci327de162019-06-14 12:52:07 +0200212 }
213 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200214 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
Radek Krejci327de162019-06-14 12:52:07 +0200215 }
Radek Krejciacc79042019-07-25 14:14:57 +0200216 if (len >= LYSC_CTX_BUFSIZE - ctx->path_len) {
217 /* output truncated */
218 ctx->path_len = LYSC_CTX_BUFSIZE - 1;
219 } else {
220 ctx->path_len += len;
221 }
Radek Krejci327de162019-06-14 12:52:07 +0200222 }
223}
224
225/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100226 * @brief Duplicate the compiled pattern structure.
227 *
228 * Instead of duplicating memory, the reference counter in the @p orig is increased.
229 *
230 * @param[in] orig The pattern structure to duplicate.
231 * @return The duplicated structure to use.
232 */
Radek Krejci19a96102018-11-15 13:38:09 +0100233static struct lysc_pattern*
234lysc_pattern_dup(struct lysc_pattern *orig)
235{
236 ++orig->refcount;
237 return orig;
238}
239
Radek Krejcib56c7502019-02-13 14:19:54 +0100240/**
241 * @brief Duplicate the array of compiled patterns.
242 *
243 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
244 *
245 * @param[in] ctx Libyang context for logging.
246 * @param[in] orig The patterns sized array to duplicate.
247 * @return New sized array as a copy of @p orig.
248 * @return NULL in case of memory allocation error.
249 */
Radek Krejci19a96102018-11-15 13:38:09 +0100250static struct lysc_pattern**
251lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
252{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100253 struct lysc_pattern **dup = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100254 unsigned int u;
255
Radek Krejcib56c7502019-02-13 14:19:54 +0100256 assert(orig);
257
Radek Krejci19a96102018-11-15 13:38:09 +0100258 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL);
259 LY_ARRAY_FOR(orig, u) {
260 dup[u] = lysc_pattern_dup(orig[u]);
261 LY_ARRAY_INCREMENT(dup);
262 }
263 return dup;
264}
265
Radek Krejcib56c7502019-02-13 14:19:54 +0100266/**
267 * @brief Duplicate compiled range structure.
268 *
269 * @param[in] ctx Libyang context for logging.
270 * @param[in] orig The range structure to be duplicated.
271 * @return New compiled range structure as a copy of @p orig.
272 * @return NULL in case of memory allocation error.
273 */
Radek Krejci19a96102018-11-15 13:38:09 +0100274struct lysc_range*
275lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
276{
277 struct lysc_range *dup;
278 LY_ERR ret;
279
Radek Krejcib56c7502019-02-13 14:19:54 +0100280 assert(orig);
281
Radek Krejci19a96102018-11-15 13:38:09 +0100282 dup = calloc(1, sizeof *dup);
283 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
284 if (orig->parts) {
285 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup);
286 LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts);
287 memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts);
288 }
289 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
290 DUP_STRING(ctx, orig->emsg, dup->emsg);
291 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
292
293 return dup;
294cleanup:
295 free(dup);
296 (void) ret; /* set but not used due to the return type */
297 return NULL;
298}
299
Radek Krejcib56c7502019-02-13 14:19:54 +0100300/**
301 * @brief Stack for processing if-feature expressions.
302 */
Radek Krejci19a96102018-11-15 13:38:09 +0100303struct iff_stack {
Radek Krejcib56c7502019-02-13 14:19:54 +0100304 int size; /**< number of items in the stack */
305 int index; /**< first empty item */
306 uint8_t *stack;/**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100307};
308
Radek Krejcib56c7502019-02-13 14:19:54 +0100309/**
310 * @brief Add @ref ifftokens into the stack.
311 * @param[in] stack The if-feature stack to use.
312 * @param[in] value One of the @ref ifftokens to store in the stack.
313 * @return LY_EMEM in case of memory allocation error
314 * @return LY_ESUCCESS if the value successfully stored.
315 */
Radek Krejci19a96102018-11-15 13:38:09 +0100316static LY_ERR
317iff_stack_push(struct iff_stack *stack, uint8_t value)
318{
319 if (stack->index == stack->size) {
320 stack->size += 4;
321 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
322 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
323 }
324 stack->stack[stack->index++] = value;
325 return LY_SUCCESS;
326}
327
Radek Krejcib56c7502019-02-13 14:19:54 +0100328/**
329 * @brief Get (and remove) the last item form the stack.
330 * @param[in] stack The if-feature stack to use.
331 * @return The value from the top of the stack.
332 */
Radek Krejci19a96102018-11-15 13:38:09 +0100333static uint8_t
334iff_stack_pop(struct iff_stack *stack)
335{
Radek Krejcib56c7502019-02-13 14:19:54 +0100336 assert(stack && stack->index);
337
Radek Krejci19a96102018-11-15 13:38:09 +0100338 stack->index--;
339 return stack->stack[stack->index];
340}
341
Radek Krejcib56c7502019-02-13 14:19:54 +0100342/**
343 * @brief Clean up the stack.
344 * @param[in] stack The if-feature stack to use.
345 */
Radek Krejci19a96102018-11-15 13:38:09 +0100346static void
347iff_stack_clean(struct iff_stack *stack)
348{
349 stack->size = 0;
350 free(stack->stack);
351}
352
Radek Krejcib56c7502019-02-13 14:19:54 +0100353/**
354 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
355 * (libyang format of the if-feature expression).
356 * @param[in,out] list The 2bits array to modify.
357 * @param[in] op The operand (@ref ifftokens) to store.
358 * @param[in] pos Position (0-based) where to store the given @p op.
359 */
Radek Krejci19a96102018-11-15 13:38:09 +0100360static void
361iff_setop(uint8_t *list, uint8_t op, int pos)
362{
363 uint8_t *item;
364 uint8_t mask = 3;
365
366 assert(pos >= 0);
367 assert(op <= 3); /* max 2 bits */
368
369 item = &list[pos / 4];
370 mask = mask << 2 * (pos % 4);
371 *item = (*item) & ~mask;
372 *item = (*item) | (op << 2 * (pos % 4));
373}
374
Radek Krejcib56c7502019-02-13 14:19:54 +0100375#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
376#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100377
Radek Krejci0af46292019-01-11 16:02:31 +0100378/**
379 * @brief Find a feature of the given name and referenced in the given module.
380 *
381 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
382 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
383 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
384 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
385 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
386 * assigned till that time will be still valid.
387 *
388 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
389 * @param[in] name Name of the feature including possible prefix.
390 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
391 * @return Pointer to the feature structure if found, NULL otherwise.
392 */
Radek Krejci19a96102018-11-15 13:38:09 +0100393static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100394lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100395{
396 size_t i;
Radek Krejci0af46292019-01-11 16:02:31 +0100397 struct lysc_feature *f, *flist;
Radek Krejci19a96102018-11-15 13:38:09 +0100398
399 for (i = 0; i < len; ++i) {
400 if (name[i] == ':') {
401 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100402 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100403 LY_CHECK_RET(!mod, NULL);
404
405 name = &name[i + 1];
406 len = len - i - 1;
407 }
408 }
409
410 /* we have the correct module, get the feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100411 if (mod->implemented) {
412 /* module is implemented so there is already the compiled schema */
413 flist = mod->compiled->features;
414 } else {
415 flist = mod->off_features;
416 }
417 LY_ARRAY_FOR(flist, i) {
418 f = &flist[i];
Radek Krejci7f9b6512019-09-18 13:11:09 +0200419 if (!ly_strncmp(f->name, name, len)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100420 return f;
421 }
422 }
423
424 return NULL;
425}
426
427static LY_ERR
Radek Krejci0935f412019-08-20 16:15:18 +0200428lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent, LYEXT_PARENT parent_type)
Radek Krejci19a96102018-11-15 13:38:09 +0100429{
Radek Krejci7c960162019-09-18 14:16:12 +0200430 LY_ERR ret = LY_EVALID;
Radek Krejci19a96102018-11-15 13:38:09 +0100431 const char *name;
432 unsigned int u;
433 const struct lys_module *mod;
Radek Krejci0935f412019-08-20 16:15:18 +0200434 struct lysc_ext *elist = NULL;
Radek Krejci7c960162019-09-18 14:16:12 +0200435 const char *prefixed_name = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100436
437 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
438 ext->insubstmt = ext_p->insubstmt;
439 ext->insubstmt_index = ext_p->insubstmt_index;
Radek Krejci28681fa2019-09-06 13:08:45 +0200440 ext->module = ctx->mod_def;
Radek Krejci0935f412019-08-20 16:15:18 +0200441 ext->parent = parent;
442 ext->parent_type = parent_type;
Radek Krejci19a96102018-11-15 13:38:09 +0100443
Radek Krejcif56e2a42019-09-09 14:15:25 +0200444 lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node*)ext->parent : NULL, "{extension}");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200445
Radek Krejci19a96102018-11-15 13:38:09 +0100446 /* get module where the extension definition should be placed */
Radek Krejci7c960162019-09-18 14:16:12 +0200447 for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u);
448 if (ext_p->yin) {
449 /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's
450 * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the
451 * YANG (import) prefix must be done here. */
452 if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) {
453 prefixed_name = lydict_insert(ctx->ctx, &ext_p->name[u], 0);
454 u = 0;
455 } else if (ctx->mod_def->compiled) {
456 unsigned int v;
457 LY_ARRAY_FOR(ctx->mod_def->compiled->imports, v) {
458 if (!ly_strncmp(ctx->mod_def->compiled->imports[v].module->ns, ext_p->name, u - 1)) {
459 char *s;
460 asprintf(&s, "%s:%s", ctx->mod_def->compiled->imports[v].prefix, &ext_p->name[u]);
461 prefixed_name = lydict_insert_zc(ctx->ctx, s);
462 u = strlen(ctx->mod_def->compiled->imports[v].prefix) + 1; /* add semicolon */
463 break;
464 }
465 }
466 }
467 if (!prefixed_name) {
468 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
469 "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name);
470 goto cleanup;
471 }
472 } else {
473 prefixed_name = ext_p->name;
474 }
475 lysc_update_path(ctx, NULL, prefixed_name);
476
477 mod = lys_module_find_prefix(ctx->mod_def, prefixed_name, u - 1);
478 if (!mod) {
479 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
480 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name);
481 goto cleanup;
482 } else if (!mod->parsed->extensions) {
483 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
484 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
485 prefixed_name, mod->name);
486 goto cleanup;
487 }
488 name = &prefixed_name[u];
Radek Krejci0935f412019-08-20 16:15:18 +0200489
Radek Krejci19a96102018-11-15 13:38:09 +0100490 /* find the extension definition there */
Radek Krejci0935f412019-08-20 16:15:18 +0200491 if (mod->off_extensions) {
492 elist = mod->off_extensions;
493 } else {
494 elist = mod->compiled->extensions;
495 }
496 LY_ARRAY_FOR(elist, u) {
497 if (!strcmp(name, elist[u].name)) {
498 ext->def = &elist[u];
Radek Krejci19a96102018-11-15 13:38:09 +0100499 break;
500 }
501 }
Radek Krejci7c960162019-09-18 14:16:12 +0200502 if (!ext->def) {
503 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
504 "Extension definition of extension instance \"%s\" not found.", prefixed_name);
505 goto cleanup;
506 }
Radek Krejci0935f412019-08-20 16:15:18 +0200507
Radek Krejcif56e2a42019-09-09 14:15:25 +0200508 /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible
509 * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have
510 * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */
Radek Krejci7c960162019-09-18 14:16:12 +0200511 if (ext_p->yin && ext->def->argument && !ext->argument) {
Radek Krejcif56e2a42019-09-09 14:15:25 +0200512 /* Schema was parsed from YIN and an argument is expected, ... */
513 struct lysp_stmt *stmt = NULL;
514
515 if (ext->def->flags & LYS_YINELEM_TRUE) {
516 /* ... argument was the first XML child element */
517 if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) {
518 /* TODO check namespace of the statement */
519 if (!strcmp(ext_p->child->stmt, ext->def->argument)) {
520 stmt = ext_p->child;
521 }
522 }
523 } else {
524 /* ... argument was one of the XML attributes which are represented as child stmt
525 * with LYS_YIN_ATTR flag */
526 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
527 if (!strcmp(stmt->stmt, ext->def->argument)) {
528 /* this is the extension's argument */
Radek Krejcif56e2a42019-09-09 14:15:25 +0200529 break;
530 }
531 }
532 }
533 if (!stmt) {
534 /* missing extension's argument */
535 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci7c960162019-09-18 14:16:12 +0200536 "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument);
537 goto cleanup;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200538
539 }
540 ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0);
541 stmt->flags |= LYS_YIN_ARGUMENT;
542 }
Radek Krejci7c960162019-09-18 14:16:12 +0200543 if (prefixed_name != ext_p->name) {
544 lydict_remove(ctx->ctx, ext_p->name);
545 ext_p->name = prefixed_name;
546 if (!ext_p->argument && ext->argument) {
547 ext_p->argument = lydict_insert(ctx->ctx, ext->argument, 0);
548 }
549 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200550
Radek Krejci0935f412019-08-20 16:15:18 +0200551 if (ext->def->plugin && ext->def->plugin->compile) {
Radek Krejciad5963b2019-09-06 16:03:05 +0200552 if (ext->argument) {
553 lysc_update_path(ctx, (struct lysc_node*)ext, ext->argument);
554 }
Radek Krejci7c960162019-09-18 14:16:12 +0200555 LY_CHECK_GOTO(ext->def->plugin->compile(ctx, ext_p, ext), cleanup);
Radek Krejciad5963b2019-09-06 16:03:05 +0200556 if (ext->argument) {
557 lysc_update_path(ctx, NULL, NULL);
558 }
Radek Krejci0935f412019-08-20 16:15:18 +0200559 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200560 ext_p->compiled = ext;
561
Radek Krejci7c960162019-09-18 14:16:12 +0200562 ret = LY_SUCCESS;
563cleanup:
564 if (prefixed_name && prefixed_name != ext_p->name) {
565 lydict_remove(ctx->ctx, prefixed_name);
566 }
567
Radek Krejcif56e2a42019-09-09 14:15:25 +0200568 lysc_update_path(ctx, NULL, NULL);
569 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0935f412019-08-20 16:15:18 +0200570
Radek Krejci7c960162019-09-18 14:16:12 +0200571 return ret;
Radek Krejci0935f412019-08-20 16:15:18 +0200572}
573
574/**
575 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
576 */
577static LY_ERR
578lys_compile_extension(struct lysc_ctx *ctx, struct lysp_ext *ext_p, struct lysc_ext *ext)
579{
580 LY_ERR ret = LY_SUCCESS;
581
582 DUP_STRING(ctx->ctx, ext_p->name, ext->name);
583 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
584 ext->module = ctx->mod_def;
585 COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext->exts, ext, LYEXT_PAR_EXT, ret, done);
586
587done:
588 return ret;
589}
590
591/**
592 * @brief Link the extensions definitions with the available extension plugins.
593 *
594 * This is done only in the compiled (implemented) module. Extensions of a non-implemented modules
595 * are not connected with even available extension plugins.
596 *
597 * @param[in] extensions List of extensions to be processed ([sized array](@ref sizedarrays)).
598 */
599static void
600lys_compile_extension_plugins(struct lysc_ext *extensions)
601{
602 unsigned int u;
603
604 LY_ARRAY_FOR(extensions, u) {
605 extensions[u].plugin = lyext_get_plugin(&extensions[u]);
606 }
607}
608
609LY_ERR
610lys_extension_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
611 struct lysp_ext *extensions_p, struct lysc_ext **extensions)
612{
613 unsigned int offset = 0, u;
614 struct lysc_ctx context = {0};
615
616 assert(ctx_sc || ctx);
617
618 if (!ctx_sc) {
619 context.ctx = ctx;
620 context.mod = module;
621 context.path_len = 1;
622 context.path[0] = '/';
623 ctx_sc = &context;
624 }
625
626 if (!extensions_p) {
627 return LY_SUCCESS;
628 }
629 if (*extensions) {
630 offset = LY_ARRAY_SIZE(*extensions);
631 }
632
633 lysc_update_path(ctx_sc, NULL, "{extension}");
634 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *extensions, LY_ARRAY_SIZE(extensions_p), LY_EMEM);
635 LY_ARRAY_FOR(extensions_p, u) {
636 lysc_update_path(ctx_sc, NULL, extensions_p[u].name);
637 LY_ARRAY_INCREMENT(*extensions);
638 COMPILE_CHECK_UNIQUENESS(ctx_sc, *extensions, name, &(*extensions)[offset + u], "extension", extensions_p[u].name);
639 LY_CHECK_RET(lys_compile_extension(ctx_sc, &extensions_p[u], &(*extensions)[offset + u]));
640 lysc_update_path(ctx_sc, NULL, NULL);
641 }
642 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100643
644 return LY_SUCCESS;
645}
646
Radek Krejcib56c7502019-02-13 14:19:54 +0100647/**
648 * @brief Compile information from the if-feature statement
649 * @param[in] ctx Compile context.
650 * @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 +0100651 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
652 * @return LY_ERR value.
653 */
Radek Krejci19a96102018-11-15 13:38:09 +0100654static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200655lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff)
Radek Krejci19a96102018-11-15 13:38:09 +0100656{
657 const char *c = *value;
658 int r, rc = EXIT_FAILURE;
659 int i, j, last_not, checkversion = 0;
660 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
661 uint8_t op;
662 struct iff_stack stack = {0, 0, NULL};
663 struct lysc_feature *f;
664
665 assert(c);
666
667 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
668 for (i = j = last_not = 0; c[i]; i++) {
669 if (c[i] == '(') {
670 j++;
671 checkversion = 1;
672 continue;
673 } else if (c[i] == ')') {
674 j--;
675 continue;
676 } else if (isspace(c[i])) {
677 checkversion = 1;
678 continue;
679 }
680
681 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 +0200682 int sp;
683 for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++);
684 if (c[i + r + sp] == '\0') {
Radek Krejci19a96102018-11-15 13:38:09 +0100685 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
686 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
687 return LY_EVALID;
688 } else if (!isspace(c[i + r])) {
689 /* feature name starting with the not/and/or */
690 last_not = 0;
691 f_size++;
692 } else if (c[i] == 'n') { /* not operation */
693 if (last_not) {
694 /* double not */
695 expr_size = expr_size - 2;
696 last_not = 0;
697 } else {
698 last_not = 1;
699 }
700 } else { /* and, or */
Radek Krejci6788abc2019-06-14 13:56:49 +0200701 if (f_exp != f_size) {
702 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
703 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]);
704 return LY_EVALID;
705 }
Radek Krejci19a96102018-11-15 13:38:09 +0100706 f_exp++;
Radek Krejci6788abc2019-06-14 13:56:49 +0200707
Radek Krejci19a96102018-11-15 13:38:09 +0100708 /* not a not operation */
709 last_not = 0;
710 }
711 i += r;
712 } else {
713 f_size++;
714 last_not = 0;
715 }
716 expr_size++;
717
718 while (!isspace(c[i])) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200719 if (!c[i] || c[i] == ')' || c[i] == '(') {
Radek Krejci19a96102018-11-15 13:38:09 +0100720 i--;
721 break;
722 }
723 i++;
724 }
725 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200726 if (j) {
Radek Krejci19a96102018-11-15 13:38:09 +0100727 /* not matching count of ( and ) */
728 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
729 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
730 return LY_EVALID;
731 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200732 if (f_exp != f_size) {
733 /* features do not match the needed arguments for the logical operations */
734 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
735 "Invalid value \"%s\" of if-feature - number of features in expression does not match "
736 "the required number of operands for the operations.", *value);
737 return LY_EVALID;
738 }
Radek Krejci19a96102018-11-15 13:38:09 +0100739
740 if (checkversion || expr_size > 1) {
741 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100742 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100743 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
744 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
745 return LY_EVALID;
746 }
747 }
748
749 /* allocate the memory */
750 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
751 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
752 stack.stack = malloc(expr_size * sizeof *stack.stack);
753 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
754
755 stack.size = expr_size;
756 f_size--; expr_size--; /* used as indexes from now */
757
758 for (i--; i >= 0; i--) {
759 if (c[i] == ')') {
760 /* push it on stack */
761 iff_stack_push(&stack, LYS_IFF_RP);
762 continue;
763 } else if (c[i] == '(') {
764 /* pop from the stack into result all operators until ) */
765 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
766 iff_setop(iff->expr, op, expr_size--);
767 }
768 continue;
769 } else if (isspace(c[i])) {
770 continue;
771 }
772
773 /* end of operator or operand -> find beginning and get what is it */
774 j = i + 1;
775 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
776 i--;
777 }
778 i++; /* go back by one step */
779
780 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
781 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
782 /* double not */
783 iff_stack_pop(&stack);
784 } else {
785 /* not has the highest priority, so do not pop from the stack
786 * as in case of AND and OR */
787 iff_stack_push(&stack, LYS_IFF_NOT);
788 }
789 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
790 /* as for OR - pop from the stack all operators with the same or higher
791 * priority and store them to the result, then push the AND to the stack */
792 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
793 op = iff_stack_pop(&stack);
794 iff_setop(iff->expr, op, expr_size--);
795 }
796 iff_stack_push(&stack, LYS_IFF_AND);
797 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
798 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
799 op = iff_stack_pop(&stack);
800 iff_setop(iff->expr, op, expr_size--);
801 }
802 iff_stack_push(&stack, LYS_IFF_OR);
803 } else {
804 /* feature name, length is j - i */
805
806 /* add it to the expression */
807 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
808
809 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100810 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
811 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
812 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
813 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100814 iff->features[f_size] = f;
815 LY_ARRAY_INCREMENT(iff->features);
816 f_size--;
817 }
818 }
819 while (stack.index) {
820 op = iff_stack_pop(&stack);
821 iff_setop(iff->expr, op, expr_size--);
822 }
823
824 if (++expr_size || ++f_size) {
825 /* not all expected operators and operands found */
826 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
827 "Invalid value \"%s\" of if-feature - processing error.", *value);
828 rc = LY_EINT;
829 } else {
830 rc = LY_SUCCESS;
831 }
832
833error:
834 /* cleanup */
835 iff_stack_clean(&stack);
836
837 return rc;
838}
839
Radek Krejcib56c7502019-02-13 14:19:54 +0100840/**
841 * @brief Compile information from the when statement
842 * @param[in] ctx Compile context.
843 * @param[in] when_p The parsed when statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100844 * @param[out] when Pointer where to store pointer to the created compiled when structure.
845 * @return LY_ERR value.
846 */
Radek Krejci19a96102018-11-15 13:38:09 +0100847static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200848lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, struct lysc_when **when)
Radek Krejci19a96102018-11-15 13:38:09 +0100849{
Radek Krejci19a96102018-11-15 13:38:09 +0100850 LY_ERR ret = LY_SUCCESS;
851
Radek Krejci00b874b2019-02-12 10:54:50 +0100852 *when = calloc(1, sizeof **when);
853 (*when)->refcount = 1;
854 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond);
Radek Krejcia0f704a2019-09-09 16:12:23 +0200855 (*when)->module = ctx->mod_def;
Radek Krejci00b874b2019-02-12 10:54:50 +0100856 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
857 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
858 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci0935f412019-08-20 16:15:18 +0200859 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100860
861done:
862 return ret;
863}
864
Radek Krejcib56c7502019-02-13 14:19:54 +0100865/**
866 * @brief Compile information from the must statement
867 * @param[in] ctx Compile context.
868 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100869 * @param[in,out] must Prepared (empty) compiled must structure to fill.
870 * @return LY_ERR value.
871 */
Radek Krejci19a96102018-11-15 13:38:09 +0100872static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200873lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100874{
Radek Krejci19a96102018-11-15 13:38:09 +0100875 LY_ERR ret = LY_SUCCESS;
876
877 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg);
878 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci462cf252019-07-24 09:49:08 +0200879 must->module = ctx->mod_def;
Radek Krejci19a96102018-11-15 13:38:09 +0100880 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
881 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100882 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
883 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejci0935f412019-08-20 16:15:18 +0200884 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100885
886done:
887 return ret;
888}
889
Radek Krejcib56c7502019-02-13 14:19:54 +0100890/**
891 * @brief Compile information from the import statement
892 * @param[in] ctx Compile context.
893 * @param[in] imp_p The parsed import statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100894 * @param[in,out] imp Prepared (empty) compiled import structure to fill.
895 * @return LY_ERR value.
896 */
Radek Krejci19a96102018-11-15 13:38:09 +0100897static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200898lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, struct lysc_import *imp)
Radek Krejci19a96102018-11-15 13:38:09 +0100899{
Radek Krejci19a96102018-11-15 13:38:09 +0100900 struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100901 LY_ERR ret = LY_SUCCESS;
902
903 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
Radek Krejci0935f412019-08-20 16:15:18 +0200904 COMPILE_EXTS_GOTO(ctx, imp_p->exts, imp->exts, imp, LYEXT_PAR_IMPORT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100905 imp->module = imp_p->module;
906
Radek Krejci7f2a5362018-11-28 13:05:37 +0100907 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
908 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100909 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100910 if (!imp->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100911 /* try to use filepath if present */
912 if (imp->module->filepath) {
913 mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath,
914 !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
Radek Krejci19a96102018-11-15 13:38:09 +0100915 if (mod != imp->module) {
916 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100917 imp->module->filepath, imp->module->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100918 mod = NULL;
919 }
920 }
921 if (!mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100922 if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, &mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100923 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 +0100924 imp->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100925 return LY_ENOTFOUND;
926 }
927 }
Radek Krejci19a96102018-11-15 13:38:09 +0100928 }
929
930done:
931 return ret;
932}
933
Radek Krejcib56c7502019-02-13 14:19:54 +0100934/**
935 * @brief Compile information from the identity statement
936 *
937 * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases().
938 *
939 * @param[in] ctx Compile context.
940 * @param[in] ident_p The parsed identity statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100941 * @param[in] idents List of so far compiled identities to check the name uniqueness.
942 * @param[in,out] ident Prepared (empty) compiled identity structure to fill.
943 * @return LY_ERR value.
944 */
Radek Krejci19a96102018-11-15 13:38:09 +0100945static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200946lys_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 +0100947{
948 unsigned int u;
949 LY_ERR ret = LY_SUCCESS;
950
Radek Krejci327de162019-06-14 12:52:07 +0200951 lysc_update_path(ctx, NULL, ident_p->name);
952
Radek Krejcid05cbd92018-12-05 14:26:40 +0100953 COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100954 DUP_STRING(ctx->ctx, ident_p->name, ident->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200955 DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc);
956 DUP_STRING(ctx->ctx, ident_p->ref, ident->ref);
Radek Krejci693262f2019-04-29 15:23:20 +0200957 ident->module = ctx->mod;
Radek Krejciec4da802019-05-02 13:02:41 +0200958 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100959 /* 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 +0200960 COMPILE_EXTS_GOTO(ctx, ident_p->exts, ident->exts, ident, LYEXT_PAR_IDENT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100961 ident->flags = ident_p->flags;
962
Radek Krejci327de162019-06-14 12:52:07 +0200963 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100964done:
965 return ret;
966}
967
Radek Krejcib56c7502019-02-13 14:19:54 +0100968/**
969 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
970 *
971 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
972 *
973 * @param[in] ctx Compile context for logging.
974 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
975 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
976 * @return LY_SUCCESS if everything is ok.
977 * @return LY_EVALID if the identity is derived from itself.
978 */
Radek Krejci38222632019-02-12 16:55:05 +0100979static LY_ERR
980lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
981{
982 LY_ERR ret = LY_EVALID;
983 unsigned int u, v;
984 struct ly_set recursion = {0};
985 struct lysc_ident *drv;
986
987 if (!derived) {
988 return LY_SUCCESS;
989 }
990
991 for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) {
992 if (ident == derived[u]) {
993 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
994 "Identity \"%s\" is indirectly derived from itself.", ident->name);
995 goto cleanup;
996 }
997 ly_set_add(&recursion, derived[u], 0);
998 }
999
1000 for (v = 0; v < recursion.count; ++v) {
1001 drv = recursion.objs[v];
1002 if (!drv->derived) {
1003 continue;
1004 }
1005 for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) {
1006 if (ident == drv->derived[u]) {
1007 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1008 "Identity \"%s\" is indirectly derived from itself.", ident->name);
1009 goto cleanup;
1010 }
1011 ly_set_add(&recursion, drv->derived[u], 0);
1012 }
1013 }
1014 ret = LY_SUCCESS;
1015
1016cleanup:
1017 ly_set_erase(&recursion, NULL);
1018 return ret;
1019}
1020
Radek Krejcia3045382018-11-22 14:30:31 +01001021/**
1022 * @brief Find and process the referenced base identities from another identity or identityref
1023 *
Radek Krejciaca74032019-06-04 08:53:06 +02001024 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +01001025 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
1026 * to distinguish these two use cases.
1027 *
1028 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1029 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
1030 * @param[in] ident Referencing identity to work with.
1031 * @param[in] bases Array of bases of identityref to fill in.
1032 * @return LY_ERR value.
1033 */
Radek Krejci19a96102018-11-15 13:38:09 +01001034static LY_ERR
Radek Krejci555cb5b2018-11-16 14:54:33 +01001035lys_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 +01001036{
Radek Krejci555cb5b2018-11-16 14:54:33 +01001037 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001038 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +01001039 struct lys_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001040 struct lysc_ident **idref;
1041
1042 assert(ident || bases);
1043
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001044 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001045 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1046 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
1047 return LY_EVALID;
1048 }
1049
1050 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
1051 s = strchr(bases_p[u], ':');
1052 if (s) {
1053 /* prefixed identity */
1054 name = &s[1];
Radek Krejcie86bf772018-12-14 11:39:53 +01001055 mod = lys_module_find_prefix(ctx->mod_def, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001056 } else {
1057 name = bases_p[u];
Radek Krejcie86bf772018-12-14 11:39:53 +01001058 mod = ctx->mod_def;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001059 }
1060 if (!mod) {
1061 if (ident) {
1062 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1063 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
1064 } else {
1065 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1066 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
1067 }
1068 return LY_EVALID;
1069 }
1070 idref = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001071 if (mod->compiled && mod->compiled->identities) {
1072 for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) {
1073 if (!strcmp(name, mod->compiled->identities[v].name)) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001074 if (ident) {
Radek Krejci38222632019-02-12 16:55:05 +01001075 if (ident == &mod->compiled->identities[v]) {
1076 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1077 "Identity \"%s\" is derived from itself.", ident->name);
1078 return LY_EVALID;
1079 }
1080 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->compiled->identities[v], ident->derived));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001081 /* we have match! store the backlink */
Radek Krejcie86bf772018-12-14 11:39:53 +01001082 LY_ARRAY_NEW_RET(ctx->ctx, mod->compiled->identities[v].derived, idref, LY_EMEM);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001083 *idref = ident;
1084 } else {
1085 /* we have match! store the found identity */
1086 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01001087 *idref = &mod->compiled->identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +01001088 }
1089 break;
1090 }
1091 }
1092 }
1093 if (!idref || !(*idref)) {
1094 if (ident) {
1095 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1096 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
1097 } else {
1098 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1099 "Unable to find base (%s) of identityref.", bases_p[u]);
1100 }
1101 return LY_EVALID;
1102 }
1103 }
1104 return LY_SUCCESS;
1105}
1106
Radek Krejcia3045382018-11-22 14:30:31 +01001107/**
1108 * @brief For the given array of identities, set the backlinks from all their base identities.
1109 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1110 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
1111 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
1112 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1113 */
Radek Krejci555cb5b2018-11-16 14:54:33 +01001114static LY_ERR
1115lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
1116{
1117 unsigned int i;
Radek Krejci19a96102018-11-15 13:38:09 +01001118
1119 for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) {
1120 if (!idents_p[i].bases) {
1121 continue;
1122 }
Radek Krejci327de162019-06-14 12:52:07 +02001123 lysc_update_path(ctx, NULL, idents[i].name);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001124 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL));
Radek Krejci327de162019-06-14 12:52:07 +02001125 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001126 }
1127 return LY_SUCCESS;
1128}
1129
Radek Krejci0af46292019-01-11 16:02:31 +01001130LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02001131lys_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 +01001132{
1133 unsigned int offset = 0, u;
1134 struct lysc_ctx context = {0};
1135
Radek Krejci327de162019-06-14 12:52:07 +02001136 assert(ctx_sc || ctx);
1137
1138 if (!ctx_sc) {
1139 context.ctx = ctx;
1140 context.mod = module;
1141 context.path_len = 1;
1142 context.path[0] = '/';
1143 ctx_sc = &context;
1144 }
Radek Krejci0af46292019-01-11 16:02:31 +01001145
1146 if (!features_p) {
1147 return LY_SUCCESS;
1148 }
1149 if (*features) {
1150 offset = LY_ARRAY_SIZE(*features);
1151 }
1152
Radek Krejci327de162019-06-14 12:52:07 +02001153 lysc_update_path(ctx_sc, NULL, "{feature}");
1154 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM);
Radek Krejci0af46292019-01-11 16:02:31 +01001155 LY_ARRAY_FOR(features_p, u) {
Radek Krejci327de162019-06-14 12:52:07 +02001156 lysc_update_path(ctx_sc, NULL, features_p[u].name);
1157
Radek Krejci0af46292019-01-11 16:02:31 +01001158 LY_ARRAY_INCREMENT(*features);
Radek Krejci327de162019-06-14 12:52:07 +02001159 COMPILE_CHECK_UNIQUENESS(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
1160 DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name);
1161 DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc);
1162 DUP_STRING(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref);
Radek Krejci0af46292019-01-11 16:02:31 +01001163 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci327de162019-06-14 12:52:07 +02001164 (*features)[offset + u].module = ctx_sc->mod;
1165
1166 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001167 }
Radek Krejci327de162019-06-14 12:52:07 +02001168 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001169
1170 return LY_SUCCESS;
1171}
1172
Radek Krejcia3045382018-11-22 14:30:31 +01001173/**
Radek Krejci09a1fc52019-02-13 10:55:17 +01001174 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +01001175 *
1176 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
1177 *
Radek Krejci09a1fc52019-02-13 10:55:17 +01001178 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +01001179 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
1180 * being currently processed).
1181 * @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 +01001182 * @return LY_SUCCESS if everything is ok.
1183 * @return LY_EVALID if the feature references indirectly itself.
1184 */
1185static LY_ERR
1186lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
1187{
1188 LY_ERR ret = LY_EVALID;
1189 unsigned int u, v;
1190 struct ly_set recursion = {0};
1191 struct lysc_feature *drv;
1192
1193 if (!depfeatures) {
1194 return LY_SUCCESS;
1195 }
1196
1197 for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) {
1198 if (feature == depfeatures[u]) {
1199 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1200 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1201 goto cleanup;
1202 }
1203 ly_set_add(&recursion, depfeatures[u], 0);
1204 }
1205
1206 for (v = 0; v < recursion.count; ++v) {
1207 drv = recursion.objs[v];
1208 if (!drv->depfeatures) {
1209 continue;
1210 }
1211 for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) {
1212 if (feature == drv->depfeatures[u]) {
1213 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1214 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1215 goto cleanup;
1216 }
1217 ly_set_add(&recursion, drv->depfeatures[u], 0);
1218 }
1219 }
1220 ret = LY_SUCCESS;
1221
1222cleanup:
1223 ly_set_erase(&recursion, NULL);
1224 return ret;
1225}
1226
1227/**
Radek Krejci0af46292019-01-11 16:02:31 +01001228 * @brief Create pre-compiled features array.
1229 *
1230 * See lys_feature_precompile() for more details.
1231 *
Radek Krejcia3045382018-11-22 14:30:31 +01001232 * @param[in] ctx Compile context.
1233 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +01001234 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +01001235 * @return LY_ERR value.
1236 */
Radek Krejci19a96102018-11-15 13:38:09 +01001237static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001238lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +01001239{
Radek Krejci0af46292019-01-11 16:02:31 +01001240 unsigned int u, v, x;
1241 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +01001242 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01001243
Radek Krejci327de162019-06-14 12:52:07 +02001244
Radek Krejci0af46292019-01-11 16:02:31 +01001245 /* find the preprecompiled feature */
1246 LY_ARRAY_FOR(features, x) {
1247 if (strcmp(features[x].name, feature_p->name)) {
1248 continue;
1249 }
1250 feature = &features[x];
Radek Krejci327de162019-06-14 12:52:07 +02001251 lysc_update_path(ctx, NULL, "{feature}");
1252 lysc_update_path(ctx, NULL, feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001253
Radek Krejci0af46292019-01-11 16:02:31 +01001254 /* finish compilation started in lys_feature_precompile() */
Radek Krejci0935f412019-08-20 16:15:18 +02001255 COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02001256 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001257 if (feature->iffeatures) {
1258 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
1259 if (feature->iffeatures[u].features) {
1260 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001261 /* check for circular dependency - direct reference first,... */
1262 if (feature == feature->iffeatures[u].features[v]) {
1263 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1264 "Feature \"%s\" is referenced from itself.", feature->name);
1265 return LY_EVALID;
1266 }
1267 /* ... and indirect circular reference */
1268 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1269
Radek Krejci0af46292019-01-11 16:02:31 +01001270 /* add itself into the dependants list */
1271 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1272 *df = feature;
1273 }
Radek Krejci19a96102018-11-15 13:38:09 +01001274 }
Radek Krejci19a96102018-11-15 13:38:09 +01001275 }
1276 }
Radek Krejci327de162019-06-14 12:52:07 +02001277 lysc_update_path(ctx, NULL, NULL);
1278 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001279 done:
1280 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01001281 }
Radek Krejci0af46292019-01-11 16:02:31 +01001282
1283 LOGINT(ctx->ctx);
1284 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +01001285}
1286
Radek Krejcib56c7502019-02-13 14:19:54 +01001287/**
1288 * @brief Revert compiled list of features back to the precompiled state.
1289 *
1290 * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status.
1291 * The features are supposed to be stored again as off_features in ::lys_module structure.
1292 *
1293 * @param[in] ctx Compilation context.
1294 * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema
1295 * and supposed to hold the off_features list.
1296 */
Radek Krejci95710c92019-02-11 15:49:55 +01001297static void
1298lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1299{
1300 unsigned int u, v;
1301
1302 /* keep the off_features list until the complete lys_module is freed */
1303 mod->off_features = mod->compiled->features;
1304 mod->compiled->features = NULL;
1305
1306 /* in the off_features list, remove all the parts (from finished compiling process)
1307 * which may points into the data being freed here */
1308 LY_ARRAY_FOR(mod->off_features, u) {
1309 LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) {
1310 lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]);
1311 }
1312 LY_ARRAY_FREE(mod->off_features[u].iffeatures);
1313 mod->off_features[u].iffeatures = NULL;
1314
1315 LY_ARRAY_FOR(mod->off_features[u].exts, v) {
1316 lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]);
1317 }
1318 LY_ARRAY_FREE(mod->off_features[u].exts);
1319 mod->off_features[u].exts = NULL;
1320 }
1321}
1322
Radek Krejcia3045382018-11-22 14:30:31 +01001323/**
1324 * @brief Validate and normalize numeric value from a range definition.
1325 * @param[in] ctx Compile context.
1326 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
1327 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
1328 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
1329 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
1330 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1331 * @param[in] value String value of the range boundary.
1332 * @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.
1333 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1334 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1335 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001336LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001337range_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 +01001338{
Radek Krejci6cba4292018-11-15 17:33:29 +01001339 size_t fraction = 0, size;
1340
Radek Krejci19a96102018-11-15 13:38:09 +01001341 *len = 0;
1342
1343 assert(value);
1344 /* parse value */
1345 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1346 return LY_EVALID;
1347 }
1348
1349 if ((value[*len] == '-') || (value[*len] == '+')) {
1350 ++(*len);
1351 }
1352
1353 while (isdigit(value[*len])) {
1354 ++(*len);
1355 }
1356
1357 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001358 if (basetype == LY_TYPE_DEC64) {
1359 goto decimal;
1360 } else {
1361 *valcopy = strndup(value, *len);
1362 return LY_SUCCESS;
1363 }
Radek Krejci19a96102018-11-15 13:38:09 +01001364 }
1365 fraction = *len;
1366
1367 ++(*len);
1368 while (isdigit(value[*len])) {
1369 ++(*len);
1370 }
1371
Radek Krejci6cba4292018-11-15 17:33:29 +01001372 if (basetype == LY_TYPE_DEC64) {
1373decimal:
1374 assert(frdigits);
Radek Krejci943177f2019-06-14 16:32:43 +02001375 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001376 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1377 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1378 *len, value, frdigits);
1379 return LY_EINVAL;
1380 }
1381 if (fraction) {
1382 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1383 } else {
1384 size = (*len) + frdigits + 1;
1385 }
1386 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001387 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1388
Radek Krejci6cba4292018-11-15 17:33:29 +01001389 (*valcopy)[size - 1] = '\0';
1390 if (fraction) {
1391 memcpy(&(*valcopy)[0], &value[0], fraction);
1392 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1393 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1394 } else {
1395 memcpy(&(*valcopy)[0], &value[0], *len);
1396 memset(&(*valcopy)[*len], '0', frdigits);
1397 }
Radek Krejci19a96102018-11-15 13:38:09 +01001398 }
1399 return LY_SUCCESS;
1400}
1401
Radek Krejcia3045382018-11-22 14:30:31 +01001402/**
1403 * @brief Check that values in range are in ascendant order.
1404 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001405 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1406 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001407 * @param[in] value Current value to check.
1408 * @param[in] prev_value The last seen value.
1409 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1410 */
Radek Krejci19a96102018-11-15 13:38:09 +01001411static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001412range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001413{
1414 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001415 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001416 return LY_EEXIST;
1417 }
1418 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001419 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001420 return LY_EEXIST;
1421 }
1422 }
1423 return LY_SUCCESS;
1424}
1425
Radek Krejcia3045382018-11-22 14:30:31 +01001426/**
1427 * @brief Set min/max value of the range part.
1428 * @param[in] ctx Compile context.
1429 * @param[in] part Range part structure to fill.
1430 * @param[in] max Flag to distinguish if storing min or max value.
1431 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1432 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1433 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1434 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1435 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001436 * @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 +01001437 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1438 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1439 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1440 * frdigits value), LY_EMEM.
1441 */
Radek Krejci19a96102018-11-15 13:38:09 +01001442static LY_ERR
1443range_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 +01001444 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001445{
1446 LY_ERR ret = LY_SUCCESS;
1447 char *valcopy = NULL;
1448 size_t len;
1449
1450 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001451 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001452 LY_CHECK_GOTO(ret, finalize);
1453 }
1454 if (!valcopy && base_range) {
1455 if (max) {
1456 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
1457 } else {
1458 part->min_64 = base_range->parts[0].min_64;
1459 }
1460 if (!first) {
1461 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1462 }
1463 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001464 }
1465
1466 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001467 case LY_TYPE_INT8: /* range */
1468 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001469 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 +01001470 } else if (max) {
1471 part->max_64 = INT64_C(127);
1472 } else {
1473 part->min_64 = INT64_C(-128);
1474 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001475 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001476 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001477 }
1478 break;
1479 case LY_TYPE_INT16: /* range */
1480 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001481 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 +01001482 } else if (max) {
1483 part->max_64 = INT64_C(32767);
1484 } else {
1485 part->min_64 = INT64_C(-32768);
1486 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001487 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001488 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001489 }
1490 break;
1491 case LY_TYPE_INT32: /* range */
1492 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001493 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 +01001494 } else if (max) {
1495 part->max_64 = INT64_C(2147483647);
1496 } else {
1497 part->min_64 = INT64_C(-2147483648);
1498 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001499 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001500 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001501 }
1502 break;
1503 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001504 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001505 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001506 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001507 max ? &part->max_64 : &part->min_64);
1508 } else if (max) {
1509 part->max_64 = INT64_C(9223372036854775807);
1510 } else {
1511 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1512 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001513 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001514 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001515 }
1516 break;
1517 case LY_TYPE_UINT8: /* range */
1518 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001519 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 +01001520 } else if (max) {
1521 part->max_u64 = UINT64_C(255);
1522 } else {
1523 part->min_u64 = UINT64_C(0);
1524 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001525 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001526 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001527 }
1528 break;
1529 case LY_TYPE_UINT16: /* range */
1530 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001531 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 +01001532 } else if (max) {
1533 part->max_u64 = UINT64_C(65535);
1534 } else {
1535 part->min_u64 = UINT64_C(0);
1536 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001537 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001538 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001539 }
1540 break;
1541 case LY_TYPE_UINT32: /* range */
1542 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001543 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 +01001544 } else if (max) {
1545 part->max_u64 = UINT64_C(4294967295);
1546 } else {
1547 part->min_u64 = UINT64_C(0);
1548 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001549 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001550 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001551 }
1552 break;
1553 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001554 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001555 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001556 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001557 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 +01001558 } else if (max) {
1559 part->max_u64 = UINT64_C(18446744073709551615);
1560 } else {
1561 part->min_u64 = UINT64_C(0);
1562 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001563 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001564 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001565 }
1566 break;
1567 default:
1568 LOGINT(ctx->ctx);
1569 ret = LY_EINT;
1570 }
1571
Radek Krejci5969f272018-11-23 10:03:58 +01001572finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001573 if (ret == LY_EDENIED) {
1574 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1575 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1576 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1577 } else if (ret == LY_EVALID) {
1578 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1579 "Invalid %s restriction - invalid value \"%s\".",
1580 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1581 } else if (ret == LY_EEXIST) {
1582 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1583 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001584 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001585 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001586 } else if (!ret && value) {
1587 *value = *value + len;
1588 }
1589 free(valcopy);
1590 return ret;
1591}
1592
Radek Krejcia3045382018-11-22 14:30:31 +01001593/**
1594 * @brief Compile the parsed range restriction.
1595 * @param[in] ctx Compile context.
1596 * @param[in] range_p Parsed range structure to compile.
1597 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1598 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1599 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1600 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1601 * range restriction must be more restrictive than the base_range.
1602 * @param[in,out] range Pointer to the created current range structure.
1603 * @return LY_ERR value.
1604 */
Radek Krejci19a96102018-11-15 13:38:09 +01001605static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001606lys_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 +01001607 struct lysc_range *base_range, struct lysc_range **range)
1608{
1609 LY_ERR ret = LY_EVALID;
1610 const char *expr;
1611 struct lysc_range_part *parts = NULL, *part;
1612 int range_expected = 0, uns;
1613 unsigned int parts_done = 0, u, v;
1614
1615 assert(range);
1616 assert(range_p);
1617
1618 expr = range_p->arg;
1619 while(1) {
1620 if (isspace(*expr)) {
1621 ++expr;
1622 } else if (*expr == '\0') {
1623 if (range_expected) {
1624 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1625 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1626 length_restr ? "length" : "range", range_p->arg);
1627 goto cleanup;
1628 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
1629 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1630 "Invalid %s restriction - unexpected end of the expression (%s).",
1631 length_restr ? "length" : "range", range_p->arg);
1632 goto cleanup;
1633 }
1634 parts_done++;
1635 break;
1636 } else if (!strncmp(expr, "min", 3)) {
1637 if (parts) {
1638 /* min cannot be used elsewhere than in the first part */
1639 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1640 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1641 expr - range_p->arg, range_p->arg);
1642 goto cleanup;
1643 }
1644 expr += 3;
1645
1646 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001647 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 +01001648 part->max_64 = part->min_64;
1649 } else if (*expr == '|') {
1650 if (!parts || range_expected) {
1651 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1652 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1653 goto cleanup;
1654 }
1655 expr++;
1656 parts_done++;
1657 /* process next part of the expression */
1658 } else if (!strncmp(expr, "..", 2)) {
1659 expr += 2;
1660 while (isspace(*expr)) {
1661 expr++;
1662 }
1663 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
1664 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1665 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1666 goto cleanup;
1667 }
1668 /* continue expecting the upper boundary */
1669 range_expected = 1;
1670 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1671 /* number */
1672 if (range_expected) {
1673 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001674 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 +01001675 range_expected = 0;
1676 } else {
1677 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1678 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 +01001679 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001680 part->max_64 = part->min_64;
1681 }
1682
1683 /* continue with possible another expression part */
1684 } else if (!strncmp(expr, "max", 3)) {
1685 expr += 3;
1686 while (isspace(*expr)) {
1687 expr++;
1688 }
1689 if (*expr != '\0') {
1690 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1691 length_restr ? "length" : "range", expr);
1692 goto cleanup;
1693 }
1694 if (range_expected) {
1695 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001696 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 +01001697 range_expected = 0;
1698 } else {
1699 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1700 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 +01001701 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001702 part->min_64 = part->max_64;
1703 }
1704 } else {
1705 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1706 length_restr ? "length" : "range", expr);
1707 goto cleanup;
1708 }
1709 }
1710
1711 /* check with the previous range/length restriction */
1712 if (base_range) {
1713 switch (basetype) {
1714 case LY_TYPE_BINARY:
1715 case LY_TYPE_UINT8:
1716 case LY_TYPE_UINT16:
1717 case LY_TYPE_UINT32:
1718 case LY_TYPE_UINT64:
1719 case LY_TYPE_STRING:
1720 uns = 1;
1721 break;
1722 case LY_TYPE_DEC64:
1723 case LY_TYPE_INT8:
1724 case LY_TYPE_INT16:
1725 case LY_TYPE_INT32:
1726 case LY_TYPE_INT64:
1727 uns = 0;
1728 break;
1729 default:
1730 LOGINT(ctx->ctx);
1731 ret = LY_EINT;
1732 goto cleanup;
1733 }
1734 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1735 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1736 goto baseerror;
1737 }
1738 /* current lower bound is not lower than the base */
1739 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1740 /* base has single value */
1741 if (base_range->parts[v].min_64 == parts[u].min_64) {
1742 /* both lower bounds are the same */
1743 if (parts[u].min_64 != parts[u].max_64) {
1744 /* current continues with a range */
1745 goto baseerror;
1746 } else {
1747 /* equal single values, move both forward */
1748 ++v;
1749 continue;
1750 }
1751 } else {
1752 /* base is single value lower than current range, so the
1753 * value from base range is removed in the current,
1754 * move only base and repeat checking */
1755 ++v;
1756 --u;
1757 continue;
1758 }
1759 } else {
1760 /* base is the range */
1761 if (parts[u].min_64 == parts[u].max_64) {
1762 /* current is a single value */
1763 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1764 /* current is behind the base range, so base range is omitted,
1765 * move the base and keep the current for further check */
1766 ++v;
1767 --u;
1768 } /* else it is within the base range, so move the current, but keep the base */
1769 continue;
1770 } else {
1771 /* both are ranges - check the higher bound, the lower was already checked */
1772 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1773 /* higher bound is higher than the current higher bound */
1774 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1775 /* but the current lower bound is also higher, so the base range is omitted,
1776 * continue with the same current, but move the base */
1777 --u;
1778 ++v;
1779 continue;
1780 }
1781 /* current range starts within the base range but end behind it */
1782 goto baseerror;
1783 } else {
1784 /* current range is smaller than the base,
1785 * move current, but stay with the base */
1786 continue;
1787 }
1788 }
1789 }
1790 }
1791 if (u != parts_done) {
1792baseerror:
1793 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1794 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1795 length_restr ? "length" : "range", range_p->arg);
1796 goto cleanup;
1797 }
1798 }
1799
1800 if (!(*range)) {
1801 *range = calloc(1, sizeof **range);
1802 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1803 }
1804
Radek Krejcic8b31002019-01-08 10:24:45 +01001805 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001806 if (range_p->eapptag) {
1807 lydict_remove(ctx->ctx, (*range)->eapptag);
1808 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1809 }
1810 if (range_p->emsg) {
1811 lydict_remove(ctx->ctx, (*range)->emsg);
1812 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1813 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001814 if (range_p->dsc) {
1815 lydict_remove(ctx->ctx, (*range)->dsc);
1816 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1817 }
1818 if (range_p->ref) {
1819 lydict_remove(ctx->ctx, (*range)->ref);
1820 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1821 }
Radek Krejci19a96102018-11-15 13:38:09 +01001822 /* extensions are taken only from the last range by the caller */
1823
1824 (*range)->parts = parts;
1825 parts = NULL;
1826 ret = LY_SUCCESS;
1827cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001828 LY_ARRAY_FREE(parts);
1829
1830 return ret;
1831}
1832
1833/**
1834 * @brief Checks pattern syntax.
1835 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02001836 * @param[in] ctx Context.
1837 * @param[in] log_path Path for logging errors.
Radek Krejci19a96102018-11-15 13:38:09 +01001838 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001839 * @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 +01001840 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001841 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001842LY_ERR
1843lys_compile_type_pattern_check(struct ly_ctx *ctx, const char *log_path, const char *pattern, pcre2_code **code)
Radek Krejci19a96102018-11-15 13:38:09 +01001844{
Radek Krejci54579462019-04-30 12:47:06 +02001845 int idx, idx2, start, end, count;
Radek Krejci19a96102018-11-15 13:38:09 +01001846 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001847 int err_code;
1848 const char *orig_ptr;
1849 PCRE2_SIZE err_offset;
1850 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001851#define URANGE_LEN 19
1852 char *ublock2urange[][2] = {
1853 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1854 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1855 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1856 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1857 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1858 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1859 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1860 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1861 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1862 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1863 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1864 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1865 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1866 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1867 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1868 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1869 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1870 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1871 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1872 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1873 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1874 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1875 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1876 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1877 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1878 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1879 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1880 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1881 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1882 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1883 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1884 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1885 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1886 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1887 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1888 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1889 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1890 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1891 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1892 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1893 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1894 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1895 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1896 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1897 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1898 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1899 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1900 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1901 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1902 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1903 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1904 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1905 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1906 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1907 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1908 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1909 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1910 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1911 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1912 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1913 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1914 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1915 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1916 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1917 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1918 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1919 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1920 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1921 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1922 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1923 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1924 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1925 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1926 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1927 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1928 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1929 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1930 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1931 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1932 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1933 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1934 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1935 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1936 {NULL, NULL}
1937 };
1938
1939 /* adjust the expression to a Perl equivalent
1940 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1941
Michal Vasko03ff5a72019-09-11 13:49:33 +02001942 /* we need to replace all "$" and "^" with "\$" and "\^", count them now */
Radek Krejci19a96102018-11-15 13:38:09 +01001943 for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$"));
1944
1945 perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001946 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01001947 perl_regex[0] = '\0';
1948
1949 ptr = perl_regex;
1950
Radek Krejci19a96102018-11-15 13:38:09 +01001951 for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) {
1952 if (orig_ptr[0] == '$') {
1953 ptr += sprintf(ptr, "\\$");
1954 } else if (orig_ptr[0] == '^') {
1955 ptr += sprintf(ptr, "\\^");
1956 } else {
1957 ptr[0] = orig_ptr[0];
1958 ++ptr;
1959 }
1960 }
Radek Krejci5819f7c2019-05-31 14:53:29 +02001961 ptr[0] = '\0';
1962 ++ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01001963
1964 /* substitute Unicode Character Blocks with exact Character Ranges */
1965 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1966 start = ptr - perl_regex;
1967
1968 ptr = strchr(ptr, '}');
1969 if (!ptr) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001970 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01001971 pattern, perl_regex + start + 2, "unterminated character property");
1972 free(perl_regex);
1973 return LY_EVALID;
1974 }
1975 end = (ptr - perl_regex) + 1;
1976
1977 /* need more space */
1978 if (end - start < URANGE_LEN) {
1979 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001980 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01001981 }
1982
1983 /* find our range */
1984 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1985 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1986 break;
1987 }
1988 }
1989 if (!ublock2urange[idx][0]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001990 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01001991 pattern, perl_regex + start + 5, "unknown block name");
1992 free(perl_regex);
1993 return LY_EVALID;
1994 }
1995
1996 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1997 for (idx2 = 0, count = 0; idx2 < start; ++idx2) {
1998 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1999 ++count;
2000 }
2001 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
2002 --count;
2003 }
2004 }
2005 if (count) {
2006 /* skip brackets */
2007 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
2008 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
2009 } else {
2010 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
2011 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
2012 }
2013 }
2014
2015 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02002016 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
2017 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02002018 &err_code, &err_offset, NULL);
2019 if (!code_local) {
2020 PCRE2_UCHAR err_msg[256] = {0};
2021 pcre2_get_error_message(err_code, err_msg, 256);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002022 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Radek Krejci19a96102018-11-15 13:38:09 +01002023 free(perl_regex);
2024 return LY_EVALID;
2025 }
2026 free(perl_regex);
2027
Radek Krejci54579462019-04-30 12:47:06 +02002028 if (code) {
2029 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01002030 } else {
Radek Krejci54579462019-04-30 12:47:06 +02002031 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01002032 }
2033
2034 return LY_SUCCESS;
2035
2036#undef URANGE_LEN
2037}
2038
Radek Krejcia3045382018-11-22 14:30:31 +01002039/**
2040 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
2041 * @param[in] ctx Compile context.
2042 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002043 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
2044 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
2045 * @param[out] patterns Pointer to the storage for the patterns of the current type.
2046 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
2047 */
Radek Krejci19a96102018-11-15 13:38:09 +01002048static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002049lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci19a96102018-11-15 13:38:09 +01002050 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
2051{
2052 struct lysc_pattern **pattern;
Radek Krejci0935f412019-08-20 16:15:18 +02002053 unsigned int u;
Radek Krejci19a96102018-11-15 13:38:09 +01002054 LY_ERR ret = LY_SUCCESS;
2055
2056 /* first, copy the patterns from the base type */
2057 if (base_patterns) {
2058 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
2059 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
2060 }
2061
2062 LY_ARRAY_FOR(patterns_p, u) {
2063 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
2064 *pattern = calloc(1, sizeof **pattern);
2065 ++(*pattern)->refcount;
2066
Michal Vasko03ff5a72019-09-11 13:49:33 +02002067 ret = lys_compile_type_pattern_check(ctx->ctx, ctx->path, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01002068 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01002069
2070 if (patterns_p[u].arg[0] == 0x15) {
2071 (*pattern)->inverted = 1;
2072 }
Radek Krejci54579462019-04-30 12:47:06 +02002073 DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +01002074 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
2075 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01002076 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
2077 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejci0935f412019-08-20 16:15:18 +02002078 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002079 }
2080done:
2081 return ret;
2082}
2083
Radek Krejcia3045382018-11-22 14:30:31 +01002084/**
2085 * @brief map of the possible restrictions combination for the specific built-in type.
2086 */
Radek Krejci19a96102018-11-15 13:38:09 +01002087static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
2088 0 /* LY_TYPE_UNKNOWN */,
2089 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01002090 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
2091 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
2092 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
2093 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
2094 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01002095 LYS_SET_BIT /* LY_TYPE_BITS */,
2096 0 /* LY_TYPE_BOOL */,
2097 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
2098 0 /* LY_TYPE_EMPTY */,
2099 LYS_SET_ENUM /* LY_TYPE_ENUM */,
2100 LYS_SET_BASE /* LY_TYPE_IDENT */,
2101 LYS_SET_REQINST /* LY_TYPE_INST */,
2102 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01002103 LYS_SET_TYPE /* LY_TYPE_UNION */,
2104 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002105 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002106 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01002107 LYS_SET_RANGE /* LY_TYPE_INT64 */
2108};
2109
2110/**
2111 * @brief stringification of the YANG built-in data types
2112 */
2113const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
2114 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
2115 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01002116};
2117
Radek Krejcia3045382018-11-22 14:30:31 +01002118/**
2119 * @brief Compile parsed type's enum structures (for enumeration and bits types).
2120 * @param[in] ctx Compile context.
2121 * @param[in] enums_p Array of the parsed enum structures to compile.
2122 * @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 +01002123 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
2124 * @param[out] enums Newly created array of the compiled enums information for the current type.
2125 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2126 */
Radek Krejci19a96102018-11-15 13:38:09 +01002127static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002128lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci693262f2019-04-29 15:23:20 +02002129 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01002130{
2131 LY_ERR ret = LY_SUCCESS;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02002132 unsigned int u, v, match = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002133 int32_t value = 0;
2134 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02002135 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01002136
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002137 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002138 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
2139 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
2140 return LY_EVALID;
2141 }
2142
2143 LY_ARRAY_FOR(enums_p, u) {
2144 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
2145 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01002146 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
2147 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci693262f2019-04-29 15:23:20 +02002148 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01002149 if (base_enums) {
2150 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
2151 LY_ARRAY_FOR(base_enums, v) {
2152 if (!strcmp(e->name, base_enums[v].name)) {
2153 break;
2154 }
2155 }
2156 if (v == LY_ARRAY_SIZE(base_enums)) {
2157 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2158 "Invalid %s - derived type adds new item \"%s\".",
2159 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
2160 return LY_EVALID;
2161 }
2162 match = v;
2163 }
2164
2165 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02002166 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01002167 if (enums_p[u].flags & LYS_SET_VALUE) {
2168 e->value = (int32_t)enums_p[u].value;
2169 if (!u || e->value >= value) {
2170 value = e->value + 1;
2171 }
2172 /* check collision with other values */
2173 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
2174 if (e->value == (*enums)[v].value) {
2175 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2176 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
2177 e->value, e->name, (*enums)[v].name);
2178 return LY_EVALID;
2179 }
2180 }
2181 } else if (base_enums) {
2182 /* inherit the assigned value */
2183 e->value = base_enums[match].value;
2184 if (!u || e->value >= value) {
2185 value = e->value + 1;
2186 }
2187 } else {
2188 /* assign value automatically */
2189 if (u && value == INT32_MIN) {
2190 /* counter overflow */
2191 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2192 "Invalid enumeration - it is not possible to auto-assign enum value for "
2193 "\"%s\" since the highest value is already 2147483647.", e->name);
2194 return LY_EVALID;
2195 }
2196 e->value = value++;
2197 }
2198 } else { /* LY_TYPE_BITS */
2199 if (enums_p[u].flags & LYS_SET_VALUE) {
2200 e->value = (int32_t)enums_p[u].value;
2201 if (!u || (uint32_t)e->value >= position) {
2202 position = (uint32_t)e->value + 1;
2203 }
2204 /* check collision with other values */
2205 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
2206 if (e->value == (*enums)[v].value) {
2207 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2208 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
2209 (uint32_t)e->value, e->name, (*enums)[v].name);
2210 return LY_EVALID;
2211 }
2212 }
2213 } else if (base_enums) {
2214 /* inherit the assigned value */
2215 e->value = base_enums[match].value;
2216 if (!u || (uint32_t)e->value >= position) {
2217 position = (uint32_t)e->value + 1;
2218 }
2219 } else {
2220 /* assign value automatically */
2221 if (u && position == 0) {
2222 /* counter overflow */
2223 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2224 "Invalid bits - it is not possible to auto-assign bit position for "
2225 "\"%s\" since the highest value is already 4294967295.", e->name);
2226 return LY_EVALID;
2227 }
2228 e->value = position++;
2229 }
2230 }
2231
2232 if (base_enums) {
2233 /* the assigned values must not change from the derived type */
2234 if (e->value != base_enums[match].value) {
2235 if (basetype == LY_TYPE_ENUM) {
2236 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2237 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
2238 e->name, base_enums[match].value, e->value);
2239 } else {
2240 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2241 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
2242 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
2243 }
2244 return LY_EVALID;
2245 }
2246 }
2247
Radek Krejciec4da802019-05-02 13:02:41 +02002248 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002249 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 +01002250
2251 if (basetype == LY_TYPE_BITS) {
2252 /* keep bits ordered by position */
2253 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
2254 if (v != u) {
2255 memcpy(&storage, e, sizeof *e);
2256 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
2257 memcpy(&(*enums)[v], &storage, sizeof storage);
2258 }
2259 }
2260 }
2261
2262done:
2263 return ret;
2264}
2265
Radek Krejcia3045382018-11-22 14:30:31 +01002266#define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \
2267 for ((NODE) = (NODE)->parent; \
Michal Vasko03ff5a72019-09-11 13:49:33 +02002268 (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_NOTIF | LYS_ACTION)); \
Radek Krejcia3045382018-11-22 14:30:31 +01002269 (NODE) = (NODE)->parent); \
2270 if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \
2271 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \
2272 TERM; \
2273 }
2274
2275/**
2276 * @brief Validate the predicate(s) from the leafref path.
2277 * @param[in] ctx Compile context
2278 * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s).
2279 * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated.
Radek Krejci4ce68932018-11-28 12:53:36 +01002280 * @param[in] start_node Path context node (where the path is instantiated).
Radek Krejcia3045382018-11-22 14:30:31 +01002281 * @param[in] context_node Predicate context node (where the predicate is placed).
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002282 * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes.
Radek Krejcia3045382018-11-22 14:30:31 +01002283 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2284 */
2285static LY_ERR
Radek Krejci4ce68932018-11-28 12:53:36 +01002286lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate, const struct lysc_node *start_node,
Radek Krejcibade82a2018-12-05 10:13:48 +01002287 const struct lysc_node_list *context_node, const struct lys_module *path_context)
Radek Krejcia3045382018-11-22 14:30:31 +01002288{
2289 LY_ERR ret = LY_EVALID;
2290 const struct lys_module *mod;
2291 const struct lysc_node *src_node, *dst_node;
2292 const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix;
2293 size_t src_len, src_prefix_len, dst_len, dst_prefix_len;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002294 unsigned int dest_parent_times, c;
Radek Krejcia3045382018-11-22 14:30:31 +01002295 const char *start, *end, *pke_end;
2296 struct ly_set keys = {0};
2297 int i;
2298
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002299 assert(path_context);
2300
Radek Krejcia3045382018-11-22 14:30:31 +01002301 while (**predicate == '[') {
2302 start = (*predicate)++;
2303
2304 while (isspace(**predicate)) {
2305 ++(*predicate);
2306 }
Radek Krejcib4a4a272019-06-10 12:44:52 +02002307 LY_CHECK_GOTO(ly_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup);
Radek Krejcia3045382018-11-22 14:30:31 +01002308 while (isspace(**predicate)) {
2309 ++(*predicate);
2310 }
2311 if (**predicate != '=') {
2312 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002313 "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.",
2314 *predicate - start + 1, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002315 goto cleanup;
2316 }
2317 ++(*predicate);
2318 while (isspace(**predicate)) {
2319 ++(*predicate);
2320 }
2321
2322 if ((end = pke_end = strchr(*predicate, ']')) == NULL) {
2323 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2324 "Invalid leafref path predicate \"%s\" - missing predicate termination.", start);
2325 goto cleanup;
2326 }
2327 --pke_end;
2328 while (isspace(*pke_end)) {
2329 --pke_end;
2330 }
Radek Krejci7adf4ff2018-12-05 08:55:00 +01002331 ++pke_end;
Radek Krejcia3045382018-11-22 14:30:31 +01002332 /* localize path-key-expr */
2333 pke_start = path_key_expr = *predicate;
2334 /* move after the current predicate */
2335 *predicate = end + 1;
2336
2337 /* source (must be leaf or leaf-list) */
2338 if (src_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002339 mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002340 if (!mod) {
2341 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2342 "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002343 *predicate - start, start, src_prefix_len, src_prefix, path_context->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002344 goto cleanup;
2345 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002346 if (!mod->implemented) {
2347 /* make the module implemented */
Radek Krejci77a8bcd2019-09-11 11:20:02 +02002348 lys_set_implemented_internal((struct lys_module*)mod, 2);
Radek Krejci92cc8512019-04-25 09:57:06 +02002349 }
Radek Krejcia3045382018-11-22 14:30:31 +01002350 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002351 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002352 }
Radek Krejcibade82a2018-12-05 10:13:48 +01002353 src_node = NULL;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002354 if (!(context_node->flags & LYS_KEYLESS)) {
2355 struct lysc_node *key;
2356 for (key = context_node->child; key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY); key = key->next) {
Radek Krejci7f9b6512019-09-18 13:11:09 +02002357 if (!ly_strncmp(key->name, src, src_len)) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02002358 src_node = key;
Radek Krejcibade82a2018-12-05 10:13:48 +01002359 break;
2360 }
2361 }
2362 }
Radek Krejcia3045382018-11-22 14:30:31 +01002363 if (!src_node) {
2364 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002365 "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002366 *predicate - start, start, src_len, src, mod->name);
Radek Krejcia3045382018-11-22 14:30:31 +01002367 goto cleanup;
2368 }
Radek Krejcia3045382018-11-22 14:30:31 +01002369
2370 /* check that there is only one predicate for the */
Radek Krejcibade82a2018-12-05 10:13:48 +01002371 c = keys.count;
Radek Krejcia3045382018-11-22 14:30:31 +01002372 i = ly_set_add(&keys, (void*)src_node, 0);
2373 LY_CHECK_GOTO(i == -1, cleanup);
Radek Krejcibade82a2018-12-05 10:13:48 +01002374 if (keys.count == c) { /* node was already present in the set */
Radek Krejcia3045382018-11-22 14:30:31 +01002375 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002376 "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".",
Radek Krejcia3045382018-11-22 14:30:31 +01002377 *predicate - start, start, src_node->name);
2378 goto cleanup;
2379 }
2380
2381 /* destination */
2382 dest_parent_times = 0;
Radek Krejci4ce68932018-11-28 12:53:36 +01002383 dst_node = start_node;
Radek Krejcia3045382018-11-22 14:30:31 +01002384
2385 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002386 if (strncmp(path_key_expr, "current", 7)) {
2387error_current_function_invocation:
Radek Krejcia3045382018-11-22 14:30:31 +01002388 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2389 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
2390 *predicate - start, start);
2391 goto cleanup;
2392 }
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002393 for (path_key_expr += 7; isspace(*path_key_expr); ++path_key_expr);
2394 if (*path_key_expr != '(') {
2395 goto error_current_function_invocation;
Radek Krejcia3045382018-11-22 14:30:31 +01002396 }
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002397 for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr);
2398 if (*path_key_expr != ')') {
2399 goto error_current_function_invocation;
2400 }
2401 for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr);
Radek Krejcia3045382018-11-22 14:30:31 +01002402
2403 if (*path_key_expr != '/') {
2404 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2405 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
2406 *predicate - start, start);
2407 goto cleanup;
2408 }
2409 ++path_key_expr;
2410 while (isspace(*path_key_expr)) {
2411 ++path_key_expr;
2412 }
2413
2414 /* rel-path-keyexpr:
2415 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
2416 while (!strncmp(path_key_expr, "..", 2)) {
2417 ++dest_parent_times;
2418 path_key_expr += 2;
2419 while (isspace(*path_key_expr)) {
2420 ++path_key_expr;
2421 }
2422 if (*path_key_expr != '/') {
2423 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2424 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
2425 *predicate - start, start);
2426 goto cleanup;
2427 }
2428 ++path_key_expr;
2429 while (isspace(*path_key_expr)) {
2430 ++path_key_expr;
2431 }
2432
2433 /* path is supposed to be evaluated in data tree, so we have to skip
2434 * all schema nodes that cannot be instantiated in data tree */
2435 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
2436 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
2437 *predicate - start, start);
2438 }
2439 if (!dest_parent_times) {
2440 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2441 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
2442 *predicate - start, start);
2443 goto cleanup;
2444 }
2445 if (path_key_expr == pke_end) {
2446 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2447 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
2448 *predicate - start, start);
2449 goto cleanup;
2450 }
2451
2452 while(path_key_expr != pke_end) {
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002453 for (;*path_key_expr == '/' || isspace(*path_key_expr); ++path_key_expr);
Radek Krejcib4a4a272019-06-10 12:44:52 +02002454 if (ly_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +01002455 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcibade82a2018-12-05 10:13:48 +01002456 "Invalid node identifier in leafref path predicate - character %d (of %.*s).",
2457 path_key_expr - start + 1, *predicate - start, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002458 goto cleanup;
2459 }
2460
2461 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002462 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002463 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002464 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002465 }
2466 if (!mod) {
2467 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002468 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002469 *predicate - start, start, dst_len, dst);
2470 goto cleanup;
2471 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002472 if (!mod->implemented) {
2473 /* make the module implemented */
Radek Krejci77a8bcd2019-09-11 11:20:02 +02002474 lys_set_implemented_internal((struct lys_module*)mod, 2);
Radek Krejci92cc8512019-04-25 09:57:06 +02002475 }
Radek Krejcia3045382018-11-22 14:30:31 +01002476
2477 dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
2478 if (!dst_node) {
2479 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002480 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002481 *predicate - start, start, path_key_expr - pke_start, pke_start);
2482 goto cleanup;
2483 }
2484 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002485 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 +01002486 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002487 "Invalid leafref path predicate \"%.*s\" - rel-path-keyexpr \"%.*s\" refers %s instead of leaf.",
Radek Krejcia3045382018-11-22 14:30:31 +01002488 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
2489 goto cleanup;
2490 }
2491 }
2492
2493 ret = LY_SUCCESS;
2494cleanup:
2495 ly_set_erase(&keys, NULL);
2496 return ret;
2497}
2498
2499/**
2500 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2501 *
2502 * path-arg = absolute-path / relative-path
2503 * absolute-path = 1*("/" (node-identifier *path-predicate))
2504 * relative-path = 1*(".." "/") descendant-path
2505 *
2506 * @param[in,out] path Path to parse.
2507 * @param[out] prefix Prefix of the token, NULL if there is not any.
2508 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2509 * @param[out] name Name of the token.
2510 * @param[out] nam_len Length of the name.
2511 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2512 * must not be changed between consecutive calls. -1 if the
2513 * path is absolute.
2514 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2515 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2516 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002517LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002518lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2519 int *parent_times, int *has_predicate)
2520{
2521 int par_times = 0;
2522
2523 assert(path && *path);
2524 assert(parent_times);
2525 assert(prefix);
2526 assert(prefix_len);
2527 assert(name);
2528 assert(name_len);
2529 assert(has_predicate);
2530
2531 *prefix = NULL;
2532 *prefix_len = 0;
2533 *name = NULL;
2534 *name_len = 0;
2535 *has_predicate = 0;
2536
2537 if (!*parent_times) {
2538 if (!strncmp(*path, "..", 2)) {
2539 *path += 2;
2540 ++par_times;
2541 while (!strncmp(*path, "/..", 3)) {
2542 *path += 3;
2543 ++par_times;
2544 }
2545 }
2546 if (par_times) {
2547 *parent_times = par_times;
2548 } else {
2549 *parent_times = -1;
2550 }
2551 }
2552
2553 if (**path != '/') {
2554 return LY_EINVAL;
2555 }
2556 /* skip '/' */
2557 ++(*path);
2558
2559 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002560 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002561
2562 if ((**path == '/' && (*path)[1]) || !**path) {
2563 /* path continues by another token or this is the last token */
2564 return LY_SUCCESS;
2565 } else if ((*path)[0] != '[') {
2566 /* unexpected character */
2567 return LY_EINVAL;
2568 } else {
2569 /* predicate starting with [ */
2570 *has_predicate = 1;
2571 return LY_SUCCESS;
2572 }
2573}
2574
2575/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002576 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2577 *
2578 * The set of features used for target must be a subset of features used for the leafref.
2579 * This is not a perfect, we should compare the truth tables but it could require too much resources
2580 * and RFC 7950 does not require it explicitely, so we simplify that.
2581 *
2582 * @param[in] refnode The leafref node.
2583 * @param[in] target Tha target node of the leafref.
2584 * @return LY_SUCCESS or LY_EVALID;
2585 */
2586static LY_ERR
2587lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2588{
2589 LY_ERR ret = LY_EVALID;
2590 const struct lysc_node *iter;
Radek Krejci58d171e2018-11-23 13:50:55 +01002591 unsigned int u, v, count;
2592 struct ly_set features = {0};
2593
2594 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002595 if (iter->iffeatures) {
2596 LY_ARRAY_FOR(iter->iffeatures, u) {
2597 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2598 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002599 }
2600 }
2601 }
2602 }
2603
2604 /* we should have, in features set, a superset of features applicable to the target node.
2605 * So when adding features applicable to the target into the features set, we should not be
2606 * able to actually add any new feature, otherwise it is not a subset of features applicable
2607 * to the leafref itself. */
2608 count = features.count;
2609 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002610 if (iter->iffeatures) {
2611 LY_ARRAY_FOR(iter->iffeatures, u) {
2612 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2613 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002614 /* new feature was added (or LY_EMEM) */
2615 goto cleanup;
2616 }
2617 }
2618 }
2619 }
2620 }
2621 ret = LY_SUCCESS;
2622
2623cleanup:
2624 ly_set_erase(&features, NULL);
2625 return ret;
2626}
2627
2628/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002629 * @brief Check whether a leafref has an external dependency or not.
2630 *
2631 * @param[in] startnode Node with the leafref.
2632 * @param[in] local_mod Local module for the leafref path.
2633 * @param[in] first_node First found node when resolving the leafref.
2634 * @param[in] abs_path Whether the leafref path is absolute or relative.
2635 * @return 0 is the leafref does not require an external dependency, non-zero is it requires.
2636 */
2637static int
2638lys_compile_leafref_has_dep_flag(const struct lysc_node *startnode, const struct lys_module *local_mod,
2639 const struct lysc_node *first_node, int abs_path)
2640{
Michal Vasko03ff5a72019-09-11 13:49:33 +02002641 const struct lysc_node *op_node, *node;
2642
2643 /* find operation schema we are in */
2644 for (op_node = startnode->parent;
2645 op_node && !(op_node->nodetype & (LYS_ACTION | LYS_NOTIF));
2646 op_node = op_node->parent);
2647
2648 if (!op_node) {
2649 /* leafref pointing to a different module */
2650 if (local_mod != first_node->module) {
2651 return 1;
2652 }
2653 } else if (op_node->parent) {
2654 /* inner operation (notif/action) */
2655 if (abs_path) {
2656 return 1;
2657 } else {
Michal Vasko2d61e162019-09-24 13:59:27 +02002658 /* if first_node parent is not op_node, it is external */
2659 for (node = first_node; node && (node != op_node); node = node->parent);
2660 if (!node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002661 return 1;
2662 }
2663 }
2664 } else {
2665 /* top-level operation (notif/rpc) */
2666 if (op_node != first_node) {
2667 return 1;
2668 }
2669 }
2670
2671 return 0;
2672}
2673
2674/**
Radek Krejcia3045382018-11-22 14:30:31 +01002675 * @brief Validate the leafref path.
2676 * @param[in] ctx Compile context
2677 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01002678 * @param[in] leafref Leafref to validate.
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02002679 * @param[out] target Optional resolved leafref target.
Radek Krejcia3045382018-11-22 14:30:31 +01002680 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2681 */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02002682LY_ERR
2683lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref,
2684 const struct lysc_node **target)
Radek Krejcia3045382018-11-22 14:30:31 +01002685{
Michal Vasko03ff5a72019-09-11 13:49:33 +02002686 const struct lysc_node *node = NULL, *parent = NULL, *tmp_parent;
Radek Krejcia3045382018-11-22 14:30:31 +01002687 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002688 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01002689 const char *id, *prefix, *name;
2690 size_t prefix_len, name_len;
2691 int parent_times = 0, has_predicate;
2692 unsigned int iter, u;
2693 LY_ERR ret = LY_SUCCESS;
2694
2695 assert(ctx);
2696 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01002697 assert(leafref);
2698
Radek Krejci1c0c3442019-07-23 16:08:47 +02002699 ctx->path[0] = '\0';
Michal Vasko03ff5a72019-09-11 13:49:33 +02002700 lysc_path(startnode, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci1c0c3442019-07-23 16:08:47 +02002701 ctx->path_len = strlen(ctx->path);
Radek Krejci327de162019-06-14 12:52:07 +02002702
Radek Krejcia3045382018-11-22 14:30:31 +01002703 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002704 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01002705 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
2706 if (!iter) { /* first iteration */
2707 /* precess ".." in relative paths */
2708 if (parent_times > 0) {
2709 /* move from the context node */
2710 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
2711 /* path is supposed to be evaluated in data tree, so we have to skip
2712 * all schema nodes that cannot be instantiated in data tree */
2713 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002714 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002715 }
2716 }
2717 }
2718
2719 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01002720 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002721 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002722 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002723 }
2724 if (!mod) {
2725 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002726 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
2727 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002728 return LY_EVALID;
2729 }
Radek Krejci3d929562019-02-21 11:25:55 +01002730 if (!mod->implemented) {
2731 /* make the module implemented */
Radek Krejci77a8bcd2019-09-11 11:20:02 +02002732 lys_set_implemented_internal((struct lys_module*)mod, 2);
Radek Krejci3d929562019-02-21 11:25:55 +01002733 }
Radek Krejcia3045382018-11-22 14:30:31 +01002734
2735 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
2736 if (!node) {
2737 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002738 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002739 return LY_EVALID;
2740 }
2741 parent = node;
2742
Michal Vasko03ff5a72019-09-11 13:49:33 +02002743 if (!iter) {
2744 /* find module whose data will actually contain this leafref */
2745 for (tmp_parent = parent; tmp_parent->parent; tmp_parent = tmp_parent->parent);
2746
2747 /* set external dependency flag, we can decide based on the first found node */
2748 if (lys_compile_leafref_has_dep_flag(startnode, tmp_parent->module, node, (parent_times == -1 ? 1 : 0))) {
2749 startnode->flags |= LYS_LEAFREF_DEP;
2750 }
2751 }
2752
Radek Krejcia3045382018-11-22 14:30:31 +01002753 if (has_predicate) {
2754 /* we have predicate, so the current result must be list */
2755 if (node->nodetype != LYS_LIST) {
2756 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2757 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002758 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002759 return LY_EVALID;
2760 }
2761
Radek Krejcibade82a2018-12-05 10:13:48 +01002762 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context),
2763 LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01002764 }
2765
2766 ++iter;
2767 }
2768 if (ret) {
2769 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002770 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002771 return LY_EVALID;
2772 }
2773
2774 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2775 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2776 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002777 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002778 return LY_EVALID;
2779 }
2780
2781 /* check status */
2782 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2783 return LY_EVALID;
2784 }
2785
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002786 /* check config */
2787 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2788 if (node->flags & LYS_CONFIG_R) {
2789 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2790 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2791 leafref->path);
2792 return LY_EVALID;
2793 }
2794 }
2795
Radek Krejci412ddfa2018-11-23 11:44:11 +01002796 /* store the target's type and check for circular chain of leafrefs */
2797 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2798 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2799 if (type == (struct lysc_type*)leafref) {
2800 /* circular chain detected */
2801 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2802 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2803 return LY_EVALID;
2804 }
2805 }
2806
Radek Krejci58d171e2018-11-23 13:50:55 +01002807 /* check if leafref and its target are under common if-features */
2808 if (lys_compile_leafref_features_validate(startnode, node)) {
2809 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2810 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2811 leafref->path);
2812 return LY_EVALID;
2813 }
2814
Radek Krejci327de162019-06-14 12:52:07 +02002815 ctx->path_len = 1;
2816 ctx->path[1] = '\0';
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02002817 if (target) {
2818 *target = node;
2819 }
Radek Krejcia3045382018-11-22 14:30:31 +01002820 return LY_SUCCESS;
2821}
2822
Radek Krejcicdfecd92018-11-26 11:27:32 +01002823static 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 +02002824 struct lysp_type *type_p, struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002825/**
2826 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2827 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002828 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2829 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2830 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2831 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002832 * @param[in] type_p Parsed type to compile.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002833 * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path)
Radek Krejcia3045382018-11-22 14:30:31 +01002834 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002835 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2836 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2837 * @param[out] type Newly created type structure with the filled information about the type.
2838 * @return LY_ERR value.
2839 */
Radek Krejci19a96102018-11-15 13:38:09 +01002840static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002841lys_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 +02002842 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002843 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002844{
2845 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002846 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002847 struct lysc_type_bin *bin;
2848 struct lysc_type_num *num;
2849 struct lysc_type_str *str;
2850 struct lysc_type_bits *bits;
2851 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002852 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002853 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002854 struct lysc_type_union *un, *un_aux;
2855 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002856
2857 switch (basetype) {
2858 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002859 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002860
2861 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002862 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002863 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2864 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002865 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002866 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 +01002867 }
2868 }
2869
2870 if (tpdfname) {
2871 type_p->compiled = *type;
2872 *type = calloc(1, sizeof(struct lysc_type_bin));
2873 }
2874 break;
2875 case LY_TYPE_BITS:
2876 /* RFC 7950 9.7 - bits */
2877 bits = (struct lysc_type_bits*)(*type);
2878 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002879 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002880 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2881 (struct lysc_type_bitenum_item**)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002882 }
2883
Radek Krejci555cb5b2018-11-16 14:54:33 +01002884 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002885 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002886 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002887 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002888 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002889 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002890 free(*type);
2891 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002892 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002893 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002894 }
2895
2896 if (tpdfname) {
2897 type_p->compiled = *type;
2898 *type = calloc(1, sizeof(struct lysc_type_bits));
2899 }
2900 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002901 case LY_TYPE_DEC64:
2902 dec = (struct lysc_type_dec*)(*type);
2903
2904 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002905 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002906 if (!type_p->fraction_digits) {
2907 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002908 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002909 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002910 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002911 free(*type);
2912 *type = NULL;
2913 }
2914 return LY_EVALID;
2915 }
2916 } else if (type_p->fraction_digits) {
2917 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002918 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002919 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2920 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002921 tpdfname);
2922 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002923 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2924 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002925 free(*type);
2926 *type = NULL;
2927 }
2928 return LY_EVALID;
2929 }
2930 dec->fraction_digits = type_p->fraction_digits;
2931
2932 /* RFC 7950 9.2.4 - range */
2933 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002934 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2935 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002936 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002937 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 +01002938 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002939 }
2940
2941 if (tpdfname) {
2942 type_p->compiled = *type;
2943 *type = calloc(1, sizeof(struct lysc_type_dec));
2944 }
2945 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002946 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002947 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002948
2949 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002950 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002951 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2952 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002953 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002954 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 +01002955 }
2956 } else if (base && ((struct lysc_type_str*)base)->length) {
2957 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2958 }
2959
2960 /* RFC 7950 9.4.5 - pattern */
2961 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002962 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002963 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002964 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2965 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2966 }
2967
2968 if (tpdfname) {
2969 type_p->compiled = *type;
2970 *type = calloc(1, sizeof(struct lysc_type_str));
2971 }
2972 break;
2973 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002974 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002975
2976 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002977 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002978 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002979 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002980 }
2981
Radek Krejci555cb5b2018-11-16 14:54:33 +01002982 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002983 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002984 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002985 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002986 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002987 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002988 free(*type);
2989 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002990 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002991 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002992 }
2993
2994 if (tpdfname) {
2995 type_p->compiled = *type;
2996 *type = calloc(1, sizeof(struct lysc_type_enum));
2997 }
2998 break;
2999 case LY_TYPE_INT8:
3000 case LY_TYPE_UINT8:
3001 case LY_TYPE_INT16:
3002 case LY_TYPE_UINT16:
3003 case LY_TYPE_INT32:
3004 case LY_TYPE_UINT32:
3005 case LY_TYPE_INT64:
3006 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01003007 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01003008
3009 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01003010 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003011 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
3012 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01003013 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02003014 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 +01003015 }
3016 }
3017
3018 if (tpdfname) {
3019 type_p->compiled = *type;
3020 *type = calloc(1, sizeof(struct lysc_type_num));
3021 }
3022 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01003023 case LY_TYPE_IDENT:
3024 idref = (struct lysc_type_identityref*)(*type);
3025
3026 /* RFC 7950 9.10.2 - base */
3027 if (type_p->bases) {
3028 if (base) {
3029 /* only the directly derived identityrefs can contain base specification */
3030 if (tpdfname) {
3031 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01003032 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01003033 tpdfname);
3034 } else {
3035 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01003036 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01003037 free(*type);
3038 *type = NULL;
3039 }
3040 return LY_EVALID;
3041 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003042 LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01003043 }
3044
3045 if (!base && !type_p->flags) {
3046 /* type derived from identityref built-in type must contain at least one base */
3047 if (tpdfname) {
3048 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
3049 } else {
3050 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
3051 free(*type);
3052 *type = NULL;
3053 }
3054 return LY_EVALID;
3055 }
3056
3057 if (tpdfname) {
3058 type_p->compiled = *type;
3059 *type = calloc(1, sizeof(struct lysc_type_identityref));
3060 }
3061 break;
Radek Krejcia3045382018-11-22 14:30:31 +01003062 case LY_TYPE_LEAFREF:
3063 /* RFC 7950 9.9.3 - require-instance */
3064 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003065 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003066 if (tpdfname) {
3067 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3068 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
3069 } else {
3070 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3071 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
3072 free(*type);
3073 *type = NULL;
3074 }
3075 return LY_EVALID;
3076 }
Radek Krejcia3045382018-11-22 14:30:31 +01003077 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01003078 } else if (base) {
3079 /* inherit */
3080 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01003081 } else {
3082 /* default is true */
3083 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
3084 }
3085 if (type_p->path) {
3086 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01003087 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01003088 } else if (base) {
3089 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01003090 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01003091 } else if (tpdfname) {
3092 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
3093 return LY_EVALID;
3094 } else {
3095 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
3096 free(*type);
3097 *type = NULL;
3098 return LY_EVALID;
3099 }
3100 if (tpdfname) {
3101 type_p->compiled = *type;
3102 *type = calloc(1, sizeof(struct lysc_type_leafref));
3103 }
3104 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01003105 case LY_TYPE_INST:
3106 /* RFC 7950 9.9.3 - require-instance */
3107 if (type_p->flags & LYS_SET_REQINST) {
3108 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
3109 } else {
3110 /* default is true */
3111 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
3112 }
3113
3114 if (tpdfname) {
3115 type_p->compiled = *type;
3116 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3117 }
3118 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003119 case LY_TYPE_UNION:
3120 un = (struct lysc_type_union*)(*type);
3121
3122 /* RFC 7950 7.4 - type */
3123 if (type_p->types) {
3124 if (base) {
3125 /* only the directly derived union can contain types specification */
3126 if (tpdfname) {
3127 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
3128 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
3129 tpdfname);
3130 } else {
3131 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
3132 "Invalid type substatement for the type not directly derived from union built-in type.");
3133 free(*type);
3134 *type = NULL;
3135 }
3136 return LY_EVALID;
3137 }
3138 /* compile the type */
3139 additional = 0;
3140 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
3141 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Radek Krejciec4da802019-05-02 13:02:41 +02003142 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 +01003143 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
3144 /* add space for additional types from the union subtype */
3145 un_aux = (struct lysc_type_union *)un->types[u + additional];
3146 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)));
3147 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
3148 un->types = (void*)((uint32_t*)(p) + 1);
3149
3150 /* copy subtypes of the subtype union */
3151 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
3152 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
3153 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
3154 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
3155 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
3156 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
3157 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
3158 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
3159 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
3160 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
3161 /* TODO extensions */
3162
3163 } else {
3164 un->types[u + additional] = un_aux->types[v];
3165 ++un_aux->types[v]->refcount;
3166 }
3167 ++additional;
3168 LY_ARRAY_INCREMENT(un->types);
3169 }
3170 /* compensate u increment in main loop */
3171 --additional;
3172
3173 /* free the replaced union subtype */
3174 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
3175 } else {
3176 LY_ARRAY_INCREMENT(un->types);
3177 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01003178 }
3179 }
3180
3181 if (!base && !type_p->flags) {
3182 /* type derived from union built-in type must contain at least one type */
3183 if (tpdfname) {
3184 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
3185 } else {
3186 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
3187 free(*type);
3188 *type = NULL;
3189 }
3190 return LY_EVALID;
3191 }
3192
3193 if (tpdfname) {
3194 type_p->compiled = *type;
3195 *type = calloc(1, sizeof(struct lysc_type_union));
3196 }
3197 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01003198 case LY_TYPE_BOOL:
3199 case LY_TYPE_EMPTY:
3200 case LY_TYPE_UNKNOWN: /* just to complete switch */
3201 break;
3202 }
3203 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
3204done:
3205 return ret;
3206}
3207
Radek Krejcia3045382018-11-22 14:30:31 +01003208/**
3209 * @brief Compile information about the leaf/leaf-list's type.
3210 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01003211 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
3212 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
3213 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
3214 * @param[in] context_name Name of the context node or referencing typedef for logging.
3215 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01003216 * @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 +01003217 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01003218 * @return LY_ERR value.
3219 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01003220static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01003221lys_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 +02003222 struct lysp_type *type_p, struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01003223{
3224 LY_ERR ret = LY_SUCCESS;
3225 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003226 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01003227 struct type_context {
3228 const struct lysp_tpdf *tpdf;
3229 struct lysp_node *node;
3230 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003231 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01003232 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01003233 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003234 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01003235 const char *dflt = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02003236 struct lys_module *dflt_mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01003237
3238 (*type) = NULL;
3239
3240 tctx = calloc(1, sizeof *tctx);
3241 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01003242 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01003243 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
3244 ret == LY_SUCCESS;
3245 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
3246 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
3247 if (basetype) {
3248 break;
3249 }
3250
3251 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003252 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
3253 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01003254 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
3255
Radek Krejcicdfecd92018-11-26 11:27:32 +01003256 if (units && !*units) {
3257 /* inherit units */
3258 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
3259 }
Radek Krejci01342af2019-01-03 15:18:08 +01003260 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003261 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01003262 dflt = tctx->tpdf->dflt;
Radek Krejcia1911222019-07-22 17:24:50 +02003263 dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003264 }
Radek Krejci01342af2019-01-03 15:18:08 +01003265 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003266 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
3267 break;
3268 }
3269
Radek Krejci19a96102018-11-15 13:38:09 +01003270 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003271 /* it is not necessary to continue, the rest of the chain was already compiled,
3272 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01003273 basetype = tctx->tpdf->type.compiled->basetype;
3274 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01003275 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003276 dummyloops = 1;
3277 goto preparenext;
3278 } else {
3279 tctx = NULL;
3280 break;
3281 }
Radek Krejci19a96102018-11-15 13:38:09 +01003282 }
3283
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003284 /* circular typedef reference detection */
3285 for (u = 0; u < tpdf_chain.count; u++) {
3286 /* local part */
3287 tctx_iter = (struct type_context*)tpdf_chain.objs[u];
3288 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3289 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3290 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3291 free(tctx);
3292 ret = LY_EVALID;
3293 goto cleanup;
3294 }
3295 }
3296 for (u = 0; u < ctx->tpdf_chain.count; u++) {
3297 /* global part for unions corner case */
3298 tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u];
3299 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3300 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3301 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3302 free(tctx);
3303 ret = LY_EVALID;
3304 goto cleanup;
3305 }
3306 }
3307
Radek Krejci19a96102018-11-15 13:38:09 +01003308 /* store information for the following processing */
3309 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3310
Radek Krejcicdfecd92018-11-26 11:27:32 +01003311preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01003312 /* prepare next loop */
3313 tctx_prev = tctx;
3314 tctx = calloc(1, sizeof *tctx);
3315 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
3316 }
3317 free(tctx);
3318
3319 /* allocate type according to the basetype */
3320 switch (basetype) {
3321 case LY_TYPE_BINARY:
3322 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01003323 break;
3324 case LY_TYPE_BITS:
3325 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01003326 break;
3327 case LY_TYPE_BOOL:
3328 case LY_TYPE_EMPTY:
3329 *type = calloc(1, sizeof(struct lysc_type));
3330 break;
3331 case LY_TYPE_DEC64:
3332 *type = calloc(1, sizeof(struct lysc_type_dec));
3333 break;
3334 case LY_TYPE_ENUM:
3335 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01003336 break;
3337 case LY_TYPE_IDENT:
3338 *type = calloc(1, sizeof(struct lysc_type_identityref));
3339 break;
3340 case LY_TYPE_INST:
3341 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3342 break;
3343 case LY_TYPE_LEAFREF:
3344 *type = calloc(1, sizeof(struct lysc_type_leafref));
3345 break;
3346 case LY_TYPE_STRING:
3347 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01003348 break;
3349 case LY_TYPE_UNION:
3350 *type = calloc(1, sizeof(struct lysc_type_union));
3351 break;
3352 case LY_TYPE_INT8:
3353 case LY_TYPE_UINT8:
3354 case LY_TYPE_INT16:
3355 case LY_TYPE_UINT16:
3356 case LY_TYPE_INT32:
3357 case LY_TYPE_UINT32:
3358 case LY_TYPE_INT64:
3359 case LY_TYPE_UINT64:
3360 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003361 break;
3362 case LY_TYPE_UNKNOWN:
3363 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3364 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3365 ret = LY_EVALID;
3366 goto cleanup;
3367 }
3368 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003369 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003370 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3371 ly_data_type2str[basetype]);
3372 free(*type);
3373 (*type) = NULL;
3374 ret = LY_EVALID;
3375 goto cleanup;
3376 }
3377
3378 /* get restrictions from the referred typedefs */
3379 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
3380 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003381
3382 /* remember the typedef context for circular check */
3383 ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3384
Radek Krejci43699232018-11-23 14:59:46 +01003385 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003386 base = tctx->tpdf->type.compiled;
3387 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003388 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003389 /* no change, just use the type information from the base */
3390 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
3391 ++base->refcount;
3392 continue;
3393 }
3394
3395 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003396 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3397 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3398 tctx->tpdf->name, ly_data_type2str[basetype]);
3399 ret = LY_EVALID;
3400 goto cleanup;
3401 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3402 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3403 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3404 tctx->tpdf->name, tctx->tpdf->dflt);
3405 ret = LY_EVALID;
3406 goto cleanup;
3407 }
3408
Radek Krejci19a96102018-11-15 13:38:09 +01003409 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003410 /* TODO user type plugins */
3411 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003412 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003413 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 +01003414 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003415 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003416 LY_CHECK_GOTO(ret, cleanup);
3417 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003418 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003419 /* remove the processed typedef contexts from the stack for circular check */
3420 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003421
Radek Krejcic5c27e52018-11-15 14:38:11 +01003422 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003423 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003424 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003425 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003426 /* TODO user type plugins */
3427 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003428 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003429 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 +01003430 LY_CHECK_GOTO(ret, cleanup);
3431 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01003432 /* no specific restriction in leaf's type definition, copy from the base */
3433 free(*type);
3434 (*type) = base;
3435 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003436 }
Radek Krejcia1911222019-07-22 17:24:50 +02003437 if (dflt && !(*type)->dflt) {
3438 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003439 (*type)->dflt_mod = dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003440 (*type)->dflt = calloc(1, sizeof *(*type)->dflt);
3441 (*type)->dflt->realtype = (*type);
3442 ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt),
3443 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003444 lys_resolve_prefix, (void*)(*type)->dflt_mod, LYD_XML, NULL, NULL, (*type)->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003445 if (err) {
3446 ly_err_print(err);
3447 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3448 "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg);
3449 ly_err_free(err);
3450 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003451 if (ret == LY_EINCOMPLETE) {
3452 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003453 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, NULL, (*type)->dflt, (*type)->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003454
3455 /* but in general result is so far ok */
3456 ret = LY_SUCCESS;
3457 }
Radek Krejcia1911222019-07-22 17:24:50 +02003458 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci01342af2019-01-03 15:18:08 +01003459 }
Radek Krejci19a96102018-11-15 13:38:09 +01003460
Radek Krejci0935f412019-08-20 16:15:18 +02003461 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003462
3463cleanup:
3464 ly_set_erase(&tpdf_chain, free);
3465 return ret;
3466}
3467
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003468/**
3469 * @brief Compile status information of the given node.
3470 *
3471 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3472 * has the status correctly set during the compilation.
3473 *
3474 * @param[in] ctx Compile context
3475 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3476 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3477 * the compatibility with the parent's status value.
3478 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3479 * @return LY_ERR value.
3480 */
3481static LY_ERR
3482lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3483{
3484 /* status - it is not inherited by specification, but it does not make sense to have
3485 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3486 if (!((*node_flags) & LYS_STATUS_MASK)) {
3487 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3488 if ((parent_flags & 0x3) != 0x3) {
3489 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3490 * combination of bits (0x3) which marks the uses_status value */
3491 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3492 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3493 }
3494 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3495 } else {
3496 (*node_flags) |= LYS_STATUS_CURR;
3497 }
3498 } else if (parent_flags & LYS_STATUS_MASK) {
3499 /* check status compatibility with the parent */
3500 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3501 if ((*node_flags) & LYS_STATUS_CURR) {
3502 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3503 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3504 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3505 } else { /* LYS_STATUS_DEPRC */
3506 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3507 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3508 }
3509 return LY_EVALID;
3510 }
3511 }
3512 return LY_SUCCESS;
3513}
3514
Radek Krejci8cce8532019-03-05 11:27:45 +01003515/**
3516 * @brief Check uniqness of the node/action/notification name.
3517 *
3518 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3519 * structures, but they share the namespace so we need to check their name collisions.
3520 *
3521 * @param[in] ctx Compile context.
3522 * @param[in] children List (linked list) of data nodes to go through.
3523 * @param[in] actions List (sized array) of actions or RPCs to go through.
3524 * @param[in] notifs List (sized array) of Notifications to go through.
3525 * @param[in] name Name of the item to find in the given lists.
3526 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3527 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3528 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3529 */
3530static LY_ERR
3531lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children,
3532 const struct lysc_action *actions, const struct lysc_notif *notifs,
3533 const char *name, void *exclude)
3534{
3535 const struct lysc_node *iter;
3536 unsigned int u;
3537
3538 LY_LIST_FOR(children, iter) {
3539 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3540 goto error;
3541 }
3542 }
3543 LY_ARRAY_FOR(actions, u) {
3544 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3545 goto error;
3546 }
3547 }
3548 LY_ARRAY_FOR(notifs, u) {
3549 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3550 goto error;
3551 }
3552 }
3553 return LY_SUCCESS;
3554error:
3555 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification");
3556 return LY_EEXIST;
3557}
3558
Radek Krejcia0f704a2019-09-09 16:12:23 +02003559/**
3560 * @brief Get the XPath context node for the given schema node.
3561 * @param[in] start The schema node where the XPath expression appears.
3562 * @return The context node to evaluate XPath expression in given schema node.
3563 * @return NULL in case the context node is the root node.
3564 */
3565static struct lysc_node *
3566lysc_xpath_context(struct lysc_node *start)
3567{
3568 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF));
3569 start = start->parent);
3570 return start;
3571}
3572
Radek Krejciec4da802019-05-02 13:02:41 +02003573static 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 +01003574
Radek Krejcia3045382018-11-22 14:30:31 +01003575/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003576 * @brief Compile parsed RPC/action schema node information.
3577 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003578 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003579 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003580 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3581 * @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).
3582 * Zero means no uses, non-zero value with no status bit set mean the default status.
3583 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3584 */
3585static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003586lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003587 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3588{
3589 LY_ERR ret = LY_SUCCESS;
3590 struct lysp_node *child_p;
3591 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003592 int opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003593
Radek Krejci327de162019-06-14 12:52:07 +02003594 lysc_update_path(ctx, parent, action_p->name);
3595
Radek Krejci8cce8532019-03-05 11:27:45 +01003596 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3597 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3598 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3599 action_p->name, action)) {
3600 return LY_EVALID;
3601 }
3602
Radek Krejciec4da802019-05-02 13:02:41 +02003603 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003604 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003605 "Action \"%s\" is placed inside %s.", action_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003606 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003607 return LY_EVALID;
3608 }
3609
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003610 action->nodetype = LYS_ACTION;
3611 action->module = ctx->mod;
3612 action->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003613 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003614 action->sp = action_p;
3615 }
3616 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3617
3618 /* status - it is not inherited by specification, but it does not make sense to have
3619 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3620 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3621
3622 DUP_STRING(ctx->ctx, action_p->name, action->name);
3623 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3624 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003625 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003626 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003627
3628 /* input */
Radek Krejci327de162019-06-14 12:52:07 +02003629 lysc_update_path(ctx, (struct lysc_node*)action, "input");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003630 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003631 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 +02003632 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003633 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003634 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003635 }
Radek Krejci327de162019-06-14 12:52:07 +02003636 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003637 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003638
3639 /* output */
Radek Krejci327de162019-06-14 12:52:07 +02003640 lysc_update_path(ctx, (struct lysc_node*)action, "output");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003641 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003642 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 +02003643 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003644 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003645 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003646 }
Radek Krejci327de162019-06-14 12:52:07 +02003647 lysc_update_path(ctx, NULL, NULL);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003648
Radek Krejci327de162019-06-14 12:52:07 +02003649 lysc_update_path(ctx, NULL, NULL);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003650cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003651 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003652 return ret;
3653}
3654
3655/**
Radek Krejci43981a32019-04-12 09:44:11 +02003656 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003657 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003658 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003659 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3660 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3661 * @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 +02003662 * Zero means no uses, non-zero value with no status bit set mean the default status.
3663 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3664 */
3665static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003666lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003667 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3668{
3669 LY_ERR ret = LY_SUCCESS;
3670 struct lysp_node *child_p;
3671 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003672 int opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003673
Radek Krejci327de162019-06-14 12:52:07 +02003674 lysc_update_path(ctx, parent, notif_p->name);
3675
Radek Krejcifc11bd72019-04-11 16:00:05 +02003676 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3677 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3678 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3679 notif_p->name, notif)) {
3680 return LY_EVALID;
3681 }
3682
Radek Krejciec4da802019-05-02 13:02:41 +02003683 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003684 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3685 "Notification \"%s\" is placed inside %s.", notif_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003686 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003687 return LY_EVALID;
3688 }
3689
3690 notif->nodetype = LYS_NOTIF;
3691 notif->module = ctx->mod;
3692 notif->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003693 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003694 notif->sp = notif_p;
3695 }
3696 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3697
3698 /* status - it is not inherited by specification, but it does not make sense to have
3699 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3700 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3701
3702 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3703 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3704 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003705 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003706 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003707 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003708
Radek Krejciec4da802019-05-02 13:02:41 +02003709 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003710 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003711 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003712 }
3713
Radek Krejci327de162019-06-14 12:52:07 +02003714 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003715cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003716 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003717 return ret;
3718}
3719
3720/**
Radek Krejcia3045382018-11-22 14:30:31 +01003721 * @brief Compile parsed container node information.
3722 * @param[in] ctx Compile context
3723 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003724 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3725 * is enriched with the container-specific information.
3726 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3727 */
Radek Krejci19a96102018-11-15 13:38:09 +01003728static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003729lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003730{
3731 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3732 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3733 struct lysp_node *child_p;
3734 unsigned int u;
3735 LY_ERR ret = LY_SUCCESS;
3736
Radek Krejcife909632019-02-12 15:34:42 +01003737 if (cont_p->presence) {
3738 cont->flags |= LYS_PRESENCE;
3739 }
3740
Radek Krejci19a96102018-11-15 13:38:09 +01003741 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003742 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003743 }
3744
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003745 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02003746 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3747 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003748
3749done:
3750 return ret;
3751}
3752
Radek Krejci33f72892019-02-21 10:36:58 +01003753/*
3754 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3755 * @param[in] ctx Compile context.
3756 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3757 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003758 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3759 * @return LY_ERR value.
3760 */
3761static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003762lys_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 +01003763{
Radek Krejcia1911222019-07-22 17:24:50 +02003764 unsigned int u;
Radek Krejci33f72892019-02-21 10:36:58 +01003765 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3766
Radek Krejciec4da802019-05-02 13:02:41 +02003767 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 +01003768 leaf->units ? NULL : &leaf->units));
3769 if (leaf->nodetype == LYS_LEAFLIST) {
3770 if (llist->type->dflt && !llist->dflts && !llist->min) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003771 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM);
3772 llist->dflts_mods[0] = llist->type->dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003773 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
Radek Krejcia1911222019-07-22 17:24:50 +02003774 llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]);
3775 llist->dflts[0]->realtype = llist->type->dflt->realtype;
3776 llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3777 llist->dflts[0]->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003778 LY_ARRAY_INCREMENT(llist->dflts);
3779 }
3780 } else {
3781 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003782 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003783 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3784 leaf->dflt->realtype = leaf->type->dflt->realtype;
3785 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
3786 leaf->dflt->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003787 }
3788 }
3789 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3790 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3791 ly_set_add(&ctx->unres, leaf, 0);
3792 } else if (leaf->type->basetype == LY_TYPE_UNION) {
3793 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3794 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3795 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3796 ly_set_add(&ctx->unres, leaf, 0);
3797 }
3798 }
3799 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003800 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3801 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3802 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3803 return LY_EVALID;
3804 }
3805 }
3806
Radek Krejci33f72892019-02-21 10:36:58 +01003807 return LY_SUCCESS;
3808}
3809
Radek Krejcia3045382018-11-22 14:30:31 +01003810/**
3811 * @brief Compile parsed leaf node information.
3812 * @param[in] ctx Compile context
3813 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003814 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3815 * is enriched with the leaf-specific information.
3816 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3817 */
Radek Krejci19a96102018-11-15 13:38:09 +01003818static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003819lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003820{
3821 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3822 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3823 unsigned int u;
3824 LY_ERR ret = LY_SUCCESS;
3825
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003826 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003827 if (leaf_p->units) {
3828 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3829 leaf->flags |= LYS_SET_UNITS;
3830 }
Radek Krejcia1911222019-07-22 17:24:50 +02003831
3832 /* the dflt member is just filled to avoid getting the default value from the type */
3833 leaf->dflt = (void*)leaf_p->dflt;
3834 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
Radek Krejci812a5bf2019-09-09 09:18:05 +02003835 if (ret) {
3836 leaf->dflt = NULL;
3837 return ret;
3838 }
Radek Krejcia1911222019-07-22 17:24:50 +02003839
Radek Krejciccd20f12019-02-15 14:12:27 +01003840 if (leaf_p->dflt) {
Radek Krejcia1911222019-07-22 17:24:50 +02003841 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003842 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02003843 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3844 leaf->dflt->realtype = leaf->type;
3845 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt),
3846 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003847 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003848 leaf->dflt->realtype->refcount++;
3849 if (err) {
3850 ly_err_print(err);
3851 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3852 "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg);
3853 ly_err_free(err);
3854 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003855 if (ret == LY_EINCOMPLETE) {
Radek Krejci1c0c3442019-07-23 16:08:47 +02003856 /* postpone default compilation when the tree is complete */
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003857 LY_CHECK_RET(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod));
Radek Krejci1c0c3442019-07-23 16:08:47 +02003858
3859 /* but in general result is so far ok */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003860 ret = LY_SUCCESS;
3861 }
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003862 LY_CHECK_RET(ret);
Radek Krejci76b3e962018-12-14 17:01:25 +01003863 leaf->flags |= LYS_SET_DFLT;
3864 }
Radek Krejci43699232018-11-23 14:59:46 +01003865
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003866
Radek Krejci19a96102018-11-15 13:38:09 +01003867done:
3868 return ret;
3869}
3870
Radek Krejcia3045382018-11-22 14:30:31 +01003871/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003872 * @brief Compile parsed leaf-list node information.
3873 * @param[in] ctx Compile context
3874 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003875 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3876 * is enriched with the leaf-list-specific information.
3877 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3878 */
3879static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003880lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003881{
3882 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3883 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Radek Krejcia1911222019-07-22 17:24:50 +02003884 unsigned int u, v;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003885 LY_ERR ret = LY_SUCCESS;
3886
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003887 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003888 if (llist_p->units) {
3889 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3890 llist->flags |= LYS_SET_UNITS;
3891 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003892
Radek Krejcia1911222019-07-22 17:24:50 +02003893 /* the dflts member is just filled to avoid getting the default value from the type */
3894 llist->dflts = (void*)llist_p->dflts;
3895 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003896 if (llist_p->dflts) {
Radek Krejcia1911222019-07-22 17:24:50 +02003897 llist->dflts = NULL; /* reset the temporary llist_p->dflts */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003898 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003899 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
3900 LY_ARRAY_FOR(llist_p->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02003901 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003902 LY_ARRAY_INCREMENT(llist->dflts_mods);
3903 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003904 LY_ARRAY_INCREMENT(llist->dflts);
Radek Krejcia1911222019-07-22 17:24:50 +02003905 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
3906 llist->dflts[u]->realtype = llist->type;
3907 ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]),
3908 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003909 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003910 llist->dflts[u]->realtype->refcount++;
3911 if (err) {
3912 ly_err_print(err);
3913 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3914 "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg);
3915 ly_err_free(err);
3916 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003917 if (ret == LY_EINCOMPLETE) {
3918 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003919 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), done);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003920
3921 /* but in general result is so far ok */
3922 ret = LY_SUCCESS;
3923 }
Radek Krejcia1911222019-07-22 17:24:50 +02003924 LY_CHECK_GOTO(ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003925 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003926 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003927 }
Radek Krejcia1911222019-07-22 17:24:50 +02003928 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
3929 /* configuration data values must be unique - so check the default values */
3930 LY_ARRAY_FOR(llist->dflts, u) {
3931 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
3932 if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) {
3933 int dynamic = 0;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003934 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 +02003935 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3936 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
3937 if (dynamic) {
3938 free((char*)val);
3939 }
3940 return LY_EVALID;
3941 }
3942 }
3943 }
3944 }
3945
3946 /* 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 +01003947
3948 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003949 if (llist->min) {
3950 llist->flags |= LYS_MAND_TRUE;
3951 }
Radek Krejcib7408632018-11-28 17:12:11 +01003952 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003953
Radek Krejci0e5d8382018-11-28 16:37:53 +01003954done:
3955 return ret;
3956}
3957
3958/**
Radek Krejci7af64242019-02-18 13:07:53 +01003959 * @brief Compile information about list's uniques.
3960 * @param[in] ctx Compile context.
3961 * @param[in] context_module Module where the prefixes are going to be resolved.
3962 * @param[in] uniques Sized array list of unique statements.
3963 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3964 * @return LY_ERR value.
3965 */
3966static LY_ERR
3967lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3968{
3969 LY_ERR ret = LY_SUCCESS;
3970 struct lysc_node_leaf **key, ***unique;
3971 const char *keystr, *delim;
3972 size_t len;
3973 unsigned int v;
3974 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003975 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003976
3977 for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) {
3978 config = -1;
3979 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3980 keystr = uniques[v];
3981 while (keystr) {
3982 delim = strpbrk(keystr, " \t\n");
3983 if (delim) {
3984 len = delim - keystr;
3985 while (isspace(*delim)) {
3986 ++delim;
3987 }
3988 } else {
3989 len = strlen(keystr);
3990 }
3991
3992 /* unique node must be present */
3993 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003994 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3995 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003996 if (ret != LY_SUCCESS) {
3997 if (ret == LY_EDENIED) {
3998 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003999 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01004000 len, keystr, lys_nodetype2str((*key)->nodetype));
4001 }
4002 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004003 } else if (flags) {
4004 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4005 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
4006 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
4007 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01004008 }
4009
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004010
Radek Krejci7af64242019-02-18 13:07:53 +01004011 /* all referenced leafs must be of the same config type */
4012 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
4013 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4014 "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]);
4015 return LY_EVALID;
4016 } else if ((*key)->flags & LYS_CONFIG_W) {
4017 config = 1;
4018 } else { /* LYS_CONFIG_R */
4019 config = 0;
4020 }
4021
4022 /* check status */
4023 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
4024 (*key)->flags, (*key)->module, (*key)->name));
4025
4026 /* mark leaf as unique */
4027 (*key)->flags |= LYS_UNIQUE;
4028
4029 /* next unique value in line */
4030 keystr = delim;
4031 }
4032 /* next unique definition */
4033 }
4034
4035 return LY_SUCCESS;
4036}
4037
4038/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004039 * @brief Compile parsed list node information.
4040 * @param[in] ctx Compile context
4041 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004042 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4043 * is enriched with the list-specific information.
4044 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4045 */
4046static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004047lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004048{
4049 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
4050 struct lysc_node_list *list = (struct lysc_node_list*)node;
4051 struct lysp_node *child_p;
Radek Krejci0fe9b512019-07-26 17:51:05 +02004052 struct lysc_node_leaf *key, *prev_key = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004053 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01004054 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004055 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004056 LY_ERR ret = LY_SUCCESS;
4057
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004058 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01004059 if (list->min) {
4060 list->flags |= LYS_MAND_TRUE;
4061 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004062 list->max = list_p->max ? list_p->max : (uint32_t)-1;
4063
4064 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004065 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004066 }
4067
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004068 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004069
4070 /* keys */
4071 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
4072 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
4073 return LY_EVALID;
4074 }
4075
4076 /* find all the keys (must be direct children) */
4077 keystr = list_p->key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02004078 if (!keystr) {
4079 /* keyless list */
4080 list->flags |= LYS_KEYLESS;
4081 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004082 while (keystr) {
4083 delim = strpbrk(keystr, " \t\n");
4084 if (delim) {
4085 len = delim - keystr;
4086 while (isspace(*delim)) {
4087 ++delim;
4088 }
4089 } else {
4090 len = strlen(keystr);
4091 }
4092
4093 /* key node must be present */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004094 key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
4095 if (!(key)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004096 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4097 "The list's key \"%.*s\" not found.", len, keystr);
4098 return LY_EVALID;
4099 }
4100 /* keys must be unique */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004101 if (key->flags & LYS_KEY) {
4102 /* the node was already marked as a key */
4103 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4104 "Duplicated key identifier \"%.*s\".", len, keystr);
4105 return LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004106 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02004107
4108 lysc_update_path(ctx, (struct lysc_node*)list, key->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004109 /* key must have the same config flag as the list itself */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004110 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004111 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
4112 return LY_EVALID;
4113 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004114 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004115 /* YANG 1.0 denies key to be of empty type */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004116 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004117 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004118 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004119 return LY_EVALID;
4120 }
4121 } else {
4122 /* when and if-feature are illegal on list keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004123 if (key->when) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004124 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004125 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004126 return LY_EVALID;
4127 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02004128 if (key->iffeatures) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004129 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004130 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004131 return LY_EVALID;
4132 }
4133 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004134
4135 /* check status */
4136 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0fe9b512019-07-26 17:51:05 +02004137 key->flags, key->module, key->name));
Radek Krejci76b3e962018-12-14 17:01:25 +01004138
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004139 /* ignore default values of the key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004140 if (key->dflt) {
4141 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
4142 lysc_type_free(ctx->ctx, key->dflt->realtype);
4143 free(key->dflt);
4144 key->dflt = NULL;
4145 key->dflt_mod = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004146 }
4147 /* mark leaf as key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004148 key->flags |= LYS_KEY;
4149
4150 /* move it to the correct position */
4151 if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) {
4152 /* fix links in closest previous siblings of the key */
4153 if (key->next) {
4154 key->next->prev = key->prev;
4155 } else {
4156 /* last child */
4157 list->child->prev = key->prev;
4158 }
4159 if (key->prev->next) {
4160 key->prev->next = key->next;
4161 }
4162 /* fix links in the key */
4163 if (prev_key) {
4164 key->prev = (struct lysc_node*)prev_key;
4165 key->next = prev_key->next;
4166 } else {
4167 key->prev = list->child->prev;
4168 key->next = list->child;
4169 }
4170 /* fix links in closes future siblings of the key */
4171 if (prev_key) {
4172 if (prev_key->next) {
4173 prev_key->next->prev = (struct lysc_node*)key;
4174 } else {
4175 list->child->prev = (struct lysc_node*)key;
4176 }
4177 prev_key->next = (struct lysc_node*)key;
4178 } else {
4179 list->child->prev = (struct lysc_node*)key;
4180 }
4181 /* fix links in parent */
4182 if (!key->prev->next) {
4183 list->child = (struct lysc_node*)key;
4184 }
4185 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004186
4187 /* next key value */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004188 prev_key = key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004189 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02004190 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004191 }
4192
4193 /* uniques */
4194 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01004195 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004196 }
4197
Radek Krejciec4da802019-05-02 13:02:41 +02004198 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
4199 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004200
4201done:
4202 return ret;
4203}
4204
Radek Krejcib56c7502019-02-13 14:19:54 +01004205/**
4206 * @brief Do some checks and set the default choice's case.
4207 *
4208 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
4209 *
4210 * @param[in] ctx Compile context.
4211 * @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,
4212 * not the reference to the imported module.
4213 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
4214 * @return LY_ERR value.
4215 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004216static LY_ERR
4217lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
4218{
4219 struct lysc_node *iter, *node = (struct lysc_node*)ch;
4220 const char *prefix = NULL, *name;
4221 size_t prefix_len = 0;
4222
4223 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
4224 name = strchr(dflt, ':');
4225 if (name) {
4226 prefix = dflt;
4227 prefix_len = name - prefix;
4228 ++name;
4229 } else {
4230 name = dflt;
4231 }
Radek Krejci7f9b6512019-09-18 13:11:09 +02004232 if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004233 /* prefixed default case make sense only for the prefix of the schema itself */
4234 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4235 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
4236 prefix_len, prefix);
4237 return LY_EVALID;
4238 }
4239 ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
4240 if (!ch->dflt) {
4241 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4242 "Default case \"%s\" not found.", dflt);
4243 return LY_EVALID;
4244 }
4245 /* no mandatory nodes directly under the default case */
4246 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01004247 if (iter->parent != (struct lysc_node*)ch->dflt) {
4248 break;
4249 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004250 if (iter->flags & LYS_MAND_TRUE) {
4251 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4252 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
4253 return LY_EVALID;
4254 }
4255 }
Radek Krejci01342af2019-01-03 15:18:08 +01004256 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004257 return LY_SUCCESS;
4258}
4259
Radek Krejciccd20f12019-02-15 14:12:27 +01004260static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004261lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01004262{
4263 struct lys_module *mod;
4264 const char *prefix = NULL, *name;
4265 size_t prefix_len = 0;
4266 struct lysc_node_case *cs;
4267 struct lysc_node *node;
4268
4269 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
4270 name = strchr(dflt, ':');
4271 if (name) {
4272 prefix = dflt;
4273 prefix_len = name - prefix;
4274 ++name;
4275 } else {
4276 name = dflt;
4277 }
4278 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
4279 if (prefix) {
4280 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
4281 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004282 "Invalid deviation adding \"default\" property \"%s\" of choice. "
4283 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01004284 return LY_EVALID;
4285 }
4286 } else {
4287 mod = ctx->mod;
4288 }
4289 /* get the default case */
4290 cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
4291 if (!cs) {
4292 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004293 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01004294 return LY_EVALID;
4295 }
4296
4297 /* check that there is no mandatory node */
4298 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01004299 if (node->parent != (struct lysc_node*)cs) {
4300 break;
4301 }
Radek Krejciccd20f12019-02-15 14:12:27 +01004302 if (node->flags & LYS_MAND_TRUE) {
4303 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004304 "Invalid deviation adding \"default\" property \"%s\" of choice - "
4305 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01004306 return LY_EVALID;
4307 }
4308 }
4309
4310 /* set the default case in choice */
4311 ch->dflt = cs;
4312 cs->flags |= LYS_SET_DFLT;
4313
4314 return LY_SUCCESS;
4315}
4316
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004317/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004318 * @brief Compile parsed choice node information.
4319 * @param[in] ctx Compile context
4320 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004321 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01004322 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01004323 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4324 */
4325static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004326lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004327{
4328 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
4329 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
4330 struct lysp_node *child_p, *case_child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004331 LY_ERR ret = LY_SUCCESS;
4332
Radek Krejci056d0a82018-12-06 16:57:25 +01004333 LY_LIST_FOR(ch_p->child, child_p) {
4334 if (child_p->nodetype == LYS_CASE) {
4335 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004336 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004337 }
4338 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004339 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004340 }
4341 }
4342
4343 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01004344 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004345 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01004346 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004347
Radek Krejci9800fb82018-12-13 14:26:23 +01004348 return ret;
4349}
4350
4351/**
4352 * @brief Compile parsed anydata or anyxml node information.
4353 * @param[in] ctx Compile context
4354 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01004355 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4356 * is enriched with the any-specific information.
4357 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4358 */
4359static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004360lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004361{
4362 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
4363 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
4364 unsigned int u;
4365 LY_ERR ret = LY_SUCCESS;
4366
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004367 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Radek Krejci9800fb82018-12-13 14:26:23 +01004368
4369 if (any->flags & LYS_CONFIG_W) {
4370 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
Radek Krejcid6b76452019-09-03 17:03:03 +02004371 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML));
Radek Krejci9800fb82018-12-13 14:26:23 +01004372 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004373done:
4374 return ret;
4375}
4376
Radek Krejcib56c7502019-02-13 14:19:54 +01004377/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004378 * @brief Connect the node into the siblings list and check its name uniqueness.
4379 *
4380 * @param[in] ctx Compile context
4381 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4382 * the choice itself is expected instead of a specific case node.
4383 * @param[in] node Schema node to connect into the list.
4384 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
4385 */
4386static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004387lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004388{
4389 struct lysc_node **children;
4390
4391 if (node->nodetype == LYS_CASE) {
4392 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
4393 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004394 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01004395 }
4396 if (children) {
4397 if (!(*children)) {
4398 /* first child */
4399 *children = node;
4400 } else if (*children != node) {
4401 /* by the condition in previous branch we cover the choice/case children
4402 * - the children list is shared by the choice and the the first case, in addition
4403 * the first child of each case must be referenced from the case node. So the node is
4404 * actually always already inserted in case it is the first children - so here such
4405 * a situation actually corresponds to the first branch */
4406 /* insert at the end of the parent's children list */
4407 (*children)->prev->next = node;
4408 node->prev = (*children)->prev;
4409 (*children)->prev = node;
4410
4411 /* check the name uniqueness */
4412 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
4413 lysc_node_notifs(parent), node->name, node)) {
4414 return LY_EEXIST;
4415 }
4416 }
4417 }
4418 return LY_SUCCESS;
4419}
4420
Radek Krejci95710c92019-02-11 15:49:55 +01004421/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004422 * @brief Prepare the case structure in choice node for the new data node.
4423 *
4424 * 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
4425 * created in the choice when the first child was processed.
4426 *
4427 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004428 * @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,
4429 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004430 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4431 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4432 * @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,
4433 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004434 */
Radek Krejci056d0a82018-12-06 16:57:25 +01004435static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02004436lys_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 +01004437{
4438 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02004439 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01004440 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01004441 unsigned int u;
4442 LY_ERR ret;
4443
Radek Krejci95710c92019-02-11 15:49:55 +01004444#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01004445 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01004446 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01004447 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
4448 return NULL; \
4449 } \
4450 }
4451
Radek Krejci95710c92019-02-11 15:49:55 +01004452 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
4453 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004454
4455 /* we have to add an implicit case node into the parent choice */
4456 cs = calloc(1, sizeof(struct lysc_node_case));
4457 DUP_STRING(ctx->ctx, child->name, cs->name);
4458 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01004459 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004460 if (ch->cases && (node_p == ch->cases->prev->sp)) {
4461 /* the case is already present since the child is not its first children */
4462 return (struct lysc_node_case*)ch->cases->prev;
4463 }
Radek Krejci95710c92019-02-11 15:49:55 +01004464 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004465
4466 /* explicit parent case is not present (this is its first child) */
4467 cs = calloc(1, sizeof(struct lysc_node_case));
4468 DUP_STRING(ctx->ctx, node_p->name, cs->name);
4469 cs->flags = LYS_STATUS_MASK & node_p->flags;
4470 cs->sp = node_p;
4471
Radek Krejcib1b59152019-01-07 13:21:56 +01004472 /* 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 +02004473 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01004474
4475 if (node_p->when) {
4476 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02004477 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004478 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01004479 (*when)->context = lysc_xpath_context(ch->parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004480 }
Radek Krejciec4da802019-05-02 13:02:41 +02004481 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01004482 } else {
4483 LOGINT(ctx->ctx);
4484 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01004485 }
4486 cs->module = ctx->mod;
4487 cs->prev = (struct lysc_node*)cs;
4488 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02004489 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01004490 cs->parent = (struct lysc_node*)ch;
4491 cs->child = child;
4492
4493 return cs;
4494error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02004495 if (cs) {
4496 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
4497 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004498 return NULL;
4499
4500#undef UNIQUE_CHECK
4501}
4502
Radek Krejcib56c7502019-02-13 14:19:54 +01004503/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004504 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004505 *
4506 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004507 * @param[in] node Target node where the config is supposed to be changed.
4508 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004509 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4510 * 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 +01004511 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4512 * @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 +01004513 * @return LY_ERR value.
4514 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004515static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004516lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci327de162019-06-14 12:52:07 +02004517 int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004518{
4519 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004520 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004521
4522 if (config == (node->flags & LYS_CONFIG_MASK)) {
4523 /* nothing to do */
4524 return LY_SUCCESS;
4525 }
4526
4527 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004528 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004529 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4530 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004531 "Invalid %s of config - configuration node cannot be child of any state data node.",
4532 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004533 return LY_EVALID;
4534 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004535 node->flags |= LYS_SET_CONFIG;
4536 } else {
4537 if (node->flags & LYS_SET_CONFIG) {
4538 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4539 /* setting config flags, but have node with explicit config true */
4540 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004541 "Invalid %s of config - configuration node cannot be child of any state data node.",
4542 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004543 return LY_EVALID;
4544 }
4545 /* do not change config on nodes where the config is explicitely set, this does not apply to
4546 * nodes, which are being changed explicitly (targets of refine or deviation) */
4547 return LY_SUCCESS;
4548 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004549 }
4550 node->flags &= ~LYS_CONFIG_MASK;
4551 node->flags |= config;
4552
4553 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004554 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004555 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004556 }
4557
Radek Krejci76b3e962018-12-14 17:01:25 +01004558 return LY_SUCCESS;
4559}
4560
Radek Krejcib56c7502019-02-13 14:19:54 +01004561/**
4562 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4563 *
4564 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4565 * the flag to such parents from a mandatory children.
4566 *
4567 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4568 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4569 * (mandatory children was removed).
4570 */
Radek Krejcife909632019-02-12 15:34:42 +01004571void
4572lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4573{
4574 struct lysc_node *iter;
4575
4576 if (add) { /* set flag */
4577 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4578 parent = parent->parent) {
4579 parent->flags |= LYS_MAND_TRUE;
4580 }
4581 } else { /* unset flag */
4582 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004583 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004584 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004585 /* there is another mandatory node */
4586 return;
4587 }
4588 }
4589 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4590 parent->flags &= ~LYS_MAND_TRUE;
4591 }
4592 }
4593}
4594
Radek Krejci056d0a82018-12-06 16:57:25 +01004595/**
Radek Krejci3641f562019-02-13 15:38:40 +01004596 * @brief Internal sorting process for the lys_compile_augment_sort().
4597 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4598 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4599 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4600 */
4601static void
4602lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4603{
4604 unsigned int v;
4605 size_t len;
4606
4607 len = strlen(aug_p->nodeid);
4608 LY_ARRAY_FOR(result, v) {
4609 if (strlen(result[v]->nodeid) <= len) {
4610 continue;
4611 }
4612 if (v < LY_ARRAY_SIZE(result)) {
4613 /* move the rest of array */
4614 memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result);
4615 break;
4616 }
4617 }
4618 result[v] = aug_p;
4619 LY_ARRAY_INCREMENT(result);
4620}
4621
4622/**
4623 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4624 *
4625 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4626 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4627 *
4628 * @param[in] ctx Compile context.
4629 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4630 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4631 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4632 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4633 * @return LY_ERR value.
4634 */
4635LY_ERR
4636lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4637{
4638 struct lysp_augment **result = NULL;
4639 unsigned int u, v;
4640 size_t count = 0;
4641
4642 assert(augments);
4643
4644 /* get count of the augments in module and all its submodules */
4645 if (aug_p) {
4646 count += LY_ARRAY_SIZE(aug_p);
4647 }
4648 LY_ARRAY_FOR(inc_p, u) {
4649 if (inc_p[u].submodule->augments) {
4650 count += LY_ARRAY_SIZE(inc_p[u].submodule->augments);
4651 }
4652 }
4653
4654 if (!count) {
4655 *augments = NULL;
4656 return LY_SUCCESS;
4657 }
4658 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4659
4660 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4661 * together, so there can be even /z/y betwwen them. */
4662 LY_ARRAY_FOR(aug_p, u) {
4663 lys_compile_augment_sort_(&aug_p[u], result);
4664 }
4665 LY_ARRAY_FOR(inc_p, u) {
4666 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4667 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4668 }
4669 }
4670
4671 *augments = result;
4672 return LY_SUCCESS;
4673}
4674
4675/**
4676 * @brief Compile the parsed augment connecting it into its target.
4677 *
4678 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4679 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4680 * are already implemented and compiled.
4681 *
4682 * @param[in] ctx Compile context.
4683 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004684 * @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
4685 * children in case of the augmenting uses data.
4686 * @return LY_SUCCESS on success.
4687 * @return LY_EVALID on failure.
4688 */
4689LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004690lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004691{
4692 LY_ERR ret = LY_SUCCESS;
4693 struct lysp_node *node_p, *case_node_p;
4694 struct lysc_node *target; /* target target of the augment */
4695 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004696 struct lysc_when **when, *when_shared;
4697 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004698 uint16_t flags = 0;
4699 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02004700 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004701
Radek Krejci327de162019-06-14 12:52:07 +02004702 lysc_update_path(ctx, NULL, "{augment}");
4703 lysc_update_path(ctx, NULL, aug_p->nodeid);
4704
Radek Krejci7af64242019-02-18 13:07:53 +01004705 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004706 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004707 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004708 if (ret != LY_SUCCESS) {
4709 if (ret == LY_EDENIED) {
4710 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4711 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4712 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4713 }
4714 return LY_EVALID;
4715 }
4716
4717 /* check for mandatory nodes
4718 * - new cases augmenting some choice can have mandatory nodes
4719 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4720 */
Radek Krejci733988a2019-02-15 15:12:44 +01004721 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004722 allow_mandatory = 1;
4723 }
4724
4725 when_shared = NULL;
4726 LY_LIST_FOR(aug_p->child, node_p) {
4727 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4728 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
4729 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004730 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4731 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004732 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004733 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4734 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004735 return LY_EVALID;
4736 }
4737
4738 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004739 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004740 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004741 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004742 } else {
4743 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004744 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004745 }
4746 }
Radek Krejciec4da802019-05-02 13:02:41 +02004747 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004748
Radek Krejcife13da42019-02-15 14:51:01 +01004749 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4750 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004751 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004752 /* 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 +01004753 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 +01004754 } else if (target->nodetype == LYS_CHOICE) {
4755 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4756 node = ((struct lysc_node_choice*)target)->cases->prev;
4757 } else {
4758 /* the compiled node is the last child of the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004759 node = lysc_node_children(target, flags)->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004760 }
4761
Radek Krejci733988a2019-02-15 15:12:44 +01004762 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004763 node->flags &= ~LYS_MAND_TRUE;
4764 lys_compile_mandatory_parents(target, 0);
4765 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004766 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004767 return LY_EVALID;
4768 }
4769
4770 /* pass augment's when to all the children */
4771 if (aug_p->when) {
4772 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4773 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004774 ret = lys_compile_when(ctx, aug_p->when, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004775 LY_CHECK_GOTO(ret, error);
4776 (*when)->context = lysc_xpath_context(target);
4777 when_shared = *when;
4778 } else {
4779 ++when_shared->refcount;
4780 (*when) = when_shared;
4781 }
4782 }
4783 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004784
Radek Krejciec4da802019-05-02 13:02:41 +02004785 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004786 switch (target->nodetype) {
4787 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004788 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004789 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004790 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004791 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004792 break;
4793 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004794 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004795 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004796 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004797 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004798 break;
4799 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004800 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004801 if (aug_p->actions) {
4802 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004803 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4804 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004805 return LY_EVALID;
4806 }
4807 if (aug_p->notifs) {
4808 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004809 "Invalid augment of %s node which is not allowed to contain Notification node \"%s\".",
4810 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004811 return LY_EVALID;
4812 }
4813 }
Radek Krejci3641f562019-02-13 15:38:40 +01004814
Radek Krejci327de162019-06-14 12:52:07 +02004815 lysc_update_path(ctx, NULL, NULL);
4816 lysc_update_path(ctx, NULL, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004817error:
Radek Krejciec4da802019-05-02 13:02:41 +02004818 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004819 return ret;
4820}
4821
4822/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004823 * @brief Apply refined or deviated mandatory flag to the target node.
4824 *
4825 * @param[in] ctx Compile context.
4826 * @param[in] node Target node where the mandatory property is supposed to be changed.
4827 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004828 * @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 +01004829 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4830 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4831 * 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 +01004832 * @return LY_ERR value.
4833 */
4834static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004835lys_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 +01004836{
4837 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4838 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004839 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4840 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004841 return LY_EVALID;
4842 }
4843
4844 if (mandatory_flag & LYS_MAND_TRUE) {
4845 /* check if node has default value */
4846 if (node->nodetype & LYS_LEAF) {
4847 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004848 if (refine_flag) {
4849 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004850 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004851 return LY_EVALID;
4852 }
Radek Krejcia1911222019-07-22 17:24:50 +02004853 } else if (((struct lysc_node_leaf*)node)->dflt) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004854 /* remove the default value taken from the leaf's type */
Radek Krejcia1911222019-07-22 17:24:50 +02004855 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejci474f9b82019-07-24 11:36:37 +02004856
4857 /* update the list of incomplete default values if needed */
4858 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
4859
Radek Krejcia1911222019-07-22 17:24:50 +02004860 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4861 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4862 free(leaf->dflt);
4863 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004864 leaf->dflt_mod = NULL;
Radek Krejcif1421c22019-02-19 13:05:20 +01004865 }
4866 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004867 if (refine_flag) {
4868 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004869 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004870 return LY_EVALID;
4871 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004872 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004873 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004874 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 +01004875 return LY_EVALID;
4876 }
4877
4878 node->flags &= ~LYS_MAND_FALSE;
4879 node->flags |= LYS_MAND_TRUE;
4880 lys_compile_mandatory_parents(node->parent, 1);
4881 } else {
4882 /* make mandatory false */
4883 node->flags &= ~LYS_MAND_TRUE;
4884 node->flags |= LYS_MAND_FALSE;
4885 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcia1911222019-07-22 17:24:50 +02004886 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 +01004887 /* get the type's default value if any */
Radek Krejcia1911222019-07-22 17:24:50 +02004888 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004889 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02004890 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4891 leaf->dflt->realtype = leaf->type->dflt->realtype;
4892 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
4893 leaf->dflt->realtype->refcount++;
Radek Krejcif1421c22019-02-19 13:05:20 +01004894 }
4895 }
4896 return LY_SUCCESS;
4897}
4898
4899/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004900 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4901 * If present, also apply uses's modificators.
4902 *
4903 * @param[in] ctx Compile context
4904 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004905 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4906 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4907 * the compile context.
4908 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4909 */
4910static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004911lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent)
Radek Krejcie86bf772018-12-14 11:39:53 +01004912{
4913 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004914 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004915 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004916 struct lysc_node_container context_node_fake =
4917 {.nodetype = LYS_CONTAINER,
4918 .module = ctx->mod,
4919 .flags = parent ? parent->flags : 0,
4920 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004921 .prev = (struct lysc_node*)&context_node_fake,
4922 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004923 struct lysp_grp *grp = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01004924 unsigned int u, v, grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004925 int found;
4926 const char *id, *name, *prefix;
4927 size_t prefix_len, name_len;
4928 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004929 struct lysp_refine *rfn;
Radek Krejcia1911222019-07-22 17:24:50 +02004930 LY_ERR ret = LY_EVALID, rc;
Radek Krejcif2271f12019-01-07 16:42:23 +01004931 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004932 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004933 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004934 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004935 struct lysp_augment **augments = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004936 unsigned int actions_index, notifs_index;
4937 struct lysc_notif **notifs = NULL;
4938 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004939
4940 /* search for the grouping definition */
4941 found = 0;
4942 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004943 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004944 if (prefix) {
4945 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4946 if (!mod) {
4947 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004948 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004949 return LY_EVALID;
4950 }
4951 } else {
4952 mod = ctx->mod_def;
4953 }
4954 if (mod == ctx->mod_def) {
4955 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004956 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004957 LY_ARRAY_FOR(grp, u) {
4958 if (!strcmp(grp[u].name, name)) {
4959 grp = &grp[u];
4960 found = 1;
4961 break;
4962 }
4963 }
4964 }
4965 }
4966 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004967 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004968 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004969 if (grp) {
4970 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
4971 if (!strcmp(grp[u].name, name)) {
4972 grp = &grp[u];
4973 found = 1;
4974 }
4975 }
4976 }
4977 if (!found && mod->parsed->includes) {
4978 /* ... and all the submodules */
4979 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
4980 grp = mod->parsed->includes[u].submodule->groupings;
4981 if (grp) {
4982 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
4983 if (!strcmp(grp[v].name, name)) {
4984 grp = &grp[v];
4985 found = 1;
4986 }
4987 }
4988 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004989 }
4990 }
4991 }
4992 if (!found) {
4993 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4994 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4995 return LY_EVALID;
4996 }
4997
4998 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4999 grp_stack_count = ctx->groupings.count;
5000 ly_set_add(&ctx->groupings, (void*)grp, 0);
5001 if (grp_stack_count == ctx->groupings.count) {
5002 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
5003 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5004 "Grouping \"%s\" references itself through a uses statement.", grp->name);
5005 return LY_EVALID;
5006 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005007 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5008 /* remember that the grouping is instantiated to avoid its standalone validation */
5009 grp->flags |= LYS_USED_GRP;
5010 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005011
5012 /* switch context's mod_def */
5013 mod_old = ctx->mod_def;
5014 ctx->mod_def = mod;
5015
5016 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005017 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 +01005018
Radek Krejcifc11bd72019-04-11 16:00:05 +02005019 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01005020 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01005021 /* 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 +02005022 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 +01005023
5024 /* some preparation for applying refines */
5025 if (grp->data == node_p) {
5026 /* remember the first child */
Radek Krejci684faf22019-05-27 14:31:16 +02005027 if (parent) {
5028 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
5029 } else if (ctx->mod->compiled->data) {
5030 child = ctx->mod->compiled->data;
5031 } else {
5032 child = NULL;
5033 }
5034 context_node_fake.child = child ? child->prev : NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01005035 }
5036 }
Radek Krejci00b874b2019-02-12 10:54:50 +01005037 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01005038 LY_LIST_FOR(context_node_fake.child, child) {
5039 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01005040
Radek Krejcifc11bd72019-04-11 16:00:05 +02005041 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01005042 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005043 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01005044 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02005045 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, when), cleanup);
Radek Krejcib56c7502019-02-13 14:19:54 +01005046 (*when)->context = lysc_xpath_context(parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01005047 when_shared = *when;
5048 } else {
5049 ++when_shared->refcount;
5050 (*when) = when_shared;
5051 }
5052 }
Radek Krejci01342af2019-01-03 15:18:08 +01005053 }
5054 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005055 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01005056 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005057 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejciec4da802019-05-02 13:02:41 +02005058 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 +01005059 }
5060
Radek Krejcifc11bd72019-04-11 16:00:05 +02005061 /* compile actions */
5062 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
5063 if (actions) {
5064 actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02005065 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005066 if (*actions && (uses_p->augments || uses_p->refines)) {
5067 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
5068 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup);
5069 LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index;
5070 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
5071 }
5072 }
5073
5074 /* compile notifications */
5075 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
5076 if (notifs) {
5077 notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02005078 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005079 if (*notifs && (uses_p->augments || uses_p->refines)) {
5080 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
5081 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup);
5082 LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index;
5083 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
5084 }
5085 }
5086
5087
Radek Krejci3641f562019-02-13 15:38:40 +01005088 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005089 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01005090 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02005091 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01005092 }
Radek Krejci12fb9142019-01-08 09:45:30 +01005093
Radek Krejcif0089082019-01-07 16:42:01 +01005094 /* reload previous context's mod_def */
5095 ctx->mod_def = mod_old;
Radek Krejci327de162019-06-14 12:52:07 +02005096 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01005097
Radek Krejci76b3e962018-12-14 17:01:25 +01005098 /* apply refine */
5099 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02005100 lysc_update_path(ctx, NULL, rfn->nodeid);
5101
Radek Krejci7af64242019-02-18 13:07:53 +01005102 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 +01005103 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02005104 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01005105 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01005106
5107 /* default value */
5108 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01005109 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01005110 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005111 "Invalid refine of default - %s cannot hold %d default values.",
5112 lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005113 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01005114 }
5115 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
5116 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005117 "Invalid refine of default - %s cannot hold default value(s).",
5118 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005119 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01005120 }
5121 if (node->nodetype == LYS_LEAF) {
Radek Krejcia1911222019-07-22 17:24:50 +02005122 struct ly_err_item *err = NULL;
5123 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
5124 if (leaf->dflt) {
5125 /* remove the previous default value */
Radek Krejci474f9b82019-07-24 11:36:37 +02005126 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02005127 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5128 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5129 } else {
5130 /* prepare a new one */
5131 leaf->dflt = calloc(1, sizeof *leaf->dflt);
5132 leaf->dflt->realtype = leaf->type;
5133 }
5134 /* parse the new one */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005135 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005136 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]),
5137 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005138 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005139 leaf->dflt->realtype->refcount++;
5140 if (err) {
5141 ly_err_print(err);
5142 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5143 "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg);
5144 ly_err_free(err);
5145 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02005146 if (rc == LY_EINCOMPLETE) {
5147 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02005148 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02005149
5150 /* but in general result is so far ok */
5151 rc = LY_SUCCESS;
5152 }
Radek Krejcia1911222019-07-22 17:24:50 +02005153 LY_CHECK_GOTO(rc, cleanup);
5154 leaf->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01005155 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejcia1911222019-07-22 17:24:50 +02005156 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
5157
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005158 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01005159 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5160 "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 +02005161 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01005162 }
Radek Krejcia1911222019-07-22 17:24:50 +02005163
5164 /* remove previous set of default values */
5165 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci474f9b82019-07-24 11:36:37 +02005166 lysc_incomplete_dflts_remove(ctx, llist->dflts[u]);
Radek Krejcia1911222019-07-22 17:24:50 +02005167 llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]);
5168 lysc_type_free(ctx->ctx, llist->dflts[u]->realtype);
5169 free(llist->dflts[u]);
Radek Krejci76b3e962018-12-14 17:01:25 +01005170 }
Radek Krejcia1911222019-07-22 17:24:50 +02005171 LY_ARRAY_FREE(llist->dflts);
5172 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005173 LY_ARRAY_FREE(llist->dflts_mods);
5174 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005175
5176 /* create the new set of the default values */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005177 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02005178 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005179 LY_ARRAY_FOR(rfn->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005180 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005181 LY_ARRAY_INCREMENT(llist->dflts_mods);
5182 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005183 LY_ARRAY_INCREMENT(llist->dflts);
5184 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
5185 llist->dflts[u]->realtype = llist->type;
Radek Krejci1c0c3442019-07-23 16:08:47 +02005186 rc = llist->type->plugin->store(ctx->ctx, llist->type, rfn->dflts[u], strlen(rfn->dflts[u]),
Radek Krejcia1911222019-07-22 17:24:50 +02005187 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005188 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005189 llist->dflts[u]->realtype->refcount++;
5190 if (err) {
5191 ly_err_print(err);
5192 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5193 "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg);
5194 ly_err_free(err);
5195 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02005196 if (rc == LY_EINCOMPLETE) {
5197 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02005198 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02005199
5200 /* but in general result is so far ok */
5201 rc = LY_SUCCESS;
5202 }
5203 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005204 }
Radek Krejcia1911222019-07-22 17:24:50 +02005205 llist->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01005206 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01005207 if (((struct lysc_node_choice*)node)->dflt) {
5208 /* unset LYS_SET_DFLT from the current default case */
5209 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
5210 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005211 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 +01005212 }
5213 }
5214
Radek Krejci12fb9142019-01-08 09:45:30 +01005215 /* description */
5216 if (rfn->dsc) {
5217 FREE_STRING(ctx->ctx, node->dsc);
5218 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
5219 }
5220
5221 /* reference */
5222 if (rfn->ref) {
5223 FREE_STRING(ctx->ctx, node->ref);
5224 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
5225 }
Radek Krejci76b3e962018-12-14 17:01:25 +01005226
5227 /* config */
5228 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005229 if (!flags) {
Radek Krejci327de162019-06-14 12:52:07 +02005230 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005231 } else {
5232 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
5233 flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path);
5234 }
Radek Krejci76b3e962018-12-14 17:01:25 +01005235 }
5236
5237 /* mandatory */
5238 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci327de162019-06-14 12:52:07 +02005239 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005240 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01005241
5242 /* presence */
5243 if (rfn->presence) {
5244 if (node->nodetype != LYS_CONTAINER) {
5245 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005246 "Invalid refine of presence statement - %s cannot hold the presence statement.",
5247 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005248 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01005249 }
5250 node->flags |= LYS_PRESENCE;
5251 }
Radek Krejci9a564c92019-01-07 14:53:57 +01005252
5253 /* must */
5254 if (rfn->musts) {
5255 switch (node->nodetype) {
5256 case LYS_LEAF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005257 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 +01005258 break;
5259 case LYS_LEAFLIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005260 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 +01005261 break;
5262 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005263 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 +01005264 break;
5265 case LYS_CONTAINER:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005266 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 +01005267 break;
5268 case LYS_ANYXML:
5269 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005270 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 +01005271 break;
5272 default:
5273 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005274 "Invalid refine of must statement - %s cannot hold any must statement.",
5275 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005276 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01005277 }
5278 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005279
5280 /* min/max-elements */
5281 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5282 switch (node->nodetype) {
5283 case LYS_LEAFLIST:
5284 if (rfn->flags & LYS_SET_MAX) {
5285 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5286 }
5287 if (rfn->flags & LYS_SET_MIN) {
5288 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005289 if (rfn->min) {
5290 node->flags |= LYS_MAND_TRUE;
5291 lys_compile_mandatory_parents(node->parent, 1);
5292 } else {
5293 node->flags &= ~LYS_MAND_TRUE;
5294 lys_compile_mandatory_parents(node->parent, 0);
5295 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005296 }
5297 break;
5298 case LYS_LIST:
5299 if (rfn->flags & LYS_SET_MAX) {
5300 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5301 }
5302 if (rfn->flags & LYS_SET_MIN) {
5303 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005304 if (rfn->min) {
5305 node->flags |= LYS_MAND_TRUE;
5306 lys_compile_mandatory_parents(node->parent, 1);
5307 } else {
5308 node->flags &= ~LYS_MAND_TRUE;
5309 lys_compile_mandatory_parents(node->parent, 0);
5310 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005311 }
5312 break;
5313 default:
5314 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005315 "Invalid refine of %s statement - %s cannot hold this statement.",
5316 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005317 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01005318 }
5319 }
Radek Krejcif0089082019-01-07 16:42:01 +01005320
5321 /* if-feature */
5322 if (rfn->iffeatures) {
5323 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02005324 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01005325 }
Radek Krejci327de162019-06-14 12:52:07 +02005326
5327 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01005328 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005329
Radek Krejcif2271f12019-01-07 16:42:23 +01005330 /* do some additional checks of the changed nodes when all the refines are applied */
5331 for (u = 0; u < refined.count; ++u) {
5332 node = (struct lysc_node*)refined.objs[u];
5333 rfn = &uses_p->refines[u];
Radek Krejci327de162019-06-14 12:52:07 +02005334 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01005335
5336 /* check possible conflict with default value (default added, mandatory left true) */
5337 if ((node->flags & LYS_MAND_TRUE) &&
5338 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
5339 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
5340 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005341 "Invalid refine of default - the node is mandatory.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005342 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005343 }
5344
5345 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5346 if (node->nodetype == LYS_LIST) {
5347 min = ((struct lysc_node_list*)node)->min;
5348 max = ((struct lysc_node_list*)node)->max;
5349 } else {
5350 min = ((struct lysc_node_leaflist*)node)->min;
5351 max = ((struct lysc_node_leaflist*)node)->max;
5352 }
5353 if (min > max) {
5354 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005355 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
5356 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005357 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005358 }
5359 }
5360 }
5361
Radek Krejci327de162019-06-14 12:52:07 +02005362 lysc_update_path(ctx, NULL, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005363 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005364
5365cleanup:
5366 /* fix connection of the children nodes from fake context node back into the parent */
5367 if (context_node_fake.child) {
5368 context_node_fake.child->prev = child;
5369 }
5370 LY_LIST_FOR(context_node_fake.child, child) {
5371 child->parent = parent;
5372 }
5373
5374 if (uses_p->augments || uses_p->refines) {
5375 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005376 if (context_node_fake.actions) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005377 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
5378 LY_ARRAY_FREE(context_node_fake.actions);
5379 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005380 if (context_node_fake.notifs) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005381 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
5382 LY_ARRAY_FREE(context_node_fake.notifs);
5383 }
5384 }
5385
Radek Krejcie86bf772018-12-14 11:39:53 +01005386 /* reload previous context's mod_def */
5387 ctx->mod_def = mod_old;
5388 /* remove the grouping from the stack for circular groupings dependency check */
5389 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5390 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01005391 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01005392 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005393
5394 return ret;
5395}
5396
Radek Krejci327de162019-06-14 12:52:07 +02005397static int
5398lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5399{
5400 struct lysp_node *iter;
5401 int len = 0;
5402
5403 *path = NULL;
5404 for (iter = node; iter && len >= 0; iter = iter->parent) {
5405 char *s = *path;
5406 char *id;
5407
5408 switch (iter->nodetype) {
5409 case LYS_USES:
5410 asprintf(&id, "{uses='%s'}", iter->name);
5411 break;
5412 case LYS_GROUPING:
5413 asprintf(&id, "{grouping='%s'}", iter->name);
5414 break;
5415 case LYS_AUGMENT:
5416 asprintf(&id, "{augment='%s'}", iter->name);
5417 break;
5418 default:
5419 id = strdup(iter->name);
5420 break;
5421 }
5422
5423 if (!iter->parent) {
5424 /* print prefix */
5425 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5426 } else {
5427 /* prefix is the same as in parent */
5428 len = asprintf(path, "/%s%s", id, s ? s : "");
5429 }
5430 free(s);
5431 free(id);
5432 }
5433
5434 if (len < 0) {
5435 free(*path);
5436 *path = NULL;
5437 } else if (len == 0) {
5438 *path = strdup("/");
5439 len = 1;
5440 }
5441 return len;
5442}
5443
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005444/**
5445 * @brief Validate groupings that were defined but not directly used in the schema itself.
5446 *
5447 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5448 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5449 */
5450static LY_ERR
5451lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5452{
5453 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005454 char *path;
5455 int len;
5456
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005457 struct lysp_node_uses fake_uses = {
5458 .parent = node_p,
5459 .nodetype = LYS_USES,
5460 .flags = 0, .next = NULL,
5461 .name = grp->name,
5462 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5463 .refines = NULL, .augments = NULL
5464 };
5465 struct lysc_node_container fake_container = {
5466 .nodetype = LYS_CONTAINER,
5467 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5468 .module = ctx->mod,
5469 .sp = NULL, .parent = NULL, .next = NULL,
5470 .prev = (struct lysc_node*)&fake_container,
5471 .name = "fake",
5472 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5473 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5474 };
5475
5476 if (grp->parent) {
5477 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5478 }
Radek Krejci327de162019-06-14 12:52:07 +02005479
5480 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5481 if (len < 0) {
5482 LOGMEM(ctx->ctx);
5483 return LY_EMEM;
5484 }
5485 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
5486 ctx->path_len = (uint16_t)len;
5487 free(path);
5488
5489 lysc_update_path(ctx, NULL, "{grouping}");
5490 lysc_update_path(ctx, NULL, grp->name);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005491 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container);
Radek Krejci327de162019-06-14 12:52:07 +02005492 lysc_update_path(ctx, NULL, NULL);
5493 lysc_update_path(ctx, NULL, NULL);
5494
5495 ctx->path_len = 1;
5496 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005497
5498 /* cleanup */
5499 lysc_node_container_free(ctx->ctx, &fake_container);
5500
5501 return ret;
5502}
Radek Krejcife909632019-02-12 15:34:42 +01005503
Radek Krejcie86bf772018-12-14 11:39:53 +01005504/**
Radek Krejcia3045382018-11-22 14:30:31 +01005505 * @brief Compile parsed schema node information.
5506 * @param[in] ctx Compile context
5507 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005508 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5509 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5510 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005511 * @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).
5512 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005513 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5514 */
Radek Krejci19a96102018-11-15 13:38:09 +01005515static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005516lys_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 +01005517{
5518 LY_ERR ret = LY_EVALID;
Radek Krejci056d0a82018-12-06 16:57:25 +01005519 struct lysc_node *node;
5520 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01005521 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01005522 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02005523 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01005524
Radek Krejci327de162019-06-14 12:52:07 +02005525 if (node_p->nodetype != LYS_USES) {
5526 lysc_update_path(ctx, parent, node_p->name);
5527 } else {
5528 lysc_update_path(ctx, NULL, "{uses}");
5529 lysc_update_path(ctx, NULL, node_p->name);
5530 }
5531
Radek Krejci19a96102018-11-15 13:38:09 +01005532 switch (node_p->nodetype) {
5533 case LYS_CONTAINER:
5534 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
5535 node_compile_spec = lys_compile_node_container;
5536 break;
5537 case LYS_LEAF:
5538 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
5539 node_compile_spec = lys_compile_node_leaf;
5540 break;
5541 case LYS_LIST:
5542 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005543 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005544 break;
5545 case LYS_LEAFLIST:
5546 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005547 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005548 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005549 case LYS_CHOICE:
5550 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005551 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005552 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005553 case LYS_ANYXML:
5554 case LYS_ANYDATA:
5555 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005556 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005557 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005558 case LYS_USES:
Radek Krejci327de162019-06-14 12:52:07 +02005559 ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent);
5560 lysc_update_path(ctx, NULL, NULL);
5561 lysc_update_path(ctx, NULL, NULL);
5562 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005563 default:
5564 LOGINT(ctx->ctx);
5565 return LY_EINT;
5566 }
5567 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5568 node->nodetype = node_p->nodetype;
5569 node->module = ctx->mod;
5570 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005571 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005572
5573 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02005574 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005575 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005576 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02005577 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5578 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005579 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005580 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005581 node->flags |= LYS_CONFIG_R;
5582 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005583 /* config not explicitely set, inherit it from parent */
5584 if (parent) {
5585 node->flags |= parent->flags & LYS_CONFIG_MASK;
5586 } else {
5587 /* default is config true */
5588 node->flags |= LYS_CONFIG_W;
5589 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005590 } else {
5591 /* config set explicitely */
5592 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01005593 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005594 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5595 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5596 "Configuration node cannot be child of any state data node.");
5597 goto error;
5598 }
Radek Krejci19a96102018-11-15 13:38:09 +01005599
Radek Krejcia6d57732018-11-29 13:40:37 +01005600 /* *list ordering */
5601 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5602 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005603 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02005604 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5605 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005606 node->flags &= ~LYS_ORDBY_MASK;
5607 node->flags |= LYS_ORDBY_SYSTEM;
5608 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5609 /* default ordering is system */
5610 node->flags |= LYS_ORDBY_SYSTEM;
5611 }
5612 }
5613
Radek Krejci19a96102018-11-15 13:38:09 +01005614 /* status - it is not inherited by specification, but it does not make sense to have
5615 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01005616 if (!parent || parent->nodetype != LYS_CHOICE) {
5617 /* in case of choice/case's children, postpone the check to the moment we know if
5618 * 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 +01005619 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 +01005620 }
5621
Radek Krejciec4da802019-05-02 13:02:41 +02005622 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005623 node->sp = node_p;
5624 }
5625 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01005626 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
5627 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01005628 if (node_p->when) {
5629 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02005630 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01005631 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01005632 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +01005633 }
Radek Krejciec4da802019-05-02 13:02:41 +02005634 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005635
5636 /* nodetype-specific part */
Radek Krejciec4da802019-05-02 13:02:41 +02005637 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005638
Radek Krejci0935f412019-08-20 16:15:18 +02005639 COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error);
5640
Radek Krejcife909632019-02-12 15:34:42 +01005641 /* inherit LYS_MAND_TRUE in parent containers */
5642 if (node->flags & LYS_MAND_TRUE) {
5643 lys_compile_mandatory_parents(parent, 1);
5644 }
5645
Radek Krejci327de162019-06-14 12:52:07 +02005646 lysc_update_path(ctx, NULL, NULL);
5647
Radek Krejci19a96102018-11-15 13:38:09 +01005648 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01005649 if (parent) {
5650 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci327de162019-06-14 12:52:07 +02005651 if (node_p->parent->nodetype == LYS_CASE) {
5652 lysc_update_path(ctx, parent, node_p->parent->name);
5653 } else {
5654 lysc_update_path(ctx, parent, node->name);
5655 }
Radek Krejciec4da802019-05-02 13:02:41 +02005656 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01005657 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01005658 if (uses_status) {
5659
5660 }
Radek Krejci056d0a82018-12-06 16:57:25 +01005661 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01005662 * it directly gets the same status flags as the choice;
5663 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005664 LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01005665 node->parent = (struct lysc_node*)cs;
Radek Krejci327de162019-06-14 12:52:07 +02005666 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005667 } else { /* other than choice */
Radek Krejci327de162019-06-14 12:52:07 +02005668 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005669 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01005670 }
Radek Krejciec4da802019-05-02 13:02:41 +02005671 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 +02005672
5673 if (parent->nodetype == LYS_CHOICE) {
5674 lysc_update_path(ctx, NULL, NULL);
5675 }
Radek Krejci19a96102018-11-15 13:38:09 +01005676 } else {
5677 /* top-level element */
5678 if (!ctx->mod->compiled->data) {
5679 ctx->mod->compiled->data = node;
5680 } else {
5681 /* insert at the end of the module's top-level nodes list */
5682 ctx->mod->compiled->data->prev->next = node;
5683 node->prev = ctx->mod->compiled->data->prev;
5684 ctx->mod->compiled->data->prev = node;
5685 }
Radek Krejci327de162019-06-14 12:52:07 +02005686 lysc_update_path(ctx, parent, node->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01005687 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
5688 ctx->mod->compiled->notifs, node->name, node)) {
5689 return LY_EVALID;
5690 }
Radek Krejci19a96102018-11-15 13:38:09 +01005691 }
Radek Krejci327de162019-06-14 12:52:07 +02005692 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005693
5694 return LY_SUCCESS;
5695
5696error:
5697 lysc_node_free(ctx->ctx, node);
5698 return ret;
5699}
5700
Radek Krejciccd20f12019-02-15 14:12:27 +01005701static void
5702lysc_disconnect(struct lysc_node *node)
5703{
Radek Krejci0a048dd2019-02-18 16:01:43 +01005704 struct lysc_node *parent, *child, *prev = NULL, *next;
5705 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005706 int remove_cs = 0;
5707
5708 parent = node->parent;
5709
5710 /* parent's first child */
5711 if (!parent) {
5712 return;
5713 }
5714 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005715 cs = (struct lysc_node_case*)node;
5716 } else if (parent->nodetype == LYS_CASE) {
5717 /* disconnecting some node in a case */
5718 cs = (struct lysc_node_case*)parent;
5719 parent = cs->parent;
5720 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
5721 if (child == node) {
5722 if (cs->child == child) {
5723 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5724 /* case with a single child -> remove also the case */
5725 child->parent = NULL;
5726 remove_cs = 1;
5727 } else {
5728 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005729 }
5730 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005731 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005732 }
5733 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005734 if (!remove_cs) {
5735 cs = NULL;
5736 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005737 } else if (lysc_node_children(parent, node->flags) == node) {
5738 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005739 }
5740
5741 if (cs) {
5742 if (remove_cs) {
5743 /* cs has only one child which is being also removed */
5744 lysc_disconnect((struct lysc_node*)cs);
5745 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5746 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005747 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5748 /* default case removed */
5749 ((struct lysc_node_choice*)parent)->dflt = NULL;
5750 }
5751 if (((struct lysc_node_choice*)parent)->cases == cs) {
5752 /* first case removed */
5753 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5754 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005755 if (cs->child) {
5756 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5757 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5758 prev = cs->child->prev;
5759 } /* else all the children are under a single case */
5760 LY_LIST_FOR_SAFE(cs->child, next, child) {
5761 if (child->parent != (struct lysc_node*)cs) {
5762 break;
5763 }
5764 lysc_node_free(node->module->ctx, child);
5765 }
5766 if (prev) {
5767 if (prev->next) {
5768 prev->next = child;
5769 }
5770 if (child) {
5771 child->prev = prev;
5772 } else {
5773 /* link from the first child under the cases */
5774 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5775 }
5776 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005777 }
5778 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005779 }
5780
5781 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005782 if (node->prev->next) {
5783 node->prev->next = node->next;
5784 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005785 if (node->next) {
5786 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005787 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005788 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005789 if (child) {
5790 child->prev = node->prev;
5791 }
5792 } else if (((struct lysc_node_choice*)parent)->cases) {
5793 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005794 }
5795}
5796
5797LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005798lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
Radek Krejciccd20f12019-02-15 14:12:27 +01005799{
Radek Krejcia1911222019-07-22 17:24:50 +02005800 LY_ERR ret = LY_EVALID, rc;
Radek Krejciccd20f12019-02-15 14:12:27 +01005801 struct ly_set devs_p = {0};
5802 struct ly_set targets = {0};
5803 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005804 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005805 struct lysc_action *rpcs;
5806 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005807 struct lysp_deviation *dev;
5808 struct lysp_deviate *d, **dp_new;
5809 struct lysp_deviate_add *d_add;
5810 struct lysp_deviate_del *d_del;
5811 struct lysp_deviate_rpl *d_rpl;
Radek Krejci7af64242019-02-18 13:07:53 +01005812 unsigned int u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005813 struct lysc_deviation {
5814 const char *nodeid;
5815 struct lysc_node *target; /* target node of the deviation */
5816 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005817 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005818 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5819 } **devs = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005820 int i, changed_type;
Radek Krejciccd20f12019-02-15 14:12:27 +01005821 size_t prefix_len, name_len;
Radek Krejcia1911222019-07-22 17:24:50 +02005822 const char *prefix, *name, *nodeid, *dflt;
Radek Krejciccd20f12019-02-15 14:12:27 +01005823 struct lys_module *mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005824 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005825 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005826
5827 /* get all deviations from the module and all its submodules ... */
5828 LY_ARRAY_FOR(mod_p->deviations, u) {
5829 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5830 }
5831 LY_ARRAY_FOR(mod_p->includes, v) {
5832 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5833 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5834 }
5835 }
5836 if (!devs_p.count) {
5837 /* nothing to do */
5838 return LY_SUCCESS;
5839 }
5840
Radek Krejci327de162019-06-14 12:52:07 +02005841 lysc_update_path(ctx, NULL, "{deviation}");
5842
Radek Krejciccd20f12019-02-15 14:12:27 +01005843 /* ... and group them by the target node */
5844 devs = calloc(devs_p.count, sizeof *devs);
5845 for (u = 0; u < devs_p.count; ++u) {
5846 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02005847 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005848
5849 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005850 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5851 (const struct lysc_node**)&target, &flags), cleanup);
Radek Krejcif538ce52019-03-05 10:46:14 +01005852 if (target->nodetype == LYS_ACTION) {
5853 /* move the target pointer to input/output to make them different from the action and
5854 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5855 * back to the RPC/action node due to a better compatibility and decision code in this function.
5856 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5857 if (flags & LYSC_OPT_RPC_INPUT) {
5858 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5859 flags |= LYSC_OPT_INTERNAL;
5860 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5861 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5862 flags |= LYSC_OPT_INTERNAL;
5863 }
5864 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005865 /* insert into the set of targets with duplicity detection */
5866 i = ly_set_add(&targets, target, 0);
5867 if (!devs[i]) {
5868 /* new record */
5869 devs[i] = calloc(1, sizeof **devs);
5870 devs[i]->target = target;
5871 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005872 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005873 }
5874 /* add deviates into the deviation's list of deviates */
5875 for (d = dev->deviates; d; d = d->next) {
5876 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5877 *dp_new = d;
5878 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5879 devs[i]->not_supported = 1;
5880 }
5881 }
Radek Krejci327de162019-06-14 12:52:07 +02005882
5883 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01005884 }
5885
5886 /* MACROS for deviates checking */
5887#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5888 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
Radek Krejci327de162019-06-14 12:52:07 +02005889 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 +01005890 goto cleanup; \
5891 }
5892
5893#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5894 if (LY_ARRAY_SIZE(ARRAY) > MAX) { \
Radek Krejci327de162019-06-14 12:52:07 +02005895 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%u) %s properties.", \
5896 lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005897 goto cleanup; \
5898 }
5899
Radek Krejcia1911222019-07-22 17:24:50 +02005900
Radek Krejciccd20f12019-02-15 14:12:27 +01005901#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejcia1911222019-07-22 17:24:50 +02005902 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
5903 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5904 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5905 PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5906 goto cleanup; \
5907 }
5908
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005909#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005910 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejcia1911222019-07-22 17:24:50 +02005911 int dynamic_ = 0; const char *val_; \
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005912 val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \
5913 lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005914 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejcia1911222019-07-22 17:24:50 +02005915 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5916 if (dynamic_) {free((void*)val_);} \
Radek Krejciccd20f12019-02-15 14:12:27 +01005917 goto cleanup; \
5918 }
5919
Radek Krejci551b12c2019-02-19 16:11:21 +01005920#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5921 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5922 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005923 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5924 PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005925 goto cleanup; \
5926 }
5927
Radek Krejciccd20f12019-02-15 14:12:27 +01005928#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5929 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
Radek Krejci327de162019-06-14 12:52:07 +02005930 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005931 goto cleanup; \
5932 }
5933
Radek Krejci551b12c2019-02-19 16:11:21 +01005934#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5935 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5936 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005937 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d_rpl->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005938 goto cleanup; \
5939 }
5940
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005941#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5942 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5943 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5944 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5945 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 +01005946 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005947 if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005948 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005949 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5950 PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005951 goto cleanup; \
5952 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005953 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5954 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5955 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5956 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
5957 (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005958 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005959 if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
5960 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5961 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005962 }
5963
5964 /* apply deviations */
5965 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005966 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target;
5967 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target;
5968 struct ly_err_item *err = NULL;
5969
5970 dflt = NULL;
5971 changed_type = 0;
5972
Radek Krejci327de162019-06-14 12:52:07 +02005973 lysc_update_path(ctx, NULL, devs[u]->nodeid);
5974
Radek Krejcif538ce52019-03-05 10:46:14 +01005975 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5976 /* fix the target pointer in case of RPC's/action's input/output */
5977 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5978 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5979 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5980 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5981 }
5982 }
5983
Radek Krejciccd20f12019-02-15 14:12:27 +01005984 /* not-supported */
5985 if (devs[u]->not_supported) {
5986 if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) {
5987 LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.",
5988 LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid);
5989 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005990
5991#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5992 if (devs[u]->target->parent) { \
5993 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
5994 } else { \
5995 ARRAY = devs[u]->target->module->compiled->ARRAY; \
5996 } \
5997 LY_ARRAY_FOR(ARRAY, x) { \
5998 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
5999 } \
6000 if (x < LY_ARRAY_SIZE(ARRAY)) { \
6001 FREEFUNC(ctx->ctx, &ARRAY[x]); \
6002 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \
6003 LY_ARRAY_DECREMENT(ARRAY); \
6004 }
6005
Radek Krejcif538ce52019-03-05 10:46:14 +01006006 if (devs[u]->target->nodetype == LYS_ACTION) {
6007 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
6008 /* remove RPC's/action's input */
6009 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
6010 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02006011 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
6012 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01006013 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
6014 /* remove RPC's/action's output */
6015 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
6016 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02006017 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
6018 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01006019 } else {
6020 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02006021 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01006022 }
6023 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02006024 /* remove Notification */
6025 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01006026 } else {
6027 /* remove the target node */
6028 lysc_disconnect(devs[u]->target);
6029 lysc_node_free(ctx->ctx, devs[u]->target);
6030 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006031
Radek Krejci474f9b82019-07-24 11:36:37 +02006032 /* mark the context for later re-compilation of objects that could reference the curently removed node */
6033 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
Radek Krejciccd20f12019-02-15 14:12:27 +01006034 continue;
6035 }
6036
6037 /* list of deviates (not-supported is not present in the list) */
6038 LY_ARRAY_FOR(devs[u]->deviates, v) {
6039 d = devs[u]->deviates[v];
6040
6041 switch (d->mod) {
6042 case LYS_DEV_ADD:
6043 d_add = (struct lysp_deviate_add*)d;
6044 /* [units-stmt] */
6045 if (d_add->units) {
6046 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
6047 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
6048
6049 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6050 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6051 }
6052
6053 /* *must-stmt */
6054 if (d_add->musts) {
6055 switch (devs[u]->target->nodetype) {
6056 case LYS_CONTAINER:
6057 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02006058 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
6059 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006060 break;
6061 case LYS_LEAF:
6062 case LYS_LEAFLIST:
6063 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02006064 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
6065 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006066 break;
6067 case LYS_NOTIF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02006068 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
6069 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006070 break;
6071 case LYS_ACTION:
6072 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02006073 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
6074 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006075 break;
6076 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02006077 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
6078 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006079 break;
6080 }
6081 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01006082 default:
6083 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006084 lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006085 goto cleanup;
6086 }
6087 }
6088
6089 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01006090 if (d_add->uniques) {
6091 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
6092 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
6093 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006094
6095 /* *default-stmt */
6096 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006097 switch (devs[u]->target->nodetype) {
6098 case LYS_LEAF:
6099 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006100 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 +02006101 if (leaf->dflt) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006102 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02006103 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006104 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6105 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6106 } else {
6107 /* prepare new default value storage */
6108 leaf->dflt = calloc(1, sizeof *leaf->dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01006109 }
Radek Krejcia1911222019-07-22 17:24:50 +02006110 dflt = d_add->dflts[0];
6111 /* parsing is done at the end after possible replace of the leaf's type */
6112
Radek Krejci551b12c2019-02-19 16:11:21 +01006113 /* mark the new default values as leaf's own */
6114 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006115 break;
6116 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006117 if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006118 /* first, remove the default value taken from the type */
Radek Krejcia1911222019-07-22 17:24:50 +02006119 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006120 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006121 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6122 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6123 free(llist->dflts[x]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006124 }
Radek Krejcia1911222019-07-22 17:24:50 +02006125 LY_ARRAY_FREE(llist->dflts);
6126 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006127 LY_ARRAY_FREE(llist->dflts_mods);
6128 llist->dflts_mods = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01006129 }
6130 /* add new default value(s) */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006131 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006132 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
6133 for (x = y = LY_ARRAY_SIZE(llist->dflts);
Radek Krejciccd20f12019-02-15 14:12:27 +01006134 x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006135 LY_ARRAY_INCREMENT(llist->dflts_mods);
6136 llist->dflts_mods[x] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006137 LY_ARRAY_INCREMENT(llist->dflts);
6138 llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]);
6139 llist->dflts[x]->realtype = llist->type;
6140 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 +02006141 LY_TYPE_OPTS_INCOMPLETE_DATA |LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, lys_resolve_prefix,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006142 (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006143 llist->dflts[x]->realtype->refcount++;
6144 if (err) {
6145 ly_err_print(err);
6146 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6147 "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).",
6148 d_add->dflts[x - y], err->msg);
6149 ly_err_free(err);
6150 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006151 if (rc == LY_EINCOMPLETE) {
6152 /* postpone default compilation when the tree is complete */
6153 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6154
6155 /* but in general result is so far ok */
6156 rc = LY_SUCCESS;
6157 }
Radek Krejcia1911222019-07-22 17:24:50 +02006158 LY_CHECK_GOTO(rc, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006159 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006160 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01006161 devs[u]->target->flags |= LYS_SET_DFLT;
6162 break;
6163 case LYS_CHOICE:
6164 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
6165 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
6166 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
6167 * to allow making the default case even the augmented case from the deviating module */
Radek Krejci327de162019-06-14 12:52:07 +02006168 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 +01006169 goto cleanup;
6170 }
6171 break;
6172 default:
6173 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006174 lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006175 goto cleanup;
6176 }
6177 }
6178
6179 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006180 if (d_add->flags & LYS_CONFIG_MASK) {
6181 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006182 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006183 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
6184 goto cleanup;
6185 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006186 if (devs[u]->flags) {
Radek Krejci327de162019-06-14 12:52:07 +02006187 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
6188 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006189 }
Radek Krejci93dcc392019-02-19 10:43:38 +01006190 if (devs[u]->target->flags & LYS_SET_CONFIG) {
6191 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006192 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
6193 devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
Radek Krejci93dcc392019-02-19 10:43:38 +01006194 goto cleanup;
6195 }
Radek Krejci327de162019-06-14 12:52:07 +02006196 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006197 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006198
6199 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006200 if (d_add->flags & LYS_MAND_MASK) {
6201 if (devs[u]->target->flags & LYS_MAND_MASK) {
6202 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006203 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
6204 devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
Radek Krejcif1421c22019-02-19 13:05:20 +01006205 goto cleanup;
6206 }
Radek Krejci327de162019-06-14 12:52:07 +02006207 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006208 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006209
6210 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006211 if (d_add->flags & LYS_SET_MIN) {
6212 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6213 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6214 /* change value */
6215 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
6216 } else if (devs[u]->target->nodetype == LYS_LIST) {
6217 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6218 /* change value */
6219 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
6220 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006221 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006222 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
6223 goto cleanup;
6224 }
6225 if (d_add->min) {
6226 devs[u]->target->flags |= LYS_MAND_TRUE;
6227 }
6228 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006229
6230 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006231 if (d_add->flags & LYS_SET_MAX) {
6232 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6233 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6234 /* change value */
6235 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
6236 } else if (devs[u]->target->nodetype == LYS_LIST) {
6237 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6238 /* change value */
6239 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
6240 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006241 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006242 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
6243 goto cleanup;
6244 }
6245 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006246
6247 break;
6248 case LYS_DEV_DELETE:
6249 d_del = (struct lysp_deviate_del*)d;
6250
6251 /* [units-stmt] */
6252 if (d_del->units) {
6253 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Radek Krejcia1911222019-07-22 17:24:50 +02006254 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units);
6255 if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) {
6256 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6257 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
6258 d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6259 goto cleanup;
6260 }
6261 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6262 ((struct lysc_node_leaf*)devs[u]->target)->units = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01006263 }
6264
6265 /* *must-stmt */
6266 if (d_del->musts) {
6267 switch (devs[u]->target->nodetype) {
6268 case LYS_CONTAINER:
6269 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006270 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006271 break;
6272 case LYS_LEAF:
6273 case LYS_LEAFLIST:
6274 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006275 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006276 break;
6277 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02006278 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006279 break;
6280 case LYS_ACTION:
6281 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
6282 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6283 break;
6284 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
6285 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6286 break;
6287 }
6288 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01006289 default:
6290 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006291 lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006292 goto cleanup;
6293 }
6294 }
6295
6296 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01006297 if (d_del->uniques) {
6298 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
6299 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
6300 LY_ARRAY_FOR(d_del->uniques, x) {
6301 LY_ARRAY_FOR(list->uniques, z) {
6302 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
6303 nodeid = strpbrk(name, " \t\n");
6304 if (nodeid) {
Radek Krejci7f9b6512019-09-18 13:11:09 +02006305 if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) {
Radek Krejci7af64242019-02-18 13:07:53 +01006306 break;
6307 }
6308 while (isspace(*nodeid)) {
6309 ++nodeid;
6310 }
6311 } else {
6312 if (strcmp(name, list->uniques[z][y]->name)) {
6313 break;
6314 }
6315 }
6316 }
6317 if (!name) {
6318 /* complete match - remove the unique */
6319 LY_ARRAY_DECREMENT(list->uniques);
6320 LY_ARRAY_FREE(list->uniques[z]);
6321 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques));
6322 --z;
6323 break;
6324 }
6325 }
6326 if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) {
6327 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006328 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
6329 d_del->uniques[x]);
Radek Krejci7af64242019-02-18 13:07:53 +01006330 goto cleanup;
6331 }
6332 }
6333 if (!LY_ARRAY_SIZE(list->uniques)) {
6334 LY_ARRAY_FREE(list->uniques);
6335 list->uniques = NULL;
6336 }
6337 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006338
6339 /* *default-stmt */
6340 if (d_del->dflts) {
6341 switch (devs[u]->target->nodetype) {
6342 case LYS_LEAF:
6343 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6344 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6345 dflt, "deleting", "default", d_del->dflts[0]);
6346
Radek Krejcia1911222019-07-22 17:24:50 +02006347 /* check that the values matches */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006348 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &i);
Radek Krejcia1911222019-07-22 17:24:50 +02006349 if (strcmp(dflt, d_del->dflts[0])) {
6350 if (i) {
6351 free((char*)dflt);
6352 }
6353 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6354 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
6355 d_del->dflts[0], dflt);
6356 goto cleanup;
6357 }
6358 if (i) {
6359 free((char*)dflt);
6360 }
6361 dflt = NULL;
6362
Radek Krejci474f9b82019-07-24 11:36:37 +02006363 /* update the list of incomplete default values if needed */
6364 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6365
Radek Krejcia1911222019-07-22 17:24:50 +02006366 /* remove the default specification */
6367 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6368 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6369 free(leaf->dflt);
6370 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006371 leaf->dflt_mod = NULL;
Radek Krejci551b12c2019-02-19 16:11:21 +01006372 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006373 break;
6374 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006375 DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]);
6376 LY_ARRAY_FOR(d_del->dflts, x) {
6377 LY_ARRAY_FOR(llist->dflts, y) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006378 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 +02006379 if (!strcmp(dflt, d_del->dflts[x])) {
6380 if (i) {
6381 free((char*)dflt);
6382 }
6383 dflt = NULL;
6384 break;
6385 }
6386 if (i) {
6387 free((char*)dflt);
6388 }
6389 dflt = NULL;
6390 }
6391 if (y == LY_ARRAY_SIZE(llist->dflts)) {
6392 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
6393 "which does not match any of the target's property values.", d_del->dflts[x]);
6394 goto cleanup;
6395 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006396
6397 /* update the list of incomplete default values if needed */
6398 lysc_incomplete_dflts_remove(ctx, llist->dflts[y]);
6399
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006400 LY_ARRAY_DECREMENT(llist->dflts_mods);
Radek Krejcia1911222019-07-22 17:24:50 +02006401 LY_ARRAY_DECREMENT(llist->dflts);
6402 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
6403 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
6404 free(llist->dflts[y]);
6405 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_SIZE(llist->dflts) - y) * (sizeof *llist->dflts));
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006406 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 +02006407 }
6408 if (!LY_ARRAY_SIZE(llist->dflts)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006409 LY_ARRAY_FREE(llist->dflts_mods);
6410 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006411 LY_ARRAY_FREE(llist->dflts);
6412 llist->dflts = NULL;
6413 llist->flags &= ~LYS_SET_DFLT;
Radek Krejci551b12c2019-02-19 16:11:21 +01006414 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006415 break;
6416 case LYS_CHOICE:
6417 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6418 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
6419 nodeid = d_del->dflts[0];
Radek Krejcib4a4a272019-06-10 12:44:52 +02006420 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006421 if (prefix) {
6422 /* use module prefixes from the deviation module to match the module of the default case */
6423 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
6424 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006425 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6426 "The prefix does not match any imported module of the deviation module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006427 goto cleanup;
6428 }
6429 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
6430 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006431 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6432 "The prefix does not match the default case's module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006433 goto cleanup;
6434 }
6435 }
6436 /* else {
6437 * strictly, the default prefix would point to the deviation module, but the value should actually
6438 * match the default string in the original module (usually unprefixed), so in this case we do not check
6439 * the module of the default case, just matching its name */
6440 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
6441 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006442 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
6443 d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01006444 goto cleanup;
6445 }
6446 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
6447 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
6448 break;
6449 default:
6450 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006451 lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006452 goto cleanup;
6453 }
6454 }
6455
6456 break;
6457 case LYS_DEV_REPLACE:
6458 d_rpl = (struct lysp_deviate_rpl*)d;
6459
6460 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01006461 if (d_rpl->type) {
6462 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6463 /* type is mandatory, so checking for its presence is not necessary */
6464 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
Radek Krejcia1911222019-07-22 17:24:50 +02006465
6466 if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) {
6467 /* the target has default from the previous type - remove it */
6468 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006469 /* update the list of incomplete default values if needed */
6470 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6471
Radek Krejcia1911222019-07-22 17:24:50 +02006472 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6473 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6474 free(leaf->dflt);
6475 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006476 leaf->dflt_mod = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006477 } else { /* LYS_LEAFLIST */
6478 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006479 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006480 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6481 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6482 free(llist->dflts[x]);
6483 }
6484 LY_ARRAY_FREE(llist->dflts);
6485 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006486 LY_ARRAY_FREE(llist->dflts_mods);
6487 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006488 }
6489 }
6490 if (!leaf->dflt) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006491 /* there is no default value, do not set changed_type after type compilation
6492 * which is used to recompile the default value */
Radek Krejcia1911222019-07-22 17:24:50 +02006493 changed_type = -1;
6494 }
Radek Krejciec4da802019-05-02 13:02:41 +02006495 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 +02006496 changed_type++;
Radek Krejci33f72892019-02-21 10:36:58 +01006497 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006498
6499 /* [units-stmt] */
6500 if (d_rpl->units) {
6501 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6502 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
6503 units, "replacing", "units", d_rpl->units);
6504
6505 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6506 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6507 }
6508
6509 /* [default-stmt] */
6510 if (d_rpl->dflt) {
6511 switch (devs[u]->target->nodetype) {
6512 case LYS_LEAF:
6513 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6514 dflt, "replacing", "default", d_rpl->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006515 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02006516 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006517 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6518 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6519 dflt = d_rpl->dflt;
6520 /* parsing is done at the end after possible replace of the leaf's type */
Radek Krejciccd20f12019-02-15 14:12:27 +01006521 break;
6522 case LYS_CHOICE:
6523 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
Radek Krejci327de162019-06-14 12:52:07 +02006524 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 +01006525 goto cleanup;
6526 }
6527 break;
6528 default:
6529 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006530 lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006531 goto cleanup;
6532 }
6533 }
6534
6535 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006536 if (d_rpl->flags & LYS_CONFIG_MASK) {
6537 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006538 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006539 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
6540 goto cleanup;
6541 }
6542 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
Radek Krejci327de162019-06-14 12:52:07 +02006543 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejci93dcc392019-02-19 10:43:38 +01006544 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
6545 goto cleanup;
6546 }
Radek Krejci327de162019-06-14 12:52:07 +02006547 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006548 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006549
6550 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006551 if (d_rpl->flags & LYS_MAND_MASK) {
6552 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
Radek Krejci327de162019-06-14 12:52:07 +02006553 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejcif1421c22019-02-19 13:05:20 +01006554 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6555 goto cleanup;
6556 }
Radek Krejci327de162019-06-14 12:52:07 +02006557 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006558 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006559
6560 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006561 if (d_rpl->flags & LYS_SET_MIN) {
6562 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6563 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6564 /* change value */
6565 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
6566 } else if (devs[u]->target->nodetype == LYS_LIST) {
6567 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6568 /* change value */
6569 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
6570 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006571 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006572 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
6573 goto cleanup;
6574 }
6575 if (d_rpl->min) {
6576 devs[u]->target->flags |= LYS_MAND_TRUE;
6577 }
6578 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006579
6580 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006581 if (d_rpl->flags & LYS_SET_MAX) {
6582 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6583 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6584 /* change value */
6585 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6586 } else if (devs[u]->target->nodetype == LYS_LIST) {
6587 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6588 /* change value */
6589 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6590 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006591 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006592 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
6593 goto cleanup;
6594 }
6595 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006596
6597 break;
6598 default:
6599 LOGINT(ctx->ctx);
6600 goto cleanup;
6601 }
6602 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006603
Radek Krejci33f72892019-02-21 10:36:58 +01006604 /* final check when all deviations of a single target node are applied */
6605
Radek Krejci551b12c2019-02-19 16:11:21 +01006606 /* check min-max compatibility */
6607 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6608 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
6609 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
6610 } else if (devs[u]->target->nodetype == LYS_LIST) {
6611 min = ((struct lysc_node_list*)devs[u]->target)->min;
6612 max = ((struct lysc_node_list*)devs[u]->target)->max;
6613 } else {
6614 min = max = 0;
6615 }
6616 if (min > max) {
6617 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 +02006618 "after deviation: min value %u is bigger than max value %u.", min, max);
Radek Krejci551b12c2019-02-19 16:11:21 +01006619 goto cleanup;
6620 }
6621
Radek Krejcia1911222019-07-22 17:24:50 +02006622 if (dflt) {
6623 /* parse added/changed default value after possible change of the type */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006624 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006625 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006626 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6627 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006628 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006629 leaf->dflt->realtype->refcount++;
6630 if (err) {
6631 ly_err_print(err);
6632 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6633 "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg);
6634 ly_err_free(err);
6635 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006636 if (rc == LY_EINCOMPLETE) {
6637 /* postpone default compilation when the tree is complete */
6638 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6639
6640 /* but in general result is so far ok */
6641 rc = LY_SUCCESS;
6642 }
Radek Krejcia1911222019-07-22 17:24:50 +02006643 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006644 } else if (changed_type) {
Radek Krejcia1911222019-07-22 17:24:50 +02006645 /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */
6646 int dynamic;
6647 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006648 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &dynamic);
Radek Krejci474f9b82019-07-24 11:36:37 +02006649
6650 /* update the list of incomplete default values if needed */
6651 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6652
6653 /* remove the previous default */
Radek Krejcia1911222019-07-22 17:24:50 +02006654 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6655 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6656 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006657 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6658 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006659 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006660 leaf->dflt->realtype->refcount++;
6661 if (err) {
6662 ly_err_print(err);
6663 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6664 "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg);
6665 ly_err_free(err);
6666 }
6667 if (dynamic) {
6668 free((void*)dflt);
6669 }
Radek Krejcia1911222019-07-22 17:24:50 +02006670 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006671 if (rc == LY_EINCOMPLETE) {
6672 /* postpone default compilation when the tree is complete */
6673 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6674
6675 /* but in general result is so far ok */
6676 rc = LY_SUCCESS;
6677 }
6678 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006679 } else { /* LYS_LEAFLIST */
6680 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006681 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 +02006682 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6683 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6684 llist->dflts[x]->realtype = llist->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006685 rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt),
6686 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006687 lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL,
6688 llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006689 llist->dflts[x]->realtype->refcount++;
6690 if (err) {
6691 ly_err_print(err);
6692 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6693 "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).",
6694 dflt, err->msg);
6695 ly_err_free(err);
6696 }
6697 if (dynamic) {
6698 free((void*)dflt);
6699 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006700 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006701 if (rc == LY_EINCOMPLETE) {
6702 /* postpone default compilation when the tree is complete */
6703 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6704
6705 /* but in general result is so far ok */
6706 rc = LY_SUCCESS;
6707 }
Radek Krejcia1911222019-07-22 17:24:50 +02006708 LY_CHECK_GOTO(rc, cleanup);
6709 }
6710 }
6711 }
6712
Radek Krejci551b12c2019-02-19 16:11:21 +01006713 /* check mandatory - default compatibility */
6714 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
6715 && (devs[u]->target->flags & LYS_SET_DFLT)
6716 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6717 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006718 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(devs[u]->target->nodetype));
Radek Krejci551b12c2019-02-19 16:11:21 +01006719 goto cleanup;
6720 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
6721 && ((struct lysc_node_choice*)devs[u]->target)->dflt
6722 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
Radek Krejci327de162019-06-14 12:52:07 +02006723 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 +01006724 goto cleanup;
6725 }
6726 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6727 /* mandatory node under a default case */
6728 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006729 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6730 lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
Radek Krejci551b12c2019-02-19 16:11:21 +01006731 goto cleanup;
6732 }
Radek Krejci33f72892019-02-21 10:36:58 +01006733
Radek Krejci327de162019-06-14 12:52:07 +02006734 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006735 }
6736
Radek Krejci327de162019-06-14 12:52:07 +02006737 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006738 ret = LY_SUCCESS;
6739
6740cleanup:
6741 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6742 LY_ARRAY_FREE(devs[u]->deviates);
6743 free(devs[u]);
6744 }
6745 free(devs);
6746 ly_set_erase(&targets, NULL);
6747 ly_set_erase(&devs_p, NULL);
6748
6749 return ret;
6750}
6751
Radek Krejcib56c7502019-02-13 14:19:54 +01006752/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006753 * @brief Compile the given YANG submodule into the main module.
6754 * @param[in] ctx Compile context
6755 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006756 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6757 */
6758LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006759lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006760{
6761 unsigned int u;
6762 LY_ERR ret = LY_SUCCESS;
6763 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006764 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006765 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006766 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006767
Radek Krejci0af46292019-01-11 16:02:31 +01006768 if (!mainmod->mod->off_features) {
6769 /* features are compiled directly into the compiled module structure,
6770 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6771 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci0935f412019-08-20 16:15:18 +02006772 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features);
6773 LY_CHECK_GOTO(ret, error);
6774 }
6775 if (!mainmod->mod->off_extensions) {
6776 /* extensions are compiled directly into the compiled module structure, compilation is finished in the main module (lys_compile()). */
6777 ret = lys_extension_precompile(ctx, NULL, NULL, submod->extensions, &mainmod->extensions);
Radek Krejci0af46292019-01-11 16:02:31 +01006778 LY_CHECK_GOTO(ret, error);
6779 }
6780
Radek Krejci327de162019-06-14 12:52:07 +02006781 lysc_update_path(ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006782 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, u, lys_compile_identity, ret, error);
Radek Krejci327de162019-06-14 12:52:07 +02006783 lysc_update_path(ctx, NULL, NULL);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006784
Radek Krejci474f9b82019-07-24 11:36:37 +02006785 /* data nodes */
6786 LY_LIST_FOR(submod->data, node_p) {
6787 ret = lys_compile_node(ctx, node_p, NULL, 0);
6788 LY_CHECK_GOTO(ret, error);
6789 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006790
Radek Krejciec4da802019-05-02 13:02:41 +02006791 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6792 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006793
Radek Krejcid05cbd92018-12-05 14:26:40 +01006794error:
6795 return ret;
6796}
6797
Radek Krejci335332a2019-09-05 13:03:35 +02006798static void *
6799lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
6800{
6801 for (unsigned int u = 0; substmts[u].stmt; ++u) {
6802 if (substmts[u].stmt == stmt) {
6803 return substmts[u].storage;
6804 }
6805 }
6806 return NULL;
6807}
6808
6809LY_ERR
6810lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
6811{
6812 LY_ERR ret = LY_EVALID, r;
6813 unsigned int u;
6814 struct lysp_stmt *stmt;
6815 void *parsed = NULL, **compiled = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006816
6817 /* check for invalid substatements */
6818 for (stmt = ext->child; stmt; stmt = stmt->next) {
Radek Krejcif56e2a42019-09-09 14:15:25 +02006819 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
6820 continue;
6821 }
Radek Krejci335332a2019-09-05 13:03:35 +02006822 for (u = 0; substmts[u].stmt; ++u) {
6823 if (substmts[u].stmt == stmt->kw) {
6824 break;
6825 }
6826 }
6827 if (!substmts[u].stmt) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006828 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
6829 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006830 goto cleanup;
6831 }
Radek Krejci335332a2019-09-05 13:03:35 +02006832 }
6833
Radek Krejciad5963b2019-09-06 16:03:05 +02006834 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
6835
Radek Krejci335332a2019-09-05 13:03:35 +02006836 /* keep order of the processing the same as the order in the defined substmts,
6837 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
6838 for (u = 0; substmts[u].stmt; ++u) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006839 int stmt_present = 0;
6840
Radek Krejci335332a2019-09-05 13:03:35 +02006841 for (stmt = ext->child; stmt; stmt = stmt->next) {
6842 if (substmts[u].stmt != stmt->kw) {
6843 continue;
6844 }
6845
Radek Krejciad5963b2019-09-06 16:03:05 +02006846 stmt_present = 1;
Radek Krejci335332a2019-09-05 13:03:35 +02006847 if (substmts[u].storage) {
6848 switch (stmt->kw) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006849 case LY_STMT_STATUS:
6850 assert(substmts[u].cardinality < LY_STMT_CARD_SOME);
6851 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
6852 break;
6853 case LY_STMT_UNITS: {
6854 const char **units;
6855
6856 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6857 /* single item */
6858 if (*((const char **)substmts[u].storage)) {
6859 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6860 goto cleanup;
6861 }
6862 units = (const char **)substmts[u].storage;
6863 } else {
6864 /* sized array */
6865 const char ***units_array = (const char ***)substmts[u].storage;
6866 LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
6867 }
6868 *units = lydict_insert(ctx->ctx, stmt->arg, 0);
6869 break;
6870 }
Radek Krejci335332a2019-09-05 13:03:35 +02006871 case LY_STMT_TYPE: {
6872 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
6873 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
6874
6875 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6876 /* single item */
Radek Krejciad5963b2019-09-06 16:03:05 +02006877 if (*(struct lysc_type**)substmts[u].storage) {
Radek Krejci335332a2019-09-05 13:03:35 +02006878 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6879 goto cleanup;
6880 }
6881 compiled = substmts[u].storage;
6882 } else {
6883 /* sized array */
6884 struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL;
6885 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
6886 compiled = (void*)type;
6887 }
6888
Radek Krejciad5963b2019-09-06 16:03:05 +02006889 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup);
Radek Krejci335332a2019-09-05 13:03:35 +02006890 LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL,
6891 flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type**)compiled,
Radek Krejci38d85362019-09-05 16:26:38 +02006892 units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup);
6893 lysp_type_free(ctx->ctx, parsed);
6894 free(parsed);
Radek Krejci335332a2019-09-05 13:03:35 +02006895 break;
6896 }
Radek Krejciad5963b2019-09-06 16:03:05 +02006897 case LY_STMT_IF_FEATURE: {
6898 struct lysc_iffeature *iff = NULL;
6899
6900 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6901 /* single item */
6902 if (((struct lysc_iffeature*)substmts[u].storage)->features) {
6903 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6904 goto cleanup;
6905 }
6906 iff = (struct lysc_iffeature*)substmts[u].storage;
6907 } else {
6908 /* sized array */
6909 struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage;
6910 LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup);
6911 }
6912 LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup);
6913 break;
6914 }
6915 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
6916 * also note that in many statements their extensions are not taken into account */
Radek Krejci335332a2019-09-05 13:03:35 +02006917 default:
Radek Krejciad5963b2019-09-06 16:03:05 +02006918 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Statement \"%s\" is not supported as an extension (found in \"%s%s%s\") substatement.",
6919 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006920 goto cleanup;
6921 }
6922 }
Radek Krejci335332a2019-09-05 13:03:35 +02006923 }
Radek Krejci335332a2019-09-05 13:03:35 +02006924
Radek Krejciad5963b2019-09-06 16:03:05 +02006925 if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) {
6926 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
6927 ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
6928 goto cleanup;
6929 }
Radek Krejci335332a2019-09-05 13:03:35 +02006930 }
6931
6932 ret = LY_SUCCESS;
6933
6934cleanup:
Radek Krejci335332a2019-09-05 13:03:35 +02006935 return ret;
6936}
6937
Radek Krejci19a96102018-11-15 13:38:09 +01006938LY_ERR
6939lys_compile(struct lys_module *mod, int options)
6940{
6941 struct lysc_ctx ctx = {0};
6942 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01006943 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01006944 struct lysp_module *sp;
6945 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01006946 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02006947 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01006948 struct lys_module *m;
Radek Krejcicdfecd92018-11-26 11:27:32 +01006949 unsigned int u, v;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006950 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01006951
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006952 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01006953
6954 if (!mod->implemented) {
6955 /* just imported modules are not compiled */
6956 return LY_SUCCESS;
6957 }
6958
Radek Krejci19a96102018-11-15 13:38:09 +01006959 sp = mod->parsed;
6960
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006961 ctx.ctx = mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01006962 ctx.mod = mod;
Radek Krejcie86bf772018-12-14 11:39:53 +01006963 ctx.mod_def = mod;
Radek Krejciec4da802019-05-02 13:02:41 +02006964 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02006965 ctx.path_len = 1;
6966 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01006967
6968 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006969 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
6970 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01006971
Radek Krejciec4da802019-05-02 13:02:41 +02006972 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006973 LY_ARRAY_FOR(sp->includes, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006974 ret = lys_compile_submodule(&ctx, &sp->includes[u]);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006975 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6976 }
Radek Krejci0935f412019-08-20 16:15:18 +02006977
6978 /* features */
Radek Krejci0af46292019-01-11 16:02:31 +01006979 if (mod->off_features) {
6980 /* there is already precompiled array of features */
6981 mod_c->features = mod->off_features;
6982 mod->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01006983 } else {
6984 /* features are compiled directly into the compiled module structure,
6985 * 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 +02006986 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006987 LY_CHECK_GOTO(ret, error);
6988 }
6989 /* finish feature compilation, not only for the main module, but also for the submodules.
6990 * Due to possible forward references, it must be done when all the features (including submodules)
6991 * are present. */
6992 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006993 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006994 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6995 }
Radek Krejci327de162019-06-14 12:52:07 +02006996 lysc_update_path(&ctx, NULL, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01006997 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02006998 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01006999 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007000 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007001 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7002 }
Radek Krejci327de162019-06-14 12:52:07 +02007003 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007004 }
Radek Krejci327de162019-06-14 12:52:07 +02007005 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007006
Radek Krejci0935f412019-08-20 16:15:18 +02007007 /* extensions */
7008 /* 2-steps: a) prepare compiled structures and ... */
7009 if (mod->off_extensions) {
7010 /* there is already precompiled array of extension definitions */
7011 mod_c->extensions = mod->off_extensions;
7012 mod->off_extensions = NULL;
7013 } else {
7014 /* extension definitions are compiled directly into the compiled module structure */
7015 ret = lys_extension_precompile(&ctx, NULL, NULL, sp->extensions, &mod_c->extensions);
Radek Krejcibfb2e142019-09-09 14:47:44 +02007016 LY_CHECK_GOTO(ret, error);
Radek Krejci0935f412019-08-20 16:15:18 +02007017 }
7018 /* ... b) connect the extension definitions with the appropriate extension plugins */
7019 lys_compile_extension_plugins(mod_c->extensions);
7020
7021 /* identities */
Radek Krejci327de162019-06-14 12:52:07 +02007022 lysc_update_path(&ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02007023 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007024 if (sp->identities) {
7025 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
7026 }
Radek Krejci327de162019-06-14 12:52:07 +02007027 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01007028
Radek Krejci95710c92019-02-11 15:49:55 +01007029 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01007030 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007031 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, node_p, NULL, 0), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007032 }
Radek Krejci95710c92019-02-11 15:49:55 +01007033
Radek Krejciec4da802019-05-02 13:02:41 +02007034 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
7035 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01007036
Radek Krejci95710c92019-02-11 15:49:55 +01007037 /* augments - sort first to cover augments augmenting other augments */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007038 LY_CHECK_GOTO(ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007039 LY_ARRAY_FOR(augments, u) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007040 LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007041 }
Radek Krejciccd20f12019-02-15 14:12:27 +01007042
Radek Krejci474f9b82019-07-24 11:36:37 +02007043 /* deviations TODO cover deviations from submodules */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007044 LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007045
Radek Krejci0935f412019-08-20 16:15:18 +02007046 /* extension instances TODO cover extension instances from submodules */
7047 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007048
Radek Krejcia3045382018-11-22 14:30:31 +01007049 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01007050 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
7051 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
7052 * 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 +01007053 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01007054 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01007055 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
7056 if (type->basetype == LY_TYPE_LEAFREF) {
7057 /* validate the path */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02007058 LY_CHECK_GOTO(ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), (struct lysc_type_leafref*)type, NULL), error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01007059 } else if (type->basetype == LY_TYPE_UNION) {
7060 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
7061 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7062 /* validate the path */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007063 LY_CHECK_GOTO(ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02007064 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v], NULL),
Radek Krejcid3acfce2019-09-09 14:48:50 +02007065 error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01007066 }
7067 }
Radek Krejcia3045382018-11-22 14:30:31 +01007068 }
7069 }
7070 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01007071 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01007072 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01007073 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
7074 if (type->basetype == LY_TYPE_LEAFREF) {
7075 /* store pointer to the real type */
7076 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
7077 typeiter->basetype == LY_TYPE_LEAFREF;
7078 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
7079 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01007080 } else if (type->basetype == LY_TYPE_UNION) {
7081 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
7082 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7083 /* store pointer to the real type */
7084 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
7085 typeiter->basetype == LY_TYPE_LEAFREF;
7086 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
7087 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
7088 }
7089 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01007090 }
7091 }
7092 }
Radek Krejciec4da802019-05-02 13:02:41 +02007093
Radek Krejci474f9b82019-07-24 11:36:37 +02007094 /* finish incomplete default values compilation */
7095 for (u = 0; u < ctx.dflts.count; ++u) {
7096 struct ly_err_item *err = NULL;
7097 struct lysc_incomplete_dflt *r = ctx.dflts.objs[u];
Radek Krejci950f6a52019-09-12 17:15:32 +02007098 ret = r->dflt->realtype->plugin->store(ctx.ctx, r->dflt->realtype, r->dflt->original, strlen(r->dflt->original),
Radek Krejci474f9b82019-07-24 11:36:37 +02007099 LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix,
7100 (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err);
7101 if (err) {
7102 ly_err_print(err);
7103 ctx.path[0] = '\0';
Michal Vasko03ff5a72019-09-11 13:49:33 +02007104 lysc_path(r->context_node, LYSC_PATH_LOG, ctx.path, LYSC_CTX_BUFSIZE);
Radek Krejci474f9b82019-07-24 11:36:37 +02007105 LOGVAL(ctx.ctx, LY_VLOG_STR, ctx.path, LYVE_SEMANTICS,
7106 "Invalid default - value does not fit the type (%s).", err->msg);
7107 ly_err_free(err);
7108 }
7109 LY_CHECK_GOTO(ret, error);
7110 }
7111
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007112 /* validate non-instantiated groupings from the parsed schema,
7113 * without it we would accept even the schemas with invalid grouping specification */
7114 ctx.options |= LYSC_OPT_GROUPING;
7115 LY_ARRAY_FOR(sp->groupings, u) {
7116 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007117 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007118 }
7119 }
7120 LY_LIST_FOR(sp->data, node_p) {
7121 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
7122 LY_ARRAY_FOR(grps, u) {
7123 if (!(grps[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007124 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &grps[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007125 }
7126 }
7127 }
7128
Radek Krejci474f9b82019-07-24 11:36:37 +02007129 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
7130 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
7131 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
7132 }
7133
Radek Krejci1c0c3442019-07-23 16:08:47 +02007134 ly_set_erase(&ctx.dflts, free);
Radek Krejcia3045382018-11-22 14:30:31 +01007135 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007136 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007137 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007138 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01007139
Radek Krejciec4da802019-05-02 13:02:41 +02007140 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejci19a96102018-11-15 13:38:09 +01007141 lysp_module_free(mod->parsed);
7142 ((struct lys_module*)mod)->parsed = NULL;
7143 }
7144
Radek Krejciec4da802019-05-02 13:02:41 +02007145 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01007146 /* remove flag of the modules implemented by dependency */
7147 for (u = 0; u < ctx.ctx->list.count; ++u) {
7148 m = ctx.ctx->list.objs[u];
7149 if (m->implemented == 2) {
7150 m->implemented = 1;
7151 }
7152 }
7153 }
7154
Radek Krejci19a96102018-11-15 13:38:09 +01007155 ((struct lys_module*)mod)->compiled = mod_c;
7156 return LY_SUCCESS;
7157
7158error:
Radek Krejci95710c92019-02-11 15:49:55 +01007159 lys_feature_precompile_revert(&ctx, mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02007160 ly_set_erase(&ctx.dflts, free);
Radek Krejcia3045382018-11-22 14:30:31 +01007161 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007162 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007163 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007164 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01007165 lysc_module_free(mod_c, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007166 mod->compiled = NULL;
7167
7168 /* revert compilation of modules implemented by dependency */
7169 for (u = 0; u < ctx.ctx->list.count; ++u) {
7170 m = ctx.ctx->list.objs[u];
7171 if (m->implemented == 2) {
7172 /* revert features list to the precompiled state */
7173 lys_feature_precompile_revert(&ctx, m);
7174 /* mark module as imported-only / not-implemented */
7175 m->implemented = 0;
7176 /* free the compiled version of the module */
7177 lysc_module_free(m->compiled, NULL);
7178 m->compiled = NULL;
7179 }
7180 }
7181
Radek Krejci19a96102018-11-15 13:38:09 +01007182 return ret;
7183}