libyang REFACTOR use lysp_module instead of lys_module as mod_def

Because imports from a specific parsed module
or submodule must be used, not imports from the
module and all its submodules.

Also, pmod (prefix module) is now the term
used for the module where import prefixes are
searched in and cur_mod (current module) is
the module of unprefixed identifiers.
diff --git a/src/parser_yin.c b/src/parser_yin.c
index 8efa2de..407c325 100644
--- a/src/parser_yin.c
+++ b/src/parser_yin.c
@@ -517,7 +517,7 @@
     saved_value[0] = 0x06;
     saved_value[len + 1] = '\0';
     LY_CHECK_RET(lydict_insert_zc(ctx->xmlctx->ctx, saved_value, &restr->arg.str));
-    restr->arg.mod = ctx->main_mod;
+    restr->arg.mod = ctx->parsed_mod;
     type->flags |= LYS_SET_PATTERN;
 
     struct yin_subelement subelems[6] = {
@@ -821,7 +821,7 @@
 
     LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
     LY_CHECK_RET(yin_parse_attribute(ctx, arg_type, &restr->arg.str, Y_STR_ARG, restr_kw));
-    restr->arg.mod = ctx->main_mod;
+    restr->arg.mod = ctx->parsed_mod;
 
     return yin_parse_content(ctx, subelems, 5, restr_kw, NULL, &restr->exts);
 }
@@ -899,32 +899,20 @@
 
     switch (kw) {
     case LY_STMT_DEFAULT:
-        if (subinfo->flags & YIN_SUBELEM_TEXT) {
-            if (subinfo->flags & YIN_SUBELEM_UNIQUE) {
-                return yin_parse_simple_element(ctx, kw, (const char **)subinfo->dest, YIN_ARG_VALUE, Y_STR_ARG, exts);
-            } else {
-                return yin_parse_simple_elements(ctx, kw, (const char ***)subinfo->dest, YIN_ARG_VALUE, Y_STR_ARG, exts);
-            }
-        } else {
-            if (subinfo->flags & YIN_SUBELEM_UNIQUE) {
-                qname = (struct lysp_qname *)subinfo->dest;
-            } else {
-                qnames = (struct lysp_qname **)subinfo->dest;
-                LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *qnames, qname, LY_EMEM);
-            }
-            qname->mod = ctx->main_mod;
-            return yin_parse_simple_element(ctx, kw, &qname->str, YIN_ARG_VALUE, Y_STR_ARG, exts);
-        }
-    case LY_STMT_IF_FEATURE:
-        assert(!(subinfo->flags & YIN_SUBELEM_UNIQUE));
-        if (subinfo->flags & YIN_SUBELEM_TEXT) {
-            return yin_parse_simple_elements(ctx, kw, (const char ***)subinfo->dest, YIN_ARG_NAME, Y_STR_ARG, exts);
+        if (subinfo->flags & YIN_SUBELEM_UNIQUE) {
+            qname = (struct lysp_qname *)subinfo->dest;
         } else {
             qnames = (struct lysp_qname **)subinfo->dest;
             LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *qnames, qname, LY_EMEM);
-            qname->mod = ctx->main_mod;
-            return yin_parse_simple_element(ctx, kw, &qname->str, YIN_ARG_NAME, Y_STR_ARG, exts);
         }
+        qname->mod = ctx->parsed_mod;
+        return yin_parse_simple_element(ctx, kw, &qname->str, YIN_ARG_VALUE, Y_STR_ARG, exts);
+    case LY_STMT_IF_FEATURE:
+        assert(!(subinfo->flags & YIN_SUBELEM_UNIQUE));
+        qnames = (struct lysp_qname **)subinfo->dest;
+        LY_ARRAY_NEW_RET(ctx->xmlctx->ctx, *qnames, qname, LY_EMEM);
+        qname->mod = ctx->parsed_mod;
+        return yin_parse_simple_element(ctx, kw, &qname->str, YIN_ARG_NAME, Y_STR_ARG, exts);
     default:
         break;
     }
@@ -1027,9 +1015,9 @@
 
     LY_CHECK_RET(lyxml_ctx_next(ctx->xmlctx));
     LY_CHECK_RET(yin_parse_attribute(ctx, YIN_ARG_MODULE, &belongsto, Y_IDENTIF_ARG, LY_STMT_BELONGS_TO));
