schema tree REFACTOR evaluate features during compilation

So that the compiled schema tree reflects the state
of all the features.
diff --git a/src/schema_compile.c b/src/schema_compile.c
index 24bc995..e32c84b 100644
--- a/src/schema_compile.c
+++ b/src/schema_compile.c
@@ -37,6 +37,7 @@
 #include "plugins_types.h"
 #include "schema_compile_amend.h"
 #include "schema_compile_node.h"
+#include "schema_features.h"
 #include "set.h"
 #include "tree.h"
 #include "tree_data.h"
@@ -45,16 +46,6 @@
 #include "tree_schema_internal.h"
 #include "xpath.h"
 
-#define COMPILE_CHECK_UNIQUENESS_ARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
-    if (ARRAY) { \
-        for (LY_ARRAY_COUNT_TYPE u__ = 0; u__ < LY_ARRAY_COUNT(ARRAY); ++u__) { \
-            if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
-                LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
-                return LY_EVALID; \
-            } \
-        } \
-    }
-
 /**
  * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
  */
@@ -317,319 +308,6 @@
 }
 
 /**
- * @brief Stack for processing if-feature expressions.
- */
-struct iff_stack {
-    size_t size;    /**< number of items in the stack */
-    size_t index;   /**< first empty item */
-    uint8_t *stack; /**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
-};
-
-/**
- * @brief Add @ref ifftokens into the stack.
- * @param[in] stack The if-feature stack to use.
- * @param[in] value One of the @ref ifftokens to store in the stack.
- * @return LY_EMEM in case of memory allocation error
- * @return LY_ESUCCESS if the value successfully stored.
- */
-static LY_ERR
-iff_stack_push(struct iff_stack *stack, uint8_t value)
-{
-    if (stack->index == stack->size) {
-        stack->size += 4;
-        stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
-        LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
-    }
-    stack->stack[stack->index++] = value;
-    return LY_SUCCESS;
-}
-
-/**
- * @brief Get (and remove) the last item form the stack.
- * @param[in] stack The if-feature stack to use.
- * @return The value from the top of the stack.
- */
-static uint8_t
-iff_stack_pop(struct iff_stack *stack)
-{
-    assert(stack && stack->index);
-
-    stack->index--;
-    return stack->stack[stack->index];
-}
-
-/**
- * @brief Clean up the stack.
- * @param[in] stack The if-feature stack to use.
- */
-static void
-iff_stack_clean(struct iff_stack *stack)
-{
-    stack->size = 0;
-    free(stack->stack);
-}
-
-/**
- * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
- * (libyang format of the if-feature expression).
- * @param[in,out] list The 2bits array to modify.
- * @param[in] op The operand (@ref ifftokens) to store.
- * @param[in] pos Position (0-based) where to store the given @p op.
- */
-static void
-iff_setop(uint8_t *list, uint8_t op, size_t pos)
-{
-    uint8_t *item;
-    uint8_t mask = 3;
-
-    assert(op <= 3); /* max 2 bits */
-
-    item = &list[pos / 4];
-    mask = mask << 2 * (pos % 4);
-    *item = (*item) & ~mask;
-    *item = (*item) | (op << 2 * (pos % 4));
-}
-
-#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
-#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
-
-/**
- * @brief Find a feature of the given name and referenced in the given module.
- *
- * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
- * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
- * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
- * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
- * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
- * assigned till that time will be still valid.
- *
- * @param[in] pmod Module where the feature was referenced (used to resolve prefix of the feature).
- * @param[in] name Name of the feature including possible prefix.
- * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
- * @return Pointer to the feature structure if found, NULL otherwise.
- */
-static struct lysc_feature *
-lys_feature_find(const struct lysp_module *pmod, const char *name, size_t len)
-{
-    LY_ARRAY_COUNT_TYPE u;
-    struct lysc_feature *f;
-    const struct lys_module *mod;
-    const char *ptr;
-
-    assert(pmod);
-
-    if ((ptr = ly_strnchr(name, ':', len))) {
-        /* we have a prefixed feature */
-        mod = lysp_module_find_prefix(pmod, name, ptr - name);
-        LY_CHECK_RET(!mod, NULL);
-
-        len = len - (ptr - name) - 1;
-        name = ptr + 1;
-    } else {
-        /* local feature */
-        mod = pmod->mod;
-    }
-
-    /* we have the correct module, get the feature */
-    LY_ARRAY_FOR(mod->features, u) {
-        f = &mod->features[u];
-        if (!ly_strncmp(f->name, name, len)) {
-            return f;
-        }
-    }
-
-    return NULL;
-}
-
-LY_ERR
-lys_compile_iffeature(struct lysc_ctx *ctx, struct lysp_qname *qname, struct lysc_iffeature *iff)
-{
-    LY_ERR rc = LY_SUCCESS;
-    const char *c = qname->str;
-    int64_t i, j;
-    int8_t op_len, last_not = 0, checkversion = 0;
-    LY_ARRAY_COUNT_TYPE f_size = 0, expr_size = 0, f_exp = 1;
-    uint8_t op;
-    struct iff_stack stack = {0, 0, NULL};
-    struct lysc_feature *f;
-
-    assert(c);
-
-    /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
-    for (i = j = 0; c[i]; i++) {
-        if (c[i] == '(') {
-            j++;
-            checkversion = 1;
-            continue;
-        } else if (c[i] == ')') {
-            j--;
-            continue;
-        } else if (isspace(c[i])) {
-            checkversion = 1;
-            continue;
-        }
-
-        if (!strncmp(&c[i], "not", op_len = 3) || !strncmp(&c[i], "and", op_len = 3) || !strncmp(&c[i], "or", op_len = 2)) {
-            uint64_t spaces;
-            for (spaces = 0; c[i + op_len + spaces] && isspace(c[i + op_len + spaces]); spaces++) {}
-            if (c[i + op_len + spaces] == '\0') {
-                LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
-                        "Invalid value \"%s\" of if-feature - unexpected end of expression.", qname->str);
-                return LY_EVALID;
-            } else if (!isspace(c[i + op_len])) {
-                /* feature name starting with the not/and/or */
-                last_not = 0;
-                f_size++;
-            } else if (c[i] == 'n') { /* not operation */
-                if (last_not) {
-                    /* double not */
-                    expr_size = expr_size - 2;
-                    last_not = 0;
-                } else {
-                    last_not = 1;
-                }
-            } else { /* and, or */
-                if (f_exp != f_size) {
-                    LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
-                            "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.",
-                            qname->str, op_len, &c[i]);
-                    return LY_EVALID;
-                }
-                f_exp++;
-
-                /* not a not operation */
-                last_not = 0;
-            }
-            i += op_len;
-        } else {
-            f_size++;
-            last_not = 0;
-        }
-        expr_size++;
-
-        while (!isspace(c[i])) {
-            if (!c[i] || (c[i] == ')') || (c[i] == '(')) {
-                i--;
-                break;
-            }
-            i++;
-        }
-    }
-    if (j) {
-        /* not matching count of ( and ) */
-        LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
-                "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", qname->str);
-        return LY_EVALID;
-    }
-    if (f_exp != f_size) {
-        /* features do not match the needed arguments for the logical operations */
-        LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
-                "Invalid value \"%s\" of if-feature - number of features in expression does not match "
-                "the required number of operands for the operations.", qname->str);
-        return LY_EVALID;
-    }
-
-    if (checkversion || (expr_size > 1)) {
-        /* check that we have 1.1 module */
-        if (qname->mod->version != LYS_VERSION_1_1) {
-            LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
-                    "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", qname->str);
-            return LY_EVALID;
-        }
-    }
-
-    /* allocate the memory */
-    LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
-    iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
-    stack.stack = malloc(expr_size * sizeof *stack.stack);
-    LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx); rc = LY_EMEM, error);
-
-    stack.size = expr_size;
-    f_size--; expr_size--; /* used as indexes from now */
-
-    for (i--; i >= 0; i--) {
-        if (c[i] == ')') {
-            /* push it on stack */
-            iff_stack_push(&stack, LYS_IFF_RP);
-            continue;
-        } else if (c[i] == '(') {
-            /* pop from the stack into result all operators until ) */
-            while ((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
-                iff_setop(iff->expr, op, expr_size--);
-            }
-            continue;
-        } else if (isspace(c[i])) {
-            continue;
-        }
-
-        /* end of operator or operand -> find beginning and get what is it */
-        j = i + 1;
-        while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
-            i--;
-        }
-        i++; /* go back by one step */
-
-        if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
-            if (stack.index && (stack.stack[stack.index - 1] == LYS_IFF_NOT)) {
-                /* double not */
-                iff_stack_pop(&stack);
-            } else {
-                /* not has the highest priority, so do not pop from the stack
-                 * as in case of AND and OR */
-                iff_stack_push(&stack, LYS_IFF_NOT);
-            }
-        } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
-            /* as for OR - pop from the stack all operators with the same or higher
-             * priority and store them to the result, then push the AND to the stack */
-            while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
-                op = iff_stack_pop(&stack);
-                iff_setop(iff->expr, op, expr_size--);
-            }
-            iff_stack_push(&stack, LYS_IFF_AND);
-        } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
-            while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
-                op = iff_stack_pop(&stack);
-                iff_setop(iff->expr, op, expr_size--);
-            }
-            iff_stack_push(&stack, LYS_IFF_OR);
-        } else {
-            /* feature name, length is j - i */
-
-            /* add it to the expression */
-            iff_setop(iff->expr, LYS_IFF_F, expr_size--);
-
-            /* now get the link to the feature definition */
-            f = lys_feature_find(qname->mod, &c[i], j - i);
-            LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
-                    "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", qname->str, j - i, &c[i]);
-                    rc = LY_EVALID, error)
-            iff->features[f_size] = f;
-            LY_ARRAY_INCREMENT(iff->features);
-            f_size--;
-        }
-    }
-    while (stack.index) {
-        op = iff_stack_pop(&stack);
-        iff_setop(iff->expr, op, expr_size--);
-    }
-
-    if (++expr_size || ++f_size) {
-        /* not all expected operators and operands found */
-        LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
-                "Invalid value \"%s\" of if-feature - processing error.", qname->str);
-        rc = LY_EINT;
-    } else {
-        rc = LY_SUCCESS;
-    }
-
-error:
-    /* cleanup */
-    iff_stack_clean(&stack);
-
-    return rc;
-}
-
-/**
  * @brief Compile information in the import statement - make sure there is the target module
  * @param[in] ctx Compile context.
  * @param[in] imp_p The parsed import statement structure to fill the module to.
@@ -652,7 +330,7 @@
                 LOGINT(ctx->ctx);
             } else {
                 LY_CHECK_RET(lys_parse(ctx->ctx, in, !strcmp(&imp_p->module->filepath[strlen(imp_p->module->filepath - 4)],
-                        ".yin") ? LYS_IN_YIN : LYS_IN_YANG, &mod));
+                        ".yin") ? LYS_IN_YIN : LYS_IN_YANG, NULL, &mod));
                 if (mod != imp_p->module) {
                     LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
                             imp_p->module->filepath, imp_p->module->name);
@@ -662,7 +340,7 @@
             ly_in_free(in, 1);
         }
         if (!mod) {
-            if (lysp_load_module(ctx->ctx, imp_p->module->name, imp_p->module->revision, 0, 1, (struct lys_module **)&mod)) {
+            if (lysp_load_module(ctx->ctx, imp_p->module->name, imp_p->module->revision, 0, 1, NULL, (struct lys_module **)&mod)) {
                 LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.",
                         imp_p->module->name, ctx->cur_mod->name);
                 return LY_ENOTFOUND;
@@ -677,9 +355,11 @@
 lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lysp_module *parsed_mod,
         struct lysp_ident *identities_p, struct lysc_ident **identities)
 {
-    LY_ARRAY_COUNT_TYPE offset = 0, u, v;
+    LY_ARRAY_COUNT_TYPE u;
     struct lysc_ctx context = {0};
+    struct lysc_ident *ident;
     LY_ERR ret = LY_SUCCESS;
+    ly_bool enabled;
 
     assert(ctx_sc || ctx);
 
@@ -695,27 +375,27 @@
     if (!identities_p) {
         return LY_SUCCESS;
     }
-    if (*identities) {
-        offset = LY_ARRAY_COUNT(*identities);
-    }
 
     lysc_update_path(ctx_sc, NULL, "{identity}");
-    LY_ARRAY_CREATE_RET(ctx_sc->ctx, *identities, LY_ARRAY_COUNT(identities_p), LY_EMEM);
     LY_ARRAY_FOR(identities_p, u) {
+        /* evaluate if-features */
+        LY_CHECK_RET(lys_eval_iffeatures(ctx, identities_p[u].iffeatures, &enabled));
+        if (!enabled) {
+            continue;
+        }
+
         lysc_update_path(ctx_sc, NULL, identities_p[u].name);
 
