blob: 567387df5c69325d050eb7dc3ca3ca721623a713 [file] [log] [blame]
Radek Krejci19a96102018-11-15 13:38:09 +01001/**
Radek Krejcie7b95092019-05-15 11:03:07 +02002 * @file tree_schema_free.c
Radek Krejci19a96102018-11-15 13:38:09 +01003 * @author Radek Krejci <rkrejci@cesnet.cz>
Radek Krejcie7b95092019-05-15 11:03:07 +02004 * @brief Freeing functions for schema tree structures.
Radek Krejci19a96102018-11-15 13:38:09 +01005 *
Radek Krejcie7b95092019-05-15 11:03:07 +02006 * Copyright (c) 2019 CESNET, z.s.p.o.
Radek Krejci19a96102018-11-15 13:38:09 +01007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Michal Vaskoee757602021-06-10 14:38:19 +020015#include <assert.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020016#include <stdlib.h>
17
Radek Krejci535ea9f2020-05-29 16:01:05 +020018#include "common.h"
Michal Vasko69730152020-10-09 16:30:07 +020019#include "compat.h"
Radek Krejci77114102021-03-10 15:21:57 +010020#include "dict.h"
Radek Krejci47fab892020-11-05 17:02:41 +010021#include "log.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020022#include "plugins_exts.h"
23#include "plugins_types.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020024#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include "tree_data.h"
Michal Vaskofc2cd072021-02-24 13:17:17 +010026#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010027#include "tree_edit.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010029#include "tree_schema_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020030#include "xml.h"
Radek Krejci19a96102018-11-15 13:38:09 +010031#include "xpath.h"
32
Radek Krejci2a9fc652021-01-22 17:44:34 +010033void lysp_grp_free(struct ly_ctx *ctx, struct lysp_node_grp *grp);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010034static void lysc_node_free_(struct ly_ctx *ctx, struct lysc_node *node);
Radek Krejci19a96102018-11-15 13:38:09 +010035
36static void
37lysp_stmt_free(struct ly_ctx *ctx, struct lysp_stmt *stmt)
38{
39 struct lysp_stmt *child, *next;
40
Michal Vaskoe180ed02021-02-05 16:31:20 +010041 lydict_remove(ctx, stmt->stmt);
42 lydict_remove(ctx, stmt->arg);
Michal Vaskofc2cd072021-02-24 13:17:17 +010043 ly_free_prefix_data(stmt->format, stmt->prefix_data);
Radek Krejci19a96102018-11-15 13:38:09 +010044
45 LY_LIST_FOR_SAFE(stmt->child, next, child) {
46 lysp_stmt_free(ctx, child);
47 }
48
49 free(stmt);
50}
51
Radek Krejci2d7a47b2019-05-16 13:34:10 +020052void
Radek Krejci19a96102018-11-15 13:38:09 +010053lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext)
54{
55 struct lysp_stmt *stmt, *next;
aPiecek60d9d672021-04-27 15:49:57 +020056 struct lysp_node *node, *next_node;
Radek Krejci19a96102018-11-15 13:38:09 +010057
Michal Vaskoe180ed02021-02-05 16:31:20 +010058 lydict_remove(ctx, ext->name);
59 lydict_remove(ctx, ext->argument);
Michal Vaskofc2cd072021-02-24 13:17:17 +010060 ly_free_prefix_data(ext->format, ext->prefix_data);
aPiecek60d9d672021-04-27 15:49:57 +020061 LY_LIST_FOR_SAFE(ext->parsed, next_node, node) {
62 lysp_node_free(ctx, node);
63 }
Radek Krejci19a96102018-11-15 13:38:09 +010064
65 LY_LIST_FOR_SAFE(ext->child, next, stmt) {
66 lysp_stmt_free(ctx, stmt);
67 }
68}
69
David Sedlák298ff6d2019-07-26 14:29:03 +020070void
Radek Krejci19a96102018-11-15 13:38:09 +010071lysp_import_free(struct ly_ctx *ctx, struct lysp_import *import)
72{
73 /* imported module is freed directly from the context's list */
Michal Vaskoe180ed02021-02-05 16:31:20 +010074 lydict_remove(ctx, import->name);
75 lydict_remove(ctx, import->prefix);
76 lydict_remove(ctx, import->dsc);
77 lydict_remove(ctx, import->ref);
Radek Krejci19a96102018-11-15 13:38:09 +010078 FREE_ARRAY(ctx, import->exts, lysp_ext_instance_free);
79}
80
Radek Krejci771928a2021-01-19 13:42:36 +010081/**
82 * @brief Common function to erase include record in main module and submodule.
83 *
84 * There is a difference since the main module is expected to have the complete list if the included submodules and
85 * the parsed submodule is shared with any include in a submodule. Therefore, the referenced submodules in the include
86 * record are freed only from main module's records.
87 *
88 * @param[in] ctx libyang context
89 * @param[in] include The include record to be erased, the record itself is not freed.
90 * @param[in] main_module Flag to get know if the include record is placed in main module so also the referenced submodule
91 * is supposed to be freed.
92 */
93static void
94lysp_include_free_(struct ly_ctx *ctx, struct lysp_include *include, ly_bool main_module)
Radek Krejci19a96102018-11-15 13:38:09 +010095{
Radek Krejci771928a2021-01-19 13:42:36 +010096 if (main_module && include->submodule) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +020097 lysp_module_free((struct lysp_module *)include->submodule);
Radek Krejci19a96102018-11-15 13:38:09 +010098 }
Michal Vaskoe180ed02021-02-05 16:31:20 +010099 lydict_remove(ctx, include->name);
100 lydict_remove(ctx, include->dsc);
101 lydict_remove(ctx, include->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100102 FREE_ARRAY(ctx, include->exts, lysp_ext_instance_free);
103}
104
David Sedlákaa854b02019-07-22 14:17:10 +0200105void
Radek Krejci771928a2021-01-19 13:42:36 +0100106lysp_include_free_submodule(struct ly_ctx *ctx, struct lysp_include *include)
107{
Michal Vaskoa0a498b2021-09-22 12:17:48 +0200108 lysp_include_free_(ctx, include, 0);
Radek Krejci771928a2021-01-19 13:42:36 +0100109}
110
111void
112lysp_include_free(struct ly_ctx *ctx, struct lysp_include *include)
113{
Michal Vaskoa0a498b2021-09-22 12:17:48 +0200114 lysp_include_free_(ctx, include, 1);
Radek Krejci771928a2021-01-19 13:42:36 +0100115}
116
117void
Radek Krejci19a96102018-11-15 13:38:09 +0100118lysp_revision_free(struct ly_ctx *ctx, struct lysp_revision *rev)
119{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100120 lydict_remove(ctx, rev->dsc);
121 lydict_remove(ctx, rev->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100122 FREE_ARRAY(ctx, rev->exts, lysp_ext_instance_free);
123}
124
David Sedlák986cb412019-07-04 13:10:11 +0200125void
Radek Krejci19a96102018-11-15 13:38:09 +0100126lysp_ext_free(struct ly_ctx *ctx, struct lysp_ext *ext)
127{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100128 lydict_remove(ctx, ext->name);
Radek Krejci9f87b0c2021-03-05 14:45:26 +0100129 lydict_remove(ctx, ext->argname);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100130 lydict_remove(ctx, ext->dsc);
131 lydict_remove(ctx, ext->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100132 FREE_ARRAY(ctx, ext->exts, lysp_ext_instance_free);
Michal Vasko5fe75f12020-03-02 13:52:37 +0100133 if (ext->compiled) {
134 lysc_extension_free(ctx, &ext->compiled);
135 }
Radek Krejci19a96102018-11-15 13:38:09 +0100136}
137
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200138void
Radek Krejci19a96102018-11-15 13:38:09 +0100139lysp_feature_free(struct ly_ctx *ctx, struct lysp_feature *feat)
140{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100141 lydict_remove(ctx, feat->name);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200142 FREE_ARRAY(ctx, feat->iffeatures, lysp_qname_free);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100143 FREE_ARRAY(ctx, feat->iffeatures_c, lysc_iffeature_free);
144 LY_ARRAY_FREE(feat->depfeatures);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100145 lydict_remove(ctx, feat->dsc);
146 lydict_remove(ctx, feat->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100147 FREE_ARRAY(ctx, feat->exts, lysp_ext_instance_free);
148}
149
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200150void
Radek Krejci19a96102018-11-15 13:38:09 +0100151lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident)
152{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100153 lydict_remove(ctx, ident->name);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200154 FREE_ARRAY(ctx, ident->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100155 FREE_STRINGS(ctx, ident->bases);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100156 lydict_remove(ctx, ident->dsc);
157 lydict_remove(ctx, ident->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100158 FREE_ARRAY(ctx, ident->exts, lysp_ext_instance_free);
159}
160
Michal Vasko7f45cf22020-10-01 12:49:44 +0200161void
Radek Krejci19a96102018-11-15 13:38:09 +0100162lysp_restr_free(struct ly_ctx *ctx, struct lysp_restr *restr)
163{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100164 lydict_remove(ctx, restr->arg.str);
165 lydict_remove(ctx, restr->emsg);
166 lydict_remove(ctx, restr->eapptag);
167 lydict_remove(ctx, restr->dsc);
168 lydict_remove(ctx, restr->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100169 FREE_ARRAY(ctx, restr->exts, lysp_ext_instance_free);
170}
171
172static void
173lysp_type_enum_free(struct ly_ctx *ctx, struct lysp_type_enum *item)
174{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100175 lydict_remove(ctx, item->name);
176 lydict_remove(ctx, item->dsc);
177 lydict_remove(ctx, item->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200178 FREE_ARRAY(ctx, item->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100179 FREE_ARRAY(ctx, item->exts, lysp_ext_instance_free);
180}
181
Radek Krejcicdfecd92018-11-26 11:27:32 +0100182void lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type);
Michal Vasko004d3152020-06-11 19:59:22 +0200183
David Sedlák32488102019-07-15 17:44:10 +0200184void
Radek Krejci19a96102018-11-15 13:38:09 +0100185lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type)
186{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100187 lydict_remove(ctx, type->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100188 FREE_MEMBER(ctx, type->range, lysp_restr_free);
189 FREE_MEMBER(ctx, type->length, lysp_restr_free);
190 FREE_ARRAY(ctx, type->patterns, lysp_restr_free);
191 FREE_ARRAY(ctx, type->enums, lysp_type_enum_free);
192 FREE_ARRAY(ctx, type->bits, lysp_type_enum_free);
Michal Vasko004d3152020-06-11 19:59:22 +0200193 lyxp_expr_free(ctx, type->path);
Radek Krejci19a96102018-11-15 13:38:09 +0100194 FREE_STRINGS(ctx, type->bases);
195 FREE_ARRAY(ctx, type->types, lysp_type_free);
196 FREE_ARRAY(ctx, type->exts, lysp_ext_instance_free);
197 if (type->compiled) {
198 lysc_type_free(ctx, type->compiled);
199 }
200}
201
David Sedlák04e17b22019-07-19 15:29:48 +0200202void
Radek Krejci19a96102018-11-15 13:38:09 +0100203lysp_tpdf_free(struct ly_ctx *ctx, struct lysp_tpdf *tpdf)
204{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100205 lydict_remove(ctx, tpdf->name);
206 lydict_remove(ctx, tpdf->units);
207 lydict_remove(ctx, tpdf->dflt.str);
208 lydict_remove(ctx, tpdf->dsc);
209 lydict_remove(ctx, tpdf->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100210 FREE_ARRAY(ctx, tpdf->exts, lysp_ext_instance_free);
211
212 lysp_type_free(ctx, &tpdf->type);
213
214}
215
Radek Krejcif538ce52019-03-05 10:46:14 +0100216void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100217lysp_grp_free(struct ly_ctx *ctx, struct lysp_node_grp *grp)
Radek Krejci19a96102018-11-15 13:38:09 +0100218{
219 struct lysp_node *node, *next;
220
Radek Krejci19a96102018-11-15 13:38:09 +0100221 FREE_ARRAY(ctx, grp->typedefs, lysp_tpdf_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100222 LY_LIST_FOR_SAFE((struct lysp_node *)grp->groupings, next, node) {
223 lysp_node_free(ctx, node);
224 }
Radek Krejci01180ac2021-01-27 08:48:22 +0100225 LY_LIST_FOR_SAFE(grp->child, next, node) {
Radek Krejci19a96102018-11-15 13:38:09 +0100226 lysp_node_free(ctx, node);
227 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100228 LY_LIST_FOR_SAFE((struct lysp_node *)grp->actions, next, node) {
229 lysp_node_free(ctx, node);
230 }
231 LY_LIST_FOR_SAFE((struct lysp_node *)grp->notifs, next, node) {
232 lysp_node_free(ctx, node);
233 }
Radek Krejci19a96102018-11-15 13:38:09 +0100234}
235
Radek Krejcif09e4e82019-06-14 15:08:11 +0200236void
Radek Krejci19a96102018-11-15 13:38:09 +0100237lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when)
238{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100239 lydict_remove(ctx, when->cond);
240 lydict_remove(ctx, when->dsc);
241 lydict_remove(ctx, when->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100242 FREE_ARRAY(ctx, when->exts, lysp_ext_instance_free);
243}
244
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200245void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100246lysp_augment_free(struct ly_ctx *ctx, struct lysp_node_augment *augment)
Radek Krejci19a96102018-11-15 13:38:09 +0100247{
248 struct lysp_node *node, *next;
249
Radek Krejci19a96102018-11-15 13:38:09 +0100250 LY_LIST_FOR_SAFE(augment->child, next, node) {
251 lysp_node_free(ctx, node);
252 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100253 LY_LIST_FOR_SAFE((struct lysp_node *)augment->actions, next, node) {
254 lysp_node_free(ctx, node);
255 }
256 LY_LIST_FOR_SAFE((struct lysp_node *)augment->notifs, next, node) {
257 lysp_node_free(ctx, node);
258 }
Radek Krejci19a96102018-11-15 13:38:09 +0100259}
260
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200261void
Michal Vasko7f45cf22020-10-01 12:49:44 +0200262lysp_qname_free(struct ly_ctx *ctx, struct lysp_qname *qname)
263{
264 if (qname) {
Michal Vaskoe180ed02021-02-05 16:31:20 +0100265 lydict_remove(ctx, qname->str);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200266 }
267}
268
269void
Radek Krejci19a96102018-11-15 13:38:09 +0100270lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d)
271{
Michal Vasko22df3f02020-08-24 13:29:22 +0200272 struct lysp_deviate_add *add = (struct lysp_deviate_add *)d;
273 struct lysp_deviate_rpl *rpl = (struct lysp_deviate_rpl *)d;
Radek Krejci19a96102018-11-15 13:38:09 +0100274
275 FREE_ARRAY(ctx, d->exts, lysp_ext_instance_free);
Michal Vaskod989ba02020-08-24 10:59:24 +0200276 switch (d->mod) {
Radek Krejci19a96102018-11-15 13:38:09 +0100277 case LYS_DEV_NOT_SUPPORTED:
278 /* nothing to do */
279 break;
280 case LYS_DEV_ADD:
281 case LYS_DEV_DELETE: /* compatible for dynamically allocated data */
Michal Vaskoe180ed02021-02-05 16:31:20 +0100282 lydict_remove(ctx, add->units);
Radek Krejci19a96102018-11-15 13:38:09 +0100283 FREE_ARRAY(ctx, add->musts, lysp_restr_free);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200284 FREE_ARRAY(ctx, add->uniques, lysp_qname_free);
285 FREE_ARRAY(ctx, add->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100286 break;
287 case LYS_DEV_REPLACE:
288 FREE_MEMBER(ctx, rpl->type, lysp_type_free);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100289 lydict_remove(ctx, rpl->units);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200290 lysp_qname_free(ctx, &rpl->dflt);
Radek Krejci19a96102018-11-15 13:38:09 +0100291 break;
292 default:
293 LOGINT(ctx);
294 break;
295 }
296}
297
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200298void
Radek Krejci19a96102018-11-15 13:38:09 +0100299lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev)
300{
301 struct lysp_deviate *next, *iter;
302
Michal Vaskoe180ed02021-02-05 16:31:20 +0100303 lydict_remove(ctx, dev->nodeid);
304 lydict_remove(ctx, dev->dsc);
305 lydict_remove(ctx, dev->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100306 LY_LIST_FOR_SAFE(dev->deviates, next, iter) {
307 lysp_deviate_free(ctx, iter);
308 free(iter);
309 }
310 FREE_ARRAY(ctx, dev->exts, lysp_ext_instance_free);
311}
312
David Sedlákd2d676a2019-07-22 11:28:19 +0200313void
Radek Krejci19a96102018-11-15 13:38:09 +0100314lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref)
315{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100316 lydict_remove(ctx, ref->nodeid);
317 lydict_remove(ctx, ref->dsc);
318 lydict_remove(ctx, ref->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200319 FREE_ARRAY(ctx, ref->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100320 FREE_ARRAY(ctx, ref->musts, lysp_restr_free);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100321 lydict_remove(ctx, ref->presence);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200322 FREE_ARRAY(ctx, ref->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100323 FREE_ARRAY(ctx, ref->exts, lysp_ext_instance_free);
324}
325
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200326void
Radek Krejci19a96102018-11-15 13:38:09 +0100327lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node)
328{
329 struct lysp_node *child, *next;
Michal Vasko856ea972021-06-09 09:44:30 +0200330 struct lysp_node_container *cont;
331 struct lysp_node_leaf *leaf;
332 struct lysp_node_leaflist *llist;
333 struct lysp_node_list *list;
334 struct lysp_node_choice *choice;
335 struct lysp_node_case *cas;
336 struct lysp_node_uses *uses;
337 struct lysp_node_action *act;
338 struct lysp_node_action_inout *inout;
339 struct lysp_node_notif *notif;
Radek Krejci9a3823e2021-01-27 20:26:46 +0100340 struct lysp_restr *musts = lysp_node_musts(node);
341 struct lysp_when *when = lysp_node_when(node);
Radek Krejci19a96102018-11-15 13:38:09 +0100342
Michal Vaskoe180ed02021-02-05 16:31:20 +0100343 lydict_remove(ctx, node->name);
344 lydict_remove(ctx, node->dsc);
345 lydict_remove(ctx, node->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200346 FREE_ARRAY(ctx, node->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100347 FREE_ARRAY(ctx, node->exts, lysp_ext_instance_free);
348
Radek Krejci9a3823e2021-01-27 20:26:46 +0100349 FREE_MEMBER(ctx, when, lysp_when_free);
350 FREE_ARRAY(ctx, musts, lysp_restr_free);
351
Michal Vaskod989ba02020-08-24 10:59:24 +0200352 switch (node->nodetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100353 case LYS_CONTAINER:
Michal Vasko856ea972021-06-09 09:44:30 +0200354 cont = (struct lysp_node_container *)node;
355
356 lydict_remove(ctx, cont->presence);
357 FREE_ARRAY(ctx, cont->typedefs, lysp_tpdf_free);
358 if (cont->groupings) {
359 LY_LIST_FOR_SAFE(&cont->groupings->node, next, child) {
360 lysp_node_free(ctx, child);
361 }
362 }
363 LY_LIST_FOR_SAFE(cont->child, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100364 lysp_node_free(ctx, child);
365 }
Michal Vasko856ea972021-06-09 09:44:30 +0200366 if (cont->actions) {
367 LY_LIST_FOR_SAFE(&cont->actions->node, next, child) {
368 lysp_node_free(ctx, child);
369 }
Radek Krejci19a96102018-11-15 13:38:09 +0100370 }
Michal Vasko856ea972021-06-09 09:44:30 +0200371 if (cont->notifs) {
372 LY_LIST_FOR_SAFE(&cont->notifs->node, next, child) {
373 lysp_node_free(ctx, child);
374 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100375 }
Radek Krejci19a96102018-11-15 13:38:09 +0100376 break;
377 case LYS_LEAF:
Michal Vasko856ea972021-06-09 09:44:30 +0200378 leaf = (struct lysp_node_leaf *)node;
379
380 lysp_type_free(ctx, &leaf->type);
381 lydict_remove(ctx, leaf->units);
382 lydict_remove(ctx, leaf->dflt.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100383 break;
384 case LYS_LEAFLIST:
Michal Vasko856ea972021-06-09 09:44:30 +0200385 llist = (struct lysp_node_leaflist *)node;
386
387 lysp_type_free(ctx, &llist->type);
388 lydict_remove(ctx, llist->units);
389 FREE_ARRAY(ctx, llist->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100390 break;
391 case LYS_LIST:
Michal Vasko856ea972021-06-09 09:44:30 +0200392 list = (struct lysp_node_list *)node;
393
394 lydict_remove(ctx, list->key);
395 FREE_ARRAY(ctx, list->typedefs, lysp_tpdf_free);
396 if (list->groupings) {
397 LY_LIST_FOR_SAFE(&list->groupings->node, next, child) {
398 lysp_node_free(ctx, child);
399 }
400 }
401 LY_LIST_FOR_SAFE(list->child, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100402 lysp_node_free(ctx, child);
403 }
Michal Vasko856ea972021-06-09 09:44:30 +0200404 if (list->actions) {
405 LY_LIST_FOR_SAFE(&list->actions->node, next, child) {
406 lysp_node_free(ctx, child);
407 }
Radek Krejci19a96102018-11-15 13:38:09 +0100408 }
Michal Vasko856ea972021-06-09 09:44:30 +0200409 if (list->notifs) {
410 LY_LIST_FOR_SAFE(&list->notifs->node, next, child) {
411 lysp_node_free(ctx, child);
412 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100413 }
Michal Vasko856ea972021-06-09 09:44:30 +0200414 FREE_ARRAY(ctx, list->uniques, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100415 break;
416 case LYS_CHOICE:
Michal Vasko856ea972021-06-09 09:44:30 +0200417 choice = (struct lysp_node_choice *)node;
418
419 LY_LIST_FOR_SAFE(choice->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100420 lysp_node_free(ctx, child);
421 }
Michal Vasko856ea972021-06-09 09:44:30 +0200422 lydict_remove(ctx, choice->dflt.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100423 break;
424 case LYS_CASE:
Michal Vasko856ea972021-06-09 09:44:30 +0200425 cas = (struct lysp_node_case *)node;
426
427 LY_LIST_FOR_SAFE(cas->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100428 lysp_node_free(ctx, child);
429 }
430 break;
431 case LYS_ANYDATA:
432 case LYS_ANYXML:
Radek Krejci9a3823e2021-01-27 20:26:46 +0100433 /* nothing special to do */
Radek Krejci19a96102018-11-15 13:38:09 +0100434 break;
435 case LYS_USES:
Michal Vasko856ea972021-06-09 09:44:30 +0200436 uses = (struct lysp_node_uses *)node;
437
438 FREE_ARRAY(ctx, uses->refines, lysp_refine_free);
439 if (uses->augments) {
440 LY_LIST_FOR_SAFE(&uses->augments->node, next, child) {
441 lysp_node_free(ctx, child);
442 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100443 }
444 break;
445 case LYS_RPC:
446 case LYS_ACTION:
Michal Vasko856ea972021-06-09 09:44:30 +0200447 act = (struct lysp_node_action *)node;
448
449 FREE_ARRAY(ctx, act->typedefs, lysp_tpdf_free);
450 if (act->groupings) {
451 LY_LIST_FOR_SAFE(&act->groupings->node, next, child) {
452 lysp_node_free(ctx, child);
453 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100454 }
Michal Vasko856ea972021-06-09 09:44:30 +0200455 if (act->input.nodetype) {
456 lysp_node_free(ctx, &act->input.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100457 }
Michal Vasko856ea972021-06-09 09:44:30 +0200458 if (act->output.nodetype) {
459 lysp_node_free(ctx, &act->output.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100460 }
461 break;
462 case LYS_INPUT:
463 case LYS_OUTPUT:
Michal Vasko856ea972021-06-09 09:44:30 +0200464 inout = (struct lysp_node_action_inout *)node;
465
466 FREE_ARRAY(ctx, inout->typedefs, lysp_tpdf_free);
467 if (inout->groupings) {
468 LY_LIST_FOR_SAFE(&inout->groupings->node, next, child) {
469 lysp_node_free(ctx, child);
470 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100471 }
Michal Vasko856ea972021-06-09 09:44:30 +0200472 LY_LIST_FOR_SAFE(inout->child, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100473 lysp_node_free(ctx, child);
474 }
475 /* do not free the node, it is never standalone but part of the action node */
476 return;
477 case LYS_NOTIF:
Michal Vasko856ea972021-06-09 09:44:30 +0200478 notif = (struct lysp_node_notif *)node;
479
480 FREE_ARRAY(ctx, notif->typedefs, lysp_tpdf_free);
481 if (notif->groupings) {
482 LY_LIST_FOR_SAFE(&notif->groupings->node, next, child) {
483 lysp_node_free(ctx, child);
484 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100485 }
Michal Vasko856ea972021-06-09 09:44:30 +0200486 LY_LIST_FOR_SAFE(notif->child, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100487 lysp_node_free(ctx, child);
488 }
489 break;
490 case LYS_GROUPING:
491 lysp_grp_free(ctx, (struct lysp_node_grp *)node);
492 break;
493 case LYS_AUGMENT:
Radek Krejci9a3823e2021-01-27 20:26:46 +0100494 lysp_augment_free(ctx, ((struct lysp_node_augment *)node));
Radek Krejci19a96102018-11-15 13:38:09 +0100495 break;
496 default:
497 LOGINT(ctx);
498 }
499
500 free(node);
501}
502
Radek Krejci15f10ab2020-11-03 14:14:14 +0100503void
Radek Krejci19a96102018-11-15 13:38:09 +0100504lysp_module_free(struct lysp_module *module)
505{
506 struct ly_ctx *ctx;
507 struct lysp_node *node, *next;
508
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100509 if (!module) {
510 return;
511 }
512 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100513
514 FREE_ARRAY(ctx, module->imports, lysp_import_free);
Radek Krejci771928a2021-01-19 13:42:36 +0100515 FREE_ARRAY(ctx, module->includes, module->is_submod ? lysp_include_free_submodule : lysp_include_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100516
Radek Krejci19a96102018-11-15 13:38:09 +0100517 FREE_ARRAY(ctx, module->revs, lysp_revision_free);
518 FREE_ARRAY(ctx, module->extensions, lysp_ext_free);
519 FREE_ARRAY(ctx, module->features, lysp_feature_free);
520 FREE_ARRAY(ctx, module->identities, lysp_ident_free);
521 FREE_ARRAY(ctx, module->typedefs, lysp_tpdf_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100522 LY_LIST_FOR_SAFE((struct lysp_node *)module->groupings, next, node) {
523 lysp_node_free(ctx, node);
524 }
Radek Krejci19a96102018-11-15 13:38:09 +0100525 LY_LIST_FOR_SAFE(module->data, next, node) {
526 lysp_node_free(ctx, node);
527 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100528 LY_LIST_FOR_SAFE((struct lysp_node *)module->augments, next, node) {
529 lysp_node_free(ctx, node);
530 }
531 LY_LIST_FOR_SAFE((struct lysp_node *)module->rpcs, next, node) {
532 lysp_node_free(ctx, node);
533 }
534 LY_LIST_FOR_SAFE((struct lysp_node *)module->notifs, next, node) {
535 lysp_node_free(ctx, node);
536 }
Radek Krejci19a96102018-11-15 13:38:09 +0100537 FREE_ARRAY(ctx, module->deviations, lysp_deviation_free);
538 FREE_ARRAY(ctx, module->exts, lysp_ext_instance_free);
539
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200540 if (module->is_submod) {
541 struct lysp_submodule *submod = (struct lysp_submodule *)module;
542
Michal Vaskoe180ed02021-02-05 16:31:20 +0100543 lydict_remove(ctx, submod->name);
544 lydict_remove(ctx, submod->filepath);
545 lydict_remove(ctx, submod->prefix);
546 lydict_remove(ctx, submod->org);
547 lydict_remove(ctx, submod->contact);
548 lydict_remove(ctx, submod->dsc);
549 lydict_remove(ctx, submod->ref);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200550 }
551
Radek Krejci19a96102018-11-15 13:38:09 +0100552 free(module);
553}
554
Radek Krejci0af46292019-01-11 16:02:31 +0100555void
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100556lysc_extension_free(struct ly_ctx *ctx, struct lysc_ext **ext)
557{
558 if (--(*ext)->refcount) {
559 return;
560 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100561 lydict_remove(ctx, (*ext)->name);
Radek Krejci9f87b0c2021-03-05 14:45:26 +0100562 lydict_remove(ctx, (*ext)->argname);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100563 FREE_ARRAY(ctx, (*ext)->exts, lysc_ext_instance_free);
564 free(*ext);
Radek Krejci720d2612021-03-03 19:44:22 +0100565 *ext = NULL;
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100566}
567
568void
Radek Krejci19a96102018-11-15 13:38:09 +0100569lysc_ext_instance_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
570{
fredganebc50572019-10-31 15:39:23 +0800571 if (ext->def && ext->def->plugin && ext->def->plugin->free) {
Radek Krejci38d85362019-09-05 16:26:38 +0200572 ext->def->plugin->free(ctx, ext);
573 }
Juraj Vijtiuk4a19ab02020-03-03 13:50:14 +0100574 if (ext->def) {
575 lysc_extension_free(ctx, &ext->def);
576 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100577 lydict_remove(ctx, ext->argument);
Radek Krejci0935f412019-08-20 16:15:18 +0200578 FREE_ARRAY(ctx, ext->exts, lysc_ext_instance_free);
579}
580
581void
Radek Krejci19a96102018-11-15 13:38:09 +0100582lysc_iffeature_free(struct ly_ctx *UNUSED(ctx), struct lysc_iffeature *iff)
583{
584 LY_ARRAY_FREE(iff->features);
585 free(iff->expr);
586}
587
588static void
Radek Krejci00b874b2019-02-12 10:54:50 +0100589lysc_when_free(struct ly_ctx *ctx, struct lysc_when **w)
Radek Krejci58d171e2018-11-23 13:50:55 +0100590{
Radek Krejci00b874b2019-02-12 10:54:50 +0100591 if (--(*w)->refcount) {
592 return;
593 }
594 lyxp_expr_free(ctx, (*w)->cond);
Radek Krejci8df109d2021-04-23 12:19:08 +0200595 ly_free_prefix_data(LY_VALUE_SCHEMA_RESOLVED, (*w)->prefixes);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100596 lydict_remove(ctx, (*w)->dsc);
597 lydict_remove(ctx, (*w)->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +0100598 FREE_ARRAY(ctx, (*w)->exts, lysc_ext_instance_free);
599 free(*w);
Radek Krejci58d171e2018-11-23 13:50:55 +0100600}
601
Radek Krejciccd20f12019-02-15 14:12:27 +0100602void
Radek Krejci58d171e2018-11-23 13:50:55 +0100603lysc_must_free(struct ly_ctx *ctx, struct lysc_must *must)
604{
605 lyxp_expr_free(ctx, must->cond);
Radek Krejci8df109d2021-04-23 12:19:08 +0200606 ly_free_prefix_data(LY_VALUE_SCHEMA_RESOLVED, must->prefixes);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100607 lydict_remove(ctx, must->emsg);
608 lydict_remove(ctx, must->eapptag);
609 lydict_remove(ctx, must->dsc);
610 lydict_remove(ctx, must->ref);
Radek Krejci58d171e2018-11-23 13:50:55 +0100611 FREE_ARRAY(ctx, must->exts, lysc_ext_instance_free);
612}
613
Michal Vasko4f9da5e2022-03-14 13:11:26 +0100614static void
615lysc_ident_derived_unlink(const struct lysc_ident *ident)
616{
617 LY_ARRAY_COUNT_TYPE u, v, w;
618 const struct lysp_submodule *submod;
Michal Vaskod84b8132022-03-15 10:45:44 +0100619 const struct lysp_module *base_pmod = NULL;
Michal Vasko4f9da5e2022-03-14 13:11:26 +0100620 const struct lysp_ident *identp = NULL;
Michal Vaskod6b5d4e2022-03-16 09:23:32 +0100621 const struct lys_module *mod, *iter;
Michal Vasko4f9da5e2022-03-14 13:11:26 +0100622 const char *base_name;
Michal Vaskod6b5d4e2022-03-16 09:23:32 +0100623 uint32_t i;
Michal Vasko4f9da5e2022-03-14 13:11:26 +0100624
625 /* find the parsed identity */
626 LY_ARRAY_FOR(ident->module->parsed->identities, u) {
627 if (ident->module->parsed->identities[u].name == ident->name) {
628 identp = &ident->module->parsed->identities[u];
629 base_pmod = ident->module->parsed;
630 break;
631 }
632 }
633 if (!identp) {
634 LY_ARRAY_FOR(ident->module->parsed->includes, v) {
635 submod = ident->module->parsed->includes[v].submodule;
636 LY_ARRAY_FOR(submod->identities, u) {
637 if (submod->identities[u].name == ident->name) {
638 identp = &submod->identities[u];
639 base_pmod = (struct lysp_module *)submod;
640 break;
641 }
642 }
643 }
644 }
645 assert(identp);
646
647 /* remove link from all the foreign bases, it may not be there if identity compilation failed */
648 LY_ARRAY_FOR(identp->bases, u) {
649 base_name = strchr(identp->bases[u], ':');
650 if (!base_name) {
651 continue;
652 }
653
654 /* prefixed identity */
655 mod = ly_resolve_prefix(ident->module->ctx, identp->bases[u], base_name - identp->bases[u], LY_VALUE_SCHEMA,
656 (void *)base_pmod);
657 if (!mod) {
658 continue;
659 }
660 ++base_name;
661
Michal Vaskod6b5d4e2022-03-16 09:23:32 +0100662 i = 0;
663 while ((iter = ly_ctx_get_module_iter(ident->module->ctx, &i))) {
664 if (iter == mod) {
665 break;
666 }
667 }
668 if (!iter) {
669 /* target module was freed already */
670 continue;
671 }
672
Michal Vasko4f9da5e2022-03-14 13:11:26 +0100673 /* find the compiled base */
674 LY_ARRAY_FOR(mod->identities, v) {
675 if (!strcmp(mod->identities[v].name, base_name)) {
676 /* find the derived link */
677 LY_ARRAY_FOR(mod->identities[v].derived, w) {
678 if (mod->identities[v].derived[w] == ident) {
679 /* remove the link */
680 LY_ARRAY_DECREMENT(mod->identities[v].derived);
Michal Vasko3fedda12022-03-16 09:23:45 +0100681 if (!LY_ARRAY_COUNT(mod->identities[v].derived)) {
682 LY_ARRAY_FREE(mod->identities[v].derived);
683 mod->identities[v].derived = NULL;
684 } else if (w < LY_ARRAY_COUNT(mod->identities[v].derived)) {
Michal Vasko4f9da5e2022-03-14 13:11:26 +0100685 memmove(mod->identities[v].derived + w, mod->identities[v].derived + w + 1,
686 (LY_ARRAY_COUNT(mod->identities[v].derived) - w) * sizeof ident);
687 }
688 break;
689 }
690 }
691 break;
692 }
693 }
694 }
695}
696
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100697void
Radek Krejci19a96102018-11-15 13:38:09 +0100698lysc_ident_free(struct ly_ctx *ctx, struct lysc_ident *ident)
699{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100700 lydict_remove(ctx, ident->name);
701 lydict_remove(ctx, ident->dsc);
702 lydict_remove(ctx, ident->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100703 LY_ARRAY_FREE(ident->derived);
704 FREE_ARRAY(ctx, ident->exts, lysc_ext_instance_free);
705}
706
Radek Krejci19a96102018-11-15 13:38:09 +0100707static void
708lysc_range_free(struct ly_ctx *ctx, struct lysc_range *range)
709{
710 LY_ARRAY_FREE(range->parts);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100711 lydict_remove(ctx, range->eapptag);
712 lydict_remove(ctx, range->emsg);
713 lydict_remove(ctx, range->dsc);
714 lydict_remove(ctx, range->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100715 FREE_ARRAY(ctx, range->exts, lysc_ext_instance_free);
716}
717
Michal Vasko51de7b72022-04-29 09:50:22 +0200718void
Radek Krejci19a96102018-11-15 13:38:09 +0100719lysc_pattern_free(struct ly_ctx *ctx, struct lysc_pattern **pattern)
720{
721 if (--(*pattern)->refcount) {
722 return;
723 }
Radek Krejci54579462019-04-30 12:47:06 +0200724 pcre2_code_free((*pattern)->code);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100725 lydict_remove(ctx, (*pattern)->expr);
726 lydict_remove(ctx, (*pattern)->eapptag);
727 lydict_remove(ctx, (*pattern)->emsg);
728 lydict_remove(ctx, (*pattern)->dsc);
729 lydict_remove(ctx, (*pattern)->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100730 FREE_ARRAY(ctx, (*pattern)->exts, lysc_ext_instance_free);
731 free(*pattern);
732}
733
Michal Vaskof4fa90d2021-11-11 15:05:19 +0100734void
Radek Krejci693262f2019-04-29 15:23:20 +0200735lysc_enum_item_free(struct ly_ctx *ctx, struct lysc_type_bitenum_item *item)
Radek Krejci19a96102018-11-15 13:38:09 +0100736{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100737 lydict_remove(ctx, item->name);
738 lydict_remove(ctx, item->dsc);
739 lydict_remove(ctx, item->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100740 FREE_ARRAY(ctx, item->exts, lysc_ext_instance_free);
741}
742
Radek Krejcia3045382018-11-22 14:30:31 +0100743static void
744lysc_type2_free(struct ly_ctx *ctx, struct lysc_type **type)
745{
746 lysc_type_free(ctx, *type);
747}
Radek Krejci0f969882020-08-21 16:56:47 +0200748
Radek Krejcicdfecd92018-11-26 11:27:32 +0100749void
Radek Krejci19a96102018-11-15 13:38:09 +0100750lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type)
751{
Michal Vaskod15c7de2022-05-09 15:34:09 +0200752 if (!type || (LY_ATOMIC_DEC_BARRIER(type->refcount) > 1)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100753 return;
754 }
Radek Krejcib915ac92020-08-14 23:31:04 +0200755
Michal Vaskod989ba02020-08-24 10:59:24 +0200756 switch (type->basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100757 case LY_TYPE_BINARY:
Michal Vasko22df3f02020-08-24 13:29:22 +0200758 FREE_MEMBER(ctx, ((struct lysc_type_bin *)type)->length, lysc_range_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100759 break;
760 case LY_TYPE_BITS:
Michal Vasko22df3f02020-08-24 13:29:22 +0200761 FREE_ARRAY(ctx, (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)type)->bits, lysc_enum_item_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100762 break;
Radek Krejci6cba4292018-11-15 17:33:29 +0100763 case LY_TYPE_DEC64:
Michal Vasko22df3f02020-08-24 13:29:22 +0200764 FREE_MEMBER(ctx, ((struct lysc_type_dec *)type)->range, lysc_range_free);
Radek Krejci6cba4292018-11-15 17:33:29 +0100765 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100766 case LY_TYPE_STRING:
Michal Vasko22df3f02020-08-24 13:29:22 +0200767 FREE_MEMBER(ctx, ((struct lysc_type_str *)type)->length, lysc_range_free);
768 FREE_ARRAY(ctx, ((struct lysc_type_str *)type)->patterns, lysc_pattern_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100769 break;
770 case LY_TYPE_ENUM:
Michal Vasko22df3f02020-08-24 13:29:22 +0200771 FREE_ARRAY(ctx, ((struct lysc_type_enum *)type)->enums, lysc_enum_item_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100772 break;
773 case LY_TYPE_INT8:
774 case LY_TYPE_UINT8:
775 case LY_TYPE_INT16:
776 case LY_TYPE_UINT16:
777 case LY_TYPE_INT32:
778 case LY_TYPE_UINT32:
779 case LY_TYPE_INT64:
780 case LY_TYPE_UINT64:
Michal Vasko22df3f02020-08-24 13:29:22 +0200781 FREE_MEMBER(ctx, ((struct lysc_type_num *)type)->range, lysc_range_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100782 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100783 case LY_TYPE_IDENT:
Michal Vasko22df3f02020-08-24 13:29:22 +0200784 LY_ARRAY_FREE(((struct lysc_type_identityref *)type)->bases);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100785 break;
Radek Krejcia3045382018-11-22 14:30:31 +0100786 case LY_TYPE_UNION:
Michal Vasko22df3f02020-08-24 13:29:22 +0200787 FREE_ARRAY(ctx, ((struct lysc_type_union *)type)->types, lysc_type2_free);
Radek Krejcia3045382018-11-22 14:30:31 +0100788 break;
789 case LY_TYPE_LEAFREF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200790 lyxp_expr_free(ctx, ((struct lysc_type_leafref *)type)->path);
Radek Krejci8df109d2021-04-23 12:19:08 +0200791 ly_free_prefix_data(LY_VALUE_SCHEMA_RESOLVED, ((struct lysc_type_leafref *)type)->prefixes);
Michal Vaskod15c7de2022-05-09 15:34:09 +0200792 lysc_type_free(ctx, ((struct lysc_type_leafref *)type)->realtype);
Radek Krejcia3045382018-11-22 14:30:31 +0100793 break;
Radek Krejci16c0f822018-11-16 10:46:10 +0100794 case LY_TYPE_INST:
Radek Krejci19a96102018-11-15 13:38:09 +0100795 case LY_TYPE_BOOL:
796 case LY_TYPE_EMPTY:
Radek Krejci43699232018-11-23 14:59:46 +0100797 case LY_TYPE_UNKNOWN:
Radek Krejci19a96102018-11-15 13:38:09 +0100798 /* nothing to do */
799 break;
800 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200801
802 FREE_ARRAY(ctx, type->exts, lysc_ext_instance_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100803 free(type);
804}
805
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100806void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100807lysc_node_action_inout_free(struct ly_ctx *ctx, struct lysc_node_action_inout *inout)
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100808{
809 struct lysc_node *child, *child_next;
810
Radek Krejcif538ce52019-03-05 10:46:14 +0100811 FREE_ARRAY(ctx, inout->musts, lysc_must_free);
Radek Krejci01180ac2021-01-27 08:48:22 +0100812 LY_LIST_FOR_SAFE(inout->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100813 lysc_node_free_(ctx, child);
Radek Krejcif538ce52019-03-05 10:46:14 +0100814 }
815}
816
817void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100818lysc_node_action_free(struct ly_ctx *ctx, struct lysc_node_action *action)
Radek Krejcif538ce52019-03-05 10:46:14 +0100819{
Michal Vasko37a0fe62021-02-03 09:53:04 +0100820 FREE_ARRAY(ctx, action->when, lysc_when_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100821 if (action->input.nodetype) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +0100822 lysc_node_free_(ctx, &action->input.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100823 }
824 if (action->output.nodetype) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +0100825 lysc_node_free_(ctx, &action->output.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100826 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100827}
828
Radek Krejcifc11bd72019-04-11 16:00:05 +0200829void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100830lysc_node_notif_free(struct ly_ctx *ctx, struct lysc_node_notif *notif)
Radek Krejcifc11bd72019-04-11 16:00:05 +0200831{
832 struct lysc_node *child, *child_next;
833
Michal Vasko37a0fe62021-02-03 09:53:04 +0100834 FREE_ARRAY(ctx, notif->when, lysc_when_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200835 FREE_ARRAY(ctx, notif->musts, lysc_must_free);
Radek Krejci01180ac2021-01-27 08:48:22 +0100836 LY_LIST_FOR_SAFE(notif->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100837 lysc_node_free_(ctx, child);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200838 }
839}
840
Radek Krejcif2de0ed2019-05-02 14:13:18 +0200841void
Radek Krejci19a96102018-11-15 13:38:09 +0100842lysc_node_container_free(struct ly_ctx *ctx, struct lysc_node_container *node)
843{
844 struct lysc_node *child, *child_next;
845
846 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100847 lysc_node_free_(ctx, child);
Radek Krejci19a96102018-11-15 13:38:09 +0100848 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100849 LY_LIST_FOR_SAFE((struct lysc_node *)node->actions, child_next, child) {
850 lysc_node_free_(ctx, child);
851 }
852 LY_LIST_FOR_SAFE((struct lysc_node *)node->notifs, child_next, child) {
853 lysc_node_free_(ctx, child);
854 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100855 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci58d171e2018-11-23 13:50:55 +0100856 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100857}
858
859static void
860lysc_node_leaf_free(struct ly_ctx *ctx, struct lysc_node_leaf *node)
861{
Radek Krejci9a3823e2021-01-27 20:26:46 +0100862 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci58d171e2018-11-23 13:50:55 +0100863 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100864 if (node->type) {
865 lysc_type_free(ctx, node->type);
866 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100867 lydict_remove(ctx, node->units);
Radek Krejcia1911222019-07-22 17:24:50 +0200868 if (node->dflt) {
869 node->dflt->realtype->plugin->free(ctx, node->dflt);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200870 lysc_type_free(ctx, (struct lysc_type *)node->dflt->realtype);
Radek Krejcia1911222019-07-22 17:24:50 +0200871 free(node->dflt);
872 }
Radek Krejci19a96102018-11-15 13:38:09 +0100873}
874
Radek Krejci42452ac2018-11-28 17:09:52 +0100875static void
876lysc_node_leaflist_free(struct ly_ctx *ctx, struct lysc_node_leaflist *node)
877{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200878 LY_ARRAY_COUNT_TYPE u;
Radek Krejci42452ac2018-11-28 17:09:52 +0100879
Radek Krejci9a3823e2021-01-27 20:26:46 +0100880 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci42452ac2018-11-28 17:09:52 +0100881 FREE_ARRAY(ctx, node->musts, lysc_must_free);
882 if (node->type) {
883 lysc_type_free(ctx, node->type);
884 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100885 lydict_remove(ctx, node->units);
Radek Krejci42452ac2018-11-28 17:09:52 +0100886 LY_ARRAY_FOR(node->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +0200887 node->dflts[u]->realtype->plugin->free(ctx, node->dflts[u]);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200888 lysc_type_free(ctx, (struct lysc_type *)node->dflts[u]->realtype);
Radek Krejcia1911222019-07-22 17:24:50 +0200889 free(node->dflts[u]);
Radek Krejci42452ac2018-11-28 17:09:52 +0100890 }
891 LY_ARRAY_FREE(node->dflts);
892}
893
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100894static void
895lysc_node_list_free(struct ly_ctx *ctx, struct lysc_node_list *node)
896{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200897 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100898 struct lysc_node *child, *child_next;
899
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100900 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100901 lysc_node_free_(ctx, child);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100902 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100903 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100904 FREE_ARRAY(ctx, node->musts, lysc_must_free);
905
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100906 LY_ARRAY_FOR(node->uniques, u) {
907 LY_ARRAY_FREE(node->uniques[u]);
908 }
909 LY_ARRAY_FREE(node->uniques);
910
Radek Krejci2a9fc652021-01-22 17:44:34 +0100911 LY_LIST_FOR_SAFE((struct lysc_node *)node->actions, child_next, child) {
912 lysc_node_free_(ctx, child);
913 }
914 LY_LIST_FOR_SAFE((struct lysc_node *)node->notifs, child_next, child) {
915 lysc_node_free_(ctx, child);
916 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100917}
918
Radek Krejci056d0a82018-12-06 16:57:25 +0100919static void
920lysc_node_choice_free(struct ly_ctx *ctx, struct lysc_node_choice *node)
921{
922 struct lysc_node *child, *child_next;
923
Radek Krejci9a3823e2021-01-27 20:26:46 +0100924 FREE_ARRAY(ctx, node->when, lysc_when_free);
Michal Vasko20424b42020-08-31 12:29:38 +0200925 LY_LIST_FOR_SAFE((struct lysc_node *)node->cases, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100926 lysc_node_free_(ctx, child);
Michal Vasko20424b42020-08-31 12:29:38 +0200927 }
928}
929
930static void
931lysc_node_case_free(struct ly_ctx *ctx, struct lysc_node_case *node)
932{
933 struct lysc_node *child, *child_next;
934
Radek Krejci9a3823e2021-01-27 20:26:46 +0100935 FREE_ARRAY(ctx, node->when, lysc_when_free);
Michal Vasko20424b42020-08-31 12:29:38 +0200936 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100937 lysc_node_free_(ctx, child);
Radek Krejci056d0a82018-12-06 16:57:25 +0100938 }
Radek Krejci9800fb82018-12-13 14:26:23 +0100939}
Radek Krejci056d0a82018-12-06 16:57:25 +0100940
Radek Krejci9800fb82018-12-13 14:26:23 +0100941static void
942lysc_node_anydata_free(struct ly_ctx *ctx, struct lysc_node_anydata *node)
943{
Radek Krejci9a3823e2021-01-27 20:26:46 +0100944 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci9800fb82018-12-13 14:26:23 +0100945 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci056d0a82018-12-06 16:57:25 +0100946}
947
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100948static void
949lysc_node_free_(struct ly_ctx *ctx, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +0100950{
Radek Krejci2a9fc652021-01-22 17:44:34 +0100951 ly_bool inout = 0;
952
Radek Krejci19a96102018-11-15 13:38:09 +0100953 /* common part */
Michal Vaskoe180ed02021-02-05 16:31:20 +0100954 lydict_remove(ctx, node->name);
955 lydict_remove(ctx, node->dsc);
956 lydict_remove(ctx, node->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100957
958 /* nodetype-specific part */
Michal Vaskod989ba02020-08-24 10:59:24 +0200959 switch (node->nodetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100960 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +0200961 lysc_node_container_free(ctx, (struct lysc_node_container *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100962 break;
963 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200964 lysc_node_leaf_free(ctx, (struct lysc_node_leaf *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100965 break;
Radek Krejci42452ac2018-11-28 17:09:52 +0100966 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200967 lysc_node_leaflist_free(ctx, (struct lysc_node_leaflist *)node);
Radek Krejci42452ac2018-11-28 17:09:52 +0100968 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100969 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200970 lysc_node_list_free(ctx, (struct lysc_node_list *)node);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100971 break;
Radek Krejci056d0a82018-12-06 16:57:25 +0100972 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200973 lysc_node_choice_free(ctx, (struct lysc_node_choice *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +0100974 break;
975 case LYS_CASE:
Michal Vasko20424b42020-08-31 12:29:38 +0200976 lysc_node_case_free(ctx, (struct lysc_node_case *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +0100977 break;
Radek Krejci9800fb82018-12-13 14:26:23 +0100978 case LYS_ANYDATA:
979 case LYS_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +0200980 lysc_node_anydata_free(ctx, (struct lysc_node_anydata *)node);
Radek Krejci9800fb82018-12-13 14:26:23 +0100981 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100982 case LYS_RPC:
983 case LYS_ACTION:
984 lysc_node_action_free(ctx, (struct lysc_node_action *)node);
985 break;
986 case LYS_INPUT:
987 case LYS_OUTPUT:
988 lysc_node_action_inout_free(ctx, (struct lysc_node_action_inout *)node);
989 inout = 1;
990 break;
991 case LYS_NOTIF:
992 lysc_node_notif_free(ctx, (struct lysc_node_notif *)node);
993 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100994 default:
995 LOGINT(ctx);
996 }
997
Radek Krejci056d0a82018-12-06 16:57:25 +0100998 FREE_ARRAY(ctx, node->exts, lysc_ext_instance_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100999
1000 if (!inout) {
1001 free(node);
1002 }
Radek Krejci19a96102018-11-15 13:38:09 +01001003}
1004
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001005void
1006lysc_node_free(struct ly_ctx *ctx, struct lysc_node *node, ly_bool unlink)
1007{
Michal Vaskoe02e7402021-07-23 08:29:28 +02001008 struct lysc_node *next, *iter, **child_p;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001009
Radek Krejci2a9fc652021-01-22 17:44:34 +01001010 if (node->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
Michal Vaskoe02e7402021-07-23 08:29:28 +02001011 /* inouts are part of actions and cannot be unlinked/freed separately, we can only free all the children */
1012 struct lysc_node_action_inout *inout = (struct lysc_node_action_inout *)node;
Michal Vasko26bbb272022-08-02 14:54:33 +02001013
Michal Vaskoe02e7402021-07-23 08:29:28 +02001014 LY_LIST_FOR_SAFE(inout->child, next, iter) {
1015 lysc_node_free_(ctx, iter);
1016 }
1017 inout->child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001018 return;
1019 }
1020
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001021 if (unlink) {
1022 /* unlink from siblings */
1023 if (node->prev->next) {
1024 node->prev->next = node->next;
1025 }
1026 if (node->next) {
1027 node->next->prev = node->prev;
1028 } else {
1029 /* unlinking the last node */
1030 if (node->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01001031 if (node->nodetype == LYS_ACTION) {
1032 iter = (struct lysc_node *)lysc_node_actions(node->parent);
1033 } else if (node->nodetype == LYS_NOTIF) {
1034 iter = (struct lysc_node *)lysc_node_notifs(node->parent);
1035 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01001036 iter = (struct lysc_node *)lysc_node_child(node->parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001037 }
Michal Vasko539c4a62020-11-03 17:21:34 +01001038 LY_CHECK_ERR_RET(!iter, LOGINT(ctx), );
Radek Krejci2a9fc652021-01-22 17:44:34 +01001039 } else if (node->nodetype == LYS_RPC) {
1040 iter = (struct lysc_node *)node->module->compiled->rpcs;
1041 } else if (node->nodetype == LYS_NOTIF) {
1042 iter = (struct lysc_node *)node->module->compiled->notifs;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001043 } else {
1044 iter = node->module->compiled->data;
1045 }
1046 /* update the "last" pointer from the first node */
1047 iter->prev = node->prev;
1048 }
1049
1050 /* unlink from parent */
1051 if (node->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01001052 if (node->nodetype == LYS_ACTION) {
1053 child_p = (struct lysc_node **)lysc_node_actions_p(node->parent);
1054 } else if (node->nodetype == LYS_NOTIF) {
1055 child_p = (struct lysc_node **)lysc_node_notifs_p(node->parent);
1056 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01001057 child_p = lysc_node_child_p(node->parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001058 }
1059 } else if (node->nodetype == LYS_RPC) {
1060 child_p = (struct lysc_node **)&node->module->compiled->rpcs;
1061 } else if (node->nodetype == LYS_NOTIF) {
1062 child_p = (struct lysc_node **)&node->module->compiled->notifs;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001063 } else {
1064 child_p = &node->module->compiled->data;
1065 }
1066 if (child_p && (*child_p == node)) {
1067 /* the node is the first child */
1068 *child_p = node->next;
1069 }
1070 }
1071
1072 lysc_node_free_(ctx, node);
1073}
1074
Radek Krejci90ed21e2021-04-12 14:47:46 +02001075void
1076lysc_module_free(struct lysc_module *module)
Radek Krejci19a96102018-11-15 13:38:09 +01001077{
1078 struct ly_ctx *ctx;
1079 struct lysc_node *node, *node_next;
1080
Radek Krejci90ed21e2021-04-12 14:47:46 +02001081 if (!module) {
1082 return;
1083 }
1084
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001085 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01001086
Radek Krejci19a96102018-11-15 13:38:09 +01001087 LY_LIST_FOR_SAFE(module->data, node_next, node) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001088 lysc_node_free_(ctx, node);
Radek Krejci19a96102018-11-15 13:38:09 +01001089 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01001090 LY_LIST_FOR_SAFE((struct lysc_node *)module->rpcs, node_next, node) {
1091 lysc_node_free_(ctx, node);
1092 }
1093 LY_LIST_FOR_SAFE((struct lysc_node *)module->notifs, node_next, node) {
1094 lysc_node_free_(ctx, node);
1095 }
Radek Krejci19a96102018-11-15 13:38:09 +01001096 FREE_ARRAY(ctx, module->exts, lysc_ext_instance_free);
1097
1098 free(module);
1099}
1100
1101void
Michal Vasko4f9da5e2022-03-14 13:11:26 +01001102lys_module_free(struct lys_module *module, ly_bool remove_links)
Radek Krejci19a96102018-11-15 13:38:09 +01001103{
Michal Vasko4f9da5e2022-03-14 13:11:26 +01001104 LY_ARRAY_COUNT_TYPE u;
1105
Radek Krejci19a96102018-11-15 13:38:09 +01001106 if (!module) {
1107 return;
1108 }
Michal Vasko4f9da5e2022-03-14 13:11:26 +01001109
Michal Vaskoee757602021-06-10 14:38:19 +02001110 assert(!module->implemented);
1111 assert(!module->compiled);
Radek Krejci19a96102018-11-15 13:38:09 +01001112
Michal Vasko4f9da5e2022-03-14 13:11:26 +01001113 if (remove_links) {
1114 /* remove derived identity links */
1115 LY_ARRAY_FOR(module->identities, u) {
1116 lysc_ident_derived_unlink(&module->identities[u]);
1117 }
1118 }
Radek Krejci80d281e2020-09-14 17:42:54 +02001119 FREE_ARRAY(module->ctx, module->identities, lysc_ident_free);
Radek Krejci19a96102018-11-15 13:38:09 +01001120 lysp_module_free(module->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001121
Michal Vasko7f45cf22020-10-01 12:49:44 +02001122 LY_ARRAY_FREE(module->augmented_by);
1123 LY_ARRAY_FREE(module->deviated_by);
1124
Michal Vaskoe180ed02021-02-05 16:31:20 +01001125 lydict_remove(module->ctx, module->name);
1126 lydict_remove(module->ctx, module->revision);
1127 lydict_remove(module->ctx, module->ns);
1128 lydict_remove(module->ctx, module->prefix);
1129 lydict_remove(module->ctx, module->filepath);
1130 lydict_remove(module->ctx, module->org);
1131 lydict_remove(module->ctx, module->contact);
1132 lydict_remove(module->ctx, module->dsc);
1133 lydict_remove(module->ctx, module->ref);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001134
Radek Krejci19a96102018-11-15 13:38:09 +01001135 free(module);
1136}
Michal Vasko33ff9422020-07-03 09:50:39 +02001137
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001138LIBYANG_API_DEF void
Radek Krejci0b013302021-03-29 15:22:32 +02001139lyplg_ext_instance_substatements_free(struct ly_ctx *ctx, struct lysc_ext_substmt *substmts)
Radek Krejci38d85362019-09-05 16:26:38 +02001140{
Radek Krejci1b2eef82021-02-17 11:17:27 +01001141 LY_ARRAY_COUNT_TYPE u;
1142
1143 LY_ARRAY_FOR(substmts, u) {
Radek Krejci38d85362019-09-05 16:26:38 +02001144 if (!substmts[u].storage) {
1145 continue;
1146 }
1147
Michal Vaskod989ba02020-08-24 10:59:24 +02001148 switch (substmts[u].stmt) {
Radek Krejci6b88a462021-02-17 12:39:34 +01001149 case LY_STMT_ACTION:
1150 case LY_STMT_ANYDATA:
1151 case LY_STMT_ANYXML:
1152 case LY_STMT_CONTAINER:
1153 case LY_STMT_CHOICE:
1154 case LY_STMT_LEAF:
1155 case LY_STMT_LEAF_LIST:
1156 case LY_STMT_LIST:
1157 case LY_STMT_NOTIFICATION:
1158 case LY_STMT_RPC:
1159 case LY_STMT_USES: {
1160 struct lysc_node *child, *child_next;
1161
1162 LY_LIST_FOR_SAFE(*((struct lysc_node **)substmts[u].storage), child_next, child) {
1163 lysc_node_free_(ctx, child);
Radek Krejci38d85362019-09-05 16:26:38 +02001164 }
1165 break;
Radek Krejci6b88a462021-02-17 12:39:34 +01001166 }
1167 case LY_STMT_CONFIG:
1168 case LY_STMT_STATUS:
1169 /* nothing to do */
1170 break;
Michal Vasko3ac60e52022-09-05 11:08:31 +02001171 case LY_STMT_CONTACT:
Radek Krejci1b2eef82021-02-17 11:17:27 +01001172 case LY_STMT_DESCRIPTION:
Michal Vasko3ac60e52022-09-05 11:08:31 +02001173 case LY_STMT_ERROR_APP_TAG:
1174 case LY_STMT_ERROR_MESSAGE:
1175 case LY_STMT_KEY:
1176 case LY_STMT_NAMESPACE:
1177 case LY_STMT_ORGANIZATION:
1178 case LY_STMT_PRESENCE:
Radek Krejci1b2eef82021-02-17 11:17:27 +01001179 case LY_STMT_REFERENCE:
Radek Krejci38d85362019-09-05 16:26:38 +02001180 case LY_STMT_UNITS:
1181 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1182 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +02001183 const char *str = *((const char **)substmts[u].storage);
Michal Vasko26bbb272022-08-02 14:54:33 +02001184
Radek Krejci38d85362019-09-05 16:26:38 +02001185 if (!str) {
1186 break;
1187 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001188 lydict_remove(ctx, str);
Radek Krejci38d85362019-09-05 16:26:38 +02001189 } else {
1190 /* multiple items */
Michal Vasko22df3f02020-08-24 13:29:22 +02001191 const char **strs = *((const char ***)substmts[u].storage);
Michal Vasko26bbb272022-08-02 14:54:33 +02001192
Radek Krejci38d85362019-09-05 16:26:38 +02001193 if (!strs) {
1194 break;
1195 }
1196 FREE_STRINGS(ctx, strs);
1197 }
1198 break;
Radek Krejci38d85362019-09-05 16:26:38 +02001199 case LY_STMT_IF_FEATURE: {
Michal Vasko22df3f02020-08-24 13:29:22 +02001200 struct lysc_iffeature *iff = *((struct lysc_iffeature **)substmts[u].storage);
Michal Vasko26bbb272022-08-02 14:54:33 +02001201
Radek Krejci38d85362019-09-05 16:26:38 +02001202 if (!iff) {
1203 break;
1204 }
1205 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1206 /* single item */
1207 lysc_iffeature_free(ctx, iff);
1208 free(iff);
1209 } else {
1210 /* multiple items */
1211 FREE_ARRAY(ctx, iff, lysc_iffeature_free);
1212 }
1213 break;
1214 }
Michal Vaskof8647fa2022-09-05 11:07:54 +02001215 case LY_STMT_TYPE:
1216 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1217 /* single item */
1218 struct lysc_type *type = *((struct lysc_type **)substmts[u].storage);
1219
1220 if (!type) {
1221 break;
1222 }
1223 lysc_type_free(ctx, type);
1224 } else {
1225 /* multiple items */
1226 struct lysc_type **types = *((struct lysc_type ***)substmts[u].storage);
1227
1228 if (!types) {
1229 break;
1230 }
1231 FREE_ARRAY(ctx, types, lysc_type2_free);
1232 }
1233 break;
Radek Krejci38d85362019-09-05 16:26:38 +02001234
Radek Krejci0f969882020-08-21 16:56:47 +02001235 /* TODO other statements */
Radek Krejci38d85362019-09-05 16:26:38 +02001236 default:
1237 LOGINT(ctx);
1238 }
1239 }
Radek Krejci1b2eef82021-02-17 11:17:27 +01001240
1241 LY_ARRAY_FREE(substmts);
Radek Krejci38d85362019-09-05 16:26:38 +02001242}
David Sedlákebd3acf2019-07-26 15:04:32 +02001243
1244void
Michal Vaskob36053d2020-03-26 15:49:30 +01001245yang_parser_ctx_free(struct lys_yang_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +02001246{
1247 if (ctx) {
aPiecek8d4e75d2021-06-24 14:47:06 +02001248 if (ctx->main_ctx == (struct lys_parser_ctx *)ctx) {
1249 ly_set_erase(&ctx->tpdfs_nodes, NULL);
1250 ly_set_erase(&ctx->grps_nodes, NULL);
1251 }
Michal Vaskodd6d5a32022-07-14 13:54:58 +02001252 assert(!ctx->tpdfs_nodes.count && !ctx->grps_nodes.count);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001253 ly_set_rm_index(ctx->parsed_mods, ctx->parsed_mods->count - 1, NULL);
1254 if (!ctx->parsed_mods->count) {
1255 ly_set_free(ctx->parsed_mods, NULL);
1256 }
David Sedlákebd3acf2019-07-26 15:04:32 +02001257 free(ctx);
1258 }
1259}
1260
1261void
Michal Vaskob36053d2020-03-26 15:49:30 +01001262yin_parser_ctx_free(struct lys_yin_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +02001263{
1264 if (ctx) {
aPiecek8d4e75d2021-06-24 14:47:06 +02001265 if (ctx->main_ctx == (struct lys_parser_ctx *)ctx) {
1266 ly_set_erase(&ctx->tpdfs_nodes, NULL);
1267 ly_set_erase(&ctx->grps_nodes, NULL);
1268 }
Michal Vaskodd6d5a32022-07-14 13:54:58 +02001269 assert(!ctx->tpdfs_nodes.count && !ctx->grps_nodes.count);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001270 ly_set_rm_index(ctx->parsed_mods, ctx->parsed_mods->count - 1, NULL);
1271 if (!ctx->parsed_mods->count) {
1272 ly_set_free(ctx->parsed_mods, NULL);
1273 }
Michal Vaskob36053d2020-03-26 15:49:30 +01001274 lyxml_ctx_free(ctx->xmlctx);
David Sedlákebd3acf2019-07-26 15:04:32 +02001275 free(ctx);
1276 }
1277}