#define _GNU_SOURCE
#include "schema_compile.h"
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "compat.h"
#include "context.h"
#include "dict.h"
#include "in.h"
#include "log.h"
#include "ly_common.h"
#include "lyb.h"
#include "parser_schema.h"
#include "path.h"
#include "plugins.h"
#include "plugins_exts.h"
#include "plugins_internal.h"
#include "plugins_types.h"
#include "schema_compile_amend.h"
#include "schema_compile_node.h"
#include "schema_features.h"
#include "set.h"
#include "tree.h"
#include "tree_data.h"
#include "tree_schema.h"
#include "tree_schema_free.h"
#include "tree_schema_internal.h"
#include "xpath.h"
void
lysc_update_path(struct lysc_ctx *ctx, const struct lys_module *parent_module, const char *name)
{
int len;
uint8_t nextlevel = 0;
if (!name) {
if (ctx->path[ctx->path_len - 1] == '}') {
for ( ; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len) {}
if (ctx->path[ctx->path_len] == '=') {
ctx->path[ctx->path_len++] = '}';
} else {
goto remove_nodelevel;
}
} else {
remove_nodelevel:
for ( ; ctx->path[ctx->path_len] != '/'; --ctx->path_len) {}
if (ctx->path_len == 0) {
ctx->path_len = 1;
}
}
ctx->path[ctx->path_len] = '\0';
} else {
if (ctx->path_len > 1) {
if (!parent_module && (ctx->path[ctx->path_len - 1] == '}') && (ctx->path[ctx->path_len - 2] != '\'')) {
nextlevel = 2;
--ctx->path_len;
} else {
nextlevel = 1;
}
}
if (nextlevel != 2) {
if ((parent_module && (parent_module == ctx->cur_mod)) || (!parent_module && (ctx->path_len > 1) && (name[0] == '{'))) {
len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s",
nextlevel ? "/" : "", name);
} else {
len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s:%s",
nextlevel ? "/" : "", ctx->cur_mod->name, name);
}
} else {
len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
}
if (len >= LYSC_CTX_BUFSIZE - (int)ctx->path_len) {
ctx->path_len = LYSC_CTX_BUFSIZE - 1;
} else {
ctx->path_len += len;
}
}
ly_log_location_revert(0, 1, 0);
ly_log_location(NULL, ctx->path, NULL);
}
LY_ERR
lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *extp, struct lysc_ext_instance *ext, void *parent)
{
LY_ERR ret = LY_SUCCESS;
struct lyplg_ext *ext_plg;
DUP_STRING_GOTO(ctx->ctx, extp->argument, ext->argument, ret, cleanup);
ext->module = ctx->cur_mod;
ext->parent = parent;
ext->parent_stmt = extp->parent_stmt;
ext->parent_stmt_index = extp->parent_stmt_index;
lysc_update_path(ctx, (ext->parent_stmt & LY_STMT_NODE_MASK) ? ((struct lysc_node *)ext->parent)->module : NULL,
"{ext-inst}");
lysc_update_path(ctx, NULL, extp->name);
LY_CHECK_GOTO(ret = lysc_ext_find_definition(ctx->ctx, extp, &ext->def), cleanup);
COMPILE_EXTS_GOTO(ctx, extp->exts, ext->exts, ext, ret, cleanup);
if (ext->def->plugin_ref && (ext_plg = LYSC_GET_EXT_PLG(ext->def->plugin_ref))->compile) {
if (ext->argument) {
lysc_update_path(ctx, ext->module, ext->argument);
}
ret = ext_plg->compile(ctx, extp, ext);
if (ret == LY_ENOT) {
lysc_ext_instance_free(ctx->ctx, ext);
}
if (ext->argument) {
lysc_update_path(ctx, NULL, NULL);
}
LY_CHECK_GOTO(ret, cleanup);
}
cleanup:
lysc_update_path(ctx, NULL, NULL);
lysc_update_path(ctx, NULL, NULL);
return ret;
}
static void
lysc_unres_must_free(struct lysc_unres_must *m)
{
LY_ARRAY_FREE(m->local_mods);
free(m);
}
static void
lysc_unres_dflt_free(const struct ly_ctx *ctx, struct lysc_unres_dflt *r)
{
assert(!r->dflt || !r->dflts);
if (r->dflt) {
lysp_qname_free((struct ly_ctx *)ctx, r->dflt);
free(r->dflt);
} else {
FREE_ARRAY((struct ly_ctx *)ctx, r->dflts, lysp_qname_free);
}
free(r);
}
LY_ERR
lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lysp_module *parsed_mod,
const struct lysp_ident *identities_p, struct lysc_ident **identities)
{
LY_ARRAY_COUNT_TYPE u, v;
struct lysc_ctx cctx = {0};
struct lysc_ident *ident;
LY_ERR ret = LY_SUCCESS;
assert(ctx_sc || ctx);
if (!ctx_sc) {
if (parsed_mod) {
LYSC_CTX_INIT_PMOD(cctx, parsed_mod, NULL);
} else {
LYSC_CTX_INIT_CTX(cctx, ctx);
}
ctx_sc = &cctx;
}
if (!identities_p) {
return LY_SUCCESS;
}
lysc_update_path(ctx_sc, NULL, "{identity}");
LY_ARRAY_FOR(identities_p, u) {
lysc_update_path(ctx_sc, NULL, identities_p[u].name);
LY_ARRAY_NEW_GOTO(ctx_sc->ctx, *identities, ident, ret, done);
DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].name, ident->name, ret, done);
DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].dsc, ident->dsc, ret, done);
DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].ref, ident->ref, ret, done);
ident->module = ctx_sc->cur_mod;
COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, ident->exts, ident, ret, done);
ident->flags = identities_p[u].flags;
lysc_update_path(ctx_sc, NULL, NULL);
}
lysc_update_path(ctx_sc, NULL, NULL);
LY_ARRAY_FOR(*identities, u) {
LY_ARRAY_FOR((*identities)[u].exts, v) {
(*identities)[u].exts[v].parent = &(*identities)[u];
}
}
done:
if (ret) {
lysc_update_path(ctx_sc, NULL, NULL);
lysc_update_path(ctx_sc, NULL, NULL);
}
return ret;
}
static LY_ERR
lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
{
LY_ERR ret = LY_SUCCESS;
LY_ARRAY_COUNT_TYPE u, v;
struct ly_set recursion = {0};
struct lysc_ident *drv;
if (!derived) {
return LY_SUCCESS;
}
for (u = 0; u < LY_ARRAY_COUNT(derived); ++u) {
if (ident == derived[u]) {
LOGVAL(ctx->ctx, NULL, LYVE_REFERENCE,
"Identity \"%s\" is indirectly derived from itself.", ident->name);
ret = LY_EVALID;
goto cleanup;
}
ret = ly_set_add(&recursion, derived[u], 0, NULL);
LY_CHECK_GOTO(ret, cleanup);
}
for (v = 0; v < recursion.count; ++v) {
drv = recursion.objs[v];
for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) {
if (ident == drv->derived[u]) {
LOGVAL(ctx->ctx, NULL, LYVE_REFERENCE,
"Identity \"%s\" is indirectly derived from itself.", ident->name);
ret = LY_EVALID;
goto cleanup;
}
ret = ly_set_add(&recursion, drv->derived[u], 0, NULL);
LY_CHECK_GOTO(ret, cleanup);
}
}
cleanup:
ly_set_erase(&recursion, NULL);
return ret;
}
LY_ERR
lys_compile_identity_bases(struct lysc_ctx *ctx, const struct lysp_module *base_pmod, const char **bases_p,
struct lysc_ident *ident, struct lysc_ident ***bases)
{
LY_ARRAY_COUNT_TYPE u, v;
const char *s, *name;
const struct lys_module *mod;
struct lysc_ident **idref;
assert(ident || bases);
if ((LY_ARRAY_COUNT(bases_p) > 1) && (ctx->pmod->version < LYS_VERSION_1_1)) {
LOGVAL(ctx->ctx, NULL, LYVE_SYNTAX_YANG,
"Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
return LY_EVALID;
}
LY_ARRAY_FOR(bases_p, u) {
s = strchr(bases_p[u], ':');
if (s) {
name = &s[1];
mod = ly_resolve_prefix(ctx->ctx, bases_p[u], s - bases_p[u], LY_VALUE_SCHEMA, (void *)base_pmod);
} else {
name = bases_p[u];
mod = base_pmod->mod;
}
if (!mod) {
if (ident) {
LOGVAL(ctx->ctx, NULL, LYVE_SYNTAX_YANG,
"Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
} else {
LOGVAL(ctx->ctx, NULL, LYVE_SYNTAX_YANG,
"Invalid prefix used for base (%s) of identityref.", bases_p[u]);
}
return LY_EVALID;
}
idref = NULL;
LY_ARRAY_FOR(mod->identities, v) {
if (!strcmp(name, mod->identities[v].name)) {
if (ident) {
if (ident == &mod->identities[v]) {
LOGVAL(ctx->ctx, NULL, LYVE_REFERENCE, "Identity \"%s\" is derived from itself.", ident->name);
return LY_EVALID;
}
LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->identities[v], ident->derived));
LY_ARRAY_NEW_RET(ctx->ctx, mod->identities[v].derived, idref, LY_EMEM);
*idref = ident;
} else {
LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
*idref = &mod->identities[v];
}
break;
}
}
if (!idref) {
if (ident) {
LOGVAL(ctx->ctx, NULL, LYVE_SYNTAX_YANG,
"Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
} else {
LOGVAL(ctx->ctx, NULL, LYVE_SYNTAX_YANG,
"Unable to find base (%s) of identityref.", bases_p[u]);
}
return LY_EVALID;
}
}
return LY_SUCCESS;
}
static LY_ERR
lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident **idents)
{
LY_ARRAY_COUNT_TYPE u, v;
lysc_update_path(ctx, NULL, "{identity}");
for (u = 0; u < LY_ARRAY_COUNT(*idents); ++u) {
for (v = 0; v < LY_ARRAY_COUNT(idents_p); ++v) {
if (idents_p[v].name == (*idents)[u].name) {
break;
}
}
if ((v == LY_ARRAY_COUNT(idents_p)) || !idents_p[v].bases) {
continue;
}
lysc_update_path(ctx, NULL, (*idents)[u].name);
LY_CHECK_RET(lys_compile_identity_bases(ctx, ctx->pmod, idents_p[v].bases, &(*idents)[u], NULL));
lysc_update_path(ctx, NULL, NULL);
}
lysc_update_path(ctx, NULL, NULL);
return LY_SUCCESS;
}
LY_ERR
lys_compile_expr_implement(const struct ly_ctx *ctx, const struct lyxp_expr *expr, LY_VALUE_FORMAT format,
void *prefix_data, ly_bool implement, struct lys_glob_unres *unres, const struct lys_module **mod_p)
{
uint32_t i;
const char *ptr, *start, **imp_f, *all_f[] = {"*", NULL};
const struct lys_module *mod;
assert(implement || mod_p);
if (mod_p) {
*mod_p = NULL;
}
for (i = 0; i < expr->used; ++i) {
if ((expr->tokens[i] != LYXP_TOKEN_NAMETEST) && (expr->tokens[i] != LYXP_TOKEN_LITERAL)) {
continue;
}
start = expr->expr + expr->tok_pos[i];
if (!(ptr = ly_strnchr(start, ':', expr->tok_len[i]))) {
continue;
}
if (!(mod = ly_resolve_prefix(ctx, start, ptr - start, format, prefix_data))) {
continue;
}
if (!mod->implemented && !implement) {
*mod_p = mod;
break;
}
if (!mod->implemented) {
imp_f = (ctx->opts & LY_CTX_ENABLE_IMP_FEATURES) ? all_f : NULL;
LY_CHECK_RET(lys_implement((struct lys_module *)mod, imp_f, unres));
}
if (!mod->compiled) {
LY_CHECK_RET(lys_compile((struct lys_module *)mod, &unres->ds_unres));
}
}
return LY_SUCCESS;
}
static LY_ERR
lys_compile_unres_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
{
struct lyxp_set tmp_set;
struct lyxp_set_scnode *xp_scnode;
uint32_t i, j, idx;
LY_ARRAY_COUNT_TYPE u;
LY_ERR ret = LY_SUCCESS;
memset(&tmp_set, 0, sizeof tmp_set);
for (i = 0; i < set->used; ++i) {
xp_scnode = &set->val.scnodes[i];
if (xp_scnode->in_ctx != LYXP_SET_SCNODE_START_USED) {
xp_scnode->in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
}
}
for (i = 0; i < set->used; ++i) {
xp_scnode = &set->val.scnodes[i];
if (xp_scnode->in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
continue;
}
if ((xp_scnode->type != LYXP_NODE_ELEM) || !lysc_node_when(xp_scnode->scnode)) {
xp_scnode->in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
continue;
}
node = xp_scnode->scnode;
do {
struct lysc_when **when_list, *when;
when_list = lysc_node_when(node);
LY_ARRAY_FOR(when_list, u) {
when = when_list[u];
ret = lyxp_atomize(set->ctx, when->cond, node->module, LY_VALUE_SCHEMA_RESOLVED, when->prefixes,
when->context, when->context, &tmp_set, LYXP_SCNODE_SCHEMA);
if (ret != LY_SUCCESS) {
LOG_LOCSET(node);
LOGVAL(set->ctx, NULL, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
LOG_LOCBACK(1);
goto cleanup;
}
for (j = 0; j < tmp_set.used; ++j) {
if (tmp_set.val.scnodes[j].type != LYXP_NODE_ELEM) {
tmp_set.val.scnodes[j].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
continue;
}
if (lyxp_set_scnode_contains(set, tmp_set.val.scnodes[j].scnode, LYXP_NODE_ELEM, -1, &idx) &&
(set->val.scnodes[idx].in_ctx == LYXP_SET_SCNODE_START_USED)) {
LOG_LOCSET(node);
LOGVAL(set->ctx, NULL, LYVE_SEMANTICS, "When condition cyclic dependency on the node \"%s\".",
tmp_set.val.scnodes[j].scnode->name);
LOG_LOCBACK(1);
ret = LY_EVALID;
goto cleanup;
}
tmp_set.val.scnodes[j].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
}
if (when->context != node) {
assert(tmp_set.val.scnodes[0].scnode == when->context);
if (tmp_set.val.scnodes[0].in_ctx == LYXP_SET_SCNODE_START_USED) {
tmp_set.val.scnodes[0].scnode = (struct lysc_node *)node;
} else {
ret = lyxp_set_scnode_insert_node(&tmp_set, node, LYXP_SET_SCNODE_START_USED, LYXP_AXIS_CHILD, NULL);
LY_CHECK_GOTO(ret, cleanup);
}
}
lyxp_set_scnode_merge(set, &tmp_set);
}
node = node->parent;
} while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
}
cleanup:
lyxp_set_free_content(&tmp_set);
return ret;
}
LY_ERR
lysc_check_status(struct lysc_ctx *ctx, const struct lysc_node *snode, uint16_t flags1, void *mod1, const char *name1,
uint16_t flags2, void *mod2, const char *name2)
{
uint16_t flg1, flg2;
flg1 = (flags1 & LYS_STATUS_MASK) ? (flags1 & LYS_STATUS_MASK) : LYS_STATUS_CURR;
flg2 = (flags2 & LYS_STATUS_MASK) ? (flags2 & LYS_STATUS_MASK) : LYS_STATUS_CURR;
if ((flg1 < flg2) && (mod1 == mod2)) {
if (ctx) {
if (snode) {
LOG_LOCSET(snode);
}
LOGVAL(ctx->ctx, NULL, LYVE_REFERENCE, "A %s definition \"%s\" is not allowed to reference %s definition \"%s\".",
flg1 == LYS_STATUS_CURR ? "current" : "deprecated", name1,
flg2 == LYS_STATUS_OBSLT ? "obsolete" : "deprecated", name2);
if (snode) {
LOG_LOCBACK(1);
}
}
return LY_EVALID;
}
return LY_SUCCESS;
}
static LY_ERR
lys_compile_unres_when(struct lysc_ctx *ctx, const struct lysc_when *when, const struct lysc_node *node)
{
struct lyxp_set tmp_set = {0};
uint32_t i, opts;
struct lysc_node *schema;
LY_ERR ret = LY_SUCCESS;
opts = LYXP_SCNODE_SCHEMA | ((node->flags & LYS_IS_OUTPUT) ? LYXP_SCNODE_OUTPUT : 0);
ret = lyxp_atomize(ctx->ctx, when->cond, node->module, LY_VALUE_SCHEMA_RESOLVED, when->prefixes, when->context,
when->context, &tmp_set, opts);
if (ret) {
LOG_LOCSET(node);
LOGVAL(ctx->ctx, NULL, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
LOG_LOCBACK(1);
goto cleanup;
}
ctx->path[0] = '\0';
lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
for (i = 0; i < tmp_set.used; ++i) {
if (tmp_set.val.scnodes[i].type != LYXP_NODE_ELEM) {
continue;
} else if (tmp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START_USED) {
continue;
}
schema = tmp_set.val.scnodes[i].scnode;
if (lysc_check_status(NULL, NULL, when->flags, node->module, node->name, schema->flags, schema->module,
schema->name)) {
LOGWRN(ctx->ctx, "When condition \"%s\" may be referencing %s node \"%s\".", when->cond->expr,
(schema->flags == LYS_STATUS_OBSLT) ? "obsolete" : "deprecated", schema->name);
}
if (lysc_data_parent(schema) == node) {
LOG_LOCSET(node);
LOGVAL(ctx->ctx, NULL, LYVE_SEMANTICS, "When condition is accessing its own conditional node children.");
LOG_LOCBACK(1);
ret = LY_EVALID;
goto cleanup;
} else if ((schema == node) && (tmp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
LOG_LOCSET(node);
LOGVAL(ctx->ctx, NULL, LYVE_SEMANTICS, "When condition is accessing its own conditional node value.");
LOG_LOCBACK(1);
ret = LY_EVALID;
goto cleanup;
}
}
if (when->context != node) {
assert(tmp_set.val.scnodes[0].scnode == when->context);
if (tmp_set.val.scnodes[0].in_ctx == LYXP_SET_SCNODE_START_USED) {
tmp_set.val.scnodes[0].scnode = (struct lysc_node *)node;
} else {
ret = lyxp_set_scnode_insert_node(&tmp_set, node, LYXP_SET_SCNODE_START_USED, LYXP_AXIS_CHILD, NULL);
LY_CHECK_GOTO(ret, cleanup);
}
}
ret = lys_compile_unres_when_cyclic(&tmp_set, node);
LY_CHECK_GOTO(ret, cleanup);
cleanup:
lyxp_set_free_content(&tmp_set);
return ret;
}
static LY_ERR
lys_compile_unres_must(struct lysc_ctx *ctx, const struct lysc_node *node, const struct lysp_module **local_mods)
{
struct lyxp_set tmp_set;
uint32_t i, opts;
LY_ARRAY_COUNT_TYPE u;
struct lysc_must *musts;
LY_ERR ret = LY_SUCCESS;
uint16_t flg;
memset(&tmp_set, 0, sizeof tmp_set);
opts = LYXP_SCNODE_SCHEMA | ((node->flags & LYS_IS_OUTPUT) ? LYXP_SCNODE_OUTPUT : 0);
musts = lysc_node_musts(node);
LY_ARRAY_FOR(musts, u) {
ret = lyxp_atomize(ctx->ctx, musts[u].cond, node->module, LY_VALUE_SCHEMA_RESOLVED, musts[u].prefixes, node,
node, &tmp_set, opts);
if (ret) {
LOG_LOCSET(node);
LOGVAL(ctx->ctx, NULL, LYVE_SEMANTICS, "Invalid must condition \"%s\".", musts[u].cond->expr);
LOG_LOCBACK(1);
goto cleanup;
}
ctx->path[0] = '\0';
lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
for (i = 0; i < tmp_set.used; ++i) {
if (tmp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
struct lysc_node *schema = tmp_set.val.scnodes[i].scnode;
if (local_mods[u]->mod == node->module) {
flg = node->flags;
} else {
flg = LYS_STATUS_CURR;
}
if (lysc_check_status(NULL, NULL, flg, local_mods[u]->mod, node->name, schema->flags, schema->module,
schema->name)) {
LOGWRN(ctx->ctx, "Must condition \"%s\" may be referencing %s node \"%s\".", musts[u].cond->expr,
(schema->flags == LYS_STATUS_OBSLT) ? "obsolete" : "deprecated", schema->name);
break;
}
}
}
lyxp_set_free_content(&tmp_set);
}
cleanup:
lyxp_set_free_content(&tmp_set);
return ret;
}
static void
lys_compile_unres_disabled_bitenum_remove(const struct ly_ctx *ctx, struct lysc_type_bitenum_item *items)
{
LY_ARRAY_COUNT_TYPE u = 0, last_u;
while (u < LY_ARRAY_COUNT(items)) {
if (items[u].flags & LYS_DISABLED) {
lysc_enum_item_free(ctx, &items[u]);
last_u = LY_ARRAY_COUNT(items) - 1;
if (u < last_u) {
memmove(items + u, items + u + 1, (last_u - u) * sizeof *items);
}
LY_ARRAY_DECREMENT(items);
continue;
}
++u;
}
}
static LY_ERR
lys_compile_unres_disabled_bitenum(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf)
{
struct lysc_type **t;
LY_ARRAY_COUNT_TYPE u, count;
struct lysc_type_enum *ent;
ly_bool has_value = 0;
if (leaf->type->basetype == LY_TYPE_UNION) {
t = ((struct lysc_type_union *)leaf->type)->types;
count = LY_ARRAY_COUNT(t);
} else {
t = &leaf->type;
count = 1;
}
for (u = 0; u < count; ++u) {
if ((t[u]->basetype == LY_TYPE_BITS) || (t[u]->basetype == LY_TYPE_ENUM)) {
ent = (struct lysc_type_enum *)(t[u]);
lys_compile_unres_disabled_bitenum_remove(ctx->ctx, ent->enums);
if (LY_ARRAY_COUNT(ent->enums)) {
has_value = 1;
}
} else {
has_value = 1;
}
}
if (!has_value) {
LOG_LOCSET((struct lysc_node *)leaf);
LOGVAL(ctx->ctx, NULL, LYVE_SEMANTICS, "Node \"%s\" without any (or all disabled) valid values.", leaf->name);
LOG_LOCBACK(1);
return LY_EVALID;
}
return LY_SUCCESS;
}
static LY_ERR
lys_compile_leafref_circ_check(const struct lysc_type *type, const struct lysc_type *lref)
{
const struct lysc_type_union *un;
LY_ARRAY_COUNT_TYPE u;
if (!type) {
return LY_SUCCESS;
}
if (type == lref) {
return LY_EVALID;
}
switch (type->basetype) {
case LY_TYPE_LEAFREF:
LY_CHECK_RET(lys_compile_leafref_circ_check(((struct lysc_type_leafref *)type)->realtype, lref));
break;
case LY_TYPE_UNION:
un = (struct lysc_type_union *)type;
LY_ARRAY_FOR(un->types, u) {
LY_CHECK_RET(lys_compile_leafref_circ_check(un->types[u], lref));
}
default:
break;
}
return LY_SUCCESS;
}
static LY_ERR
lys_compile_unres_leafref(struct lysc_ctx *ctx, const struct lysc_node *node, struct lysc_type_leafref *lref,
const struct lysp_module *local_mod)
{
const struct lysc_node *target = NULL;
struct ly_path *p;
uint16_t flg;
assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
if (lref->realtype) {
return LY_SUCCESS;
}
LY_CHECK_RET(ly_path_compile_leafref(ctx->ctx, node, lref->path,
(node->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY,
LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p));
target = p[LY_ARRAY_COUNT(p) - 1].node;
ly_path_free(p);
if (!(target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
LOG_LOCSET(node);
LOGVAL(ctx->ctx, NULL, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
lref->path->expr, lys_nodetype2str(target->nodetype));
LOG_LOCBACK(1);
return LY_EVALID;
}
ctx->path[0] = '\0';
lysc_path(node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
ctx->path_len = strlen(ctx->path);
if (node->module == local_mod->mod) {
flg = node->flags;
} else {
flg = LYS_STATUS_CURR;
}
if (lysc_check_status(ctx, node, flg, local_mod->mod, node->name, target->flags, target->module, target->name)) {
return LY_EVALID;
}
ctx->path_len = 1;
ctx->path[1] = '\0';
if (lref->require_instance) {
if ((node->flags & LYS_CONFIG_W) && (target->flags & LYS_CONFIG_R)) {
LOG_LOCSET(node);
LOGVAL(ctx->ctx, NULL, LYVE_REFERENCE, "Invalid leafref path \"%s\" - target is supposed"
" to represent configuration data (as the leafref does), but it does not.", lref->path->expr);
LOG_LOCBACK(1);
return LY_EVALID;
}
}
if (lys_compile_leafref_circ_check(((struct lysc_node_leaf *)target)->type, (struct lysc_type *)lref)) {
LOG_LOCSET(node);
LOGVAL(ctx->ctx, NULL, LYVE_REFERENCE, "Invalid leafref path \"%s\" - circular chain of leafrefs detected.",
lref->path->expr);
LOG_LOCBACK(1);
return LY_EVALID;
}
lref->realtype = ((struct lysc_node_leaf *)target)->type;
++lref->realtype->refcount;
return LY_SUCCESS;
}
static LY_ERR
lys_compile_unres_dflt(struct lysc_ctx *ctx, struct lysc_node *node, struct lysc_type *type, const char *dflt,
const struct lysp_module *dflt_pmod, struct lysc_value *value, struct lys_glob_unres *unres)
{
LY_ERR rc = LY_SUCCESS;
uint32_t options;
struct lyd_value storage = {0}, *val;
struct ly_err_item *err = NULL;
LY_VALUE_FORMAT format;
struct lyplg_type *type_plg;
options = (ctx->ctx->opts & LY_CTX_REF_IMPLEMENTED) ? LYPLG_TYPE_STORE_IMPLEMENT : 0;
type_plg = LYSC_GET_TYPE_PLG(type->plugin_ref);
rc = type_plg->store(ctx->ctx, type, dflt, strlen(dflt) * 8, options, LY_VALUE_SCHEMA, (void *)dflt_pmod,
LYD_HINT_SCHEMA, node, &storage, unres, &err);
if (rc == LY_ERECOMPILE) {
return rc;
} else if (rc == LY_EINCOMPLETE) {
rc = LY_SUCCESS;
}
if (!rc) {
LY_CHECK_GOTO(rc = lysdict_insert(ctx->ctx, dflt, 0, &value->str), cleanup);
LY_CHECK_GOTO(rc = lyplg_type_prefix_data_new(ctx->ctx, dflt, strlen(dflt), LY_VALUE_SCHEMA, dflt_pmod, &format,
(void **)&value->prefixes), cleanup);
} else {
LOG_LOCSET(node);
if (err) {
LOGVAL(ctx->ctx, NULL, LYVE_SEMANTICS, "Invalid default - value does not fit the type (%s).", err->msg);
ly_err_free(err);
} else {
LOGVAL(ctx->ctx, NULL, LYVE_SEMANTICS, "Invalid default - value does not fit the type.");
}
LOG_LOCBACK(1);
}
cleanup:
if (!rc) {
if (storage.realtype->basetype == LY_TYPE_UNION) {
val = &storage.subvalue->value;
} else {
val = &storage;
}
if (val->realtype->basetype == LY_TYPE_INST) {
ly_path_free(val->target);
val->target = NULL;
}
type_plg->free(ctx->ctx, &storage);
}
return rc;
}
static LY_ERR
lys_compile_unres_leaf_dlft(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, struct lysp_qname *dflt,
struct lys_glob_unres *unres)
{
assert(!leaf->dflt.str);
if (leaf->flags & (LYS_MAND_TRUE | LYS_KEY)) {
return LY_SUCCESS;
}
return lys_compile_unres_dflt(ctx, &leaf->node, leaf->type, dflt->str, dflt->mod, &leaf->dflt, unres);
}
static LY_ERR
lys_compile_unres_llist_dflts(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, struct lysp_qname *dflt,
struct lysp_qname *dflts, struct lys_glob_unres *unres)
{
LY_ARRAY_COUNT_TYPE orig_count, u, v;
assert(dflt || dflts);
orig_count = LY_ARRAY_COUNT(llist->dflts);
LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + (dflts ? LY_ARRAY_COUNT(dflts) : 1), LY_EMEM);
if (dflts) {
LY_ARRAY_FOR(dflts, u) {
LY_CHECK_RET(lys_compile_unres_dflt(ctx, &llist->node, llist->type, dflts[u].str, dflts[u].mod,
&llist->dflts[orig_count + u], unres));
LY_ARRAY_INCREMENT(llist->dflts);
}
} else {
LY_CHECK_RET(lys_compile_unres_dflt(ctx, &llist->node, llist->type, dflt->str, dflt->mod,
&llist->dflts[orig_count], unres));
LY_ARRAY_INCREMENT(llist->dflts);
}
if (llist->flags & LYS_CONFIG_W) {
for (u = orig_count; u < LY_ARRAY_COUNT(llist->dflts); ++u) {
for (v = 0; v < u; ++v) {
if (llist->dflts[u].str == llist->dflts[v].str) {
LOG_LOCSET((struct lysc_node *)llist);
LOGVAL(ctx->ctx, NULL, LYVE_SEMANTICS,
"Configuration leaf-list has multiple defaults of the same value \"%s\".", llist->dflts[u].str);
LOG_LOCBACK(1);
return LY_EVALID;
}
}
}
}
return LY_SUCCESS;
}
static struct lysc_type_leafref *
lys_type_leafref_next(const struct lysc_node *node, LY_ARRAY_COUNT_TYPE *index)
{
struct lysc_type_leafref *ret = NULL;
struct lysc_type_union *uni;
struct lysc_type *leaf_type;
assert(node->nodetype & LYD_NODE_TERM);
leaf_type = ((struct lysc_node_leaf *)node)->type;
if (leaf_type->basetype == LY_TYPE_UNION) {
uni = (struct lysc_type_union *)leaf_type;
while (*index < LY_ARRAY_COUNT(uni->types)) {
if (uni->types[*index]->basetype == LY_TYPE_LEAFREF) {
ret = (struct lysc_type_leafref *)uni->types[*index];
++(*index);
break;
}
++(*index);
}
} else {
if (*index == 0) {
++(*index);
assert(leaf_type->basetype == LY_TYPE_LEAFREF);
ret = (struct lysc_type_leafref *)leaf_type;
}
}
return ret;
}
static LY_ERR
lys_compile_unres_depset_implement(struct ly_ctx *ctx, struct lys_glob_unres *unres)
{
struct lys_depset_unres *ds_unres = &unres->ds_unres;
struct lysc_type_leafref *lref;
const struct lys_module *mod;
LY_ARRAY_COUNT_TYPE u;
struct lysc_unres_leafref *l;
struct lysc_unres_when *w;
struct lysc_unres_must *m;
struct lysc_must *musts;
ly_bool not_implemented;
uint32_t di = 0, li = 0, wi = 0, mi = 0;
implement_all:
while (di < ds_unres->disabled_leafrefs.count) {
l = ds_unres->disabled_leafrefs.objs[di];
u = 0;
while ((lref = lys_type_leafref_next(l->node, &u))) {
LY_CHECK_RET(lys_compile_expr_implement(ctx, lref->path, LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, 1, unres, NULL));
}
++di;
}
while (li < ds_unres->leafrefs.count) {
l = ds_unres->leafrefs.objs[li];
u = 0;
while ((lref = lys_type_leafref_next(l->node, &u))) {
LY_CHECK_RET(lys_compile_expr_implement(ctx, lref->path, LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, 1, unres, NULL));
}
++li;
}
while (wi < ds_unres->whens.count) {
w = ds_unres->whens.objs[wi];
LY_CHECK_RET(lys_compile_expr_implement(ctx, w->when->cond, LY_VALUE_SCHEMA_RESOLVED, w->when->prefixes,
ctx->opts & LY_CTX_REF_IMPLEMENTED, unres, &mod));
if (mod) {
LOGWRN(ctx, "When condition \"%s\" check skipped because referenced module \"%s\" is not implemented.",
w->when->cond->expr, mod->name);
ly_set_rm_index(&ds_unres->whens, wi, free);
continue;
}
++wi;
}
while (mi < ds_unres->musts.count) {
m = ds_unres->musts.objs[mi];
not_implemented = 0;
musts = lysc_node_musts(m->node);
LY_ARRAY_FOR(musts, u) {
LY_CHECK_RET(lys_compile_expr_implement(ctx, musts[u].cond, LY_VALUE_SCHEMA_RESOLVED, musts[u].prefixes,
ctx->opts & LY_CTX_REF_IMPLEMENTED, unres, &mod));
if (mod) {
LOGWRN(ctx, "Must condition \"%s\" check skipped because referenced module \"%s\" is not implemented.",
musts[u].cond->expr, mod->name);
not_implemented = 1;
}
}
if (not_implemented) {
lysc_unres_must_free(m);
ly_set_rm_index(&ds_unres->musts, mi, NULL);
continue;
}
++mi;
}
if ((di < ds_unres->disabled_leafrefs.count) || (li < ds_unres->leafrefs.count) || (wi < ds_unres->whens.count)) {
goto implement_all;
}
return LY_SUCCESS;
}
static LY_ERR
lys_compile_unres_check_disabled(const struct lysc_node *node)
{
const struct lysc_node *parent;
struct lysc_node_list *slist;
LY_ARRAY_COUNT_TYPE u, v;
int found;
if (node->flags & LYS_KEY) {
LOG_LOCSET(node);
LOGVAL(node->module->ctx, NULL, LYVE_REFERENCE, "Key \"%s\" is disabled.", node->name);
LOG_LOCBACK(1);
return LY_EVALID;
}
for (parent = node->parent; parent; parent = parent->parent) {
if (parent->nodetype != LYS_LIST) {
continue;
}
slist = (struct lysc_node_list *)parent;
found = 0;
LY_ARRAY_FOR(slist->uniques, u) {
LY_ARRAY_FOR(slist->uniques[u], v) {
if (slist->uniques[u][v] == (struct lysc_node_leaf *)node) {
found = 1;
break;
}
}
if (found) {
break;
}
}
if (found) {
if (LY_ARRAY_COUNT(slist->uniques[u]) > 1) {
if (v < LY_ARRAY_COUNT(slist->uniques[u]) - 1) {
memmove(&slist->uniques[u][v], &slist->uniques[u][v + 1],
(LY_ARRAY_COUNT(slist->uniques[u]) - v - 1) * sizeof slist->uniques[u][v]);
}
LY_ARRAY_DECREMENT(slist->uniques[u]);
} else {
LY_ARRAY_FREE(slist->uniques[u]);
if (LY_ARRAY_COUNT(slist->uniques) > 1) {
if (u < LY_ARRAY_COUNT(slist->uniques) - 1) {
memmove(&slist->uniques[u], &slist->uniques[u + 1],
(LY_ARRAY_COUNT(slist->uniques) - u - 1) * sizeof slist->uniques[u]);
}
LY_ARRAY_DECREMENT(slist->uniques);
} else {
LY_ARRAY_FREE(slist->uniques);
slist->uniques = NULL;
}
}
}
}
return LY_SUCCESS;
}
static LY_ERR
lys_compile_unres_depset(struct ly_ctx *ctx, struct lys_glob_unres *unres)
{
LY_ERR ret = LY_SUCCESS;
struct lysc_node *node;
struct lysc_type *typeiter;
struct lysc_type_leafref *lref;
struct lysc_ctx cctx = {0};
struct lys_depset_unres *ds_unres = &unres->ds_unres;
struct ly_path *path;
LY_ARRAY_COUNT_TYPE v;
struct lysc_unres_leafref *l;
struct lysc_unres_when *w;
struct lysc_unres_must *m;
struct lysc_unres_dflt *d;
uint32_t i, processed_leafrefs = 0;
resolve_all:
if ((ret = lys_compile_unres_depset_implement(ctx, unres))) {
goto cleanup;
}
while (ds_unres->disabled_leafrefs.count) {
i = ds_unres->disabled_leafrefs.count - 1;
l = ds_unres->disabled_leafrefs.objs[i];
LYSC_CTX_INIT_PMOD(cctx, l->node->module->parsed, l->ext);
v = 0;
while ((ret == LY_SUCCESS) && (lref = lys_type_leafref_next(l->node, &v))) {
ret = lys_compile_unres_leafref(&cctx, l->node, lref, l->local_mod);
}
LY_CHECK_GOTO(ret, cleanup);
ly_set_rm_index(&ds_unres->disabled_leafrefs, i, free);
}
for (i = processed_leafrefs; i < ds_unres->leafrefs.count; ++i) {
l = ds_unres->leafrefs.objs[i];
LYSC_CTX_INIT_PMOD(cctx, l->node->module->parsed, l->ext);
v = 0;
while ((ret == LY_SUCCESS) && (lref = lys_type_leafref_next(l->node, &v))) {
ret = lys_compile_unres_leafref(&cctx, l->node, lref, l->local_mod);
}
LY_CHECK_GOTO(ret, cleanup);
}
for (i = processed_leafrefs; i < ds_unres->leafrefs.count; ++i) {
l = ds_unres->leafrefs.objs[i];
v = 0;
while ((lref = lys_type_leafref_next(l->node, &v))) {
for (typeiter = lref->realtype;
typeiter->basetype == LY_TYPE_LEAFREF;
typeiter = ((struct lysc_type_leafref *)typeiter)->realtype) {}
lysc_type_free(cctx.ctx, lref->realtype);
lref->realtype = typeiter;
++lref->realtype->refcount;
}
processed_leafrefs++;
}
while (ds_unres->whens.count) {
i = ds_unres->whens.count - 1;
w = ds_unres->whens.objs[i];
LYSC_CTX_INIT_PMOD(cctx, w->node->module->parsed, NULL);
ret = lys_compile_unres_when(&cctx, w->when, w->node);
LY_CHECK_GOTO(ret, cleanup);
free(w);
ly_set_rm_index(&ds_unres->whens, i, NULL);
}
while (ds_unres->musts.count) {
i = ds_unres->musts.count - 1;
m = ds_unres->musts.objs[i];
LYSC_CTX_INIT_PMOD(cctx, m->node->module->parsed, m->ext);
ret = lys_compile_unres_must(&cctx, m->node, m->local_mods);
LY_CHECK_GOTO(ret, cleanup);
lysc_unres_must_free(m);
ly_set_rm_index(&ds_unres->musts, i, NULL);
}
while (ds_unres->disabled_bitenums.count) {
i = ds_unres->disabled_bitenums.count - 1;
node = ds_unres->disabled_bitenums.objs[i];
LYSC_CTX_INIT_PMOD(cctx, node->module->parsed, NULL);
ret = lys_compile_unres_disabled_bitenum(&cctx, (struct lysc_node_leaf *)node);
LY_CHECK_GOTO(ret, cleanup);
ly_set_rm_index(&ds_unres->disabled_bitenums, i, NULL);
}
while (ds_unres->dflts.count) {
i = ds_unres->dflts.count - 1;
d = ds_unres->dflts.objs[i];
LYSC_CTX_INIT_PMOD(cctx, d->leaf->module->parsed, NULL);
if (d->leaf->nodetype == LYS_LEAF) {
ret = lys_compile_unres_leaf_dlft(&cctx, d->leaf, d->dflt, unres);
} else {
ret = lys_compile_unres_llist_dflts(&cctx, d->llist, d->dflt, d->dflts, unres);
}
LY_CHECK_GOTO(ret, cleanup);
lysc_unres_dflt_free(ctx, d);
ly_set_rm_index(&ds_unres->dflts, i, NULL);
}
if ((processed_leafrefs != ds_unres->leafrefs.count) || ds_unres->disabled_leafrefs.count ||
ds_unres->whens.count || ds_unres->musts.count || ds_unres->dflts.count) {
goto resolve_all;
}
for (i = 0; i < ds_unres->disabled.count; ++i) {
node = ds_unres->disabled.snodes[i];
ret = lys_compile_unres_check_disabled(node);
LY_CHECK_GOTO(ret, cleanup);
LYSC_CTX_INIT_PMOD(cctx, node->module->parsed, NULL);
lysc_node_free(cctx.ctx, node, 1);
}
for (i = 0; i < ds_unres->leafrefs.count; ++i) {
l = ds_unres->leafrefs.objs[i];
LYSC_CTX_INIT_PMOD(cctx, l->node->module->parsed, l->ext);
v = 0;
while ((lref = lys_type_leafref_next(l->node, &v))) {
ret = ly_path_compile_leafref(cctx.ctx, l->node, lref->path,
(l->node->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY,
LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &path);
ly_path_free(path);
assert(ret != LY_ERECOMPILE);
if (ret) {
LOG_LOCSET(l->node);
LOGVAL(ctx, NULL, LYVE_REFERENCE, "Target of leafref \"%s\" cannot be referenced because it is disabled.",
l->node->name);
LOG_LOCBACK(1);
ret = LY_EVALID;
goto cleanup;
}
}
}
cleanup:
return ret;
}
static void
lys_compile_unres_depset_erase(const struct ly_ctx *ctx, struct lys_glob_unres *unres)
{
uint32_t i;
ly_set_erase(&unres->ds_unres.whens, free);
for (i = 0; i < unres->ds_unres.musts.count; ++i) {
lysc_unres_must_free(unres->ds_unres.musts.objs[i]);
}
ly_set_erase(&unres->ds_unres.musts, NULL);
ly_set_erase(&unres->ds_unres.leafrefs, free);
for (i = 0; i < unres->ds_unres.dflts.count; ++i) {
lysc_unres_dflt_free(ctx, unres->ds_unres.dflts.objs[i]);
}
ly_set_erase(&unres->ds_unres.dflts, NULL);
ly_set_erase(&unres->ds_unres.disabled, NULL);
ly_set_erase(&unres->ds_unres.disabled_leafrefs, free);
ly_set_erase(&unres->ds_unres.disabled_bitenums, NULL);
}
static LY_ERR
lys_compile_depset_r(struct ly_ctx *ctx, struct ly_set *dep_set, struct lys_glob_unres *unres)
{
LY_ERR ret = LY_SUCCESS;
struct lys_module *mod;
uint32_t i;
for (i = 0; i < dep_set->count; ++i) {
mod = dep_set->objs[i];
if (!mod->to_compile) {
continue;
}
assert(mod->implemented);
lysc_module_free(ctx, mod->compiled);
mod->compiled = NULL;
LY_CHECK_GOTO(ret = lys_compile(mod, &unres->ds_unres), cleanup);
}
resolve_unres:
ret = lys_compile_unres_depset(ctx, unres);
lys_compile_unres_depset_erase(ctx, unres);
if (ret == LY_ERECOMPILE) {
return lys_compile_depset_r(ctx, dep_set, unres);
} else if (ret) {
goto cleanup;
}
for (i = 0; i < dep_set->count; ++i) {
mod = dep_set->objs[i];
if (mod->to_compile && !mod->compiled) {
LY_CHECK_GOTO(ret = lys_compile(mod, &unres->ds_unres), cleanup);
goto resolve_unres;
}
mod->to_compile = 0;
}
cleanup:
lys_compile_unres_depset_erase(ctx, unres);
return ret;
}
static LY_ERR
lys_compile_depset_check_features(struct ly_set *dep_set)
{
struct lys_module *mod;
uint32_t i;
for (i = 0; i < dep_set->count; ++i) {
mod = dep_set->objs[i];
if (!mod->to_compile) {
continue;
}
LY_CHECK_RET(lys_check_features(mod->parsed));
}
return LY_SUCCESS;
}
LY_ERR
lys_compile_depset_all(struct ly_ctx *ctx, struct lys_glob_unres *unres)
{
uint32_t i;
for (i = 0; i < unres->dep_sets.count; ++i) {
LY_CHECK_RET(lys_compile_depset_check_features(unres->dep_sets.objs[i]));
LY_CHECK_RET(lys_compile_depset_r(ctx, unres->dep_sets.objs[i], unres));
}
return LY_SUCCESS;
}
static LY_ERR
lys_compile_unres_mod(struct lysc_ctx *ctx)
{
struct lysc_augment *aug;
struct lysc_deviation *dev;
struct lys_module *orig_mod = ctx->cur_mod;
uint32_t i;
for (i = 0; i < ctx->augs.count; ++i) {
aug = ctx->augs.objs[i];
ctx->cur_mod = aug->aug_pmod->mod;
if (aug->ext) {
lysc_update_path(ctx, NULL, "{ext-inst}");
lysc_update_path(ctx, NULL, aug->ext->name);
}
lysc_update_path(ctx, NULL, "{augment}");
lysc_update_path(ctx, NULL, aug->nodeid->str);
LOGVAL(ctx->ctx, NULL, LYVE_REFERENCE, "Augment%s target node \"%s\" from module \"%s\" was not found.",
aug->ext ? " ext-inst" : "", aug->nodeid->str, LYSP_MODULE_NAME(aug->aug_pmod));
ctx->cur_mod = orig_mod;
lysc_update_path(ctx, NULL, NULL);
lysc_update_path(ctx, NULL, NULL);
if (aug->ext) {
lysc_update_path(ctx, NULL, NULL);
lysc_update_path(ctx, NULL, NULL);
}
}
if (ctx->augs.count) {
return LY_ENOTFOUND;
}
for (i = 0; i < ctx->devs.count; ++i) {
dev = ctx->devs.objs[i];
lysc_update_path(ctx, NULL, "{deviation}");
lysc_update_path(ctx, NULL, dev->nodeid->str);
LOGVAL(ctx->ctx, NULL, LYVE_REFERENCE, "Deviation(s) target node \"%s\" from module \"%s\" was not found.",
dev->nodeid->str, LYSP_MODULE_NAME(dev->dev_pmods[0]));
lysc_update_path(ctx, NULL, NULL);
lysc_update_path(ctx, NULL, NULL);
}
if (ctx->devs.count) {
return LY_ENOTFOUND;
}
return LY_SUCCESS;
}
static void
lys_compile_unres_mod_erase(struct lysc_ctx *ctx, ly_bool error)
{
uint32_t i;
ly_set_erase(&ctx->groupings, NULL);
ly_set_erase(&ctx->tpdf_chain, NULL);
if (!error) {
LY_CHECK_ERR_RET(ctx->augs.count, LOGINT(ctx->ctx), );
LY_CHECK_ERR_RET(ctx->devs.count, LOGINT(ctx->ctx), );
ly_set_erase(&ctx->augs, NULL);
ly_set_erase(&ctx->devs, NULL);
ly_set_erase(&ctx->uses_augs, NULL);
ly_set_erase(&ctx->uses_rfns, NULL);
} else {
for (i = 0; i < ctx->augs.count; ++i) {
lysc_augment_free(ctx->ctx, ctx->augs.objs[i]);
}
ly_set_erase(&ctx->augs, NULL);
for (i = 0; i < ctx->devs.count; ++i) {
lysc_deviation_free(ctx->ctx, ctx->devs.objs[i]);
}
ly_set_erase(&ctx->devs, NULL);
for (i = 0; i < ctx->uses_augs.count; ++i) {
lysc_augment_free(ctx->ctx, ctx->uses_augs.objs[i]);
}
ly_set_erase(&ctx->uses_augs, NULL);
for (i = 0; i < ctx->uses_rfns.count; ++i) {
lysc_refine_free(ctx->ctx, ctx->uses_rfns.objs[i]);
}
ly_set_erase(&ctx->uses_rfns, NULL);
}
}
static LY_ERR
lys_compile_enabled_features(struct lys_module *mod)
{
LY_ERR rc = LY_SUCCESS;
struct lysp_feature *f = NULL;
uint32_t idx = 0;
const char **feat_p;
while ((f = lysp_feature_next(f, mod->parsed, &idx))) {
if (f->flags & LYS_FENABLED) {
LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->features, feat_p, rc, cleanup);
LY_CHECK_GOTO(rc = lysdict_dup(mod->ctx, f->name, feat_p), cleanup);
}
}
LY_ARRAY_NEW_GOTO(mod->ctx, mod->compiled->features, feat_p, rc, cleanup);
LY_ARRAY_DECREMENT(mod->compiled->features);
cleanup:
return rc;
}
LY_ERR
lys_compile(struct lys_module *mod, struct lys_depset_unres *unres)
{
struct lysc_ctx ctx = {0};
struct lysc_module *mod_c = NULL;
struct lysp_module *sp;
struct lysp_submodule *submod;
struct lysp_node *pnode;
struct lysp_node_grp *grp;
LY_ARRAY_COUNT_TYPE u;
LY_ERR ret = LY_SUCCESS;
LY_CHECK_ARG_RET(NULL, mod, mod->parsed, !mod->compiled, mod->ctx, LY_EINVAL);
assert(mod->implemented && mod->to_compile);
sp = mod->parsed;
LYSC_CTX_INIT_PMOD(ctx, sp, NULL);
ctx.unres = unres;
mod->compiled = mod_c = calloc(1, sizeof *mod_c);
LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
mod_c->mod = mod;
LY_CHECK_GOTO(ret = lys_compile_enabled_features(mod), cleanup);
LY_CHECK_GOTO(ret = lys_precompile_own_augments(&ctx), cleanup);
LY_CHECK_GOTO(ret = lys_precompile_own_deviations(&ctx), cleanup);
LY_LIST_FOR(sp->data, pnode) {
LY_CHECK_GOTO(ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL), cleanup);
}
LY_LIST_FOR((struct lysp_node *)sp->rpcs, pnode) {
LY_CHECK_GOTO(ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL), cleanup);
}
LY_LIST_FOR((struct lysp_node *)sp->notifs, pnode) {
LY_CHECK_GOTO(ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL), cleanup);
}
COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, ret, cleanup);
LY_ARRAY_FOR(sp->includes, u) {
submod = sp->includes[u].submodule;
ctx.pmod = (struct lysp_module *)submod;
LY_LIST_FOR(submod->data, pnode) {
ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL);
LY_CHECK_GOTO(ret, cleanup);
}
LY_LIST_FOR((struct lysp_node *)submod->rpcs, pnode) {
ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL);
LY_CHECK_GOTO(ret, cleanup);
}
LY_LIST_FOR((struct lysp_node *)submod->notifs, pnode) {
ret = lys_compile_node(&ctx, pnode, NULL, 0, NULL);
LY_CHECK_GOTO(ret, cleanup);
}
COMPILE_EXTS_GOTO(&ctx, submod->exts, mod_c->exts, mod_c, ret, cleanup);
}
ctx.pmod = sp;
ctx.compile_opts |= LYS_COMPILE_GROUPING;
LY_LIST_FOR(sp->groupings, grp) {
if (!(grp->flags & LYS_USED_GRP)) {
LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, NULL, grp), cleanup);
}
}
LY_LIST_FOR(sp->data, pnode) {
LY_LIST_FOR((struct lysp_node_grp *)lysp_node_groupings(pnode), grp) {
if (!(grp->flags & LYS_USED_GRP)) {
LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, pnode, grp), cleanup);
}
}
}
LY_ARRAY_FOR(sp->includes, u) {
submod = sp->includes[u].submodule;
ctx.pmod = (struct lysp_module *)submod;
LY_LIST_FOR(submod->groupings, grp) {
if (!(grp->flags & LYS_USED_GRP)) {
LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, NULL, grp), cleanup);
}
}
LY_LIST_FOR(submod->data, pnode) {
LY_LIST_FOR((struct lysp_node_grp *)lysp_node_groupings(pnode), grp) {
if (!(grp->flags & LYS_USED_GRP)) {
LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, pnode, grp), cleanup);
}
}
}
}
ctx.pmod = sp;
ly_log_location_revert(0, 1, 0);
LY_CHECK_GOTO(ret = lys_compile_unres_mod(&ctx), cleanup);
if (mod->ctx->opts & LY_CTX_LYB_HASHES) {
lysc_module_dfs_full(mod, lyb_cache_node_hash_cb, NULL);
}
cleanup:
ly_log_location_revert(0, 1, 0);
lys_compile_unres_mod_erase(&ctx, ret);
if (ret) {
lysc_module_free(ctx.ctx, mod_c);
mod->compiled = NULL;
}
return ret;
}
static LY_ERR
lys_compile_extensions_pmod(struct lys_module *mod, struct lysp_module *pmod)
{
LY_ERR rc = LY_SUCCESS;
struct lysc_ctx ctx = {0};
LY_ARRAY_COUNT_TYPE u;
struct lysp_ext *ep;
struct lysc_ext *ec;
LYSC_CTX_INIT_PMOD(ctx, pmod, NULL);
LY_ARRAY_FOR(pmod->extensions, u) {
ep = &pmod->extensions[u];
LY_ARRAY_NEW_GOTO(mod->ctx, mod->extensions, ec, rc, cleanup);
DUP_STRING_GOTO(ctx.ctx, ep->name, ec->name, rc, cleanup);
DUP_STRING_GOTO(ctx.ctx, ep->argname, ec->argname, rc, cleanup);
ec->module = mod;
ec->plugin_ref = ep->plugin_ref;
}
cleanup:
return rc;
}
static LY_ERR
lys_compile_extensions_ext_pmod(struct lys_module *mod, LY_ARRAY_COUNT_TYPE mod_ext_idx, struct lysp_module *pmod)
{
LY_ERR rc = LY_SUCCESS;
struct lysc_ctx ctx = {0};
LY_ARRAY_COUNT_TYPE u;
struct lysp_ext *ep;
struct lysc_ext *ec;
LYSC_CTX_INIT_PMOD(ctx, pmod, NULL);
LY_ARRAY_FOR(pmod->extensions, u) {
ep = &pmod->extensions[u];
ec = &mod->extensions[mod_ext_idx];
lysc_update_path(&ctx, NULL, "{extension}");
lysc_update_path(&ctx, NULL, ep->name);
COMPILE_EXTS_GOTO(&ctx, ep->exts, ec->exts, ec, rc, next_line);
next_line:
lysc_update_path(&ctx, NULL, NULL);
lysc_update_path(&ctx, NULL, NULL);
LY_CHECK_GOTO(rc, cleanup);
++mod_ext_idx;
}
cleanup:
return rc;
}
LY_ERR
lys_compile_extensions(struct lys_module *mod)
{
LY_ERR rc = LY_SUCCESS;
LY_ARRAY_COUNT_TYPE u, v;
struct lysp_include *inc;
LY_CHECK_GOTO(rc = lys_compile_extensions_pmod(mod, mod->parsed), cleanup);
LY_ARRAY_FOR(mod->parsed->includes, u) {
LY_CHECK_GOTO(rc = lys_compile_extensions_pmod(mod, (struct lysp_module *)mod->parsed->includes[u].submodule),
cleanup);
}
v = 0;
LY_CHECK_GOTO(rc = lys_compile_extensions_ext_pmod(mod, v, mod->parsed), cleanup);
v += LY_ARRAY_COUNT(mod->parsed->extensions);
LY_ARRAY_FOR(mod->parsed->includes, u) {
inc = &mod->parsed->includes[u];
LY_CHECK_GOTO(rc = lys_compile_extensions_ext_pmod(mod, v, (struct lysp_module *)inc->submodule), cleanup);
v += LY_ARRAY_COUNT(inc->submodule->extensions);
}
assert(v == LY_ARRAY_COUNT(mod->extensions));
cleanup:
return rc;
}
LY_ERR
lys_compile_identities(struct lys_module *mod)
{
LY_ERR rc = LY_SUCCESS;
struct lysc_ctx ctx = {0};
struct lysp_submodule *submod;
LY_ARRAY_COUNT_TYPE u;
rc = lys_identity_precompile(NULL, mod->ctx, mod->parsed, mod->parsed->identities, &mod->identities);
LY_CHECK_GOTO(rc, cleanup);
LY_ARRAY_FOR(mod->parsed->includes, u) {
submod = mod->parsed->includes[u].submodule;
rc = lys_identity_precompile(NULL, mod->ctx, (struct lysp_module *)submod, submod->identities, &mod->identities);
LY_CHECK_GOTO(rc, cleanup);
}
LYSC_CTX_INIT_PMOD(ctx, mod->parsed, NULL);
if (mod->parsed->identities) {
rc = lys_compile_identities_derived(&ctx, mod->parsed->identities, &mod->identities);
LY_CHECK_GOTO(rc, cleanup);
}
lysc_update_path(&ctx, NULL, "{submodule}");
LY_ARRAY_FOR(mod->parsed->includes, u) {
submod = mod->parsed->includes[u].submodule;
if (submod->identities) {
ctx.pmod = (struct lysp_module *)submod;
lysc_update_path(&ctx, NULL, submod->name);
rc = lys_compile_identities_derived(&ctx, submod->identities, &mod->identities);
lysc_update_path(&ctx, NULL, NULL);
}
if (rc) {
break;
}
}
lysc_update_path(&ctx, NULL, NULL);
cleanup:
ly_log_location_revert(0, 1, 0);
return rc;
}
static LY_ERR
lys_has_compiled_import_r(struct lys_module *mod)
{
LY_ARRAY_COUNT_TYPE u;
struct lys_module *m;
LY_ARRAY_FOR(mod->parsed->imports, u) {
m = mod->parsed->imports[u].module;
if (!m->implemented) {
continue;
}
if (!m->to_compile) {
m->to_compile = 1;
return LY_ERECOMPILE;
}
LY_CHECK_RET(lys_has_compiled_import_r(m));
}
return LY_SUCCESS;
}
LY_ERR
lys_implement(struct lys_module *mod, const char **features, struct lys_glob_unres *unres)
{
LY_ERR r;
struct lys_module *m;
assert(!mod->implemented);
m = ly_ctx_get_module_implemented(mod->ctx, mod->name);
if (m) {
assert(m != mod);
LOGERR(mod->ctx, LY_EDENIED, "Module \"%s@%s\" is already implemented in revision \"%s\".",
mod->name, mod->revision ? mod->revision : "<none>", m->revision ? m->revision : "<none>");
return LY_EDENIED;
}
r = lys_set_features(mod->parsed, features);
if (r && (r != LY_EEXIST)) {
return r;
}
mod->implemented = 1;
mod->to_compile = 1;
LY_CHECK_RET(ly_set_add(&unres->implementing, mod, 1, NULL));
LY_CHECK_RET(lys_precompile_augments_deviations(mod, unres));
LY_CHECK_RET(lys_has_compiled_import_r(mod));
return LY_SUCCESS;
}