-        LY_ARRAY_INCREMENT(*identities);
-        COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *identities, name, &(*identities)[offset + u], "identity", identities_p[u].name);
-        DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].name, (*identities)[offset + u].name, ret, done);
-        DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].dsc, (*identities)[offset + u].dsc, ret, done);
-        DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].ref, (*identities)[offset + u].ref, ret, done);
-        (*identities)[offset + u].module = ctx_sc->cur_mod;
-        COMPILE_ARRAY_GOTO(ctx_sc, identities_p[u].iffeatures, (*identities)[offset + u].iffeatures, v,
-                lys_compile_iffeature, ret, done);
+        /* add new compiled identity */
+        LY_ARRAY_NEW_RET(ctx_sc->ctx, *identities, ident, LY_EMEM);
+
+        DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].name, ident->name, ret, done);
+        DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].dsc, ident->dsc, ret, done);
+        DUP_STRING_GOTO(ctx_sc->ctx, identities_p[u].ref, ident->ref, ret, done);
+        ident->module = ctx_sc->cur_mod;
         /* backlinks (derived) can be added no sooner than when all the identities in the current module are present */
-        COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, (*identities)[offset + u].exts, &(*identities)[offset + u],
-                LYEXT_PAR_IDENT, ret, done);
-        (*identities)[offset + u].flags = identities_p[u].flags;
+        COMPILE_EXTS_GOTO(ctx_sc, identities_p[u].exts, ident->exts, ident, LYEXT_PAR_IDENT, ret, done);
+        ident->flags = identities_p[u].flags;
 
         lysc_update_path(ctx_sc, NULL, NULL);
     }
