extensions CHANGE cleanup API of the schema compiler for extension plugins

Be more clear by what API of the schema compiler is available for the
extension plugins. While the compilation context is needed, we want to
keep its members hidden to be able to make future changes. Therefore,
there is only a (possibly extensible in a future) set of getter to
access the context's members to be read and updated by the plugins'
compilation callbacks. Another part of the API is a set of compilation
flags to modify the behavior of the internal schema compiler.
diff --git a/src/plugins_exts.c b/src/plugins_exts.c
index f3ede62..ded7c5e 100644
--- a/src/plugins_exts.c
+++ b/src/plugins_exts.c
@@ -22,6 +22,10 @@
 #include "plugins_exts_nacm.c"
 #include "plugins_exts_yangdata.c"
 
+/* internal libyang headers - do not make them accessible to the extension plugins in plugins_exts_*.c */
+#include "common.h"
+#include "schema_compile.h"
+
 /**
  * @brief list of all extension plugins implemented internally
  */
@@ -51,3 +55,21 @@
 
     return NULL;
 }
+
+API struct ly_ctx *
+lysc_ctx_get_ctx(const struct lysc_ctx *ctx)
+{
+    return ctx->ctx;
+}
+
+API uint32_t *
+lysc_ctx_get_options(const struct lysc_ctx *ctx)
+{
+    return &((struct lysc_ctx *)ctx)->options;
+}
+
+API const char *
+lysc_ctx_get_path(const struct lysc_ctx *ctx)
+{
+    return ctx->path;
+}
diff --git a/src/plugins_exts.h b/src/plugins_exts.h
index 9f820d3..ebeead8 100644
--- a/src/plugins_exts.h
+++ b/src/plugins_exts.h
@@ -19,9 +19,10 @@
 #include "tree_edit.h"
 #include "tree_schema.h"
 
+#include "plugins_exts_compile.h"
+
 struct ly_ctx;
 struct lyd_node;
