schema tree REFACTOR put parsing and compilation into one function
diff --git a/src/tree_schema_compile.c b/src/tree_schema_compile.c
index 87a5734..8fbe2f1 100644
--- a/src/tree_schema_compile.c
+++ b/src/tree_schema_compile.c
@@ -7300,7 +7300,7 @@
 }
 
 LY_ERR
-lys_compile(struct lys_module **mod, uint32_t options)
+lys_compile(struct lys_module *mod, uint32_t options)
 {
     struct lysc_ctx ctx = {0};
     struct lysc_module *mod_c;
@@ -7314,26 +7314,26 @@
     uint16_t compile_id;
     LY_ERR ret = LY_SUCCESS;
 
-    LY_CHECK_ARG_RET(NULL, mod, *mod, (*mod)->parsed, (*mod)->ctx, LY_EINVAL);
+    LY_CHECK_ARG_RET(NULL, mod, mod->parsed, !mod->compiled, mod->ctx, LY_EINVAL);
 
-    if (!(*mod)->implemented) {
+    if (!mod->implemented) {
         /* just imported modules are not compiled */
         return LY_SUCCESS;
     }
 
-    compile_id = ++(*mod)->ctx->module_set_id;
-    sp = (*mod)->parsed;
+    compile_id = ++mod->ctx->module_set_id;
+    sp = mod->parsed;
 
-    ctx.ctx = (*mod)->ctx;
-    ctx.mod = *mod;
-    ctx.mod_def = *mod;
+    ctx.ctx = mod->ctx;
+    ctx.mod = mod;
+    ctx.mod_def = mod;
     ctx.options = options;
     ctx.path_len = 1;
     ctx.path[0] = '/';
 
-    (*mod)->compiled = mod_c = calloc(1, sizeof *mod_c);
-    LY_CHECK_ERR_RET(!mod_c, LOGMEM((*mod)->ctx), LY_EMEM);
-    mod_c->mod = *mod;
+    mod->compiled = mod_c = calloc(1, sizeof *mod_c);
+    LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
+    mod_c->mod = mod;
 
     LY_ARRAY_FOR(sp->imports, u) {
         LY_CHECK_GOTO(ret = lys_compile_import(&ctx, &sp->imports[u]), error);
@@ -7343,10 +7343,10 @@
     }
 
     /* features */
-    if ((*mod)->dis_features) {
+    if (mod->dis_features) {
         /* there is already precompiled array of features */
-        mod_c->features = (*mod)->dis_features;
-        (*mod)->dis_features = NULL;
+        mod_c->features = mod->dis_features;
+        mod->dis_features = NULL;
     } else {
         /* features are compiled directly into the compiled module structure,
          * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */
@@ -7373,9 +7373,9 @@
     lysc_update_path(&ctx, NULL, NULL);
 
     /* identities, work similarly to features with the precompilation */
-    if ((*mod)->dis_identities) {
-        mod_c->identities = (*mod)->dis_identities;
-        (*mod)->dis_identities = NULL;
+    if (mod->dis_identities) {
+        mod_c->identities = mod->dis_identities;
+        mod->dis_identities = NULL;
     } else {
         ret = lys_identity_precompile(&ctx, NULL, NULL, sp->identities, &mod_c->identities);
         LY_CHECK_GOTO(ret, error);
@@ -7453,8 +7453,8 @@
 #endif
 
     /* add ietf-netconf-with-defaults "default" metadata to the compiled module */
-    if (!strcmp((*mod)->name, "ietf-netconf-with-defaults")) {
-        LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, *mod), error);
+    if (!strcmp(mod->name, "ietf-netconf-with-defaults")) {
+        LY_CHECK_GOTO(ret = lys_compile_ietf_netconf_wd_annotation(&ctx, mod), error);
     }
 
     ly_set_erase(&ctx.dflts, free);
@@ -7465,8 +7465,8 @@
     LY_ARRAY_FREE(augments);
 
     if (ctx.options & LYSC_OPT_FREE_SP) {
-        lysp_module_free((*mod)->parsed);
-        (*mod)->parsed = NULL;
+        lysp_module_free(mod->parsed);
+        mod->parsed = NULL;
     }
 
     if (!(ctx.options & LYSC_OPT_INTERNAL)) {
@@ -7482,7 +7482,7 @@
     return LY_SUCCESS;
 
 error:
-    lys_feature_precompile_revert(&ctx, *mod);
+    lys_feature_precompile_revert(&ctx, mod);
     ly_set_erase(&ctx.dflts, free);
     ly_set_erase(&ctx.xpath, NULL);
     ly_set_erase(&ctx.leafrefs, NULL);
@@ -7490,7 +7490,7 @@
     ly_set_erase(&ctx.tpdf_chain, NULL);
     LY_ARRAY_FREE(augments);
     lysc_module_free(mod_c, NULL);
-    (*mod)->compiled = NULL;
+    mod->compiled = NULL;
 
     /* revert compilation of modules implemented by dependency, but only by (directly or indirectly) by dependency
      * of this module, since this module can be also compiled from dependency, there can be some other modules being
@@ -7508,10 +7508,5 @@
         }
     }
 
-    /* remove the module itself from the context and free it */
-    ly_set_rm(&ctx.ctx->list, *mod, NULL);
-    lys_module_free(*mod, NULL);
-    *mod = NULL;
-
     return ret;
 }