@@ -782,14 +462,14 @@
 
 LY_ERR
 lys_compile_identity_bases(struct lysc_ctx *ctx, const struct lysp_module *base_pmod, const char **bases_p,
-        struct lysc_ident *ident, struct lysc_ident ***bases)
+        struct lysc_ident *ident, struct lysc_ident ***bases, ly_bool *enabled)
 {
     LY_ARRAY_COUNT_TYPE u, v;
     const char *s, *name;
     const struct lys_module *mod;
     struct lysc_ident **idref;
 
-    assert(ident || bases);
+    assert((ident && enabled) || bases);
 
     if ((LY_ARRAY_COUNT(bases_p) > 1) && (ctx->pmod->version < LYS_VERSION_1_1)) {
         LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
@@ -841,6 +521,13 @@
         }
         if (!idref || !(*idref)) {
             if (ident) {
+                /* look into the parsed module to check whether the identity is not merely disabled */
+                LY_ARRAY_FOR(mod->parsed->identities, v) {
+                    if (!strcmp(mod->parsed->identities[v].name, name)) {
+                        *enabled = 0;
+                        return LY_SUCCESS;
+                    }
+                }
                 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
                         "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
             } else {
@@ -850,6 +537,10 @@
             return LY_EVALID;
         }
     }
+
+    if (ident) {
+        *enabled = 1;
+    }
     return LY_SUCCESS;
 }
 