-struct lysc_ctx;
 
 #ifdef __cplusplus
 extern "C" {
@@ -45,19 +46,6 @@
 #define LYEXT_VERSION_CHECK uint32_t lyext_api_version = LYEXT_API_VERSION;
 
 /**
- * @defgroup extensionscompile YANG Extensions - Compilation Helpers
- * @ingroup extensions
- * @brief Helper functions to compile (via lyext_clb_compile callback) statements inside the extension instance.
- *
- * NOTE: There is a lot of useful static functions in the tree_schema_compile.c which could be provided here. Since we don't want
- * to have a large API with functions which will be never used, we provide here just the functions which are evidently needed.
- * If you, as an extension plugin author, need to make some of the compile functions available, please contact libyang maintainers
- * via the GITHUB issue tracker.
- *
- * @{
- */
-
-/**
  * @brief Possible cardinalities of the YANG statements.
  *
  * Used in extensions plugins to define cardinalities of the extension instance substatements.
@@ -91,7 +79,11 @@
 };
 
 /**
- * @brief YANG printer context.
+ * @brief Compiled YANG printer context for use in ::lyext_clb_schema_printer callback implementation.
+ *
+ * The structure provides basic information how the compiled schema is supposed to be printed and where. In the most simple
+ * case, the provided context is just passed into ::lysc_print_extension_instance() function which handles printing the
+ * extension's substatements in the standard way.
  */
 struct lys_ypr_ctx {
     struct ly_out *out;              /**< output specification */
@@ -102,13 +94,6 @@
 };
 
 /**
- * @brief Compile substatements of an extension instance.
- * TODO
- * @return LY_ENOT if the extension is disabled and should be ignored.
- */
-LY_ERR lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext);
-
-/**
  * @brief Print substatements of an extension instance
  *
  * Generic function to access YANG printer functions from the extension plugins (::lyext_clb_schema_printer).
@@ -130,16 +115,6 @@
 void lysc_extension_instance_substatements_free(struct ly_ctx *ctx, struct lysc_ext_substmt *substmts);
 
 /**
- * @brief Duplicate the compiled extension (definition) structure.
- * TODO should this be in API? currently required by nacm_compile()
- * Instead of duplicating memory, the reference counter in the @p orig is increased.
- *
- * @param[in] orig The extension structure to duplicate.
- * @return The duplicated structure to use.
- */
-struct lysc_ext *lysc_ext_dup(struct lysc_ext *orig);
-
-/**
  * @brief Get pointer to the storage of the specified substatement in the given extension instance.
  *
  * The function simplifies access into the ::lysc_ext_instance.substmts sized array.
@@ -159,18 +134,6 @@
         void **instance_p, enum ly_stmt_cardinality *cardinality_p);
 
 /**
- * @brief Update path in the compile context, which is used for logging where the compilation failed.
- *
- * @param[in] ctx Compile context with the path.
- * @param[in] parent_module Module of the current node's parent to check difference with the currently processed module (taken from @p ctx).
- * @param[in] name Name of the node to update path with. If NULL, the last segment is removed. If the format is `{keyword}`, the following
- * call updates the segment to the form `{keyword='name'}` (to remove this compound segment, 2 calls with NULL @p name must be used).
- */
-void lysc_update_path(struct lysc_ctx *ctx, struct lys_module *parent_module, const char *name);
-
-/** @} extensionscompile */
-
-/**
  * @brief Callback to compile extension from the lysp_ext_instance to the lysc_ext_instance. The later structure is generally prepared
  * and only the extension specific data are supposed to be added (if any).
  *
diff --git a/src/plugins_exts_compile.h b/src/plugins_exts_compile.h
new file mode 100644
index 0000000..42e21fd
--- /dev/null
+++ b/src/plugins_exts_compile.h
@@ -0,0 +1,133 @@
+/**
+ * @file plugins_exts_compile.h
+ * @author Radek Krejci <rkrejci@cesnet.cz>
+ * @brief libyang support for YANG extensions implementation - schema compilation related items.
+ *
+ * Copyright (c) 2015 - 2021 CESNET, z.s.p.o.
+ *
+ * This source code is licensed under BSD 3-Clause License (the "License").
+ * You may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     https://opensource.org/licenses/BSD-3-Clause
+ */
+
+#ifndef LY_PLUGINS_EXTS_COMPILE_H_
+#define LY_PLUGINS_EXTS_COMPILE_H_
+
+#include <stdint.h>
+
+#include "log.h"
+#include "tree_schema.h"
+
+struct ly_ctx;
+struct lyd_node;
+struct lysc_ctx;
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * @ingroup extensions YANG Extensions
+ *
+ * @{
+ */
+
+/**
+ * @defgroup scflags Schema compile flags
+ *
+ * Flags to modify schema compilation process and change the way how the particular statements are being compiled. *
+ * @{
+ */
+#define LYS_COMPILE_GROUPING        0x01            /**< Compiling (validation) of a non-instantiated grouping.
+                                                      In this case not all the restrictions are checked since they can
+                                                      be valid only in the real placement of the grouping.
+                                                      TODO - what specifically is not done */
+#define LYS_COMPILE_DISABLED        0x02            /**< Compiling a disabled subtree (by its if-features). Meaning
+                                                      it will be removed at the end of compilation and should not be
+                                                      added to any unres sets. */
+#define LYS_COMPILE_NO_CONFIG       0x04            /**< ignore config statements, neither inherit config value */
+#define LYS_COMPILE_NO_DISABLED     0x08            /**< ignore if-feature statements */
+
+#define LYS_COMPILE_RPC_INPUT       (LYS_IS_INPUT | LYS_COMPILE_NO_CONFIG)  /**< Internal option when compiling schema tree of RPC/action input */
+#define LYS_COMPILE_RPC_OUTPUT      (LYS_IS_OUTPUT | LYS_COMPILE_NO_CONFIG) /**< Internal option when compiling schema tree of RPC/action output */
+#define LYS_COMPILE_NOTIFICATION    (LYS_IS_NOTIF | LYS_COMPILE_NO_CONFIG)  /**< Internal option when compiling schema tree of Notification */
+
+/** @} scflags */
+
+/**
+ * @brief YANG schema compilation context for use in ::lyext_clb_compile callback implementation.
+ *
+ * The structure stores complex information connected with the schema compilation process. In the most simple case,
+ * the callback is just supposed to pass the provided callback to ::lys_compile_extension_instance() functions.
+ *
+ * To access various items from the context, use some of the following lysc_ctx_get_* getters.
+ */
+struct lysc_ctx;
+
+/**
+ * @brief YANG schema compilation context getter for libyang context.
+ * @param[in] ctx YANG schema compilation context.
+ * @return libyang context connected with the compilation context.
+ */
+struct ly_ctx *lysc_ctx_get_ctx(const struct lysc_ctx *ctx);
+
+/**
+ * @brief YANG schema compilation context getter for compilation options.
+ * @param[in] ctx YANG schema compilation context.
+ * @return pointer to the compilation context to allow modifying the options with @ref scflags values.
+ */
+uint32_t *lysc_ctx_get_options(const struct lysc_ctx *ctx);
+
+/**
+ * @brief YANG schema compilation context getter for path being currently processed.
+ * @param[in] ctx YANG schema compilation context.
+ * @return path identifying the place in schema being currently processed by the schema compiler.
+ */
+const char *lysc_ctx_get_path(const struct lysc_ctx *ctx);
+
+/**
+ * @brief Compile substatements of an extension instance.
+ *
+ * Uses standard libyang schema compiler to transform YANG statements into the compiled schema structures. The plugins are
+ * supposed to use this function when the extension instance's substatements are supposed to be compiled in a standard way
+ * (or if just the @ref scflags are enough to modify the compilation process).
+ *
+ * @param[in] ctx YANG schema compile context to track the compilation state.
+ * @param[in] ext_p Parsed representation of the extension instance being processed.
+ * @param[in,out] ext Compiled extension instance with the prepared ::lysc_ext_instance.substmts array, which will be updated
+ * by storing the compiled data.
+ * @return LY_SUCCESS on success.
+ * @return LY_EVALID if compilation of the substatements fails.
+ * @return LY_ENOT if the extension is disabled (by if-feature) and should be ignored.
+ */
+LY_ERR lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext);
+
+/**
+ * @brief Update path in the compile context, which is used for logging where the compilation failed.
+ *
+ * @param[in] ctx Compile context with the path.
+ * @param[in] parent_module Module of the current node's parent to check difference with the currently processed module (taken from @p ctx).
+ * @param[in] name Name of the node to update path with. If NULL, the last segment is removed. If the format is `{keyword}`, the following
+ * call updates the segment to the form `{keyword='name'}` (to remove this compound segment, 2 calls with NULL @p name must be used).
+ */
+void lysc_update_path(struct lysc_ctx *ctx, struct lys_module *parent_module, const char *name);
+
+/**
+ * @brief Duplicate the compiled extension (definition) structure.
+ * TODO should this be in API? currently required by nacm_compile()
+ * Instead of duplicating memory, the reference counter in the @p orig is increased.
+ *
+ * @param[in] orig The extension structure to duplicate.
+ * @return The duplicated structure to use.
+ */
+struct lysc_ext *lysc_ext_dup(struct lysc_ext *orig);
+
+/** @} extensions */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* LY_PLUGINS_EXTS_COMPILE_H_ */
diff --git a/src/plugins_exts_metadata.c b/src/plugins_exts_metadata.c
index f8cd559..f4ab6f8 100644
--- a/src/plugins_exts_metadata.c
+++ b/src/plugins_exts_metadata.c
@@ -12,11 +12,8 @@
  *     https://opensource.org/licenses/BSD-3-Clause
  */
 
