#define _GNU_SOURCE
#include "schema_compile_node.h"
#include <assert.h>
#include <ctype.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "compat.h"
#include "dict.h"
#include "log.h"
#include "ly_common.h"
#include "plugins.h"
#include "plugins_internal.h"
#include "plugins_types.h"
#include "schema_compile.h"
#include "schema_compile_amend.h"
#include "schema_features.h"
#include "set.h"
#include "tree.h"
#include "tree_data.h"
#include "tree_edit.h"
#include "tree_schema.h"
#include "tree_schema_internal.h"
#include "xpath.h"
struct lys_type_item {
const struct lysp_tpdf *tpdf;
struct lysp_node *node;
};
static LY_ERR
lysc_unres_when_add(struct lysc_ctx *ctx, struct lysc_when *when, struct lysc_node *node)
{
LY_ERR rc = LY_SUCCESS;
struct lysc_unres_when *w = NULL;
if (ctx->compile_opts & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED)) {
goto cleanup;
}
w = calloc(1, sizeof *w);
LY_CHECK_ERR_GOTO(!w, LOGMEM(ctx->ctx); rc = LY_EMEM, cleanup);
w->node = node;
w->when = when;
LY_CHECK_ERR_GOTO(ly_set_add(&ctx->unres->whens, w, 1, NULL), LOGMEM(ctx->ctx); rc = LY_EMEM, cleanup);
w = NULL;
cleanup:
free(w);
return rc;
}
static LY_ERR
lysc_unres_must_add(struct lysc_ctx *ctx, struct lysc_node *node, struct lysp_node *pnode)
{
struct lysc_unres_must *m = NULL;
LY_ARRAY_COUNT_TYPE u;
struct lysc_must *musts;
struct lysp_restr *pmusts;
LY_ERR ret;
if (ctx->compile_opts & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED)) {
return LY_SUCCESS;
}
musts = lysc_node_musts(node);
pmusts = lysp_node_musts(pnode);
assert(LY_ARRAY_COUNT(musts) == LY_ARRAY_COUNT(pmusts));
if (!musts) {
return LY_SUCCESS;
}
m = calloc(1, sizeof *m);
LY_CHECK_ERR_GOTO(!m, ret = LY_EMEM, error);
m->node = node;
LY_ARRAY_CREATE_GOTO(ctx->ctx, m->local_mods, LY_ARRAY_COUNT(pmusts), ret, error);
LY_ARRAY_FOR(pmusts, u) {
m->local_mods[u] = pmusts[u].arg.mod;
LY_ARRAY_INCREMENT(m->local_mods);
}
m->ext = ctx->ext;
LY_CHECK_ERR_GOTO(ly_set_add(&ctx->unres->musts, m, 1, NULL), ret = LY_EMEM, error);
return LY_SUCCESS;
error:
if (m) {
LY_ARRAY_FREE(m->local_mods);
free(m);
}
LOGMEM(ctx->ctx);
return ret;
}
static LY_ERR
lysc_unres_leafref_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, const struct lysp_module *local_mod)
{
struct lysc_unres_leafref *l = NULL;
struct ly_set *leafrefs_set;
LY_ARRAY_COUNT_TYPE u;
int is_lref = 0;
if (ctx->compile_opts & LYS_COMPILE_GROUPING) {
return LY_SUCCESS;
}
leafrefs_set = ctx->compile_opts & LYS_COMPILE_DISABLED ? &ctx->unres->disabled_leafrefs : &ctx->unres->leafrefs;
if (leaf->type->basetype == LY_TYPE_LEAFREF) {
is_lref = 1;
} else if (leaf->type->basetype == LY_TYPE_UNION) {
LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) {
if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
is_lref = 1;
break;
}
}
}
if (is_lref) {
l = calloc(1, sizeof *l);
LY_CHECK_ERR_RET(!l, LOGMEM(ctx->ctx), LY_EMEM);
l->node = &leaf->node;
l->local_mod = local_mod;
l->ext = ctx->ext;
LY_CHECK_ERR_RET(ly_set_add(leafrefs_set, l, 1, NULL), free(l); LOGMEM(ctx->ctx), LY_EMEM);
}
return LY_SUCCESS;
}
static LY_ERR
lysc_unres_leaf_dflt_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, struct lysp_qname *dflt)
{
struct lysc_unres_dflt *r = NULL;
uint32_t i;
if (ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
return LY_SUCCESS;
}
for (i = 0; i < ctx->unres->dflts.count; ++i) {
if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->leaf == leaf) {
r = ctx->unres->dflts.objs[i];
lysp_qname_free(ctx->ctx, r->dflt);
free(r->dflt);
break;
}
}
if (!r) {
r = calloc(1, sizeof *r);
LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
r->leaf = leaf;
LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
}
r->dflt = malloc(sizeof *r->dflt);
LY_CHECK_GOTO(!r->dflt, error);
LY_CHECK_GOTO(lysp_qname_dup(ctx->ctx, dflt, r->dflt), error);
return LY_SUCCESS;
error:
free(r->dflt);
LOGMEM(ctx->ctx);
return LY_EMEM;
}
static LY_ERR
lysc_unres_llist_dflts_add(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, struct lysp_qname *dflts)
{
struct lysc_unres_dflt *r = NULL;
uint32_t i;
if (ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
return LY_SUCCESS;
}
for (i = 0; i < ctx->unres->dflts.count; ++i) {
if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->llist == llist) {
r = ctx->unres->dflts.objs[i];
lysp_qname_free(ctx->ctx, r->dflt);
free(r->dflt);
r->dflt = NULL;
FREE_ARRAY(ctx->ctx, r->dflts, lysp_qname_free);
r->dflts = NULL;
break;
}
}
if (!r) {
r = calloc(1, sizeof *r);
LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
r->llist = llist;
LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
}
DUP_ARRAY(ctx->ctx, dflts, r->dflts, lysp_qname_dup);
return LY_SUCCESS;
}
static LY_ERR
lysc_unres_bitenum_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf)
{
if (ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
return LY_SUCCESS;
}
LY_CHECK_RET(ly_set_add(&ctx->unres->disabled_bitenums, leaf, 1, NULL));
return LY_SUCCESS;
}
static struct lysc_pattern *
lysc_pattern_dup(struct lysc_pattern *orig)
{
++orig->refcount;
return orig;
}
static struct lysc_pattern **
lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
{
struct lysc_pattern **dup = NULL;
LY_ARRAY_COUNT_TYPE u;
assert(orig);
LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL);
LY_ARRAY_FOR(orig, u) {
dup[u] = lysc_pattern_dup(orig[u]);
LY_ARRAY_INCREMENT(dup);
}
return dup;
}
static struct lysc_range *
lysc_range_dup(struct lysc_ctx *ctx, const struct lysc_range *orig, struct ly_set *tpdf_chain, uint32_t tpdf_chain_last)
{
struct lysc_range *dup;
LY_ERR ret;
struct lys_type_item *tpdf_item;
uint32_t i;
assert(orig);
dup = calloc(1, sizeof *dup);
LY_CHECK_ERR_RET(!dup, LOGMEM(ctx->ctx), NULL);
if (orig->parts) {
LY_ARRAY_CREATE_GOTO(ctx->ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, error);
(*((LY_ARRAY_COUNT_TYPE *)(dup->parts) - 1)) = LY_ARRAY_COUNT(orig->parts);
memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
}
DUP_STRING_GOTO(ctx->ctx, orig->eapptag, dup->eapptag, ret, error);
DUP_STRING_GOTO(ctx->ctx, orig->emsg, dup->emsg, ret, error);
if (tpdf_chain->count > tpdf_chain_last) {
i = tpdf_chain->count;
do {
--i;
tpdf_item = tpdf_chain->objs[i];
if (!tpdf_item->tpdf->type.range) {
continue;
}
COMPILE_EXTS_GOTO(ctx, tpdf_item->tpdf->type.range->exts, dup->exts, dup, ret, error);
} while (i > tpdf_chain_last);
}
return dup;
error:
if (dup) {
lysc_range_free(ctx->ctx, dup);
free(dup);
}
return NULL;
}
static const char *
lys_status2str(uint16_t flags)
{
flags &= LYS_STATUS_MASK;
switch (flags) {
case 0:
case LYS_STATUS_CURR:
return "current";
case LYS_STATUS_DEPRC:
return "deprecated";
case LYS_STATUS_OBSLT:
return "obsolete";
default:
LOGINT(NULL);
return NULL;
}
}
static LY_ERR
lys_compile_status(struct lysc_ctx *ctx, uint16_t parsed_flags, uint16_t inherited_flags, uint16_t parent_flags,
const char *parent_name, const char *stmt_name, uint16_t *stmt_flags)
{
parsed_flags &= LYS_STATUS_MASK;
inherited_flags &= LYS_STATUS_MASK;
parent_flags &= LYS_STATUS_MASK;
if (parent_flags && parsed_flags && (parent_flags > parsed_flags)) {
LOGVAL(ctx->ctx, NULL, LYVE_SEMANTICS, "Status \"%s\" of \"%s\" is in conflict with \"%s\" status of parent \"%s\".",
lys_status2str(parsed_flags), stmt_name, lys_status2str(parent_flags), parent_name);
return LY_EVALID;
} else if (inherited_flags && parsed_flags && (inherited_flags > parsed_flags)) {
LOGVAL(ctx->ctx, NULL, LYVE_SEMANTICS, "Inherited schema-only status \"%s\" is in conflict with \"%s\" status of \"%s\".",
lys_status2str(inherited_flags), lys_status2str(parsed_flags), stmt_name);
return LY_EVALID;
} else if (parent_flags && inherited_flags && (parent_flags > inherited_flags)) {
LOGVAL(ctx->ctx, NULL, LYVE_SEMANTICS,
"Status \"%s\" of parent \"%s\" is in conflict with inherited schema-only status \"%s\".",
lys_status2str(parent_flags), parent_name, lys_status2str(inherited_flags));
return LY_EVALID;
}
(*stmt_flags) &= ~LYS_STATUS_MASK;
if (parsed_flags) {
(*stmt_flags) |= parsed_flags;
} else if (inherited_flags) {
(*stmt_flags) |= inherited_flags;
} else if (parent_flags) {
(*stmt_flags) |= parent_flags;
} else {
(*stmt_flags) |= LYS_STATUS_CURR;
}
return LY_SUCCESS;
}
static LY_ERR
lys_compile_when_(struct lysc_ctx *ctx, const struct lysp_when *when_p, uint16_t inherited_flags,
const struct lysc_node *parent, const struct lysc_node *ctx_node, struct lysc_when **when)
{
LY_ERR ret = LY_SUCCESS;
LY_VALUE_FORMAT format;
*when = calloc(1, sizeof **when);
LY_CHECK_ERR_RET(!(*when), LOGMEM(ctx->ctx), LY_EMEM);
(*when)->refcount = 1;
LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, NULL, when_p->cond, 0, 1, &(*when)->cond));
LY_CHECK_RET(lyplg_type_prefix_data_new(ctx->ctx, when_p->cond, strlen(when_p->cond),
LY_VALUE_SCHEMA, ctx->pmod, &format, (void **)&(*when)->prefixes));
(*when)->context = (struct lysc_node *)ctx_node;
DUP_STRING_GOTO(ctx->ctx, when_p->dsc, (*when)->dsc, ret, done);
DUP_STRING_GOTO(ctx->ctx, when_p->ref, (*when)->ref, ret, done);
COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), ret, done);
LY_CHECK_RET(lys_compile_status(ctx, 0, inherited_flags, parent ? parent->flags : 0, parent ? parent->name : NULL,
"when", &(*when)->flags));
done:
return ret;
}
LY_ERR
lys_compile_when(struct lysc_ctx *ctx, const struct lysp_when *when_p, uint16_t inherited_flags, const struct lysc_node *parent,
const struct lysc_node *ctx_node, struct lysc_node *node, struct lysc_when **when_c)
{
LY_ERR rc = LY_SUCCESS;
struct lysc_when **new_when, ***node_when, *ptr;
assert(when_p && (node || when_c));
if (node) {
node_when = lysc_node_when_p(node);
LY_ARRAY_NEW_GOTO(ctx->ctx, *node_when, new_when, rc, cleanup);
} else {
new_when = &ptr;
*new_when = calloc(1, sizeof **new_when);
LY_CHECK_ERR_GOTO(!*new_when, LOGMEM(ctx->ctx); rc = LY_EMEM, cleanup);
}
if (!when_c || !(*when_c)) {
LY_CHECK_GOTO(rc = lys_compile_when_(ctx, when_p, inherited_flags, parent, ctx_node, new_when), cleanup);
if (when_c) {
*when_c = *new_when;
}
} else {
++(*when_c)->refcount;
*new_when = *when_c;
}
if (node) {
LY_CHECK_GOTO(rc = lysc_unres_when_add(ctx, *new_when, node), cleanup);
}
cleanup:
return rc;
}
LY_ERR
lys_compile_must(struct lysc_ctx *ctx, const struct lysp_restr *must_p, struct lysc_must *must)
{
LY_ERR ret = LY_SUCCESS;
LY_VALUE_FORMAT format;
LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, NULL, must_p->arg.str, 0, 1, &must->cond));
LY_CHECK_RET(lyplg_type_prefix_data_new(ctx->ctx, must_p->arg.str, strlen(must_p->arg.str),
LY_VALUE_SCHEMA, must_p->arg.mod, &format, (void **)&must->prefixes));
DUP_STRING_GOTO(ctx->ctx, must_p->eapptag, must->eapptag, ret, done);
DUP_STRING_GOTO(ctx->ctx, must_p->emsg, must->emsg, ret, done);
DUP_STRING_GOTO(ctx->ctx, must_p->dsc, must->dsc, ret, done);
DUP_STRING_GOTO(ctx->ctx, must_p->ref, must->ref, ret, done);
COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, ret, done);
done:
return ret;
}
static LY_ERR
range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value,
size_t *len, char **valcopy)
{
size_t fraction = 0, size;
*len = 0;
assert(value);
if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
return LY_EVALID;
}
if ((value[*len] == '-') || (value[*len] == '+')) {
++(*len);
}
while (isdigit(value[*len])) {
++(*len);
}
if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
if (basetype == LY_TYPE_DEC64) {
goto decimal;
} else {
*valcopy = strndup(value, *len);
return LY_SUCCESS;
}
}
fraction = *len;
++(*len);
while (isdigit(value[*len])) {
++(*len);
}
if (basetype == LY_TYPE_DEC64) {
decimal:
assert(frdigits);
if (fraction && (*len - 1 - fraction > frdigits)) {
LOGVAL(ctx->ctx, NULL, LYVE_SYNTAX_YANG,
"Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
(int)(*len), value, frdigits);
return LY_EINVAL;
}
if (fraction) {
size = (*len) + (frdigits - ((*len) - 1 - fraction));
} else {
size = (*len) + frdigits + 1;
}
*valcopy = malloc(size * sizeof **valcopy);
LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
(*valcopy)[size - 1] = '\0';
if (fraction) {
memcpy(&(*valcopy)[0], &value[0], fraction);
memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
} else {
memcpy(&(*valcopy)[0], &value[0], *len);
memset(&(*valcopy)[*len], '0', frdigits);
}
}
return LY_SUCCESS;
}
static LY_ERR
range_part_check_ascendancy(ly_bool unsigned_value, ly_bool max, int64_t value, int64_t prev_value)
{
if (unsigned_value) {
if ((max && ((uint64_t)prev_value > (uint64_t)value)) || (!max && ((uint64_t)prev_value >= (uint64_t)value))) {
return LY_EEXIST;
}
} else {
if ((max && (prev_value > value)) || (!max && (prev_value >= value))) {
return LY_EEXIST;
}
}
return LY_SUCCESS;
}
static LY_ERR
range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, ly_bool max, int64_t prev, LY_DATA_TYPE basetype,
ly_bool first, ly_bool length_restr, uint8_t frdigits, struct lysc_range *base_range, const char **value)
{
LY_ERR ret = LY_SUCCESS;
char *valcopy = NULL;
size_t len = 0;
if (value) {
ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
LY_CHECK_GOTO(ret, finalize);
}
if (!valcopy && base_range) {
if (max) {
part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64;
} else {
part->min_64 = base_range->parts[0].min_64;
}
if (!first) {
ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
}
goto finalize;
}
switch (basetype) {
case LY_TYPE_INT8:
if (valcopy) {
ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-128), INT64_C(127), LY_BASE_DEC,
max ? &part->max_64 : &part->min_64);
} else if (max) {
part->max_64 = INT64_C(127);
} else {
part->min_64 = INT64_C(-128);
}
if (!ret && !first) {
ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
}
break;
case LY_TYPE_INT16:
if (valcopy) {
ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-32768), INT64_C(32767), LY_BASE_DEC,
max ? &part->max_64 : &part->min_64);
} else if (max) {
part->max_64 = INT64_C(32767);
} else {
part->min_64 = INT64_C(-32768);
}
if (!ret && !first) {
ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
}
break;
case LY_TYPE_INT32:
if (valcopy) {
ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-2147483648), INT64_C(2147483647), LY_BASE_DEC,
max ? &part->max_64 : &part->min_64);
} else if (max) {
part->max_64 = INT64_C(2147483647);
} else {
part->min_64 = INT64_C(-2147483648);
}
if (!ret && !first) {
ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
}
break;
case LY_TYPE_INT64:
case LY_TYPE_DEC64:
if (valcopy) {
ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807),
LY_BASE_DEC, max ? &part->max_64 : &part->min_64);
} else if (max) {
part->max_64 = INT64_C(9223372036854775807);
} else {
part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
}
if (!ret && !first) {
ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
}
break;
case LY_TYPE_UINT8:
if (valcopy) {
ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(255), LY_BASE_DEC, max ? &part->max_u64 : &part->min_u64);
} else if (max) {
part->max_u64 = UINT64_C(255);
} else {
part->min_u64 = UINT64_C(0);
}
if (!ret && !first) {
ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
}
break;
case LY_TYPE_UINT16:
if (valcopy) {
ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(65535), LY_BASE_DEC, max ? &part->max_u64 : &part->min_u64);
} else if (max) {
part->max_u64 = UINT64_C(65535);
} else {
part->min_u64 = UINT64_C(0);
}
if (!ret && !first) {
ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
}
break;
case LY_TYPE_UINT32:
if (valcopy) {
ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(4294967295), LY_BASE_DEC,
max ? &part->max_u64 : &part->min_u64);
} else if (max) {
part->max_u64 = UINT64_C(4294967295);
} else {
part->min_u64 = UINT64_C(0);
}
if (!ret && !first) {
ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
}
break;
case LY_TYPE_UINT64:
case LY_TYPE_STRING:
case LY_TYPE_BINARY:
if (valcopy) {
ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(18446744073709551615), LY_BASE_DEC,
max ? &part->max_u64 : &part->min_u64);
} else if (max) {
part->max_u64 = UINT64_C(18446744073709551615);
} else {
part->min_u64 = UINT64_C(0);
}
if (!ret && !first) {
ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
}
break;
default:
LOGINT(ctx->ctx);
ret = LY_EINT;
}
finalize:
if (ret == LY_EDENIED) {
LOGVAL(ctx->ctx, NULL, LYVE_SYNTAX_YANG,
"Invalid %s restriction - value \"%s\" does not fit the type limitations.",
length_restr ? "length" : "range", valcopy ? valcopy : *value);
} else if (ret == LY_EVALID) {
LOGVAL(ctx->ctx, NULL, LYVE_SYNTAX_YANG,
"Invalid %s restriction - invalid value \"%s\".",
length_restr ? "length" : "range", valcopy ? valcopy : *value);
} else if (ret == LY_EEXIST) {
LOGVAL(ctx->ctx, NULL, LYVE_SYNTAX_YANG,
"Invalid %s restriction - values are not in ascending order (%s).",
length_restr ? "length" : "range",
(valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
} else if (!ret && value) {
*value = *value + len;
}
free(valcopy);
return ret;
}
LY_ERR
lys_compile_type_range(struct lysc_ctx *ctx, const struct lysp_restr *range_p, LY_DATA_TYPE basetype, ly_bool length_restr,
uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range)
{
LY_ERR ret = LY_SUCCESS;
const char *expr;
struct lysc_range_part *parts = NULL, *part;
ly_bool range_expected = 0, uns;
LY_ARRAY_COUNT_TYPE parts_done = 0, u, v;
assert(range);
assert(range_p);
expr = range_p->arg.str;
while (1) {
if (isspace(*expr)) {
++expr;
} else if (*expr == '\0') {
if (range_expected) {
LOGVAL(ctx->ctx, NULL, LYVE_SYNTAX_YANG,
"Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
length_restr ? "length" : "range", range_p->arg.str);
ret = LY_EVALID;
goto cleanup;
} else if (!parts || (parts_done == LY_ARRAY_COUNT(parts))) {
LOGVAL(ctx->ctx, NULL, LYVE_SYNTAX_YANG,
"Invalid %s restriction - unexpected end of the expression (%s).",
length_restr ? "length" : "range", range_p->arg.str);
ret = LY_EVALID;
goto cleanup;
}
parts_done++;
break;
} else if (!strncmp(expr, "min", ly_strlen_const("min"))) {
if (parts) {
LOGVAL(ctx->ctx, NULL, LYVE_SYNTAX_YANG,
"Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
(int)(expr - range_p->arg.str), range_p->arg.str);
ret = LY_EVALID;
goto cleanup;
}
expr += ly_strlen_const("min");
LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 0, 0, basetype, 1, length_restr, frdigits, base_range, NULL), cleanup);
part->max_64 = part->min_64;
} else if (*expr == '|') {
if (!parts || range_expected) {
LOGVAL(ctx->ctx, NULL, LYVE_SYNTAX_YANG,
"Invalid %s restriction - unexpected beginning of the expression (%s).",
length_restr ? "length" : "range", expr);
ret = LY_EVALID;
goto cleanup;
}
expr++;
parts_done++;
} else if (!strncmp(expr, "..", 2)) {
expr += 2;
while (isspace(*expr)) {
expr++;
}
if (!parts || (LY_ARRAY_COUNT(parts) == parts_done)) {
LOGVAL(ctx->ctx, NULL, LYVE_SYNTAX_YANG,
"Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
ret = LY_EVALID;
goto cleanup;
}
range_expected = 1;
} else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
if (range_expected) {
part = &parts[LY_ARRAY_COUNT(parts) - 1];
LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, NULL, &expr), cleanup);
range_expected = 0;
} else {
LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
part->max_64 = part->min_64;
}
} else if (!strncmp(expr, "max", ly_strlen_const("max"))) {
expr += ly_strlen_const("max");
while (isspace(*expr)) {
expr++;
}
if (*expr != '\0') {
LOGVAL(ctx->ctx, NULL, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
length_restr ? "length" : "range", expr);
ret = LY_EVALID;
goto cleanup;
}
if (range_expected) {
part = &parts[LY_ARRAY_COUNT(parts) - 1];
ret = range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, base_range, NULL);
LY_CHECK_GOTO(ret, cleanup);
range_expected = 0;
} else {
LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
part->min_64 = part->max_64;
}
} else {
LOGVAL(ctx->ctx, NULL, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
length_restr ? "length" : "range", expr);
ret = LY_EVALID;
goto cleanup;
}
}
if (base_range) {
switch (basetype) {
case LY_TYPE_BINARY:
case LY_TYPE_UINT8:
case LY_TYPE_UINT16:
case LY_TYPE_UINT32:
case LY_TYPE_UINT64:
case LY_TYPE_STRING:
uns = 1;
break;
case LY_TYPE_DEC64:
case LY_TYPE_INT8:
case LY_TYPE_INT16:
case LY_TYPE_INT32:
case LY_TYPE_INT64:
uns = 0;
break;
default:
LOGINT(ctx->ctx);
ret = LY_EINT;
goto cleanup;
}
for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
if ((uns && (parts[u].min_u64 < base_range->parts[v].min_u64)) ||
(!uns && (parts[u].min_64 < base_range->parts[v].min_64))) {
goto baseerror;
}
if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
if (base_range->parts[v].min_64 == parts[u].min_64) {
if (parts[u].min_64 != parts[u].max_64) {
goto baseerror;
} else {
++v;
continue;
}
} else {
++v;
--u;
continue;
}
} else {
if (parts[u].min_64 == parts[u].max_64) {
if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
++v;
--u;
}
continue;
} else {
if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) ||
(!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
if ((uns && (parts[u].min_u64 > base_range->parts[v].max_u64)) ||
(!uns && (parts[u].min_64 > base_range->parts[v].max_64))) {
--u;
++v;
continue;
}
goto baseerror;
} else {
continue;
}
}
}
}
if (u != parts_done) {
baseerror:
LOGVAL(ctx->ctx, NULL, LYVE_SYNTAX_YANG,
"Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
length_restr ? "length" : "range", range_p->arg.str);
ret = LY_EVALID;
goto cleanup;
}
}
if (!(*range)) {
*range = calloc(1, sizeof **range);
LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
}
if (range_p->eapptag) {
lysdict_remove(ctx->ctx, (*range)->eapptag);
LY_CHECK_GOTO(ret = lysdict_insert(ctx->ctx, range_p->eapptag, 0, &(*range)->eapptag), cleanup);
}
if (range_p->emsg) {
lysdict_remove(ctx->ctx, (*range)->emsg);
LY_CHECK_GOTO(ret = lysdict_insert(ctx->ctx, range_p->emsg, 0, &(*range)->emsg), cleanup);
}
if (range_p->dsc) {
lysdict_remove(ctx->ctx, (*range)->dsc);
LY_CHECK_GOTO(ret = lysdict_insert(ctx->ctx, range_p->dsc, 0, &(*range)->dsc), cleanup);
}
if (range_p->ref) {
lysdict_remove(ctx->ctx, (*range)->ref);
LY_CHECK_GOTO(ret = lysdict_insert(ctx->ctx, range_p->ref, 0, &(*range)->ref), cleanup);
}
(*range)->parts = parts;
parts = NULL;
cleanup:
LY_ARRAY_FREE(parts);
return ret;
}
static ly_bool
lys_compile_type_patterns_has_oc_posix_ext(const struct lysp_module *pmod)
{
const struct lysp_ext_instance *ext;
LY_ARRAY_COUNT_TYPE u;
const struct lys_module *mod;
const char *tmp, *name, *prefix;
uint32_t pref_len, name_len;
LY_ARRAY_FOR(pmod->exts, u) {
ext = &pmod->exts[u];
tmp = ext->name;
ly_parse_nodeid(&tmp, &prefix, &pref_len, &name, &name_len);
mod = ly_resolve_prefix(pmod->mod->ctx, prefix, pref_len, ext->format, ext->prefix_data);
if ((name_len != 12) || strncmp(name, "regexp-posix", name_len)) {
continue;
}
if (!mod || strcmp(mod->name, "openconfig-extensions")) {
continue;
}
return 1;
}
return 0;
}
LY_ERR
lys_compile_type_patterns(struct lysc_ctx *ctx, const struct lysp_restr *patterns_p, struct lysc_pattern **base_patterns,
struct lysc_pattern ***patterns)
{
LY_ERR rc = LY_SUCCESS;
struct lysc_pattern **pattern;
LY_ARRAY_COUNT_TYPE u;
uint32_t format = 0;
if (ctx->pmod) {
format = lys_compile_type_patterns_has_oc_posix_ext(ctx->pmod);
}
if (base_patterns) {
*patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
}
LY_ARRAY_FOR(patterns_p, u) {
LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
*pattern = calloc(1, sizeof **pattern);
(*pattern)->refcount = 1;
(*pattern)->format = format;
if (patterns_p[u].arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
(*pattern)->inverted = 1;
}
LY_CHECK_GOTO(rc = lysdict_insert(ctx->ctx, &patterns_p[u].arg.str[1], 0, &(*pattern)->expr), cleanup);
DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, rc, cleanup);
DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, rc, cleanup);
DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, rc, cleanup);
DUP_STRING_GOTO(ctx->ctx, patterns_p[u].ref, (*pattern)->ref, rc, cleanup);
COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), rc, cleanup);
LY_CHECK_GOTO(rc = ly_ctx_shared_data_pattern_get(ctx->ctx, (*pattern)->expr, (*pattern)->format, NULL), cleanup);
}
cleanup:
return rc;
}
static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
0 ,
LYS_SET_LENGTH ,
LYS_SET_RANGE ,
LYS_SET_RANGE ,
LYS_SET_RANGE ,
LYS_SET_RANGE ,
LYS_SET_LENGTH | LYS_SET_PATTERN ,
LYS_SET_BIT ,
0 ,
LYS_SET_FRDIGITS | LYS_SET_RANGE ,
0 ,
LYS_SET_ENUM ,
LYS_SET_BASE ,
LYS_SET_REQINST ,
LYS_SET_REQINST | LYS_SET_PATH ,
LYS_SET_TYPE ,
LYS_SET_RANGE ,
LYS_SET_RANGE ,
LYS_SET_RANGE ,
LYS_SET_RANGE
};
const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = {
LY_TYPE_UNKNOWN_STR,
LY_TYPE_BINARY_STR,
LY_TYPE_UINT8_STR,
LY_TYPE_UINT16_STR,
LY_TYPE_UINT32_STR,
LY_TYPE_UINT64_STR,
LY_TYPE_STRING_STR,
LY_TYPE_BITS_STR,
LY_TYPE_BOOL_STR,
LY_TYPE_DEC64_STR,
LY_TYPE_EMPTY_STR,
LY_TYPE_ENUM_STR,
LY_TYPE_IDENT_STR,
LY_TYPE_INST_STR,
LY_TYPE_LEAFREF_STR,
LY_TYPE_UNION_STR,
LY_TYPE_INT8_STR,
LY_TYPE_INT16_STR,
LY_TYPE_INT32_STR,
LY_TYPE_INT64_STR
};
LY_ERR
lys_compile_type_enums(struct lysc_ctx *ctx, const struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **bitenums)
{
LY_ERR ret = LY_SUCCESS;
LY_ARRAY_COUNT_TYPE u, v, match = 0;
int32_t highest_value = INT32_MIN, cur_val = INT32_MIN;
uint32_t highest_position = 0, cur_pos = 0;
struct lysc_type_bitenum_item *e, storage;
ly_bool enabled;
if (base_enums && (ctx->pmod->version < LYS_VERSION_1_1)) {
LOGVAL(ctx->ctx, NULL, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
return LY_EVALID;
}
LY_ARRAY_FOR(enums_p, u) {
if (base_enums) {
LY_ARRAY_FOR(base_enums, v) {
if (!strcmp(enums_p[u].name, base_enums[v].name)) {
break;
}
}
if (v == LY_ARRAY_COUNT(base_enums)) {
LOGVAL(ctx->ctx, NULL, LYVE_SYNTAX_YANG, "Invalid %s - derived type adds new item \"%s\".",
basetype == LY_TYPE_ENUM ? "enumeration" : "bits", enums_p[u].name);
return LY_EVALID;
}
match = v;
}
if (basetype == LY_TYPE_ENUM) {
if (enums_p[u].flags & LYS_SET_VALUE) {
cur_val = (int32_t)enums_p[u].value;
LY_ARRAY_FOR(*bitenums, v) {
if (cur_val == (*bitenums)[v].value) {
LOGVAL(ctx->ctx, NULL, LYVE_SYNTAX_YANG,
"Invalid enumeration - value %" PRId32 " collide in items \"%s\" and \"%s\".",
cur_val, enums_p[u].name, (*bitenums)[v].name);
return LY_EVALID;
}
}
} else if (base_enums) {
cur_val = base_enums[match].value;
} else {
if (u == 0) {
cur_val = 0;
} else if (highest_value == INT32_MAX) {
LOGVAL(ctx->ctx, NULL, LYVE_SYNTAX_YANG,
"Invalid enumeration - it is not possible to auto-assign enum value for "
"\"%s\" since the highest value is already 2147483647.", enums_p[u].name);
return LY_EVALID;
} else {
cur_val = highest_value + 1;
}
}
if (highest_value < cur_val) {
highest_value = cur_val;
}
} else {
if (enums_p[u].flags & LYS_SET_VALUE) {
cur_pos = (uint32_t)enums_p[u].value;
LY_ARRAY_FOR(*bitenums, v) {
if (cur_pos == (*bitenums)[v].position) {
LOGVAL(ctx->ctx, NULL, LYVE_SYNTAX_YANG,
"Invalid bits - position %" PRIu32 " collide in items \"%s\" and \"%s\".",
cur_pos, enums_p[u].name, (*bitenums)[v].name);
return LY_EVALID;
}
}
} else if (base_enums) {
cur_pos = base_enums[match].position;
} else {
if (u == 0) {
cur_pos = 0;
} else if (highest_position == UINT32_MAX) {
LOGVAL(ctx->ctx, NULL, LYVE_SYNTAX_YANG,
"Invalid bits - it is not possible to auto-assign bit position for "
"\"%s\" since the highest value is already 4294967295.", enums_p[u].name);
return LY_EVALID;
} else {
cur_pos = highest_position + 1;
}
}
if (highest_position < cur_pos) {
highest_position = cur_pos;
}
}
if (base_enums) {
if (basetype == LY_TYPE_ENUM) {
if (cur_val != base_enums[match].value) {
LOGVAL(ctx->ctx, NULL, LYVE_SYNTAX_YANG,
"Invalid enumeration - value of the item \"%s\" has changed from %" PRId32 " to %" PRId32
" in the derived type.", enums_p[u].name, base_enums[match].value, cur_val);
return LY_EVALID;
}
} else {
if (cur_pos != base_enums[match].position) {
LOGVAL(ctx->ctx, NULL, LYVE_SYNTAX_YANG,
"Invalid bits - position of the item \"%s\" has changed from %" PRIu32 " to %" PRIu32
" in the derived type.", enums_p[u].name, base_enums[match].position, cur_pos);
return LY_EVALID;
}
}
}
LY_ARRAY_NEW_RET(ctx->ctx, *bitenums, e, LY_EMEM);
DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done);
DUP_STRING_GOTO(ctx->ctx, enums_p[u].dsc, e->dsc, ret, done);
DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done);
e->flags = (enums_p[u].flags & LYS_FLAGS_COMPILED_MASK) & ~LYS_STATUS_MASK;
LY_CHECK_RET(lys_compile_status(ctx, enums_p[u].flags, 0, 0, NULL, (basetype == LY_TYPE_ENUM) ? "enum" : "bit",
&e->flags));
e->flags |= (basetype == LY_TYPE_ENUM) ? LYS_IS_ENUM : 0;
if (basetype == LY_TYPE_ENUM) {
e->value = cur_val;
} else {
e->position = cur_pos;
}
COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, ret, done);
LY_CHECK_RET(lys_eval_iffeatures(ctx->ctx, enums_p[u].iffeatures, &enabled));
if (!enabled) {
e->flags |= LYS_DISABLED;
}
if (basetype == LY_TYPE_BITS) {
for (v = u; v && (*bitenums)[v - 1].position > e->position; --v) {}
if (v != u) {
memcpy(&storage, e, sizeof *e);
memmove(&(*bitenums)[v + 1], &(*bitenums)[v], (u - v) * sizeof **bitenums);
memcpy(&(*bitenums)[v], &storage, sizeof storage);
}
}
}
LY_ARRAY_FOR(*bitenums, u) {
LY_ARRAY_FOR((*bitenums)[u].exts, v) {
(*bitenums)[u].exts[v].parent = &(*bitenums)[u];
}
}
done:
return ret;
}
static LY_ERR
lys_compile_type_union(struct lysc_ctx *ctx, struct lysp_type *ptypes, struct lysp_node *context_pnode, uint16_t context_flags,
const char *context_name, struct lysc_type ***utypes_p)
{
LY_ERR ret = LY_SUCCESS;
struct lysc_type **utypes = *utypes_p;
struct lysc_type_union *un_aux = NULL;
LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes, LY_ARRAY_COUNT(ptypes), ret, error);
for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(ptypes); ++u) {
ret = lys_compile_type(ctx, context_pnode, context_flags, context_name, &ptypes[u], &utypes[u + additional],
NULL, NULL);
LY_CHECK_GOTO(ret, error);
LY_ATOMIC_INC_BARRIER(utypes[u + additional]->refcount);
if (utypes[u + additional]->basetype == LY_TYPE_UNION) {
un_aux = (struct lysc_type_union *)utypes[u + additional];
LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes,
LY_ARRAY_COUNT(ptypes) + additional + LY_ARRAY_COUNT(un_aux->types) - LY_ARRAY_COUNT(utypes), ret, error);
for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
utypes[u + additional] = un_aux->types[v];
LY_ATOMIC_INC_BARRIER(un_aux->types[v]->refcount);
++additional;
LY_ARRAY_INCREMENT(utypes);
}
assert(additional);
--additional;
lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
un_aux = NULL;
} else {
LY_ARRAY_INCREMENT(utypes);
}
}
*utypes_p = utypes;
return LY_SUCCESS;
error:
if (un_aux) {
lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
}
*utypes_p = utypes;
return ret;
}
static LY_ERR
lys_new_type(const struct ly_ctx *ctx, LY_DATA_TYPE basetype, const char *tpdf_name, struct lysc_type **type)
{
LY_ERR rc = LY_SUCCESS;
struct lysc_type *t = NULL;
*type = NULL;
switch (basetype) {
case LY_TYPE_BINARY:
t = calloc(1, sizeof(struct lysc_type_bin));
break;
case LY_TYPE_BITS:
t = calloc(1, sizeof(struct lysc_type_bits));
break;
case LY_TYPE_DEC64:
t = calloc(1, sizeof(struct lysc_type_dec));
break;
case LY_TYPE_STRING:
t = calloc(1, sizeof(struct lysc_type_str));
break;
case LY_TYPE_ENUM:
t = calloc(1, sizeof(struct lysc_type_enum));
break;
case LY_TYPE_INT8:
case LY_TYPE_UINT8:
case LY_TYPE_INT16:
case LY_TYPE_UINT16:
case LY_TYPE_INT32:
case LY_TYPE_UINT32:
case LY_TYPE_INT64:
case LY_TYPE_UINT64:
t = calloc(1, sizeof(struct lysc_type_num));
break;
case LY_TYPE_IDENT:
t = calloc(1, sizeof(struct lysc_type_identityref));
break;
case LY_TYPE_LEAFREF:
t = calloc(1, sizeof(struct lysc_type_leafref));
break;
case LY_TYPE_INST:
t = calloc(1, sizeof(struct lysc_type_instanceid));
break;
case LY_TYPE_UNION:
t = calloc(1, sizeof(struct lysc_type_union));
break;
case LY_TYPE_BOOL:
case LY_TYPE_EMPTY:
t = calloc(1, sizeof(struct lysc_type));
break;
case LY_TYPE_UNKNOWN:
break;
}
LY_CHECK_ERR_GOTO(!t, LOGMEM(ctx); rc = LY_EMEM, cleanup);
if (tpdf_name) {
rc = lysdict_insert(ctx, tpdf_name, 0, &t->name);
LY_CHECK_GOTO(rc, cleanup);
}
cleanup:
if (rc) {
free(t);
} else {
*type = t;
}
return rc;
}
static LY_ERR
lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, const char *context_name,
const struct lysp_type *type_p, LY_DATA_TYPE basetype, const char *tpdfname, const struct lysc_type *base,
uintptr_t plugin_ref, struct ly_set *tpdf_chain, uint32_t tpdf_chain_last, struct lysc_type **type)
{
LY_ERR rc = LY_SUCCESS;
struct lysc_type_bin *bin;
struct lysc_type_num *num;
struct lysc_type_str *str;
struct lysc_type_bits *bits;
struct lysc_type_enum *enumeration;
struct lysc_type_dec *dec;
struct lysc_type_identityref *idref;
struct lysc_type_leafref *lref;
struct lysc_type_union *un;
struct lys_type_item *tpdf_item;
const struct lysp_type *base_type_p;
struct lysc_prefix *prefixes;
uint32_t i;
rc = lys_new_type(ctx->ctx, basetype, tpdfname, type);
LY_CHECK_GOTO(rc, cleanup);
(*type)->basetype = basetype;
(*type)->plugin_ref = plugin_ref;
switch (basetype) {
case LY_TYPE_BINARY:
bin = (struct lysc_type_bin *)*type;
if (type_p->length) {
LY_CHECK_GOTO(rc = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length), cleanup);
if (!tpdfname) {
COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, rc, cleanup);
}
}
break;
case LY_TYPE_BITS:
bits = (struct lysc_type_bits *)*type;
if (type_p->bits) {
LY_CHECK_GOTO(rc = lys_compile_type_enums(ctx, type_p->bits, basetype,
base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL,
(struct lysc_type_bitenum_item **)&bits->bits), cleanup);
} else if (base) {
assert(tpdf_chain->count > tpdf_chain_last);
base_type_p = NULL;
i = tpdf_chain->count;
do {
--i;
tpdf_item = tpdf_chain->objs[i];
if (tpdf_item->tpdf->type.bits) {
base_type_p = &tpdf_item->tpdf->type;
break;
}
} while (i > tpdf_chain_last);
assert(base_type_p);
LY_CHECK_GOTO(rc = lys_compile_type_enums(ctx, base_type_p->bits, basetype, NULL,
(struct lysc_type_bitenum_item **)&bits->bits), cleanup);
} else {
if (tpdfname) {
LOGVAL(ctx->ctx, NULL, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
} else {
LOGVAL(ctx->ctx, NULL, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
}
rc = LY_EVALID;
goto cleanup;
}
break;
case LY_TYPE_DEC64:
dec = (struct lysc_type_dec *)*type;
if (!base) {
if (!type_p->fraction_digits) {
if (tpdfname) {
LOGVAL(ctx->ctx, NULL, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
} else {
LOGVAL(ctx->ctx, NULL, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
}
rc = LY_EVALID;
goto cleanup;
}
dec->fraction_digits = type_p->fraction_digits;
} else {
if (type_p->fraction_digits) {
if (tpdfname) {
LOGVAL(ctx->ctx, NULL, LYVE_SYNTAX_YANG,
"Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
tpdfname);
} else {
LOGVAL(ctx->ctx, NULL, LYVE_SYNTAX_YANG,
"Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
}
rc = LY_EVALID;
goto cleanup;
}
dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits;
}
if (type_p->range) {
LY_CHECK_GOTO(rc = lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range), cleanup);
if (!tpdfname) {
COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, rc, cleanup);
}
}
break;
case LY_TYPE_STRING:
str = (struct lysc_type_str *)*type;
if (type_p->length) {
LY_CHECK_GOTO(rc = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
base ? ((struct lysc_type_str *)base)->length : NULL, &str->length), cleanup);
if (!tpdfname) {
COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, rc, cleanup);
}
} else if (base && ((struct lysc_type_str *)base)->length) {
str->length = lysc_range_dup(ctx, ((struct lysc_type_str *)base)->length, tpdf_chain, tpdf_chain_last);
}
if (type_p->patterns) {
LY_CHECK_GOTO(rc = lys_compile_type_patterns(ctx, type_p->patterns,
base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns), cleanup);
} else if (base && ((struct lysc_type_str *)base)->patterns) {
str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns);
}
break;
case LY_TYPE_ENUM:
enumeration = (struct lysc_type_enum *)*type;
if (type_p->enums) {
LY_CHECK_GOTO(rc = lys_compile_type_enums(ctx, type_p->enums, basetype,
base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums), cleanup);
} else if (base) {
assert(tpdf_chain->count > tpdf_chain_last);
base_type_p = NULL;
i = tpdf_chain->count;
do {
--i;
tpdf_item = tpdf_chain->objs[i];
if (tpdf_item->tpdf->type.enums) {
base_type_p = &tpdf_item->tpdf->type;
break;
}
} while (i > tpdf_chain_last);
assert(base_type_p);
LY_CHECK_GOTO(rc = lys_compile_type_enums(ctx, base_type_p->enums, basetype, NULL, &enumeration->enums), cleanup);
} else {
if (tpdfname) {
LOGVAL(ctx->ctx, NULL, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
} else {
LOGVAL(ctx->ctx, NULL, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
}
rc = LY_EVALID;
goto cleanup;
}
break;
case LY_TYPE_INT8:
case LY_TYPE_UINT8:
case LY_TYPE_INT16:
case LY_TYPE_UINT16:
case LY_TYPE_INT32:
case LY_TYPE_UINT32:
case LY_TYPE_INT64:
case LY_TYPE_UINT64:
num = (struct lysc_type_num *)*type;
if (type_p->range) {
LY_CHECK_GOTO(rc = lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
base ? ((struct lysc_type_num *)base)->range : NULL, &num->range), cleanup);
if (!tpdfname) {
COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, rc, cleanup);
}
}
break;
case LY_TYPE_IDENT:
idref = (struct lysc_type_identityref *)*type;
if (type_p->bases) {
if (base) {
if (tpdfname) {
LOGVAL(ctx->ctx, NULL, LYVE_SYNTAX_YANG,
"Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
tpdfname);
} else {
LOGVAL(ctx->ctx, NULL, LYVE_SYNTAX_YANG,
"Invalid base substatement for the type not directly derived from identityref built-in type.");
}
rc = LY_EVALID;
goto cleanup;
}
LY_CHECK_GOTO(rc = lys_compile_identity_bases(ctx, type_p->pmod, type_p->bases, NULL, &idref->bases), cleanup);
} else if (base) {
const struct lysc_type_identityref *idref_base = (struct lysc_type_identityref *)base;
LY_ARRAY_COUNT_TYPE u;
LY_ARRAY_CREATE_GOTO(ctx->ctx, idref->bases, LY_ARRAY_COUNT(idref_base->bases), rc, cleanup);
LY_ARRAY_FOR(idref_base->bases, u) {
idref->bases[u] = idref_base->bases[u];
LY_ARRAY_INCREMENT(idref->bases);
}
} else {
if (tpdfname) {
LOGVAL(ctx->ctx, NULL, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
} else {
LOGVAL(ctx->ctx, NULL, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
}
rc = LY_EVALID;
goto cleanup;
}
break;
case LY_TYPE_LEAFREF:
lref = (struct lysc_type_leafref *)*type;
if (type_p->flags & LYS_SET_REQINST) {
if (type_p->pmod->version < LYS_VERSION_1_1) {
if (tpdfname) {
LOGVAL(ctx->ctx, NULL, LYVE_SEMANTICS,
"Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
} else {
LOGVAL(ctx->ctx, NULL, LYVE_SEMANTICS,
"Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
}
rc = LY_EVALID;
goto cleanup;
}
lref->require_instance = type_p->require_instance;
} else if (base) {
lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
} else {
lref->require_instance = 1;
}
if (type_p->path) {
LY_VALUE_FORMAT format;
LY_CHECK_GOTO(rc = lyxp_expr_dup(ctx->ctx, type_p->path, 0, 0, &lref->path), cleanup);
LY_CHECK_GOTO(lyplg_type_prefix_data_new(ctx->ctx, type_p->path->expr, strlen(type_p->path->expr),
LY_VALUE_SCHEMA, type_p->pmod, &format, (void **)&lref->prefixes), cleanup);
assert(format == LY_VALUE_SCHEMA_RESOLVED);
prefixes = lref->prefixes;
assert(!prefixes[0].prefix);
prefixes[0].mod = ctx->cur_mod;
} else if (base) {
LY_CHECK_GOTO(rc = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path, 0, 0, &lref->path), cleanup);
LY_CHECK_GOTO(rc = lyplg_type_prefix_data_dup(ctx->ctx, LY_VALUE_SCHEMA_RESOLVED,
((struct lysc_type_leafref *)base)->prefixes, (void **)&lref->prefixes), cleanup);
} else {
if (tpdfname) {
LOGVAL(ctx->ctx, NULL, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
} else {
LOGVAL(ctx->ctx, NULL, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
}
rc = LY_EVALID;
goto cleanup;
}
break;
case LY_TYPE_INST:
if (type_p->flags & LYS_SET_REQINST) {
((struct lysc_type_instanceid *)*type)->require_instance = type_p->require_instance;
} else {
((struct lysc_type_instanceid *)*type)->require_instance = 1;
}
break;
case LY_TYPE_UNION:
un = (struct lysc_type_union *)*type;
if (type_p->types) {
if (base) {
if (tpdfname) {
LOGVAL(ctx->ctx, NULL, LYVE_SYNTAX_YANG,
"Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
tpdfname);
} else {
LOGVAL(ctx->ctx, NULL, LYVE_SYNTAX_YANG,
"Invalid type substatement for the type not directly derived from union built-in type.");
}
rc = LY_EVALID;
goto cleanup;
}
LY_CHECK_GOTO(rc = lys_compile_type_union(ctx, type_p->types, context_pnode, context_flags, context_name,
&un->types), cleanup);
} else if (base) {
const struct lysc_type_union *un_base = (struct lysc_type_union *)base;
LY_ARRAY_COUNT_TYPE u;
LY_ARRAY_CREATE_GOTO(ctx->ctx, un->types, LY_ARRAY_COUNT(un_base->types), rc, cleanup);
LY_ARRAY_FOR(un_base->types, u) {
un->types[u] = un_base->types[u];
LY_ATOMIC_INC_BARRIER(un->types[u]->refcount);
LY_ARRAY_INCREMENT(un->types);
}
} else {
if (tpdfname) {
LOGVAL(ctx->ctx, NULL, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
} else {
LOGVAL(ctx->ctx, NULL, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
}
rc = LY_EVALID;
goto cleanup;
}
break;
case LY_TYPE_BOOL:
case LY_TYPE_EMPTY:
case LY_TYPE_UNKNOWN:
break;
}
if (tpdf_chain->count > tpdf_chain_last) {
i = tpdf_chain->count;
do {
--i;
tpdf_item = tpdf_chain->objs[i];
COMPILE_EXTS_GOTO(ctx, tpdf_item->tpdf->type.exts, (*type)->exts, *type, rc, cleanup);
} while (i > tpdf_chain_last);
}
COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, *type, rc, cleanup);
cleanup:
if (rc) {
LY_ATOMIC_INC_BARRIER((*type)->refcount);
lysc_type_free(ctx->ctx, *type);
*type = NULL;
}
return rc;
}
LY_ERR
lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, const char *context_name,
const struct lysp_type *type_p, struct lysc_type **type, const char **units, struct lysp_qname **dflt)
{
LY_ERR ret = LY_SUCCESS;
ly_bool dummyloops = 0, has_leafref;
struct lys_type_item *tctx, *tctx_prev = NULL, *tctx_iter;
LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
struct lysc_type *base = NULL;
struct lysc_type_union *base_un;
LY_ARRAY_COUNT_TYPE u;
struct ly_set tpdf_chain = {0};
uintptr_t plugin_ref = 0;
*type = NULL;
if (dflt) {
*dflt = NULL;
}
tctx = calloc(1, sizeof *tctx);
LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
for (ret = lysp_type_find(type_p->name, context_pnode, type_p->pmod, ctx->ext, &basetype, &tctx->tpdf, &tctx->node);
ret == LY_SUCCESS;
ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->tpdf->type.pmod, ctx->ext,
&basetype, &tctx->tpdf, &tctx->node)) {
if (basetype) {
break;
}
ret = lysc_check_status(ctx, NULL, context_flags, (void *)type_p->pmod, context_name, tctx->tpdf->flags,
(void *)tctx->tpdf->type.pmod, tctx->node ? tctx->node->name : tctx->tpdf->name);
LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
if (units && !*units) {
DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret);
LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
}
if (dflt && !*dflt && tctx->tpdf->dflt.str) {
*dflt = (struct lysp_qname *)&tctx->tpdf->dflt;
}
if (dummyloops && (!units || *units) && dflt && *dflt) {
basetype = ((struct lys_type_item *)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
break;
}
if (tctx->tpdf->type.compiled && (tctx->tpdf->type.compiled->refcount == 1)) {
lysc_type_free(ctx->ctx, tctx->tpdf->type.compiled);
((struct lysp_tpdf *)tctx->tpdf)->type.compiled = NULL;
}
if (tctx->tpdf->type.compiled) {
basetype = tctx->tpdf->type.compiled->basetype;
ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
if ((units && !*units) || (dflt && !*dflt)) {
dummyloops = 1;
goto preparenext;
} else {
tctx = NULL;
break;
}
}
for (uint32_t u = 0; u < tpdf_chain.count; u++) {
tctx_iter = (struct lys_type_item *)tpdf_chain.objs[u];
if (tctx_iter->tpdf == tctx->tpdf) {
LOGVAL(ctx->ctx, NULL, LYVE_REFERENCE, "Invalid \"%s\" type reference - circular chain of types detected.",
tctx->tpdf->name);
free(tctx);
ret = LY_EVALID;
goto cleanup;
}
}
for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) {
tctx_iter = (struct lys_type_item *)ctx->tpdf_chain.objs[u];
if (tctx_iter->tpdf == tctx->tpdf) {
LOGVAL(ctx->ctx, NULL, LYVE_REFERENCE, "Invalid \"%s\" type reference - circular chain of types detected.",
tctx->tpdf->name);
free(tctx);
ret = LY_EVALID;
goto cleanup;
}
}
ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
preparenext:
tctx_prev = tctx;
tctx = calloc(1, sizeof *tctx);
LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
}
free(tctx);
if (basetype == LY_TYPE_UNKNOWN) {
LOGVAL(ctx->ctx, NULL, LYVE_REFERENCE, "Referenced type \"%s\" not found.",
tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
ret = LY_EVALID;
goto cleanup;
}
if (~type_substmt_map[basetype] & type_p->flags) {
LOGVAL(ctx->ctx, NULL, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", ly_data_type2str[basetype]);
ret = LY_EVALID;
goto cleanup;
}
for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) {
tctx = (struct lys_type_item *)tpdf_chain.objs[u];
ret = ly_set_add(&ctx->tpdf_chain, tctx, 1, NULL);
LY_CHECK_GOTO(ret, cleanup);
if (tctx->tpdf->type.compiled) {
base = tctx->tpdf->type.compiled;
continue;
}
plugin_ref = lyplg_type_plugin_find(ctx->ctx, tctx->tpdf->type.pmod->mod->name, tctx->tpdf->type.pmod->mod->revision,
tctx->tpdf->name);
if (!plugin_ref && base) {
plugin_ref = base->plugin_ref;
}
if (!plugin_ref) {
plugin_ref = lyplg_type_plugin_find(ctx->ctx, "", NULL, ly_data_type2str[basetype]);
}
assert(plugin_ref);
if ((basetype != LY_TYPE_LEAFREF) && (u != tpdf_chain.count - 1) && !tctx->tpdf->type.flags &&
!tctx->tpdf->type.exts && (plugin_ref == base->plugin_ref)) {
((struct lysp_tpdf *)tctx->tpdf)->type.compiled = base;
LY_ATOMIC_INC_BARRIER(base->refcount);
continue;
}
if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
LOGVAL(ctx->ctx, NULL, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
tctx->tpdf->name, ly_data_type2str[basetype]);
ret = LY_EVALID;
goto cleanup;
} else if ((basetype == LY_TYPE_EMPTY) && tctx->tpdf->dflt.str) {
LOGVAL(ctx->ctx, NULL, LYVE_SEMANTICS, "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
tctx->tpdf->name, tctx->tpdf->dflt.str);
ret = LY_EVALID;
goto cleanup;
}
ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->tpdf->name, &tctx->tpdf->type, basetype,
tctx->tpdf->name, base, plugin_ref, &tpdf_chain, u + 1, &base);
LY_CHECK_GOTO(ret, cleanup);
((struct lysp_tpdf *)tctx->tpdf)->type.compiled = base;
LY_ATOMIC_INC_BARRIER(base->refcount);
}
ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
has_leafref = 0;
if (basetype == LY_TYPE_LEAFREF) {
has_leafref = 1;
} else if ((basetype == LY_TYPE_UNION) && base) {
base_un = (struct lysc_type_union *)base;
LY_ARRAY_FOR(base_un->types, u) {
if (base_un->types[u]->basetype == LY_TYPE_LEAFREF) {
has_leafref = 1;
break;
}
}
}
if (type_p->flags || type_p->exts || !base || has_leafref) {
if (base) {
plugin_ref = base->plugin_ref;
} else {
plugin_ref = lyplg_type_plugin_find(ctx->ctx, "", NULL, ly_data_type2str[basetype]);
}
ret = lys_compile_type_(ctx, context_pnode, context_flags, context_name, (struct lysp_type *)type_p, basetype,
NULL, base, plugin_ref, &tpdf_chain, 0, type);
LY_CHECK_GOTO(ret, cleanup);
if (tpdf_chain.count) {
ret = lysdict_insert(ctx->ctx, ((struct lys_type_item *)tpdf_chain.objs[0])->tpdf->name, 0, &(*type)->name);
LY_CHECK_GOTO(ret, cleanup);
}
} else {
*type = base;
}
cleanup:
ly_set_erase(&tpdf_chain, free);
return ret;
}
static LY_ERR
lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name,
const struct lysc_node *exclude)
{
const struct lysc_node *iter, *iter2, *dup = NULL, **node_list_p;
const struct lysc_node_action *actions;
const struct lysc_node_notif *notifs;
uint32_t getnext_flags;
struct ly_set parent_choices = {0};
const char *node_type_str = "data definition/RPC/action/notification";
char *spath;
#define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name))
if (exclude->nodetype == LYS_CASE) {
assert(parent->nodetype == LYS_CHOICE);
LY_LIST_FOR(lysc_node_child(parent), iter) {
if (CHECK_NODE(iter, exclude, name)) {
node_type_str = "case";
dup = iter;
goto cleanup;
}
}
return LY_SUCCESS;
}
assert(!parent || (parent->nodetype != LYS_CHOICE));
if (parent && (parent->nodetype == LYS_CASE)) {
iter = lysc_data_parent(parent);
do {
parent = parent->parent;
if (parent && (parent->nodetype == LYS_CHOICE)) {
ly_set_add(&parent_choices, (void *)parent, 1, NULL);
}
} while (parent != iter);
}
getnext_flags = LYS_GETNEXT_WITHCHOICE;
if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION))) {
if (exclude->flags & LYS_IS_OUTPUT) {
getnext_flags |= LYS_GETNEXT_OUTPUT;
parent = lysc_node_child(parent)->next;
} else {
parent = lysc_node_child(parent);
}
}
iter = NULL;
if (!parent && ctx->ext) {
lyplg_ext_get_storage_p(ctx->ext, LY_STMT_DATA_NODE_MASK, (void ***)&node_list_p);
iter = node_list_p ? *node_list_p : NULL;
while (iter) {
if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
dup = iter;
goto cleanup;
}
if (iter->nodetype == LYS_CHOICE) {
iter2 = NULL;
while ((iter2 = lys_getnext(iter2, iter, NULL, 0))) {
if (CHECK_NODE(iter2, exclude, name)) {
dup = iter2;
goto cleanup;
}
}
}
iter = lys_getnext(iter, parent, NULL, getnext_flags);
}
} else {
while ((iter = lys_getnext(iter, parent, ctx->cur_mod->compiled, getnext_flags))) {
if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
dup = iter;
goto cleanup;
}
if (iter->nodetype == LYS_CHOICE) {
iter2 = NULL;
while ((iter2 = lys_getnext(iter2, iter, NULL, 0))) {
if (CHECK_NODE(iter2, exclude, name)) {
dup = iter2;
goto cleanup;
}
}
}
}
actions = parent ? lysc_node_actions(parent) : ctx->cur_mod->compiled->rpcs;
LY_LIST_FOR((struct lysc_node *)actions, iter) {
if (CHECK_NODE(iter, exclude, name)) {
dup = iter;
goto cleanup;
}
}
notifs = parent ? lysc_node_notifs(parent) : ctx->cur_mod->compiled->notifs;
LY_LIST_FOR((struct lysc_node *)notifs, iter) {
if (CHECK_NODE(iter, exclude, name)) {
dup = iter;
goto cleanup;
}
}
}
cleanup:
ly_set_erase(&parent_choices, NULL);
if (dup) {
spath = lysc_path(dup, LYSC_PATH_LOG, NULL, 0);
LOGVAL(ctx->ctx, NULL, LY_VCODE_DUPIDENT, spath, node_type_str);
free(spath);
return LY_EEXIST;
}
return LY_SUCCESS;
#undef CHECK_NODE
}
LY_ERR
lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
{
struct lysc_node **children, *anchor = NULL;
int insert_after = 0;
node->parent = parent;
if (parent) {
if (node->nodetype == LYS_INPUT) {
assert(parent->nodetype & (LYS_ACTION | LYS_RPC));
node->next = &((struct lysc_node_action *)parent)->output.node;
node->prev = node->next;
return LY_SUCCESS;
} else if (node->nodetype == LYS_OUTPUT) {
node->next = NULL;
node->prev = &((struct lysc_node_action *)parent)->input.node;
return LY_SUCCESS;
} else if (node->nodetype == LYS_ACTION) {
children = (struct lysc_node **)lysc_node_actions_p(parent);
} else if (node->nodetype == LYS_NOTIF) {
children = (struct lysc_node **)lysc_node_notifs_p(parent);
} else {
children = lysc_node_child_p(parent);
}
assert(children);
if (!(*children)) {
*children = node;
} else if (node->flags & LYS_KEY) {
assert(node->module == parent->module);
anchor = *children;
if (anchor->flags & LYS_KEY) {
while ((anchor->flags & LYS_KEY) && anchor->next) {
anchor = anchor->next;
}
insert_after = 1;
}
} else if ((*children)->prev->module == node->module) {
anchor = (*children)->prev;
insert_after = 1;
} else if (parent->module == node->module) {
for (anchor = *children; anchor->module == node->module; anchor = anchor->next) {}
} else {
anchor = *children;
do {
anchor = anchor->prev;
if ((anchor->module == node->module) || (strcmp(anchor->module->name, node->module->name) < 0) ||
(anchor->module == parent->module)) {
insert_after = 1;
break;
}
} while (anchor->prev->next);
}
if (anchor) {
if (insert_after) {
node->next = anchor->next;
node->prev = anchor;
anchor->next = node;
if (node->next) {
node->next->prev = node;
} else {
(*children)->prev = node;
}
} else {
node->next = anchor;
node->prev = anchor->prev;
anchor->prev = node;
if (anchor == *children) {
*children = node;
} else {
node->prev->next = node;
}
}
}
if (lys_compile_node_uniqness(ctx, parent, node->name, node)) {
return LY_EEXIST;
}
} else {
struct lysc_node **list = NULL;
if (ctx->ext) {
lyplg_ext_get_storage_p(ctx->ext, LY_STMT_DATA_NODE_MASK, (void ***)&list);
} else if (node->nodetype == LYS_RPC) {
list = (struct lysc_node **)&ctx->cur_mod->compiled->rpcs;
} else if (node->nodetype == LYS_NOTIF) {
list = (struct lysc_node **)&ctx->cur_mod->compiled->notifs;
} else {
list = &ctx->cur_mod->compiled->data;
}
if (!(*list)) {
*list = node;
} else {
(*list)->prev->next = node;
node->prev = (*list)->prev;
(*list)->prev = node;
}
if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) {
return LY_EEXIST;
}
}
return LY_SUCCESS;
}
static LY_ERR
lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node)
{
assert((node->nodetype != LYS_CASE) || !(node->flags & LYS_CONFIG_MASK));
if (ctx->compile_opts & LYS_COMPILE_NO_CONFIG) {
node->flags &= ~LYS_CONFIG_MASK;
} else if (!(node->flags & LYS_CONFIG_MASK)) {
assert(!node->parent || (node->parent->flags & LYS_CONFIG_MASK) || (node->parent->nodetype & LYS_AUGMENT));
if (node->parent && (node->parent->flags & LYS_CONFIG_MASK)) {
node->flags |= node->parent->flags & LYS_CONFIG_MASK;
} else {
node->flags |= LYS_CONFIG_W;
}
} else {
node->flags |= LYS_SET_CONFIG;
}
if (node->parent && (node->parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
LOGVAL(ctx->ctx, NULL, LYVE_SEMANTICS, "Configuration node cannot be child of any state data node.");
return LY_EVALID;
}
return LY_SUCCESS;
}
static LY_ERR
lys_compile_node_flags(struct lysc_ctx *ctx, uint16_t parsed_flags, uint16_t inherited_flags, struct lysc_node *node)
{
uint16_t parent_flags;
const char *parent_name;
node->flags = (parsed_flags & LYS_FLAGS_COMPILED_MASK) & ~LYS_STATUS_MASK;
LY_CHECK_RET(lys_compile_config(ctx, node));
parent_flags = node->parent ? node->parent->flags : 0;
parent_name = node->parent ? node->parent->name : NULL;
LY_CHECK_RET(lys_compile_status(ctx, parsed_flags, inherited_flags, parent_flags, parent_name, node->name, &node->flags));
if ((ctx->compile_opts & LYS_IS_INPUT) && (node->nodetype != LYS_INPUT)) {
node->flags |= LYS_IS_INPUT;
} else if ((ctx->compile_opts & LYS_IS_OUTPUT) && (node->nodetype != LYS_OUTPUT)) {
node->flags |= LYS_IS_OUTPUT;
} else if ((ctx->compile_opts & LYS_IS_NOTIF) && (node->nodetype != LYS_NOTIF)) {
node->flags |= LYS_IS_NOTIF;
}
return LY_SUCCESS;
}
static LY_ERR
lys_compile_node_(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t inherited_flags,
LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *),
struct lysc_node *node, struct ly_set *child_set)
{
LY_ERR ret = LY_SUCCESS;
ly_bool not_supported, enabled;
struct lysp_node *dev_pnode = NULL;
struct lysp_when *pwhen = NULL;
uint32_t prev_opts = ctx->compile_opts;
node->nodetype = pnode->nodetype;
node->module = ctx->cur_mod;
node->parent = parent;
node->prev = node;
node->priv = ctx->ctx->opts & LY_CTX_SET_PRIV_PARSED ? pnode : NULL;
LY_CHECK_GOTO(ret = lys_compile_node_deviations_refines(ctx, pnode, parent, &dev_pnode, ¬_supported), error);
if (not_supported && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
ly_set_add(&ctx->unres->disabled, node, 1, NULL);
ctx->compile_opts |= LYS_COMPILE_DISABLED;
}
if (dev_pnode) {
pnode = dev_pnode;
}
DUP_STRING_GOTO(ctx->ctx, pnode->name, node->name, ret, error);
DUP_STRING_GOTO(ctx->ctx, pnode->dsc, node->dsc, ret, error);
DUP_STRING_GOTO(ctx->ctx, pnode->ref, node->ref, ret, error);
LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), error);
if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
ly_set_add(&ctx->unres->disabled, node, 1, NULL);
ctx->compile_opts |= LYS_COMPILE_DISABLED;
}
LY_CHECK_GOTO(ret = lys_compile_node_flags(ctx, pnode->flags, inherited_flags, node), error);
if ((node->flags & LYS_STATUS_OBSLT) && !(ctx->ctx->opts & LY_CTX_COMPILE_OBSOLETE) &&
!(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
ly_set_add(&ctx->unres->disabled, node, 1, NULL);
ctx->compile_opts |= LYS_COMPILE_DISABLED;
}
if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
if ((node->flags & (LYS_CONFIG_R | LYS_IS_OUTPUT | LYS_IS_NOTIF)) && (node->flags & LYS_ORDBY_MASK)) {
node->flags &= ~LYS_ORDBY_MASK;
LOGVRB("The ordered-by statement is ignored in lists representing %s (%s).",
(node->flags & LYS_IS_OUTPUT) ? "RPC/action output parameters" :
(ctx->compile_opts & LYS_IS_NOTIF) ? "notification content" : "state data", ctx->path);
}
if (node->flags & (LYS_IS_OUTPUT | LYS_IS_NOTIF | LYS_CONFIG_R)) {
node->flags |= LYS_ORDBY_USER;
} else if (!(node->flags & LYS_ORDBY_MASK)) {
node->flags |= LYS_ORDBY_SYSTEM;
}
}
LY_CHECK_GOTO(ret = lys_compile_node_connect(ctx, parent, node), cleanup);
if ((pwhen = lysp_node_when(pnode))) {
ret = lys_compile_when(ctx, pwhen, pnode->flags, node, lysc_data_node(node), node, NULL);
LY_CHECK_GOTO(ret, cleanup);
}
LY_CHECK_GOTO(ret = node_compile_spec(ctx, pnode, node), cleanup);
COMPILE_EXTS_GOTO(ctx, pnode->exts, node->exts, node, ret, cleanup);
if (node->flags & LYS_MAND_TRUE) {
lys_compile_mandatory_parents(parent, 1);
}
if (child_set) {
LY_CHECK_GOTO(ret = ly_set_add(child_set, node, 1, NULL), cleanup);
}
goto cleanup;
error:
lysc_node_free(ctx->ctx, node, 0);
cleanup:
if (ret && dev_pnode) {
LOGVAL(ctx->ctx, NULL, LYVE_OTHER, "Compilation of a deviated and/or refined node failed.");
}
ctx->compile_opts = prev_opts;
lysp_dev_node_free(ctx, dev_pnode);
return ret;
}
LY_ERR
lys_compile_node_action_inout(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
{
LY_ERR ret = LY_SUCCESS;
struct lysp_node *child_p;
uint32_t prev_options = ctx->compile_opts;
struct lysp_node_action_inout *inout_p = (struct lysp_node_action_inout *)pnode;
struct lysc_node_action_inout *inout = (struct lysc_node_action_inout *)node;
COMPILE_ARRAY_GOTO(ctx, inout_p->musts, inout->musts, lys_compile_must, ret, done);
ctx->compile_opts |= (inout_p->nodetype == LYS_INPUT) ? LYS_COMPILE_RPC_INPUT : LYS_COMPILE_RPC_OUTPUT;
LY_LIST_FOR(inout_p->child, child_p) {
LY_CHECK_GOTO(ret = lys_compile_node(ctx, child_p, node, 0, NULL), done);
}
LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
ctx->compile_opts = prev_options;
done:
return ret;
}
static LY_ERR
lys_compile_node_action(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
{
LY_ERR ret;
struct lysp_node_action *action_p = (struct lysp_node_action *)pnode;
struct lysc_node_action *action = (struct lysc_node_action *)node;
struct lysp_node_action_inout *input, implicit_input = {
.nodetype = LYS_INPUT,
.name = "input",
.parent = pnode,
};
struct lysp_node_action_inout *output, implicit_output = {
.nodetype = LYS_OUTPUT,
.name = "output",
.parent = pnode,
};
lysc_update_path(ctx, action->module, "input");
if (action_p->input.nodetype == LYS_UNKNOWN) {
input = &implicit_input;
} else {
input = &action_p->input;
}
ret = lys_compile_node_(ctx, &input->node, &action->node, 0, lys_compile_node_action_inout, &action->input.node, NULL);
lysc_update_path(ctx, NULL, NULL);
LY_CHECK_GOTO(ret, done);
ret = lysc_unres_must_add(ctx, &action->input.node, &input->node);
LY_CHECK_GOTO(ret, done);
lysc_update_path(ctx, action->module, "output");
if (action_p->output.nodetype == LYS_UNKNOWN) {
output = &implicit_output;
} else {
output = &action_p->output;
}
ret = lys_compile_node_(ctx, &output->node, &action->node, 0, lys_compile_node_action_inout, &action->output.node, NULL);
lysc_update_path(ctx, NULL, NULL);
LY_CHECK_GOTO(ret, done);
ret = lysc_unres_must_add(ctx, &action->output.node, &output->node);
LY_CHECK_GOTO(ret, done);
done:
return ret;
}
static LY_ERR
lys_compile_node_notif(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
{
LY_ERR ret = LY_SUCCESS;
struct lysp_node_notif *notif_p = (struct lysp_node_notif *)pnode;
struct lysc_node_notif *notif = (struct lysc_node_notif *)node;
struct lysp_node *child_p;
COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, lys_compile_must, ret, done);
ret = lysc_unres_must_add(ctx, node, pnode);
LY_CHECK_GOTO(ret, done);
LY_LIST_FOR(notif_p->child, child_p) {
ret = lys_compile_node(ctx, child_p, node, 0, NULL);
LY_CHECK_GOTO(ret, done);
}
LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
done:
return ret;
}
static LY_ERR
lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
{
struct lysp_node_container *cont_p = (struct lysp_node_container *)pnode;
struct lysc_node_container *cont = (struct lysc_node_container *)node;
struct lysp_node *child_p;
LY_ERR ret = LY_SUCCESS;
if (cont_p->presence) {
cont->flags |= LYS_PRESENCE;
}
LY_LIST_FOR(cont_p->child, child_p) {
ret = lys_compile_node(ctx, child_p, node, 0, NULL);
LY_CHECK_GOTO(ret, done);
}
COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, lys_compile_must, ret, done);
ret = lysc_unres_must_add(ctx, node, pnode);
LY_CHECK_GOTO(ret, done);
LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
LY_LIST_FOR((struct lysp_node *)cont_p->actions, child_p) {
ret = lys_compile_node(ctx, child_p, node, 0, NULL);
LY_CHECK_GOTO(ret, done);
}
LY_LIST_FOR((struct lysp_node *)cont_p->notifs, child_p) {
ret = lys_compile_node(ctx, child_p, node, 0, NULL);
LY_CHECK_GOTO(ret, done);
}
done:
return ret;
}
static LY_ERR
lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p,
struct lysc_node_leaf *leaf)
{
struct lysp_qname *dflt;
struct lysc_type **t;
LY_ARRAY_COUNT_TYPE u, count;
ly_bool in_unres = 0;
LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, leaf->name, type_p, &leaf->type,
leaf->units ? NULL : &leaf->units, &dflt));
LY_ATOMIC_INC_BARRIER(leaf->type->refcount);
if (dflt && !(leaf->flags & LYS_SET_DFLT)) {
LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, dflt));
}
LY_CHECK_RET(lysc_unres_leafref_add(ctx, leaf, type_p->pmod));
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_EMPTY) {
if ((leaf->nodetype == LYS_LEAFLIST) && (ctx->pmod->version < LYS_VERSION_1_1)) {
LOGVAL(ctx->ctx, NULL, LYVE_SEMANTICS, "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
return LY_EVALID;
}
} else if (!in_unres && ((t[u]->basetype == LY_TYPE_BITS) || (t[u]->basetype == LY_TYPE_ENUM))) {
LY_CHECK_RET(lysc_unres_bitenum_add(ctx, leaf));
in_unres = 1;
}
}
return LY_SUCCESS;
}
static LY_ERR
lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
{
struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)pnode;
struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
LY_ERR ret = LY_SUCCESS;
COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, lys_compile_must, ret, done);
ret = lysc_unres_must_add(ctx, node, pnode);
LY_CHECK_GOTO(ret, done);
if (leaf_p->units) {
LY_CHECK_GOTO(ret = lysdict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done);
leaf->flags |= LYS_SET_UNITS;
}
ret = lys_compile_node_type(ctx, pnode, &leaf_p->type, leaf);
LY_CHECK_GOTO(ret, done);
if (leaf_p->dflt.str) {
LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, &leaf_p->dflt));
leaf->flags |= LYS_SET_DFLT;
}
if ((leaf->flags & LYS_SET_DFLT) && (leaf->flags & LYS_MAND_TRUE)) {
LOGVAL(ctx->ctx, NULL, LYVE_SEMANTICS, "Invalid mandatory leaf with a default value.");
return LY_EVALID;
}
done:
return ret;
}
static LY_ERR
lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
{
struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)pnode;
struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
LY_ERR ret = LY_SUCCESS;
COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, lys_compile_must, ret, done);
ret = lysc_unres_must_add(ctx, node, pnode);
LY_CHECK_GOTO(ret, done);
if (llist_p->units) {
LY_CHECK_GOTO(ret = lysdict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done);
llist->flags |= LYS_SET_UNITS;
}
ret = lys_compile_node_type(ctx, pnode, &llist_p->type, (struct lysc_node_leaf *)llist);
LY_CHECK_GOTO(ret, done);
if (llist_p->dflts) {
if (ctx->pmod->version < LYS_VERSION_1_1) {
LOGVAL(ctx->ctx, NULL, LYVE_SEMANTICS, "Leaf-list default values are allowed only in YANG 1.1 modules.");
return LY_EVALID;
}
LY_CHECK_GOTO(lysc_unres_llist_dflts_add(ctx, llist, llist_p->dflts), done);
llist->flags |= LYS_SET_DFLT;
}
llist->min = llist_p->min;
if (llist->min) {
llist->flags |= LYS_MAND_TRUE;
}
llist->max = llist_p->max ? llist_p->max : UINT32_MAX;
if (llist->flags & LYS_CONFIG_R) {
llist->flags &= ~LYS_ORDBY_SYSTEM;
llist->flags |= LYS_ORDBY_USER;
}
if ((llist->flags & LYS_SET_DFLT) && (llist->flags & LYS_MAND_TRUE)) {
LOGVAL(ctx->ctx, NULL, LYVE_SEMANTICS, "The default statement is present on leaf-list with a nonzero min-elements.");
return LY_EVALID;
}
if (llist->min > llist->max) {
LOGVAL(ctx->ctx, NULL, LYVE_SEMANTICS, "Leaf-list min-elements %" PRIu32 " is bigger than max-elements %" PRIu32 ".",
llist->min, llist->max);
return LY_EVALID;
}
done:
return ret;
}
static LY_ERR
lysc_resolve_schema_nodeid(struct lysc_ctx *ctx, const char *nodeid, size_t nodeid_len, const struct lysc_node *ctx_node,
LY_VALUE_FORMAT format, void *prefix_data, uint16_t nodetype, const struct lysc_node **target, uint16_t *result_flag)
{
LY_ERR ret = LY_EVALID;
const char *name, *prefix, *id;
uint32_t name_len, prefix_len, getnext_extra_flag = 0;
const struct lys_module *mod = NULL;
const char *nodeid_type;
uint16_t current_nodetype = 0;
assert(nodeid);
assert(target);
assert(result_flag);
*target = NULL;
*result_flag = 0;
id = nodeid;
if (ctx_node) {
nodeid_type = "descendant";
if (*id == '/') {
LOGVAL(ctx->ctx, NULL, LYVE_REFERENCE,
"Invalid descendant-schema-nodeid value \"%.*s\" - absolute-schema-nodeid used.",
(int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
return LY_EVALID;
}
} else {
nodeid_type = "absolute";
if (*id != '/') {
LOGVAL(ctx->ctx, NULL, LYVE_REFERENCE,
"Invalid absolute-schema-nodeid value \"%.*s\" - missing starting \"/\".",
(int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
return LY_EVALID;
}
++id;
}
while (*id && (ret = ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len)) == LY_SUCCESS) {
if (prefix) {
mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, format, prefix_data);
if (!mod) {
assert(prefix);
LOGVAL(ctx->ctx, NULL, LYVE_REFERENCE,
"Invalid %s-schema-nodeid value \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
nodeid_type, (int)(id - nodeid), nodeid, (int)prefix_len, prefix, LYSP_MODULE_NAME(ctx->pmod));
return LY_ENOTFOUND;
}
} else {
switch (format) {
case LY_VALUE_SCHEMA:
case LY_VALUE_SCHEMA_RESOLVED:
mod = ctx->cur_mod;
break;
case LY_VALUE_JSON:
case LY_VALUE_LYB:
if (!ctx_node) {
LOGINT_RET(ctx->ctx);
}
mod = ctx_node->module;
break;
case LY_VALUE_CANON:
case LY_VALUE_XML:
case LY_VALUE_STR_NS:
LOGINT_RET(ctx->ctx);
}
}
if (ctx_node && (ctx_node->nodetype & (LYS_RPC | LYS_ACTION))) {
if (mod != ctx_node->module) {
LOGVAL(ctx->ctx, NULL, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
nodeid_type, (int)(id - nodeid), nodeid);
return LY_ENOTFOUND;
}
if (!ly_strncmp("input", name, name_len)) {
ctx_node = &((struct lysc_node_action *)ctx_node)->input.node;
} else if (!ly_strncmp("output", name, name_len)) {
ctx_node = &((struct lysc_node_action *)ctx_node)->output.node;
getnext_extra_flag = LYS_GETNEXT_OUTPUT;
} else {
ctx_node = NULL;
}
} else {
ctx_node = lys_find_child(ctx->ctx, ctx_node, mod, NULL, 0, name, name_len,
getnext_extra_flag | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE);
getnext_extra_flag = 0;
}
if (!ctx_node) {
LOGVAL(ctx->ctx, NULL, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
nodeid_type, (int)(id - nodeid), nodeid);
return LY_ENOTFOUND;
}
current_nodetype = ctx_node->nodetype;
if (current_nodetype == LYS_NOTIF) {
(*result_flag) |= LYS_COMPILE_NOTIFICATION;
} else if (current_nodetype == LYS_INPUT) {
(*result_flag) |= LYS_COMPILE_RPC_INPUT;
} else if (current_nodetype == LYS_OUTPUT) {
(*result_flag) |= LYS_COMPILE_RPC_OUTPUT;
}
if (!*id || (nodeid_len && ((size_t)(id - nodeid) >= nodeid_len))) {
break;
}
if (*id != '/') {
LOGVAL(ctx->ctx, NULL, LYVE_REFERENCE,
"Invalid %s-schema-nodeid value \"%.*s\" - missing \"/\" as node-identifier separator.",
nodeid_type, (int)(id - nodeid + 1), nodeid);
return LY_EVALID;
}
++id;
}
if (ret == LY_SUCCESS) {
*target = ctx_node;
if (nodetype && !(current_nodetype & nodetype)) {
return LY_EDENIED;
}
} else {
LOGVAL(ctx->ctx, NULL, LYVE_REFERENCE,
"Invalid %s-schema-nodeid value \"%.*s\" - unexpected end of expression.",
nodeid_type, (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
}
return ret;
}
static LY_ERR
lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lysp_qname *uniques, struct lysc_node_list *list)
{
LY_ERR ret = LY_SUCCESS;
struct lysc_node_leaf **key, ***unique;
struct lysc_node *parent;
const char *keystr, *delim;
size_t len;
LY_ARRAY_COUNT_TYPE v;
int8_t config;
uint16_t flags;
LY_ARRAY_FOR(uniques, v) {
config = -1;
LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
keystr = uniques[v].str;
while (keystr) {
delim = strpbrk(keystr, " \t\n");
if (delim) {
len = delim - keystr;
while (isspace(*delim)) {
++delim;
}
} else {
len = strlen(keystr);
}
LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
ret = lysc_resolve_schema_nodeid(ctx, keystr, len, &list->node, LY_VALUE_SCHEMA, (void *)uniques[v].mod,
LYS_LEAF, (const struct lysc_node **)key, &flags);
if (ret != LY_SUCCESS) {
if (ret == LY_EDENIED) {
LOGVAL(ctx->ctx, NULL, LYVE_REFERENCE,
"Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
(int)len, keystr, lys_nodetype2str((*key)->nodetype));
}
return LY_EVALID;
} else if (flags) {
LOGVAL(ctx->ctx, NULL, LYVE_REFERENCE,
"Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
(int)len, keystr, flags & LYS_IS_NOTIF ? "notification" : "RPC/action");
return LY_EVALID;
}
if ((config != -1) && ((((*key)->flags & LYS_CONFIG_W) && (config == 0)) ||
(((*key)->flags & LYS_CONFIG_R) && (config == 1)))) {
LOGVAL(ctx->ctx, NULL, LYVE_SEMANTICS,
"Unique statement \"%s\" refers to leaves with different config type.", uniques[v].str);
return LY_EVALID;
} else if ((*key)->flags & LYS_CONFIG_W) {
config = 1;
} else {
config = 0;
}
for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
if (parent->nodetype == LYS_LIST) {
LOGVAL(ctx->ctx, NULL, LYVE_SEMANTICS,
"Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v].str, parent->name);
return LY_EVALID;
}
}
LY_CHECK_RET(lysc_check_status(ctx, NULL, list->flags, uniques[v].mod->mod, list->name,
(*key)->flags, (*key)->module, (*key)->name));
(*key)->flags |= LYS_UNIQUE;
keystr = delim;
}
}
return LY_SUCCESS;
}
static LY_ERR
lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
{
struct lysp_node_list *list_p = (struct lysp_node_list *)pnode;
struct lysc_node_list *list = (struct lysc_node_list *)node;
struct lysp_node *child_p;
struct lysc_node *parent;
struct lysc_node_leaf *key, *prev_key = NULL;
size_t len;
const char *keystr, *delim;
LY_ERR ret = LY_SUCCESS;
list->min = list_p->min;
if (list->min) {
list->flags |= LYS_MAND_TRUE;
}
list->max = list_p->max ? list_p->max : UINT32_MAX;
LY_LIST_FOR(list_p->child, child_p) {
LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
}
COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, lys_compile_must, ret, done);
ret = lysc_unres_must_add(ctx, node, pnode);
LY_CHECK_GOTO(ret, done);
if (list->flags & LYS_CONFIG_W) {
parent = node;
if (ctx->compile_opts & LYS_COMPILE_GROUPING) {
while (parent) {
if (parent->flags & LYS_SET_CONFIG) {
break;
}
parent = parent->parent;
}
}
if (parent && (!list_p->key || !list_p->key[0])) {
LOGVAL(ctx->ctx, NULL, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
return LY_EVALID;
}
}
keystr = list_p->key;
if (!keystr) {
list->flags &= ~LYS_ORDBY_SYSTEM;
list->flags |= LYS_KEYLESS | LYS_ORDBY_USER;
}
while (keystr) {
delim = strpbrk(keystr, " \t\n");
if (delim) {
len = delim - keystr;
while (isspace(*delim)) {
++delim;
}
} else {
len = strlen(keystr);
}
key = (struct lysc_node_leaf *)lys_find_child(NULL, node, node->module, NULL, 0, keystr, len, LYS_GETNEXT_NOCHOICE);
if (key && (key->nodetype != LYS_LEAF)) {
key = NULL;
}
if (!key) {
LOGVAL(ctx->ctx, NULL, LYVE_REFERENCE, "The list's key \"%.*s\" not found.", (int)len, keystr);
return LY_EVALID;
}
if (key->flags & LYS_KEY) {
LOGVAL(ctx->ctx, NULL, LYVE_SEMANTICS, "Duplicated key identifier \"%.*s\".", (int)len, keystr);
return LY_EVALID;
}
lysc_update_path(ctx, list->module, key->name);
if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
LOGVAL(ctx->ctx, NULL, LYVE_SEMANTICS, "Key of a configuration list must not be a state leaf.");
return LY_EVALID;
}
if (ctx->pmod->version < LYS_VERSION_1_1) {
if (key->type->basetype == LY_TYPE_EMPTY) {
LOGVAL(ctx->ctx, NULL, LYVE_SEMANTICS,
"List key of the \"empty\" type is allowed only in YANG 1.1 modules.");
return LY_EVALID;
}
} else {
if (key->when) {
LOGVAL(ctx->ctx, NULL, LYVE_SEMANTICS, "List's key must not have any \"when\" statement.");
return LY_EVALID;
}
}
LY_CHECK_RET(lysc_check_status(ctx, NULL, list->flags, list->module, list->name, key->flags, key->module, key->name));
if (key->dflt.str) {
lysc_value_free(ctx->ctx, &key->dflt);
memset(&key->dflt, 0, sizeof key->dflt);
}
key->flags |= LYS_KEY;
if ((prev_key && ((struct lysc_node *)prev_key != key->prev)) || (!prev_key && key->prev->next)) {
if (key->next) {
key->next->prev = key->prev;
} else {
list->child->prev = key->prev;
}
if (key->prev->next) {
key->prev->next = key->next;
}
if (prev_key) {
key->prev = &prev_key->node;
key->next = prev_key->next;
} else {
key->prev = list->child->prev;
key->next = list->child;
}
if (prev_key) {
if (prev_key->next) {
prev_key->next->prev = &key->node;
} else {
list->child->prev = &key->node;
}
prev_key->next = &key->node;
} else {
list->child->prev = &key->node;
}
if (!key->prev->next) {
list->child = &key->node;
}
}
prev_key = key;
keystr = delim;
lysc_update_path(ctx, NULL, NULL);
}
LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
if (list_p->uniques) {
LY_CHECK_RET(lys_compile_node_list_unique(ctx, list_p->uniques, list));
}
LY_LIST_FOR((struct lysp_node *)list_p->actions, child_p) {
ret = lys_compile_node(ctx, child_p, node, 0, NULL);
LY_CHECK_GOTO(ret, done);
}
LY_LIST_FOR((struct lysp_node *)list_p->notifs, child_p) {
ret = lys_compile_node(ctx, child_p, node, 0, NULL);
LY_CHECK_GOTO(ret, done);
}
if (list->min > list->max) {
LOGVAL(ctx->ctx, NULL, LYVE_SEMANTICS, "List min-elements %" PRIu32 " is bigger than max-elements %" PRIu32 ".",
list->min, list->max);
return LY_EVALID;
}
done:
return ret;
}
static LY_ERR
lys_compile_node_choice_dflt(struct lysc_ctx *ctx, struct lysp_qname *dflt, struct lysc_node_choice *ch)
{
struct lysc_node *iter;
const struct lys_module *mod;
const char *prefix = NULL, *name;
size_t prefix_len = 0;
name = strchr(dflt->str, ':');
if (name) {
prefix = dflt->str;
prefix_len = name - prefix;
++name;
} else {
name = dflt->str;
}
if (prefix) {
mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, (void *)dflt->mod);
if (!mod) {
LOGVAL(ctx->ctx, NULL, LYVE_REFERENCE, "Default case prefix \"%.*s\" not found "
"in imports of \"%s\".", (int)prefix_len, prefix, LYSP_MODULE_NAME(dflt->mod));
return LY_EVALID;
}
} else {
mod = ch->module;
}
ch->dflt = (struct lysc_node_case *)lys_find_child(NULL, &ch->node, mod, NULL, 0, name, 0, LYS_GETNEXT_WITHCASE);
if (ch->dflt && (ch->dflt->nodetype != LYS_CASE)) {
ch->dflt = NULL;
}
if (!ch->dflt) {
LOGVAL(ctx->ctx, NULL, LYVE_SEMANTICS, "Default case \"%s\" not found.", dflt->str);
return LY_EVALID;
}
LY_LIST_FOR(ch->dflt->child, iter) {
if (iter->parent != (struct lysc_node *)ch->dflt) {
break;
}
if (iter->flags & LYS_MAND_TRUE) {
LOGVAL(ctx->ctx, NULL, LYVE_SEMANTICS,
"Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt->str);
return LY_EVALID;
}
}
if (ch->flags & LYS_MAND_TRUE) {
LOGVAL(ctx->ctx, NULL, LYVE_SEMANTICS, "Invalid mandatory choice with a default case.");
return LY_EVALID;
}
ch->dflt->flags |= LYS_SET_DFLT;
return LY_SUCCESS;
}
LY_ERR
lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node,
struct ly_set *child_set)
{
LY_ERR ret = LY_SUCCESS;
struct lysp_node *child_p_next = child_p->next;
struct lysp_node_case *cs_p;
struct lysc_node_case *cs_c;
if (child_p->nodetype == LYS_CASE) {
ret = lys_compile_node(ctx, child_p, node, 0, child_set);
} else {
cs_p = calloc(1, sizeof *cs_p);
LY_CHECK_ERR_RET(!cs_p, LOGMEM(ctx->ctx), LY_EMEM);
cs_p->nodetype = LYS_CASE;
DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, revert_sh_case);
cs_p->child = child_p;
child_p->next = NULL;
LY_CHECK_GOTO(ret = lys_compile_node(ctx, (struct lysp_node *)cs_p, node, 0, child_set), revert_sh_case);
if (((struct lysc_node_choice *)node)->cases) {
cs_c = (struct lysc_node_case *)((struct lysc_node_choice *)node)->cases;
while (cs_c->name != cs_p->name) {
cs_c = (struct lysc_node_case *)cs_c->next;
assert(cs_c);
}
if (ctx->ctx->opts & LY_CTX_SET_PRIV_PARSED) {
assert(cs_c->priv == cs_p);
cs_c->priv = NULL;
}
if (cs_c->child) {
cs_c->flags &= ~LYS_STATUS_MASK;
cs_c->flags |= (LYS_STATUS_MASK & cs_c->child->flags);
}
}
revert_sh_case:
cs_p->child = NULL;
lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p);
child_p->next = child_p_next;
}
return ret;
}
static LY_ERR
lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
{
struct lysp_node_choice *ch_p = (struct lysp_node_choice *)pnode;
struct lysc_node_choice *ch = (struct lysc_node_choice *)node;
struct lysp_node *child_p;
LY_ERR ret = LY_SUCCESS;
assert(node->nodetype == LYS_CHOICE);
LY_LIST_FOR(ch_p->child, child_p) {
LY_CHECK_GOTO(ret = lys_compile_node_choice_child(ctx, child_p, node, NULL), done);
}
LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
if (ch_p->dflt.str) {
LY_CHECK_GOTO(ret = lys_compile_node_choice_dflt(ctx, &ch_p->dflt, ch), done);
}
done:
return ret;
}
static LY_ERR
lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
{
struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)pnode;
struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
LY_ERR ret = LY_SUCCESS;
COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, lys_compile_must, ret, done);
ret = lysc_unres_must_add(ctx, node, pnode);
LY_CHECK_GOTO(ret, done);
if (any->flags & LYS_CONFIG_W) {
LOGVRB("Use of %s to define configuration data is not recommended. %s",
lyplg_ext_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
}
done:
return ret;
}
static LY_ERR
lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
{
LY_ERR ret = LY_SUCCESS;
struct lysp_node *child_p;
struct lysp_node_case *cs_p = (struct lysp_node_case *)pnode;
if (pnode->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
} else if (pnode->nodetype == LYS_CASE) {
LY_LIST_FOR(cs_p->child, child_p) {
LY_CHECK_GOTO(ret = lys_compile_node(ctx, child_p, node, 0, NULL), done);
}
} else {
LOGINT_RET(ctx->ctx);
}
LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
done:
return ret;
}
void
lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add)
{
const struct lysc_node *iter;
if (add) {
for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
parent = parent->parent) {
parent->flags |= LYS_MAND_TRUE;
}
} else {
for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
for (iter = lysc_node_child(parent); iter; iter = iter->next) {
if (iter->flags & LYS_MAND_TRUE) {
return;
}
}
parent->flags &= ~LYS_MAND_TRUE;
}
}
}
static struct lysp_node_grp *
match_grouping(const struct lysp_node_grp *node, const char *name)
{
LY_LIST_FOR(node, node) {
if ((node->nodetype == LYS_GROUPING) && !strcmp(node->name, name)) {
return (struct lysp_node_grp *)node;
}
}
return NULL;
}
static LY_ERR
lys_compile_uses_find_grouping(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysp_node_grp **grp_p,
struct lysp_module **grp_pmod)
{
struct lysp_node *pnode;
struct lysp_node_grp *grp;
const struct lysp_node_grp *ext_grp;
LY_ARRAY_COUNT_TYPE u;
const char *id, *name, *prefix, *local_pref;
uint32_t prefix_len, name_len;
struct lysp_module *pmod, *found = NULL;
const struct lys_module *mod;
*grp_p = NULL;
*grp_pmod = NULL;
id = uses_p->name;
LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
local_pref = ctx->pmod->is_submod ? ((struct lysp_submodule *)ctx->pmod)->prefix : ctx->pmod->mod->prefix;
if (!prefix || !ly_strncmp(local_pref, prefix, prefix_len)) {
pmod = ctx->pmod->mod->parsed;
for (pnode = uses_p->parent; !found && pnode; pnode = pnode->parent) {
if ((grp = match_grouping(lysp_node_groupings(pnode), name))) {
found = ctx->pmod;
break;
}
}
if (!found && ctx->ext) {
lyplg_ext_parsed_get_storage(ctx->ext, LY_STMT_GROUPING, sizeof ext_grp, (const void **)&ext_grp);
if ((grp = match_grouping(ext_grp, name))) {
found = ctx->pmod;
}
}
} else {
mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, ctx->pmod);
if (!mod) {
LOGVAL(ctx->ctx, NULL, LYVE_REFERENCE, "Invalid prefix used for grouping \"%s\" reference.", uses_p->name);
return LY_EVALID;
}
pmod = mod->parsed;
}
if (!found) {
if ((grp = match_grouping(pmod->groupings, name))) {
found = pmod;
} else {
LY_ARRAY_FOR(pmod->includes, u) {
if ((grp = match_grouping(pmod->includes[u].submodule->groupings, name))) {
found = (struct lysp_module *)pmod->includes[u].submodule;
break;
}
}
}
}
if (!found) {
LOGVAL(ctx->ctx, NULL, LYVE_SEMANTICS, "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
return LY_EVALID;
}
if (!(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
grp->flags |= LYS_USED_GRP;
}
*grp_p = grp;
*grp_pmod = found;
return LY_SUCCESS;
}
static LY_ERR
lys_compile_uses_children(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, uint16_t inherited_flags,
struct lysp_node *child, struct lysp_module *grp_mod, struct lysc_node *parent, struct ly_set *child_set,
ly_bool child_unres_disabled)
{
LY_ERR rc = LY_SUCCESS;
struct lysp_module *mod_old = ctx->pmod;
uint32_t child_i, opt_prev = ctx->compile_opts;
ly_bool enabled;
struct lysp_node *pnode;
struct lysc_node *node;
struct lysc_when *when_shared = NULL;
assert(child_set);
child_i = child_set->count;
LY_LIST_FOR(child, pnode) {
ctx->pmod = grp_mod;
LY_CHECK_GOTO(rc = lys_compile_node(ctx, pnode, parent, inherited_flags, child_set), cleanup);
LY_CHECK_GOTO(rc = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
ctx->compile_opts |= LYS_COMPILE_DISABLED;
}
ctx->pmod = mod_old;
while (child_i < child_set->count) {
node = child_set->snodes[child_i];
if (uses_p->when) {
rc = lys_compile_when(ctx, uses_p->when, inherited_flags, parent, lysc_data_node(parent), node, &when_shared);
LY_CHECK_GOTO(rc, cleanup);
}
if (child_unres_disabled) {
ly_set_add(&ctx->unres->disabled, node, 1, NULL);
}
++child_i;
}
ctx->compile_opts = opt_prev;
}
cleanup:
ctx->compile_opts = opt_prev;
return rc;
}
static LY_ERR
lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, uint16_t inherited_flags,
struct ly_set *child_set)
{
LY_ERR rc = LY_SUCCESS;
ly_bool enabled, child_unres_disabled = 0;
uint32_t i, grp_stack_count, opt_prev = ctx->compile_opts;
struct lysp_node_grp *grp = NULL;
uint16_t uses_flags = 0;
struct lysp_module *grp_mod;
struct ly_set uses_child_set = {0};
LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod));
grp_stack_count = ctx->groupings.count;
LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL));
if (grp_stack_count == ctx->groupings.count) {
LOGVAL(ctx->ctx, NULL, LYVE_REFERENCE,
"Grouping \"%s\" references itself through a uses statement.", grp->name);
return LY_EVALID;
}
if (grp->actions && (parent && !lysc_node_actions_p(parent))) {
LOGVAL(ctx->ctx, NULL, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
grp->actions->name, lys_nodetype2str(grp->actions->nodetype),
parent->name, lys_nodetype2str(parent->nodetype));
rc = LY_EVALID;
goto cleanup;
}
if (grp->notifs && (parent && !lysc_node_notifs_p(parent))) {
LOGVAL(ctx->ctx, NULL, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
grp->notifs->name, lys_nodetype2str(grp->notifs->nodetype),
parent->name, lys_nodetype2str(parent->nodetype));
rc = LY_EVALID;
goto cleanup;
}
rc = lysc_check_status(ctx, NULL, uses_p->flags, ctx->pmod, uses_p->name, grp->flags, grp_mod, grp->name);
LY_CHECK_GOTO(rc, cleanup);
rc = lys_precompile_uses_augments_refines(ctx, uses_p, parent);
LY_CHECK_GOTO(rc, cleanup);
rc = lys_compile_status(ctx, uses_p->flags, inherited_flags, parent ? parent->flags : 0,
parent ? parent->name : NULL, "<uses>", &uses_flags);
LY_CHECK_GOTO(rc, cleanup);
LY_CHECK_GOTO(rc = lys_eval_iffeatures(ctx->ctx, uses_p->iffeatures, &enabled), cleanup);
if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
ctx->compile_opts |= LYS_COMPILE_DISABLED;
child_unres_disabled = 1;
}
rc = lys_compile_uses_children(ctx, uses_p, uses_flags, grp->child, grp_mod, parent,
child_set ? child_set : &uses_child_set, child_unres_disabled);
LY_CHECK_GOTO(rc, cleanup);
rc = lys_compile_uses_children(ctx, uses_p, uses_flags, (struct lysp_node *)grp->actions, grp_mod, parent,
child_set ? child_set : &uses_child_set, child_unres_disabled);
LY_CHECK_GOTO(rc, cleanup);
rc = lys_compile_uses_children(ctx, uses_p, uses_flags, (struct lysp_node *)grp->notifs, grp_mod, parent,
child_set ? child_set : &uses_child_set, child_unres_disabled);
LY_CHECK_GOTO(rc, cleanup);
for (i = 0; i < ctx->uses_augs.count; ++i) {
if (((struct lysc_augment *)ctx->uses_augs.objs[i])->aug_p->parent != (struct lysp_node *)uses_p) {
continue;
}
LOGVAL(ctx->ctx, NULL, LYVE_REFERENCE, "Augment target node \"%s\" in grouping \"%s\" was not found.",
((struct lysc_augment *)ctx->uses_augs.objs[i])->nodeid->str, grp->name);
rc = LY_ENOTFOUND;
}
LY_CHECK_GOTO(rc, cleanup);
for (i = 0; i < ctx->uses_rfns.count; ++i) {
if (((struct lysc_refine *)ctx->uses_rfns.objs[i])->uses_p != uses_p) {
continue;
}
LOGVAL(ctx->ctx, NULL, LYVE_REFERENCE, "Refine(s) target node \"%s\" in grouping \"%s\" was not found.",
((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid->str, grp->name);
rc = LY_ENOTFOUND;
}
LY_CHECK_GOTO(rc, cleanup);
COMPILE_EXTS_GOTO(ctx, uses_p->exts, parent->exts, parent, rc, cleanup);
COMPILE_EXTS_GOTO(ctx, grp->exts, parent->exts, parent, rc, cleanup);
cleanup:
ctx->compile_opts = opt_prev;
ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
assert(ctx->groupings.count == grp_stack_count);
ly_set_erase(&uses_child_set, NULL);
return rc;
}
static int
lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
{
struct lysp_node *iter;
int len = 0, r;
char *s, *id;
*path = NULL;
for (iter = node; iter && (len >= 0); iter = iter->parent) {
s = *path;
switch (iter->nodetype) {
case LYS_USES:
r = asprintf(&id, "{uses='%s'}", iter->name);
break;
case LYS_GROUPING:
r = asprintf(&id, "{grouping='%s'}", iter->name);
break;
case LYS_AUGMENT:
r = asprintf(&id, "{augment='%s'}", iter->name);
break;
default:
id = strdup(iter->name);
r = id ? 1 : -1;
break;
}
if (r == -1) {
len = -1;
goto cleanup;
}
if (!iter->parent) {
r = asprintf(path, "/%s:%s%s", ctx->cur_mod->name, id, s ? s : "");
} else {
r = asprintf(path, "/%s%s", id, s ? s : "");
}
free(s);
free(id);
if (r == -1) {
len = -1;
goto cleanup;
}
len = r;
}
if (!len) {
*path = strdup("/");
if (!*path) {
len = -1;
goto cleanup;
}
len = 1;
}
cleanup:
if (len < 0) {
free(*path);
*path = NULL;
}
return len;
}
LY_ERR
lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysp_node_grp *grp)
{
LY_ERR rc = LY_SUCCESS;
char *path;
int len;
struct lysp_node_uses fake_uses = {
.parent = pnode,
.nodetype = LYS_USES,
.flags = grp->flags & LYS_STATUS_MASK, .next = NULL,
.name = grp->name,
.dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
.refines = NULL, .augments = NULL
};
struct lysc_node_container fake_container = {
.nodetype = LYS_CONTAINER,
.flags = 0,
.module = ctx->cur_mod,
.parent = NULL, .next = NULL,
.prev = &fake_container.node,
.name = "fake",
.dsc = NULL, .ref = NULL, .exts = NULL, .when = NULL,
.child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
};
LY_CHECK_GOTO(rc = lys_compile_node_flags(ctx, pnode ? pnode->flags : 0, 0, &fake_container.node), cleanup);
if (grp->parent) {
LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
}
len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
if (len < 0) {
LOGMEM(ctx->ctx);
return LY_EMEM;
}
strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
ctx->path_len = (uint32_t)len;
free(path);
lysc_update_path(ctx, NULL, "{grouping}");
lysc_update_path(ctx, NULL, grp->name);
rc = lys_compile_uses(ctx, &fake_uses, &fake_container.node, 0, NULL);
lysc_update_path(ctx, NULL, NULL);
lysc_update_path(ctx, NULL, NULL);
ctx->path_len = 1;
ctx->path[1] = '\0';
cleanup:
lysc_node_container_free(ctx->ctx, &fake_container);
FREE_ARRAY(ctx->ctx, fake_container.exts, lysc_ext_instance_free);
return rc;
}
LY_ERR
lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t inherited_flags,
struct ly_set *child_set)
{
LY_ERR ret = LY_SUCCESS;
struct lysc_node *node = NULL;
uint32_t prev_opts = ctx->compile_opts;
LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *);
if (pnode->nodetype != LYS_USES) {
lysc_update_path(ctx, parent ? parent->module : NULL, pnode->name);
} else {
lysc_update_path(ctx, NULL, "{uses}");
lysc_update_path(ctx, NULL, pnode->name);
}
switch (pnode->nodetype) {
case LYS_CONTAINER:
node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container));
node_compile_spec = lys_compile_node_container;
break;
case LYS_LEAF:
node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf));
node_compile_spec = lys_compile_node_leaf;
break;
case LYS_LIST:
node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list));
node_compile_spec = lys_compile_node_list;
break;
case LYS_LEAFLIST:
node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist));
node_compile_spec = lys_compile_node_leaflist;
break;
case LYS_CHOICE:
node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice));
node_compile_spec = lys_compile_node_choice;
break;
case LYS_CASE:
node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case));
node_compile_spec = lys_compile_node_case;
break;
case LYS_ANYXML:
case LYS_ANYDATA:
node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata));
node_compile_spec = lys_compile_node_any;
break;
case LYS_RPC:
case LYS_ACTION:
if (ctx->compile_opts & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
LOGVAL(ctx->ctx, NULL, LYVE_SEMANTICS,
"Action \"%s\" is placed inside %s.", pnode->name,
(ctx->compile_opts & LYS_IS_NOTIF) ? "notification" : "another RPC/action");
return LY_EVALID;
}
node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_action));
node_compile_spec = lys_compile_node_action;
ctx->compile_opts |= LYS_COMPILE_NO_CONFIG;
break;
case LYS_NOTIF:
if (ctx->compile_opts & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
LOGVAL(ctx->ctx, NULL, LYVE_SEMANTICS,
"Notification \"%s\" is placed inside %s.", pnode->name,
(ctx->compile_opts & LYS_IS_NOTIF) ? "another notification" : "RPC/action");
return LY_EVALID;
}
node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_notif));
node_compile_spec = lys_compile_node_notif;
ctx->compile_opts |= LYS_COMPILE_NOTIFICATION;
break;
case LYS_USES:
ret = lys_compile_uses(ctx, (struct lysp_node_uses *)pnode, parent, inherited_flags, child_set);
lysc_update_path(ctx, NULL, NULL);
lysc_update_path(ctx, NULL, NULL);
return ret;
default:
LOGINT(ctx->ctx);
return LY_EINT;
}
LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
ret = lys_compile_node_(ctx, pnode, parent, inherited_flags, node_compile_spec, node, child_set);
ctx->compile_opts = prev_opts;
lysc_update_path(ctx, NULL, NULL);
return ret;
}