extension instances CHANGE access to the extension instance's substatements

Provide libyang a transparent access to the substatements of a specific
extension instances for various generic processing.

The patch introduces lysc_ext_substmt() to simplify work with the newly
added lysc_ext_instance.substmts sized array providing the extension
instance's substatements.
diff --git a/src/plugins_exts_metadata.c b/src/plugins_exts_metadata.c
index c25e4e1..99df4f0 100644
--- a/src/plugins_exts_metadata.c
+++ b/src/plugins_exts_metadata.c
@@ -26,22 +26,18 @@
 LYEXT_VERSION_CHECK
  */
 
-#define ANNOTATION_SUBSTMT_IFF     0
-#define ANNOTATION_SUBSTMT_UNITS   1
-#define ANNOTATION_SUBSTMT_STATUS  2
-#define ANNOTATION_SUBSTMT_TYPE    3
-#define ANNOTATION_SUBSTMT_DSC     4
-#define ANNOTATION_SUBSTMT_REF     5
-
-#define INIT_ANNOTATION_SUBSTMT { \
-    {LY_STMT_IF_FEATURE, LY_STMT_CARD_ANY, NULL}, \
-    {LY_STMT_UNITS, LY_STMT_CARD_OPT, NULL}, \
-    {LY_STMT_STATUS, LY_STMT_CARD_OPT, NULL}, \
-    {LY_STMT_TYPE, LY_STMT_CARD_MAND, NULL}, \
-    {LY_STMT_DESCRIPTION, LY_STMT_CARD_OPT, NULL}, \
-    {LY_STMT_REFERENCE, LY_STMT_CARD_OPT, NULL}, \
-    {0, 0, 0} /* terminating item */ \
-} \
+/**
+ * @brief Representation of the compiled metadata substatements - simplify storage for the items available via
+ * ::lysc_ext_substmt.
+ */
+struct lyext_metadata {
+    struct lysc_type *type;            /**< type of the metadata (mandatory) */
+    const char *units;                 /**< units of the leaf's type */
+    struct lysc_iffeature *iffeatures; /**< list of if-feature expressions ([sized array](@ref sizedarrays)) */
+    const char *dsc;                   /**< description */
+    const char *ref;                   /**< reference */
+    uint16_t flags;                    /**< [schema node flags](@ref snodeflags) - only LYS_STATUS_* values are allowed */
+};
 
 /**
  * @brief Compile annotation extension instances.
@@ -54,7 +50,6 @@
     struct lyext_metadata *annotation;
     struct lysc_module *mod_c;
     LY_ARRAY_COUNT_TYPE u;
-    struct lysc_ext_substmt annotation_substmt[] = INIT_ANNOTATION_SUBSTMT;
 
     /* annotations can appear only at the top level of a YANG module or submodule */
     if (c_ext->parent_type != LYEXT_PAR_MODULE) {
@@ -81,15 +76,46 @@
     }
 
     /* compile annotation substatements */
+    LY_ARRAY_CREATE_RET(cctx->ctx, c_ext->substmts, 6, LY_EMEM);
     c_ext->data = annotation = calloc(1, sizeof *annotation);
-    LY_CHECK_ERR_RET(!annotation, LOGMEM(cctx->ctx), LY_EMEM);
-    annotation_substmt[ANNOTATION_SUBSTMT_IFF].storage = &annotation->iffeatures;
-    annotation_substmt[ANNOTATION_SUBSTMT_UNITS].storage = &annotation->units;
-    annotation_substmt[ANNOTATION_SUBSTMT_STATUS].storage = &annotation->flags;
-    annotation_substmt[ANNOTATION_SUBSTMT_TYPE].storage = &annotation->type;
-    /* description and reference are allowed, but not compiled */
+    if (!annotation) {
+        LOGMEM(cctx->ctx);
+        LY_ARRAY_FREE(c_ext->substmts);
+        c_ext->substmts = NULL;
+        return LY_EMEM;
+    }
 