-#include <stdlib.h>
-
-#include "plugins_exts.h"
 #include "plugins_exts_metadata.h"
-#include "schema_compile.h"
+
 #include "tree_edit.h"
 #include "tree_schema.h"
 
@@ -54,14 +51,15 @@
 
     /* annotations can appear only at the top level of a YANG module or submodule */
     if ((c_ext->parent_stmt != LY_STMT_MODULE) && (c_ext->parent_stmt != LY_STMT_SUBMODULE)) {
-        lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path, "Extension %s is allowed only at the top level of a YANG module or submodule, but it is placed in \"%s\" statement.",
+        lyext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
+                "Extension %s is allowed only at the top level of a YANG module or submodule, but it is placed in \"%s\" statement.",
                 p_ext->name, ly_stmt2str(c_ext->parent_stmt));
         return LY_EVALID;
     }
     /* check mandatory argument */
     if (!c_ext->argument) {
-        lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path, "Extension %s is instantiated without mandatory argument representing metadata name.",
-                p_ext->name);
+        lyext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
+                "Extension %s is instantiated without mandatory argument representing metadata name.", p_ext->name);
         return LY_EVALID;
     }
 
@@ -71,7 +69,7 @@
     LY_ARRAY_FOR(mod_c->exts, u) {
         if ((&mod_c->exts[u] != c_ext) && (mod_c->exts[u].def == c_ext->def) && !strcmp(mod_c->exts[u].argument, c_ext->argument)) {
             /* duplication of the same annotation extension in a single module */
-            lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path, "Extension %s is instantiated multiple times.", p_ext->name);
+            lyext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx), "Extension %s is instantiated multiple times.", p_ext->name);
             return LY_EVALID;
         }
     }
