schema tree CHANGE redesign revision in compiled schema
diff --git a/src/tree_schema.c b/src/tree_schema.c
index cd55950..9db0f77 100644
--- a/src/tree_schema.c
+++ b/src/tree_schema.c
@@ -99,6 +99,7 @@
     if (include->submodule && !(--include->submodule->refcount)) {
         lysp_module_free(include->submodule);
     }
+    dict = 1; /* includes not present in compiled tree, so the data are not reused there in anyway */
     FREE_STRING(ctx, include->name, dict);
     FREE_STRING(ctx, include->dsc, dict);
     FREE_STRING(ctx, include->ref, dict);
@@ -514,6 +515,7 @@
     FREE_STRING(ctx, module->name, dict);
     FREE_STRING(ctx, module->ns, dict);
     FREE_STRING(ctx, module->prefix, dict);
+    FREE_STRING(ctx, module->revision, 1);
 
     FREE_ARRAY(ctx, module->imports, lysc_import_free);
     FREE_ARRAY(ctx, module->features, lysc_feature_free);
@@ -1074,7 +1076,7 @@
 lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, int options, struct lysc_import *imp)
 {
     unsigned int u;
-    struct lys_module *mod;
+    struct lys_module *mod = NULL;
     struct lysc_module *comp;
     LY_ERR ret = LY_SUCCESS;
 
@@ -1103,14 +1105,14 @@
             }
         }
         if (!mod) {
-            if (lysp_load_module(ctx->mod->ctx, comp->name, comp->revs ? comp->revs[0].date : NULL, 0, 1, &mod)) {
+            if (lysp_load_module(ctx->mod->ctx, comp->name, comp->revision, 0, 1, &mod)) {
                 LOGERR(ctx->mod->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.",
                        comp->name, ctx->mod->name);
                 return LY_ENOTFOUND;
             }
         }
     } else if (!imp->module->compiled) {
-        return lys_compile(imp->module->parsed, options, &imp->module->compiled);
+        return lys_compile(imp->module, options);
     }
 
 done:
@@ -1217,21 +1219,23 @@
 }
 
 LY_ERR
-lys_compile(struct lysp_module *sp, int options, struct lysc_module **sc)
+lys_compile(const struct lys_module *mod, int options)
 {
     struct lysc_ctx ctx = {0};
     struct lysc_module *mod_c;
+    struct lysp_module *sp;
     unsigned int u;
     LY_ERR ret;
 
-    LY_CHECK_ARG_RET(NULL, sc, sp, sp->ctx, LY_EINVAL);
+    LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->parsed->ctx, LY_EINVAL);
+    sp = mod->parsed;
 
     if (sp->submodule) {
         LOGERR(sp->ctx, LY_EINVAL, "Submodules (%s) are not supposed to be compiled, compile only the main modules.", sp->name);
         return LY_EINVAL;
     }
 
-    ctx.mod = *sc = mod_c = calloc(1, sizeof *mod_c);
+    ctx.mod = ((struct lys_module*)mod)->compiled = mod_c = calloc(1, sizeof *mod_c);
     LY_CHECK_ERR_RET(!mod_c, LOGMEM(sp->ctx), LY_EMEM);
     mod_c->ctx = sp->ctx;
     mod_c->version = sp->version;
@@ -1247,7 +1251,9 @@
         mod_c->ns = lydict_insert(sp->ctx, sp->ns, 0);
         mod_c->prefix = lydict_insert(sp->ctx, sp->prefix, 0);
     }
-
+    if (sp->revs) {
+        mod_c->revision = lydict_insert(sp->ctx, sp->revs[0].date, 10);
+    }
     COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, options, u, lys_compile_import, ret, error);
     COMPILE_ARRAY_GOTO(&ctx, sp->features, mod_c->features, options, u, lys_compile_feature, ret, error);
     COMPILE_ARRAY_GOTO(&ctx, sp->identities, mod_c->identities, options, u, lys_compile_identity, ret, error);
@@ -1258,14 +1264,16 @@
     COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, options, u, lys_compile_ext, ret, error);
 
     if (options & LYSC_OPT_FREE_SP) {
-        lysp_module_free_(sp, 0);
+        lysp_module_free_(mod->parsed, 0);
+        ((struct lys_module*)mod)->parsed = NULL;
     }
 
+    ((struct lys_module*)mod)->compiled = mod_c;
     return LY_SUCCESS;
 
 error:
     lysc_module_free_(mod_c, (options & LYSC_OPT_FREE_SP) ? 0 : 1);
-
+    ((struct lys_module*)mod)->compiled = NULL;
     return ret;
 }
 
@@ -1396,11 +1404,11 @@
         latest = (struct lys_module*)ly_ctx_get_module_latest(ctx, mod->parsed->name);
         if (latest) {
             if (mod->parsed->revs) {
-                if ((latest->parsed && !latest->parsed->revs) || (!latest->parsed && !latest->compiled->revs)) {
+                if ((latest->parsed && !latest->parsed->revs) || (!latest->parsed && !latest->compiled->revision)) {
                     /* latest has no revision, so mod is anyway newer */
                     lys_latest_switch(latest, mod->parsed);
                 } else {
-                    if (strcmp(mod->parsed->revs[0].date, latest->parsed ? latest->parsed->revs[0].date : latest->compiled->revs[0].date) > 0) {
+                    if (strcmp(mod->parsed->revs[0].date, latest->parsed ? latest->parsed->revs[0].date : latest->compiled->revision) > 0) {
                         lys_latest_switch(latest, mod->parsed);
                     }
                 }