@@ -857,263 +548,63 @@
  * @brief For the given array of identities, set the backlinks from all their base identities.
  * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
  * @param[in] idents_p Array of identities definitions from the parsed schema structure.
- * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
+ * @param[in,out] idents Array of referencing identities to which the backlinks are supposed to be set. Any
+ * identities with disabled bases are removed.
  * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
  */
 static LY_ERR
-lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
+lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident **idents)
 {
-    LY_ARRAY_COUNT_TYPE u;
+    LY_ARRAY_COUNT_TYPE u, v;
+    ly_bool enabled;
 
     lysc_update_path(ctx, NULL, "{identity}");
-    for (u = 0; u < LY_ARRAY_COUNT(idents_p); ++u) {
-        if (!idents_p[u].bases) {
+
+restart:
+    for (u = 0, v = 0; *idents && (u < LY_ARRAY_COUNT(*idents)); ++u) {
+        /* find matching parsed identity, the disabled ones are missing in the compiled array */
+        while (v < LY_ARRAY_COUNT(idents_p)) {
+            if (idents_p[v].name == (*idents)[u].name) {
+                break;
+            }
+            ++v;
+        }
+        assert(v < LY_ARRAY_COUNT(idents_p));
+
+        if (!idents_p[v].bases) {
             continue;
         }
-        lysc_update_path(ctx, NULL, idents[u].name);
-        LY_CHECK_RET(lys_compile_identity_bases(ctx, idents[u].module->parsed, idents_p[u].bases, &idents[u], NULL));
+        lysc_update_path(ctx, NULL, (*idents)[u].name);
+        LY_CHECK_RET(lys_compile_identity_bases(ctx, (*idents)[u].module->parsed, idents_p[v].bases, &(*idents)[u], NULL,
+                &enabled));
         lysc_update_path(ctx, NULL, NULL);
+
+        if (!enabled) {
+            /* remove the identity */
+            lysc_ident_free(ctx->ctx, &(*idents)[u]);
+            LY_ARRAY_DECREMENT(*idents);
+            if (u < LY_ARRAY_COUNT(*idents)) {
+                memmove(&(*idents)[u], &(*idents)[u + 1], (LY_ARRAY_COUNT(*idents) - u) * sizeof **idents);
+            }
+            if (!LY_ARRAY_COUNT(*idents)) {
+                LY_ARRAY_FREE(*idents);
+                *idents = NULL;
+            }
+
+            /* revert compilation of all the previous identities */
+            for (v = 0; v < u; ++v) {
+                LY_ARRAY_FREE((*idents)[v].derived);
+            }
+
+            /* restart the whole process without this identity */
+            goto restart;
+        }
     }
+
     lysc_update_path(ctx, NULL, NULL);
     return LY_SUCCESS;
 }
 
-LY_ERR
-lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lysp_module *parsed_mod,
-        struct lysp_feature *features_p, struct lysc_feature **features)
-{
-    LY_ERR ret = LY_SUCCESS;
-    LY_ARRAY_COUNT_TYPE offset = 0, u;
-    struct lysc_ctx context = {0};
-
-    assert(ctx_sc || ctx);
-
-    if (!ctx_sc) {
-        context.ctx = ctx;
-        context.cur_mod = parsed_mod->mod;
-        context.pmod = parsed_mod;
-        context.path_len = 1;
-        context.path[0] = '/';
-        ctx_sc = &context;
-    }
-
-    if (!features_p) {
-        return LY_SUCCESS;
-    }
-    if (*features) {
-        offset = LY_ARRAY_COUNT(*features);
-    }
-
-    lysc_update_path(ctx_sc, NULL, "{feature}");
-    LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_COUNT(features_p), LY_EMEM);
-    LY_ARRAY_FOR(features_p, u) {
-        lysc_update_path(ctx_sc, NULL, features_p[u].name);
-
-        LY_ARRAY_INCREMENT(*features);
-        COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
-        DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name, ret, done);
-        DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc, ret, done);
-        DUP_STRING_GOTO(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref, ret, done);
-        (*features)[offset + u].flags = features_p[u].flags;
-        (*features)[offset + u].module = ctx_sc->cur_mod;
-
-        lysc_update_path(ctx_sc, NULL, NULL);
-    }
-    lysc_update_path(ctx_sc, NULL, NULL);
-
-done:
-    return ret;
-}
-
-/**
- * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
- *
- * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
- *
- * @param[in] ctx Compile context for logging.
- * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
- *            being currently processed).
- * @param[in] depfeatures The list of depending features of the feature being currently processed (not the one provided as @p feature)
- * @return LY_SUCCESS if everything is ok.
- * @return LY_EVALID if the feature references indirectly itself.
- */
-static LY_ERR
-lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
-{
-    LY_ERR ret = LY_SUCCESS;
-    LY_ARRAY_COUNT_TYPE u, v;
-    struct ly_set recursion = {0};
-    struct lysc_feature *drv;
-
-    if (!depfeatures) {
-        return LY_SUCCESS;
-    }
-
-    for (u = 0; u < LY_ARRAY_COUNT(depfeatures); ++u) {
-        if (feature == depfeatures[u]) {
-            LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
-                    "Feature \"%s\" is indirectly referenced from itself.", feature->name);
-            ret = LY_EVALID;
-            goto cleanup;
-        }
-        ret = ly_set_add(&recursion, depfeatures[u], 0, NULL);
-        LY_CHECK_GOTO(ret, cleanup);
-    }
-
-    for (v = 0; v < recursion.count; ++v) {
-        drv = recursion.objs[v];
-        if (!drv->depfeatures) {
-            continue;
-        }
-        for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) {
-            if (feature == drv->depfeatures[u]) {
-                LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
-                        "Feature \"%s\" is indirectly referenced from itself.", feature->name);
-                ret = LY_EVALID;
-                goto cleanup;
-            }
-            ly_set_add(&recursion, drv->depfeatures[u], 0, NULL);
-            LY_CHECK_GOTO(ret, cleanup);
-        }
-    }
-
-cleanup:
-    ly_set_erase(&recursion, NULL);
-    return ret;
-}
-
-/**
- * @brief Create pre-compiled features array.
- *
- * See lys_feature_precompile() for more details.
- *
- * @param[in] ctx Compile context.
- * @param[in] feature_p Parsed feature definition to compile.
- * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
- * @return LY_ERR value.
- */
-static LY_ERR
-lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
-{
-    LY_ARRAY_COUNT_TYPE u, v, x;
-    struct lysc_feature *feature, **df;
-    LY_ERR ret = LY_SUCCESS;
-
-    /* find the preprecompiled feature */
-    LY_ARRAY_FOR(features, x) {
-        if (strcmp(features[x].name, feature_p->name)) {
-            continue;
-        }
-        feature = &features[x];
-        lysc_update_path(ctx, NULL, "{feature}");
-        lysc_update_path(ctx, NULL, feature_p->name);
-
-        /* finish compilation started in lys_feature_precompile() */
-        COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done);
-        COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
-        if (feature->iffeatures) {
-            for (u = 0; u < LY_ARRAY_COUNT(feature->iffeatures); ++u) {
-                if (feature->iffeatures[u].features) {
-                    for (v = 0; v < LY_ARRAY_COUNT(feature->iffeatures[u].features); ++v) {
-                        /* check for circular dependency - direct reference first,... */
-                        if (feature == feature->iffeatures[u].features[v]) {
-                            LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
-                                    "Feature \"%s\" is referenced from itself.", feature->name);
-                            return LY_EVALID;
-                        }
-                        /* ... and indirect circular reference */
-                        LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
-
-                        /* add itself into the dependants list */
-                        LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
-                        *df = feature;
-                    }
-                }
-            }
-        }
-        lysc_update_path(ctx, NULL, NULL);
-        lysc_update_path(ctx, NULL, NULL);
-done:
-        return ret;
-    }
-
-    LOGINT(ctx->ctx);
-    return LY_EINT;
-}
-
-void
-lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
-{
-    LY_ARRAY_COUNT_TYPE u, v;
-
-    /* in the dis_features list, remove all the parts (from finished compiling process)
-     * which may points into the data being freed here */
-    LY_ARRAY_FOR(mod->features, u) {
-        LY_ARRAY_FOR(mod->features[u].iffeatures, v) {
-            lysc_iffeature_free(ctx->ctx, &mod->features[u].iffeatures[v]);
-        }
-        LY_ARRAY_FREE(mod->features[u].iffeatures);
-        mod->features[u].iffeatures = NULL;
-
-        LY_ARRAY_FOR(mod->features[u].exts, v) {
-            lysc_ext_instance_free(ctx->ctx, &(mod->features[u].exts)[v]);
-        }
-        LY_ARRAY_FREE(mod->features[u].exts);
-        mod->features[u].exts = NULL;
-    }
-}
-
-/**
- * @brief Check the features used in if-feature statements applicable to the leafref and its target.
- *
- * The set of features used for target must be a subset of features used for the leafref.
- * This is not a perfect, we should compare the truth tables but it could require too much resources
- * and RFC 7950 does not require it explicitely, so we simplify that.
- *
- * @param[in] refnode The leafref node.
- * @param[in] target Tha target node of the leafref.
- * @return LY_SUCCESS or LY_EVALID;
- */
-static LY_ERR
-lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
-{
-    LY_ERR ret = LY_EVALID;
-    const struct lysc_node *iter;
-    LY_ARRAY_COUNT_TYPE u, v;
-    struct ly_set features = {0};
-
-    for (iter = refnode; iter; iter = iter->parent) {
-        if (iter->iffeatures) {
-            LY_ARRAY_FOR(iter->iffeatures, u) {
-                LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
-                    LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0, NULL), cleanup);
-                }
-            }
-        }
-    }
-
-    /* we should have, in features set, a superset of features applicable to the target node.
-     * If the feature is not present, we don;t have a subset of features applicable
-     * to the leafref itself. */
-    for (iter = target; iter; iter = iter->parent) {
-        if (iter->iffeatures) {
-            LY_ARRAY_FOR(iter->iffeatures, u) {
-                LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
-                    if (!ly_set_contains(&features, iter->iffeatures[u].features[v], NULL)) {
-                        /* feature not present */
-                        goto cleanup;
-                    }
-                }
-            }
-        }
-    }
-    ret = LY_SUCCESS;
-
-cleanup:
-    ly_set_erase(&features, NULL);
-    return ret;
-}
-
 static void *
 lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
 {
@@ -1131,8 +622,9 @@
     LY_ERR ret = LY_EVALID, r;
     LY_ARRAY_COUNT_TYPE u;
     struct lysp_stmt *stmt;
-    struct lysp_qname qname;
+    struct lysp_qname *iffeatures, *iffeat;
     void *parsed = NULL, **compiled = NULL;
+    ly_bool enabled;
 
     /* check for invalid substatements */
     for (stmt = ext->child; stmt; stmt = stmt->next) {
@@ -1216,26 +708,19 @@
                     LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
                     break;
                 }
-                case LY_STMT_IF_FEATURE: {
-                    struct lysc_iffeature *iff = NULL;
-
-                    if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
-                        /* single item */
-                        if (((struct lysc_iffeature *)substmts[u].storage)->features) {
-                            LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
-                            goto cleanup;
-                        }
-                        iff = (struct lysc_iffeature *)substmts[u].storage;
-                    } else {
-                        /* sized array */
-                        struct lysc_iffeature **iffs = (struct lysc_iffeature **)substmts[u].storage;
-                        LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup);
+                case LY_STMT_IF_FEATURE:
+                    iffeatures = NULL;
+                    LY_ARRAY_NEW_GOTO(ctx->ctx, iffeatures, iffeat, ret, cleanup);
+                    iffeat->str = stmt->arg;
+                    iffeat->mod = ctx->pmod;
+                    r = lys_eval_iffeatures(ctx->ctx, iffeatures, &enabled);
+                    LY_ARRAY_FREE(iffeatures);
+                    LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
+                    if (!enabled) {
+                        /* it is disabled, remove the whole extension instance */
+                        return LY_ENOT;
                     }
-                    qname.str = stmt->arg;
-                    qname.mod = ctx->pmod;
-                    LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &qname, iff), ret = r, cleanup);
                     break;