@@ -81,7 +79,7 @@
     if (!annotation) {
         goto emem;
     }
-    LY_ARRAY_CREATE_GOTO(cctx->ctx, c_ext->substmts, 6, ret, emem);
+    LY_ARRAY_CREATE_GOTO(lysc_ctx_get_ctx(cctx), c_ext->substmts, 6, ret, emem);
 
     LY_ARRAY_INCREMENT(c_ext->substmts);
     c_ext->substmts[ANNOTATION_SUBSTMT_IFF].stmt = LY_STMT_IF_FEATURE;
@@ -117,7 +115,7 @@
     return ret;
 
 emem:
-    lyext_log(c_ext, LY_LLERR, LY_EMEM, cctx->path, "Memory allocation failed (%s()).", __func__);
+    lyext_log(c_ext, LY_LLERR, LY_EMEM, lysc_ctx_get_path(cctx), "Memory allocation failed (%s()).", __func__);
     return LY_EMEM;
 }
 
diff --git a/src/plugins_exts_metadata.h b/src/plugins_exts_metadata.h
index 541891b..bc78d91 100644
--- a/src/plugins_exts_metadata.h
+++ b/src/plugins_exts_metadata.h
@@ -15,8 +15,6 @@
 #ifndef LY_PLUGINS_EXTS_METADATA_H_
 #define LY_PLUGINS_EXTS_METADATA_H_
 
-#include "tree_schema.h"
-
 #ifdef __cplusplus
 extern "C" {
 #endif
diff --git a/src/plugins_exts_nacm.c b/src/plugins_exts_nacm.c
index c8ee553..9cd1f3d 100644
--- a/src/plugins_exts_nacm.c
+++ b/src/plugins_exts_nacm.c
@@ -15,6 +15,8 @@
 #include <stdlib.h>
 
 #include "plugins_exts.h"
+
+#include "dict.h"
 #include "tree_edit.h"
 #include "tree_schema.h"
 
@@ -103,7 +105,8 @@
 
     /* check that the extension is instantiated at an allowed place - data node */
     if (!LY_STMT_IS_NODE(c_ext->parent_stmt)) {
-        lyext_log(c_ext, LY_LLWRN, 0, cctx->path, "Extension %s is allowed only in a data nodes, but it is placed in \"%s\" statement.",
+        lyext_log(c_ext, LY_LLWRN, 0, lysc_ctx_get_path(cctx),
+                "Extension %s is allowed only in a data nodes, but it is placed in \"%s\" statement.",
                 p_ext->name, ly_stmt2str(c_ext->parent_stmt));
         return LY_ENOT;
     } else {
@@ -113,7 +116,7 @@
             /* note LYS_AUGMENT and LYS_USES is not in the list since they are not present in the compiled tree. Instead, libyang
              * passes all their extensions to their children nodes */
 invalid_parent:
-            lyext_log(c_ext, LY_LLWRN, 0, cctx->path,
+            lyext_log(c_ext, LY_LLWRN, 0, lysc_ctx_get_path(cctx),
                     "Extension %s is not allowed in %s statement.", p_ext->name, lys_nodetype2str(parent->nodetype));
             return LY_ENOT;
         }
@@ -129,9 +132,11 @@
              * We check plugin since we want to catch even the situation that there is default-deny-all
              * AND default-deny-write */
             if (parent->exts[u].def == c_ext->def) {
-                lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path, "Extension %s is instantiated multiple times.", p_ext->name);
+                lyext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
+                        "Extension %s is instantiated multiple times.", p_ext->name);
             } else {
-                lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path, "Extension nacm:default-deny-write is mixed with nacm:default-deny-all.");
+                lyext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
+                        "Extension nacm:default-deny-write is mixed with nacm:default-deny-all.");
             }
             return LY_EVALID;
         }
