schema tree REFACTOR overloaded implemented flag (#1223)

It no longer needs to be overloaded by
marking modules that are currently being
implemented. Instead, there is a set with
al such modules.
diff --git a/src/tree_schema_compile.c b/src/tree_schema_compile.c
index b7d449e..7e23961 100644
--- a/src/tree_schema_compile.c
+++ b/src/tree_schema_compile.c
@@ -4667,7 +4667,7 @@
 
             /* all the modules must be implemented */
             if (!mod->implemented) {
-                ret = lys_set_implemented_internal(mod, ctx->ctx->module_set_id);
+                ret = lys_set_implemented(mod);
                 LY_CHECK_GOTO(ret, cleanup);
             }
         }
@@ -7143,7 +7143,8 @@
 
 /**
  * @brief Compile top-level augments and deviations defined in the current module.
- * Generally, just add the module refence to the target modules.
+ * Generally, just add the module refence to the target modules. But in case
+ * of foreign augments, they are directly applied.
  *
  * @param[in] ctx Compile context.
  * @return LY_ERR value.
@@ -7161,13 +7162,18 @@
     uint16_t flags;
     uint32_t idx, opt_prev = ctx->options;
 
-    mod_p = ctx->mod->parsed;
-
-    if (mod_p->mod->implemented == 1) {
+    for (idx = 0; idx < ctx->ctx->implementing.count; ++idx) {
+        if (ctx->mod == ctx->ctx->implementing.objs[idx]) {
+            break;
+        }
+    }
+    if (idx == ctx->ctx->implementing.count) {
         /* it was already implemented and all the augments and deviations fully applied */
         return LY_SUCCESS;
     }
 
+    mod_p = ctx->mod->parsed;
+
     LY_ARRAY_FOR(mod_p->augments, u) {
         lysc_update_path(ctx, NULL, "{augment}");
         lysc_update_path(ctx, NULL, mod_p->augments[u].nodeid);
@@ -7288,7 +7294,7 @@
 
         if (mod->implemented) {
             /* compile */
-            LY_CHECK_GOTO(ret = lys_compile(mod, LYSC_OPT_INTERNAL), cleanup);
+            LY_CHECK_GOTO(ret = lys_compile(mod, 0), cleanup);
         }
     }
 
@@ -8030,22 +8036,15 @@
     return LY_SUCCESS;
 }
 
-/**
- * @brief Revert precompilation of module augments and deviations. Meaning remove its reference from
- * all the target modules.
- *
- * @param[in] ctx Compile context.
- * @param[in] mod Mod whose precompilation to revert.
- */
-static void
-lys_precompile_augments_deviations_revert(struct lysc_ctx *ctx, const struct lys_module *mod)
+void
+lys_precompile_augments_deviations_revert(struct ly_ctx *ctx, const struct lys_module *mod)
 {
     uint32_t i;
     LY_ARRAY_COUNT_TYPE u, count;
     struct lys_module *m;
 
-    for (i = 0; i < ctx->ctx->list.count; ++i) {
-        m = ctx->ctx->list.objs[i];
+    for (i = 0; i < ctx->list.count; ++i) {
+        m = ctx->list.objs[i];
 
         if (m->augmented_by) {
             count = LY_ARRAY_COUNT(m->augmented_by);
@@ -8176,7 +8175,6 @@
     struct lysp_submodule *submod;
     struct lysp_node *pnode;
     struct lysp_grp *grps;
-    struct lys_module *m;
     LY_ARRAY_COUNT_TYPE u, v;
     uint32_t i;
     LY_ERR ret = LY_SUCCESS;
@@ -8323,20 +8321,10 @@
         mod->parsed = NULL;
     }
 
-    if (!(ctx.options & LYSC_OPT_INTERNAL)) {
-        /* remove flag of the modules implemented by dependency */
-        for (i = 0; i < ctx.ctx->list.count; ++i) {
-            m = ctx.ctx->list.objs[i];
-            if (m->implemented > 1) {
-                m->implemented = 1;
-            }
-        }
-    }
-
     return LY_SUCCESS;
 
 error:
-    lys_precompile_augments_deviations_revert(&ctx, mod);
+    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]);
@@ -8365,31 +8353,5 @@
     lysc_module_free(mod_c, NULL);
     mod->compiled = NULL;
 
-    /* revert compilation of modules implemented by dependency */
-    if (!(ctx.options & LYSC_OPT_INTERNAL)) {
-        for (i = 0; i < ctx.ctx->list.count; ++i) {
-            m = ctx.ctx->list.objs[i];
-            if (m->implemented > 1) {
-                /* make the module non-implemented */
-                m->implemented = 0;
-            }
-
-            /* free the compiled version of the module, if any */
-            lysc_module_free(m->compiled, NULL);
-            m->compiled = NULL;
-
-            if (m->implemented) {
-                /* 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 */
-                uint32_t prev_lo = ly_log_options(0);
-                LY_ERR r = lys_compile(m, LYSC_OPT_INTERNAL);
-                ly_log_options(prev_lo);
-                if (r) {
-                    LOGERR(ctx.ctx, r, "Recompilation of module \"%s\" failed.", m->name);
-                }
-            }
-        }
-    }
-
     return ret;
 }