-                }
                 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
                  * also note that in many statements their extensions are not taken into account  */
                 default:
@@ -1401,7 +886,7 @@
         if (!mod->implemented) {
             /* unimplemented module found */
             if (implement) {
-                LY_CHECK_RET(lys_set_implemented((struct lys_module *)mod));
+                LY_CHECK_RET(lys_set_implemented((struct lys_module *)mod, NULL));
             } else {
                 *mod_p = mod;
                 break;
@@ -1647,13 +1132,7 @@
         }
     }
 
-    /* check if leafref and its target are under common if-features */
-    if (lys_compile_leafref_features_validate(node, target)) {
-        LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE,
-                "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of"
-                " features applicable to the leafref itself.", lref->path->expr);
-        return LY_EVALID;
-    }
+    /* TODO check if leafref and its target are under common if-features */
 
     return LY_SUCCESS;
 }
@@ -1688,7 +1167,17 @@
     ext_mod = ly_ctx_get_module_latest(ctx->ctx, "ietf-yang-metadata");
 
     /* compile the extension instance */
-    LY_CHECK_GOTO(ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod), cleanup);
+    ret = lys_compile_ext(ctx, ext_p, ext, mod->compiled, LYEXT_PAR_MODULE, ext_mod);
+    if (ret == LY_ENOT) {
+        /* free the extension */
+        lysc_ext_instance_free(ctx->ctx, ext);
+        LY_ARRAY_DECREMENT_FREE(mod->compiled->exts);
+
+        ret = LY_SUCCESS;
+        goto cleanup;
+    } else if (ret) {
+        goto cleanup;
+    }
 
 cleanup:
     lysp_ext_instance_free(ctx->ctx, ext_p);