-    LY_CHECK_RET(lys_compile_extension_instance(cctx, p_ext, annotation_substmt));
+    LY_ARRAY_INCREMENT(c_ext->substmts);
+    c_ext->substmts[ANNOTATION_SUBSTMT_IFF].stmt = LY_STMT_IF_FEATURE;
+    c_ext->substmts[ANNOTATION_SUBSTMT_IFF].cardinality = LY_STMT_CARD_ANY;
+    c_ext->substmts[ANNOTATION_SUBSTMT_IFF].storage = &annotation->iffeatures;
+
+    LY_ARRAY_INCREMENT(c_ext->substmts);
+    c_ext->substmts[ANNOTATION_SUBSTMT_UNITS].stmt = LY_STMT_UNITS;
+    c_ext->substmts[ANNOTATION_SUBSTMT_UNITS].cardinality = LY_STMT_CARD_OPT;
+    c_ext->substmts[ANNOTATION_SUBSTMT_UNITS].storage = &annotation->units;
+
+    LY_ARRAY_INCREMENT(c_ext->substmts);
+    c_ext->substmts[ANNOTATION_SUBSTMT_STATUS].stmt = LY_STMT_STATUS;
+    c_ext->substmts[ANNOTATION_SUBSTMT_STATUS].cardinality = LY_STMT_CARD_OPT;
+    c_ext->substmts[ANNOTATION_SUBSTMT_STATUS].storage = &annotation->flags;
+
+    LY_ARRAY_INCREMENT(c_ext->substmts);
+    c_ext->substmts[ANNOTATION_SUBSTMT_TYPE].stmt = LY_STMT_TYPE;
+    c_ext->substmts[ANNOTATION_SUBSTMT_TYPE].cardinality = LY_STMT_CARD_MAND;
+    c_ext->substmts[ANNOTATION_SUBSTMT_TYPE].storage = &annotation->type;
+
+    LY_ARRAY_INCREMENT(c_ext->substmts);
+    c_ext->substmts[ANNOTATION_SUBSTMT_DSC].stmt = LY_STMT_DESCRIPTION;
+    c_ext->substmts[ANNOTATION_SUBSTMT_DSC].cardinality = LY_STMT_CARD_OPT;
+    c_ext->substmts[ANNOTATION_SUBSTMT_DSC].storage = &annotation->dsc;
+
+    LY_ARRAY_INCREMENT(c_ext->substmts);
+    c_ext->substmts[ANNOTATION_SUBSTMT_REF].stmt = LY_STMT_REFERENCE;
+    c_ext->substmts[ANNOTATION_SUBSTMT_REF].cardinality = LY_STMT_CARD_OPT;
+    c_ext->substmts[ANNOTATION_SUBSTMT_REF].storage = &annotation->ref;
+
+    LY_CHECK_RET(lys_compile_extension_instance(cctx, p_ext, c_ext->substmts));
 
     return LY_SUCCESS;
 }
@@ -102,20 +128,11 @@
 void
 annotation_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
 {
-    struct lysc_ext_substmt annotation_substmt[] = INIT_ANNOTATION_SUBSTMT;
-
-    if (!ext->data) {
+    if (!ext->substmts) {
         return;
     }
 
-    struct lyext_metadata *annotation = (struct lyext_metadata *)ext->data;
-
-    annotation_substmt[ANNOTATION_SUBSTMT_IFF].storage = &annotation->iffeatures;
-    annotation_substmt[ANNOTATION_SUBSTMT_UNITS].storage = &annotation->units;
-    annotation_substmt[ANNOTATION_SUBSTMT_STATUS].storage = &annotation->flags;
-    annotation_substmt[ANNOTATION_SUBSTMT_TYPE].storage = &annotation->type;
-
-    lysc_extension_instance_free(ctx, annotation_substmt);
+    lysc_extension_instance_substatements_free(ctx, ext->substmts);
     free(ext->data);
 }