-    if (ctx->main_mod->name != belongsto) {
+    if (ctx->parsed_mod->mod->name != belongsto) {
         LOGVAL_PARSER(ctx, LYVE_SYNTAX_YIN, "Submodule \"belongs-to\" value \"%s\" does not match its module name \"%s\".",
-                belongsto, ctx->main_mod->name);
+                belongsto, ctx->parsed_mod->mod->name);
         lydict_remove(ctx->xmlctx->ctx, belongsto);
         return LY_EVALID;
     }
@@ -1113,7 +1101,7 @@
         type = (struct lysp_type *)subinfo->dest;
     }
 
-    type->mod = ctx->main_mod;
+    type->pmod = ctx->parsed_mod;
 
     /* type as child of another type */
     if (parent == LY_STMT_TYPE) {
@@ -1517,9 +1505,9 @@
     /* parse content */
     struct yin_subelement subelems[11] = {
         {LY_STMT_CONFIG, &rf->flags, YIN_SUBELEM_UNIQUE},
-        {LY_STMT_DEFAULT, &rf->dflts, YIN_SUBELEM_TEXT},
+        {LY_STMT_DEFAULT, &rf->dflts, 0},
         {LY_STMT_DESCRIPTION, &rf->dsc, YIN_SUBELEM_UNIQUE},
-        {LY_STMT_IF_FEATURE, &rf->iffeatures, YIN_SUBELEM_TEXT},
+        {LY_STMT_IF_FEATURE, &rf->iffeatures, 0},
         {LY_STMT_MANDATORY, &rf->flags, YIN_SUBELEM_UNIQUE},
         {LY_STMT_MAX_ELEMENTS, rf, YIN_SUBELEM_UNIQUE},
         {LY_STMT_MIN_ELEMENTS, rf, YIN_SUBELEM_UNIQUE},
@@ -1741,7 +1729,6 @@
         return LY_EVALID;
     }
     FREE_STRING(ctx->xmlctx->ctx, temp_version);
-    ctx->mod_version = *version;
 
     return yin_parse_content(ctx, subelems, 1, LY_STMT_YANG_VERSION, NULL, exts);
 }
@@ -2497,7 +2484,7 @@
             LY_STMT_CHOICE, &aug->child, 0,
             LY_STMT_CONTAINER, &aug->child, 0,
             LY_STMT_DESCRIPTION, &aug->dsc, YIN_SUBELEM_UNIQUE,
-            LY_STMT_IF_FEATURE, &aug->iffeatures, YIN_SUBELEM_TEXT,
+            LY_STMT_IF_FEATURE, &aug->iffeatures, 0,
             LY_STMT_LEAF, &aug->child, 0,
             LY_STMT_LEAF_LIST, &aug->child, 0,
             LY_STMT_LIST, &aug->child, 0,
@@ -2571,7 +2558,7 @@
         struct minmax_dev_meta max = {&d_add->max, &d_add->flags, &d_add->exts};
         struct yin_subelement subelems[9] = {
             {LY_STMT_CONFIG, &d_add->flags, YIN_SUBELEM_UNIQUE},
-            {LY_STMT_DEFAULT, &d_add->dflts, YIN_SUBELEM_TEXT},
+            {LY_STMT_DEFAULT, &d_add->dflts, 0},
             {LY_STMT_MANDATORY, &d_add->flags, YIN_SUBELEM_UNIQUE},
             {LY_STMT_MAX_ELEMENTS, &max, YIN_SUBELEM_UNIQUE},
             {LY_STMT_MIN_ELEMENTS, &min, YIN_SUBELEM_UNIQUE},
@@ -2590,7 +2577,7 @@
         struct minmax_dev_meta max = {&d_rpl->max, &d_rpl->flags, &d_rpl->exts};
         struct yin_subelement subelems[8] = {
             {LY_STMT_CONFIG, &d_rpl->flags, YIN_SUBELEM_UNIQUE},
-            {LY_STMT_DEFAULT, &d_rpl->dflt, YIN_SUBELEM_UNIQUE | YIN_SUBELEM_TEXT},
+            {LY_STMT_DEFAULT, &d_rpl->dflt, YIN_SUBELEM_UNIQUE},
             {LY_STMT_MANDATORY, &d_rpl->flags, YIN_SUBELEM_UNIQUE},
             {LY_STMT_MAX_ELEMENTS, &max, YIN_SUBELEM_UNIQUE},
             {LY_STMT_MIN_ELEMENTS, &min, YIN_SUBELEM_UNIQUE},
@@ -2605,7 +2592,7 @@
         LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->xmlctx->ctx), LY_EMEM);
         d = (struct lysp_deviate *)d_del;
         struct yin_subelement subelems[5] = {
-            {LY_STMT_DEFAULT, &d_del->dflts, YIN_SUBELEM_TEXT},
+            {LY_STMT_DEFAULT, &d_del->dflts, 0},
             {LY_STMT_MUST, &d_del->musts, 0},
             {LY_STMT_UNIQUE, &d_del->uniques, 0},
             {LY_STMT_UNITS, &d_del->units, YIN_SUBELEM_UNIQUE},
@@ -2935,7 +2922,7 @@
             }
             if (subelem->flags & YIN_SUBELEM_VER2) {
                 /* subelement is supported only in version 1.1 or higher */
-                if (ctx->mod_version < 2) {
+                if (ctx->parsed_mod->version < LYS_VERSION_1_1) {
                     LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INSUBELEM2, ly_stmt2str(kw), ly_stmt2str(current_element));
                     ret = LY_EVALID;
                     goto cleanup;
@@ -3524,7 +3511,7 @@
             LY_STMT_RPC, &mod->rpcs, 0,
             LY_STMT_TYPEDEF, &mod->typedefs, 0,
             LY_STMT_USES, &mod->data, 0,
-            LY_STMT_YANG_VERSION, &mod->mod->version, YIN_SUBELEM_UNIQUE,
+            LY_STMT_YANG_VERSION, &mod->version, YIN_SUBELEM_UNIQUE,
             LY_STMT_EXTENSION_INSTANCE, NULL, 0));
 
     ret = yin_parse_content(ctx, subelems, 28, LY_STMT_MODULE, NULL, &mod->exts);