@@ -1858,9 +1347,67 @@
     struct lysc_type_leafref *lref;
     struct lysc_augment *aug;
     struct lysc_deviation *dev;
+    struct ly_set disabled_op = {0};
     LY_ARRAY_COUNT_TYPE v;
     uint32_t i;
 
+#define ARRAY_DEL_ITEM(array, item) \
+    { \
+        LY_ARRAY_COUNT_TYPE u__; \
+        LY_ARRAY_FOR(array, u__) { \
+            if ((array) + u__ == item) { \
+                LY_ARRAY_DECREMENT(array); \
+                if (u__ < LY_ARRAY_COUNT(array)) { \
+                    memmove((array) + u__, (array) + u__ + 1, (LY_ARRAY_COUNT(array) - u__) * sizeof *(array)); \
+                } \
+                if (!LY_ARRAY_COUNT(array)) { \
+                    LY_ARRAY_FREE(array); \
+                    (array) = NULL; \
+                } \
+                break; \
+            } \
+        } \
+    }
+
+    /* remove all disabled nodes */
+    for (i = 0; i < ctx->disabled.count; ++i) {
+        node = ctx->disabled.snodes[i];
+        if (node->flags & LYS_KEY) {
+            LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_REFERENCE, "Key \"%s\" is disabled by its if-features.",
+                    node->name);
+            return LY_EVALID;
+        }
+
+        if (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
+            if (node->nodetype & (LYS_RPC | LYS_ACTION)) {
+                lysc_action_free(ctx->ctx, (struct lysc_action *)node);
+            } else {
+                lysc_notif_free(ctx->ctx, (struct lysc_notif *)node);
+            }
+            /* remember all freed RPCs/actions/notifs */
+            ly_set_add(&disabled_op, node, 1, NULL);
+        } else {
+            lysc_node_free(ctx->ctx, node, 1);
+        }
+    }
+
+    /* remove ops also from their arrays, from end so as not to move other items and change these pointer targets */
+    i = disabled_op.count;
+    while (i) {
+        --i;
+        node = disabled_op.snodes[i];
+        if (node->nodetype == LYS_RPC) {
+            ARRAY_DEL_ITEM(node->module->compiled->rpcs, (struct lysc_action *)node);
+        } else if (node->nodetype == LYS_ACTION) {
+            ARRAY_DEL_ITEM(*lysc_node_actions_p(node->parent), (struct lysc_action *)node);
+        } else if (node->parent) {
+            ARRAY_DEL_ITEM(*lysc_node_notifs_p(node->parent), (struct lysc_notif *)node);
+        } else {
+            ARRAY_DEL_ITEM(node->module->compiled->notifs, (struct lysc_notif *)node);
+        }
+    }
+    ly_set_erase(&disabled_op, NULL);
+
     /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
      * can be also leafref, in case it is already resolved, go through the chain and check that it does not
      * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */
