schema compile REFACTOR recompile using explicit compilation

... and leave it up to calling functions.
diff --git a/src/context.c b/src/context.c
index 4286db5..e41daea 100644
--- a/src/context.c
+++ b/src/context.c
@@ -547,14 +547,22 @@
 ly_ctx_set_options(struct ly_ctx *ctx, uint16_t option)
 {
     LY_ERR lyrc = LY_SUCCESS;
+    struct lys_module *mod;
+    uint32_t i;
 
     LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL);
     LY_CHECK_ERR_RET(option & LY_CTX_NO_YANGLIBRARY, LOGARG(ctx, option), LY_EINVAL);
 
     if (!(ctx->flags & LY_CTX_SET_PRIV_PARSED) && (option & LY_CTX_SET_PRIV_PARSED)) {
         ctx->flags |= LY_CTX_SET_PRIV_PARSED;
-        /* recompile to set the priv pointers */
-        lyrc = lys_recompile(ctx);
+        /* recompile the whole context to set the priv pointers */
+        for (i = 0; i < ctx->list.count; ++i) {
+            mod = ctx->list.objs[i];
+            if (mod->implemented) {
+                mod->to_compile = 1;
+            }
+        }
+        lyrc = ly_ctx_compile(ctx);
         if (lyrc) {
             ly_ctx_unset_options(ctx, LY_CTX_SET_PRIV_PARSED);
         }
diff --git a/src/schema_compile.c b/src/schema_compile.c
index 25d19fd..e4a55a4 100644
--- a/src/schema_compile.c
+++ b/src/schema_compile.c
@@ -1239,12 +1239,7 @@
         }
 
         LOG_LOCBACK(1, 0, 0, 0);
-        if (ret == LY_ERECOMPILE) {
-            /* leafref caused a new module to be implemented, following leafrefs referencing the module would fail */
-            return lys_recompile(ctx);
-        } else if (ret) {
-            return ret;
-        }
+        LY_CHECK_RET(ret);
     }
     while (unres->leafrefs.count) {
         node = unres->leafrefs.objs[unres->leafrefs.count - 1];
@@ -1285,11 +1280,7 @@
 
         ret = lys_compile_unres_xpath(&cctx, node, unres);
         LOG_LOCBACK(1, 0, 0, 0);
-        if (ret == LY_ERECOMPILE) {
-            return lys_recompile(ctx);
-        } else if (ret) {
-            return ret;
-        }
+        LY_CHECK_RET(ret);
 
         ly_set_rm_index(&unres->xpath, unres->xpath.count - 1, NULL);
     }
@@ -1308,11 +1299,7 @@
             ret = lys_compile_unres_llist_dflts(&cctx, r->llist, r->dflt, r->dflts, unres);
         }
         LOG_LOCBACK(1, 0, 0, 0);
-        if (ret == LY_ERECOMPILE) {
-            return lys_recompile(ctx);
-        } else if (ret) {
-            return ret;
-        }
+        LY_CHECK_RET(ret);
 
         lysc_unres_dflt_free(ctx, r);
         ly_set_rm_index(&unres->dflts, unres->dflts.count - 1, NULL);
@@ -1609,24 +1596,6 @@
     return ret;
 }
 
-LY_ERR
-lys_recompile(struct ly_ctx *ctx)
-{
-    uint32_t idx;
-    struct lys_module *mod;
-
-    /* mark all modules for recompilation */
-    for (idx = 0; idx < ctx->list.count; ++idx) {
-        mod = ctx->list.objs[idx];
-        if (mod->implemented) {
-            mod->to_compile = 1;
-        }
-    }
-
-    /* recompile */
-    return ly_ctx_compile(ctx);
-}
-
 /**
  * @brief Compile identites in a module and all its submodules.
  *
diff --git a/src/tree_schema.c b/src/tree_schema.c
index d1392e5..01efc11 100644
--- a/src/tree_schema.c
+++ b/src/tree_schema.c
@@ -809,32 +809,17 @@
         if (r == LY_EEXIST) {
             /* no changes */
             return LY_SUCCESS;
-        } else if (r) {
-            /* error */
-            return r;
-        }
-
-        if (mod->ctx->flags & LY_CTX_EXPLICIT_COMPILE) {
-            /* just mark the module as changed */
+        } else if (!r) {
+            /* mark the module as changed */
             mod->to_compile = 1;
-            return LY_SUCCESS;
-        } else {
-            /* full recompilation */
-            return lys_recompile(mod->ctx);
         }
+
+        return r;
     }
 
-    /* implement */
-    ret = lys_implement(mod, features, unres);
-    if (ret == LY_ERECOMPILE) {
-        /* recompile */
-        LY_CHECK_GOTO(ret = lys_recompile(mod->ctx), cleanup);
-
-        /* reset unres, it is no longer valid after recompilation, which has actually resolved it all */
-        lys_compile_unres_glob_erase(mod->ctx, unres, 1);
-    } else if (ret) {
-        goto cleanup;
-    }
+    /* implement, ignore recompilation because it must always take place later */
+    r = lys_implement(mod, features, unres);
+    LY_CHECK_ERR_GOTO(r && (r != LY_ERECOMPILE), ret = r, cleanup);
 
     if (mod->ctx->flags & LY_CTX_ALL_IMPLEMENTED) {
         /* implement all the imports as well */
@@ -844,9 +829,8 @@
                 continue;
             }
 
-            ret = lys_implement(mod, NULL, unres);
-            if (ret == LY_ERECOMPILE) {
-                LY_CHECK_GOTO(ret = lys_recompile(mod->ctx), cleanup);
+            r = lys_implement(mod, NULL, unres);
+            LY_CHECK_ERR_GOTO(r && (r != LY_ERECOMPILE), ret = r, cleanup);
 
                 lys_compile_unres_glob_erase(mod->ctx, unres, 1);
             } else if (ret) {