@@ -3615,9 +3602,14 @@
     *yin_ctx = calloc(1, sizeof **yin_ctx);
     LY_CHECK_ERR_RET(!(*yin_ctx), LOGMEM(ctx), LY_EMEM);
     (*yin_ctx)->format = LYS_IN_YIN;
-    (*yin_ctx)->main_mod = main_ctx->main_mod;
     LY_CHECK_RET(lyxml_ctx_new(ctx, in, &(*yin_ctx)->xmlctx));
 
+    mod_p = calloc(1, sizeof *mod_p);
+    LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx); ret = LY_EMEM, cleanup);
+    mod_p->mod = main_ctx->parsed_mod->mod;
+    mod_p->parsing = 1;
+    (*yin_ctx)->parsed_mod = (struct lysp_module *)mod_p;
+
     /* map the typedefs and groupings list from main context to the submodule's context */
     memcpy(&(*yin_ctx)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
     memcpy(&(*yin_ctx)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
@@ -3635,11 +3627,6 @@
         goto cleanup;
     }
 
-    mod_p = calloc(1, sizeof *mod_p);
-    LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ctx); ret = LY_EMEM, cleanup);
-    mod_p->mod = main_ctx->main_mod;
-    mod_p->parsing = 1;
-
     ret = yin_parse_submod(*yin_ctx, mod_p);
     LY_CHECK_GOTO(ret, cleanup);
 
@@ -3659,7 +3646,7 @@
 
 cleanup:
     if (ret) {
-        lysp_submodule_free(ctx, mod_p);
+        lysp_module_free((struct lysp_module *)mod_p);
         yin_parser_ctx_free(*yin_ctx);
         *yin_ctx = NULL;
     }
@@ -3677,9 +3664,14 @@
     *yin_ctx = calloc(1, sizeof **yin_ctx);
     LY_CHECK_ERR_RET(!(*yin_ctx), LOGMEM(mod->ctx), LY_EMEM);
     (*yin_ctx)->format = LYS_IN_YIN;
-    (*yin_ctx)->main_mod = mod;
     LY_CHECK_RET(lyxml_ctx_new(mod->ctx, in, &(*yin_ctx)->xmlctx));
 
+    mod_p = calloc(1, sizeof *mod_p);
+    LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
+    mod_p->mod = mod;
+    mod_p->parsing = 1;
+    (*yin_ctx)->parsed_mod = mod_p;
+
     /* check module */
     kw = yin_match_keyword(*yin_ctx, (*yin_ctx)->xmlctx->name, (*yin_ctx)->xmlctx->name_len, (*yin_ctx)->xmlctx->prefix,
             (*yin_ctx)->xmlctx->prefix_len, LY_STMT_NONE);
@@ -3693,12 +3685,6 @@
         goto cleanup;
     }
 
-    /* allocate module */
-    mod_p = calloc(1, sizeof *mod_p);
-    LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
-    mod_p->mod = mod;
-    mod_p->parsing = 1;
-
     /* parse module substatements */
     ret = yin_parse_mod(*yin_ctx, mod_p);
     LY_CHECK_GOTO(ret, cleanup);