@@ -1937,50 +1484,7 @@
     }
 
     return LY_SUCCESS;
-}
-
-/**
- * @brief Compile features in the current module and all its submodules.
- *
- * @param[in] ctx Compile context.
- * @return LY_ERR value.
- */
-static LY_ERR
-lys_compile_features(struct lysc_ctx *ctx)
-{
-    struct lysp_submodule *submod;
-    LY_ARRAY_COUNT_TYPE u, v;
-
-    if (!ctx->cur_mod->features) {
-        /* features are compiled directly into the module structure,
-         * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */
-        LY_CHECK_RET(lys_feature_precompile(ctx, NULL, NULL, ctx->cur_mod->parsed->features, &ctx->cur_mod->features));
-        LY_ARRAY_FOR(ctx->cur_mod->parsed->includes, v) {
-            submod = ctx->cur_mod->parsed->includes[v].submodule;
-            LY_CHECK_RET(lys_feature_precompile(ctx, NULL, NULL, submod->features, &ctx->cur_mod->features));
-        }
-    }
-
-    /* finish feature compilation, not only for the main module, but also for the submodules.
-     * Due to possible forward references, it must be done when all the features (including submodules)
-     * are present. */
-    LY_ARRAY_FOR(ctx->cur_mod->parsed->features, u) {
-        LY_CHECK_RET(lys_feature_precompile_finish(ctx, &ctx->cur_mod->parsed->features[u], ctx->cur_mod->features));
-    }
-
-    lysc_update_path(ctx, NULL, "{submodule}");
-    LY_ARRAY_FOR(ctx->cur_mod->parsed->includes, v) {
-        submod = ctx->cur_mod->parsed->includes[v].submodule;
-
-        lysc_update_path(ctx, NULL, submod->name);
-        LY_ARRAY_FOR(submod->features, u) {
-            LY_CHECK_RET(lys_feature_precompile_finish(ctx, &submod->features[u], ctx->cur_mod->features));
-        }
-        lysc_update_path(ctx, NULL, NULL);
-    }
-    lysc_update_path(ctx, NULL, NULL);
-
-    return LY_SUCCESS;
+#undef ARRAY_DEL_ITEM
 }
 
 /**
@@ -2004,7 +1508,7 @@
     }
 
     if (ctx->cur_mod->parsed->identities) {
-        LY_CHECK_RET(lys_compile_identities_derived(ctx, ctx->cur_mod->parsed->identities, ctx->cur_mod->identities));
+        LY_CHECK_RET(lys_compile_identities_derived(ctx, ctx->cur_mod->parsed->identities, &ctx->cur_mod->identities));
     }
     lysc_update_path(ctx, NULL, "{submodule}");
     LY_ARRAY_FOR(ctx->cur_mod->parsed->includes, u) {
@@ -2012,7 +1516,7 @@
         submod = ctx->cur_mod->parsed->includes[u].submodule;
         if (submod->identities) {
             lysc_update_path(ctx, NULL, submod->name);
-            LY_CHECK_RET(lys_compile_identities_derived(ctx, submod->identities, ctx->cur_mod->identities));
+            LY_CHECK_RET(lys_compile_identities_derived(ctx, submod->identities, &ctx->cur_mod->identities));
             lysc_update_path(ctx, NULL, NULL);
         }
     }
@@ -2021,6 +1525,86 @@
     return LY_SUCCESS;
 }
 
+static LY_ERR
+lys_recompile_mod(struct lys_module *mod, ly_bool log)
+{
+    LY_ERR ret;
+    uint32_t prev_lo;
+    struct lysp_import *imp;
+
+    if (!mod->implemented || mod->compiled) {
+        /* nothing to do */
+        return LY_SUCCESS;
+    }
+
+    /* we need all implemented imports to be recompiled */
+    LY_ARRAY_FOR(mod->parsed->imports, struct lysp_import, imp) {
+        LY_CHECK_RET(lys_recompile_mod(imp->module, log));
+    }
+
+    if (!log) {
+        /* recompile, must succeed because it was already compiled; hide messages because any
+         * warnings were already printed, are not really relevant, and would hide the real error */
+        prev_lo = ly_log_options(0);
+    }
+    ret = lys_compile(mod, 0);
+    if (!log) {
+        ly_log_options(prev_lo);
+    }
+
+    if (ret && !log) {
+        LOGERR(mod->ctx, ret, "Recompilation of module \"%s\" failed.", mod->name);
+    }
+    return ret;
+}
+
+LY_ERR
+lys_recompile(struct ly_ctx *ctx, const struct lys_module *skip)
+{
+    uint32_t idx;
+    struct lys_module *mod;
+    LY_ERR ret = LY_SUCCESS, r;
+
+    /* free all the modules */
+    for (idx = 0; idx < ctx->list.count; ++idx) {
+        mod = ctx->list.objs[idx];
+        /* skip this module */
+        if (skip && (mod == skip)) {
+            continue;
+        }
+
+        if (mod->compiled) {
+            /* free the module */
+            lysc_module_free(mod->compiled, NULL);
+            mod->compiled = NULL;
+        }
+
+        /* free precompiled iffeatures */
+        lys_free_feature_iffeatures(mod->parsed);
+    }
+
+    /* recompile all the modules */
+    for (idx = 0; idx < ctx->list.count; ++idx) {
+        mod = ctx->list.objs[idx];
+
+        /* skip this module */
+        if (skip && (mod == skip)) {
+            continue;
+        }
+
+        /* compile again */
+        r = lys_recompile_mod(mod, skip ? 1 : 0);
+        if (r) {
+            if (skip) {
+                return r;
+            }
+            ret = r;
+        }
+    }
+
+    return ret;
+}
+
 LY_ERR
 lys_compile(struct lys_module *mod, uint32_t options)
 {
@@ -2062,10 +1646,7 @@
         LY_CHECK_GOTO(ret = lys_compile_import(&ctx, &sp->imports[u]), error);
     }
 