diff --git a/src/plugins_exts_yangdata.c b/src/plugins_exts_yangdata.c
index dd14e43..f398cac 100644
--- a/src/plugins_exts_yangdata.c
+++ b/src/plugins_exts_yangdata.c
@@ -15,7 +15,6 @@
 #include <stdlib.h>
 
 #include "plugins_exts.h"
-#include "schema_compile.h"
 
 #include "tree_edit.h"
 #include "tree_schema.h"
@@ -50,11 +49,11 @@
     struct lysc_module *mod_c;
     const struct lysc_node *child;
     ly_bool valid = 1;
-    uint32_t prev_options = cctx->options;
+    uint32_t prev_options = *lysc_ctx_get_options(cctx);
 
     /* yang-data can appear only at the top level of a YANG module or submodule */
     if ((c_ext->parent_stmt != LY_STMT_MODULE) && (c_ext->parent_stmt != LY_STMT_SUBMODULE)) {
-        lyext_log(c_ext, LY_LLWRN, 0, cctx->path,
+        lyext_log(c_ext, LY_LLWRN, 0, lysc_ctx_get_path(cctx),
                 "Extension %s is ignored since it appears as a non top-level statement in \"%s\" statement.",
                 p_ext->name, ly_stmt2str(c_ext->parent_stmt));
         return LY_ENOT;
@@ -66,7 +65,7 @@
     LY_ARRAY_FOR(mod_c->exts, u) {
         if ((&mod_c->exts[u] != c_ext) && (mod_c->exts[u].def == c_ext->def) && !strcmp(mod_c->exts[u].argument, c_ext->argument)) {
             /* duplication of the same yang-data extension in a single module */
-            lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path, "Extension %s is instantiated multiple times.", p_ext->name);
+            lyext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx), "Extension %s is instantiated multiple times.", p_ext->name);
             return LY_EVALID;
         }
     }
@@ -91,23 +90,25 @@
     c_ext->substmts[2].cardinality = LY_STMT_CARD_OPT;
     c_ext->substmts[2].storage = &c_ext->data;
 
-    cctx->options |= LYS_COMPILE_NO_CONFIG | LYS_COMPILE_NO_DISABLED;
+    *lysc_ctx_get_options(cctx) |= LYS_COMPILE_NO_CONFIG | LYS_COMPILE_NO_DISABLED;
     ret = lys_compile_extension_instance(cctx, p_ext, c_ext);
-    cctx->options = prev_options;
+    *lysc_ctx_get_options(cctx) = prev_options;
     LY_ARRAY_DECREMENT(c_ext->substmts);
     LY_ARRAY_DECREMENT(c_ext->substmts);