-    /* features */
-    LY_CHECK_GOTO(ret = lys_compile_features(&ctx), error);
-
-    /* identities, work similarly to features with the precompilation */
+    /* identities */
     LY_CHECK_GOTO(ret = lys_compile_identities(&ctx), error);
 
     /* augments and deviations */
@@ -2161,7 +1742,8 @@
         LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, mod), error);
     }
 
-    /* there can be no leftover deviations */
+    /* there can be no leftover deviations or augments */
+    LY_CHECK_ERR_GOTO(ctx.augs.count, LOGINT(ctx.ctx); ret = LY_EINT, error);
     LY_CHECK_ERR_GOTO(ctx.devs.count, LOGINT(ctx.ctx); ret = LY_EINT, error);
 
     for (i = 0; i < ctx.dflts.count; ++i) {
@@ -2172,6 +1754,7 @@
     ly_set_erase(&ctx.leafrefs, NULL);
     ly_set_erase(&ctx.groupings, NULL);
     ly_set_erase(&ctx.tpdf_chain, NULL);
+    ly_set_erase(&ctx.disabled, NULL);
     ly_set_erase(&ctx.augs, NULL);
     ly_set_erase(&ctx.devs, NULL);
     ly_set_erase(&ctx.uses_augs, NULL);
@@ -2181,7 +1764,6 @@
 
 error:
     lys_precompile_augments_deviations_revert(ctx.ctx, mod);
-    lys_feature_precompile_revert(&ctx, mod);
     for (i = 0; i < ctx.dflts.count; ++i) {
         lysc_unres_dflt_free(ctx.ctx, ctx.dflts.objs[i]);
     }
@@ -2190,6 +1772,7 @@
     ly_set_erase(&ctx.leafrefs, NULL);
     ly_set_erase(&ctx.groupings, NULL);
     ly_set_erase(&ctx.tpdf_chain, NULL);
+    ly_set_erase(&ctx.disabled, NULL);
     for (i = 0; i < ctx.augs.count; ++i) {
         lysc_augment_free(ctx.ctx, ctx.augs.objs[i]);
     }