-    LY_CHECK_RET(ret);
+    if (ret) {
+        return ret;
+    }
 
     /* check that we have really just a single container data definition in the top */
     child = *(struct lysc_node **)c_ext->substmts[0].storage;
     if (!child) {
         valid = 0;
-        lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path,
+        lyext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
                 "Extension %s is instantiated without any top level data node, but exactly one container data node is expected.",
                 p_ext->name);
     } else if (child->next) {
         valid = 0;
-        lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path,
+        lyext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
                 "Extension %s is instantiated with multiple top level data nodes, but only a single container data node is allowed.",
                 p_ext->name);
     } else if (child->nodetype == LYS_CHOICE) {
@@ -117,13 +118,13 @@
         while ((snode = lys_getnext(snode, child, mod_c, 0))) {
             if (snode->next) {
                 valid = 0;
-                lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path,
+                lyext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
                         "Extension %s is instantiated with multiple top level data nodes (inside a single choice's case), "
                         "but only a single container data node is allowed.", p_ext->name);
                 break;
             } else if (snode->nodetype != LYS_CONTAINER) {
                 valid = 0;
-                lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path,
+                lyext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
                         "Extension %s is instantiated with %s top level data node (inside a choice), "
                         "but only a single container data node is allowed.", p_ext->name, lys_nodetype2str(snode->nodetype));
                 break;
@@ -132,13 +133,13 @@
     } else if (child->nodetype != LYS_CONTAINER) {
         /* via uses */
         valid = 0;
-        lyext_log(c_ext, LY_LLERR, LY_EVALID, cctx->path,
+        lyext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
                 "Extension %s is instantiated with %s top level data node, but only a single container data node is allowed.",
                 p_ext->name, lys_nodetype2str(child->nodetype));
     }
 
     if (!valid) {
-        yangdata_free(cctx->ctx, c_ext);
+        yangdata_free(lysc_ctx_get_ctx(cctx), c_ext);
         c_ext->data = c_ext->substmts = NULL;
         return LY_EVALID;
     }
@@ -146,7 +147,7 @@
     return LY_SUCCESS;
 
 emem:
-    lyext_log(c_ext, LY_LLERR, LY_EMEM, cctx->path, "Memory allocation failed (%s()).", __func__);
+    lyext_log(c_ext, LY_LLERR, LY_EMEM, lysc_ctx_get_path(cctx), "Memory allocation failed (%s()).", __func__);
     return LY_EMEM;
 }
 
diff --git a/src/schema_compile.h b/src/schema_compile.h
index 3eaa550..0f114f0 100644
--- a/src/schema_compile.h
+++ b/src/schema_compile.h
@@ -21,6 +21,7 @@
 #include "common.h"
 #include "dict.h"
 #include "log.h"
+#include "plugins_exts_compile.h"
 #include "set.h"
 #include "tree.h"
 #include "tree_data.h"
@@ -30,35 +31,10 @@
 struct lyxp_expr;
 
 /**
- * @defgroup scflags Schema compile flags
- *
- * Flags are currently used only internally - the compilation process does not have a public interface and it is
- * integrated in the schema parsers. The current options set does not make sense for public used, but it can be a way
- * to modify behavior of the compilation process in future.
- *
- * @{
- */
-#define LYS_COMPILE_GROUPING        0x01            /**< Compiling (validation) of a non-instantiated grouping.
-                                                      In this case not all the restrictions are checked since they can
-                                                      be valid only in the real placement of the grouping.
-                                                      TODO - what specifically is not done */
-#define LYS_COMPILE_DISABLED        0x02            /**< Compiling a disabled subtree (by its if-features). Meaning
-                                                      it will be removed at the end of compilation and should not be
-                                                      added to any unres sets. */
-#define LYS_COMPILE_NO_CONFIG       0x04            /**< ignore config statements, neither inherit config value */
-#define LYS_COMPILE_NO_DISABLED     0x08            /**< ignore if-feature statements */
-
-#define LYS_COMPILE_RPC_INPUT       (LYS_IS_INPUT | LYS_COMPILE_NO_CONFIG)  /**< Internal option when compiling schema tree of RPC/action input */
-#define LYS_COMPILE_RPC_OUTPUT      (LYS_IS_OUTPUT | LYS_COMPILE_NO_CONFIG) /**< Internal option when compiling schema tree of RPC/action output */
-#define LYS_COMPILE_NOTIFICATION    (LYS_IS_NOTIF | LYS_COMPILE_NO_CONFIG)  /**< Internal option when compiling schema tree of Notification */
-
-/** @} scflags */
-
-/**
- * @brief internal context for compilation
+ * @brief YANG schema compilation context.
  */
 struct lysc_ctx {
-    struct ly_ctx *ctx;
+    struct ly_ctx *ctx;         /**< libyang context */
     struct lys_module *cur_mod; /**< module currently being compiled,
                                      - identifier/path - used as the current module for unprefixed nodes
                                      - augment - module where the augment is defined
@@ -72,17 +48,17 @@
     struct lysc_ext_instance *ext; /**< extension instance being processed and serving as a source for its substatements
                                      instead of the module itself */
     struct ly_set groupings;    /**< stack for groupings circular check */
-    struct ly_set tpdf_chain;
-    struct ly_set disabled;     /**< set of compiled nodes whose if-feature(s) was not satisifed */
-    struct ly_set augs;         /**< set of compiled non-applied top-level augments */
-    struct ly_set devs;         /**< set of compiled non-applied deviations */
-    struct ly_set uses_augs;    /**< set of compiled non-applied uses augments */
-    struct ly_set uses_rfns;    /**< set of compiled non-applied uses refines */
+    struct ly_set tpdf_chain;   /**< stack for typedefs circular check */
+    struct ly_set disabled;     /**< set of compiled nodes whose if-feature(s) was not satisfied (stored ::lysc_node *) */
+    struct ly_set augs;         /**< set of compiled non-applied top-level augments (stored ::lysc_augment *) */
+    struct ly_set devs;         /**< set of compiled non-applied deviations (stored ::lysc_deviation *) */
+    struct ly_set uses_augs;    /**< set of compiled non-applied uses augments (stored ::lysc_augment *) */
+    struct ly_set uses_rfns;    /**< set of compiled non-applied uses refines (stored ::lysc_refine *) */
     struct lys_glob_unres *unres;  /**< global unres sets */
-    uint32_t path_len;
+    uint32_t path_len;          /**< number of path bytes used */
     uint32_t options;           /**< various @ref scflags. */
 #define LYSC_CTX_BUFSIZE 4078
-    char path[LYSC_CTX_BUFSIZE];
+    char path[LYSC_CTX_BUFSIZE];/**< Path identifying the schema node currently being processed */
 };
 
 /**
diff --git a/src/tree_schema.h b/src/tree_schema.h
index e8ef50d..7d7eacc 100644
--- a/src/tree_schema.h
+++ b/src/tree_schema.h
@@ -515,9 +515,10 @@
     void *prefix_data;                      /**< Format-specific data for prefix resolution
                                                  (see ::ly_type_store_resolve_prefix()) */
 
+    struct lysp_stmt *child;                /**< list of the extension's substatements (linked list) */
+
     void *parent;                           /**< pointer to the parent element holding the extension instance(s), use
                                                  ::lysp_ext_instance#parent_stmt to access the schema element */
-    struct lysp_stmt *child;                /**< list of the extension's substatements (linked list) */
     enum ly_stmt parent_stmt;               /**< value identifying placement of the extension instance */
     LY_ARRAY_COUNT_TYPE parent_stmt_index;  /**< in case the instance is in a substatement, this identifies
                                                  the index of that substatement in its [sized array](@ref sizedarrays) (if any) */
@@ -1398,7 +1399,8 @@
     const char *argument;            /**< optional value of the extension's argument */
     struct lys_module *module;       /**< module where the extension instantiated is defined */
     struct lysc_ext_instance *exts;  /**< list of the extension instances ([sized array](@ref sizedarrays)) */
-    struct lysc_ext_substmt *substmts; /**< list of allowed substatements with the storage to access the present substatements */
+    struct lysc_ext_substmt *substmts; /**< list of allowed substatements with the storage to access the present
+                                          substatements ([sized array](@ref sizedarrays)) */
     void *data;                      /**< private plugins's data, not used by libyang */
 
     void *parent;                    /**< pointer to the parent element holding the extension instance(s), use