blob: 07f32cbb63429fe6698ac35d94566f4292f339d3 [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
718static void
719lysc_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 Vaskod3105042021-09-01 08:11:57 +0200752 if (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);
Radek Krejcia3045382018-11-22 14:30:31 +0100792 break;
Radek Krejci16c0f822018-11-16 10:46:10 +0100793 case LY_TYPE_INST:
Radek Krejci19a96102018-11-15 13:38:09 +0100794 case LY_TYPE_BOOL:
795 case LY_TYPE_EMPTY:
Radek Krejci43699232018-11-23 14:59:46 +0100796 case LY_TYPE_UNKNOWN:
Radek Krejci19a96102018-11-15 13:38:09 +0100797 /* nothing to do */
798 break;
799 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200800
801 FREE_ARRAY(ctx, type->exts, lysc_ext_instance_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100802 free(type);
803}
804
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100805void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100806lysc_node_action_inout_free(struct ly_ctx *ctx, struct lysc_node_action_inout *inout)
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100807{
808 struct lysc_node *child, *child_next;
809
Radek Krejcif538ce52019-03-05 10:46:14 +0100810 FREE_ARRAY(ctx, inout->musts, lysc_must_free);
Radek Krejci01180ac2021-01-27 08:48:22 +0100811 LY_LIST_FOR_SAFE(inout->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100812 lysc_node_free_(ctx, child);
Radek Krejcif538ce52019-03-05 10:46:14 +0100813 }
814}
815
816void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100817lysc_node_action_free(struct ly_ctx *ctx, struct lysc_node_action *action)
Radek Krejcif538ce52019-03-05 10:46:14 +0100818{
Michal Vasko37a0fe62021-02-03 09:53:04 +0100819 FREE_ARRAY(ctx, action->when, lysc_when_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100820 if (action->input.nodetype) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +0100821 lysc_node_free_(ctx, &action->input.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100822 }
823 if (action->output.nodetype) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +0100824 lysc_node_free_(ctx, &action->output.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100825 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100826}
827
Radek Krejcifc11bd72019-04-11 16:00:05 +0200828void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100829lysc_node_notif_free(struct ly_ctx *ctx, struct lysc_node_notif *notif)
Radek Krejcifc11bd72019-04-11 16:00:05 +0200830{
831 struct lysc_node *child, *child_next;
832
Michal Vasko37a0fe62021-02-03 09:53:04 +0100833 FREE_ARRAY(ctx, notif->when, lysc_when_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200834 FREE_ARRAY(ctx, notif->musts, lysc_must_free);
Radek Krejci01180ac2021-01-27 08:48:22 +0100835 LY_LIST_FOR_SAFE(notif->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100836 lysc_node_free_(ctx, child);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200837 }
838}
839
Radek Krejcif2de0ed2019-05-02 14:13:18 +0200840void
Radek Krejci19a96102018-11-15 13:38:09 +0100841lysc_node_container_free(struct ly_ctx *ctx, struct lysc_node_container *node)
842{
843 struct lysc_node *child, *child_next;
844
845 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100846 lysc_node_free_(ctx, child);
Radek Krejci19a96102018-11-15 13:38:09 +0100847 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100848 LY_LIST_FOR_SAFE((struct lysc_node *)node->actions, child_next, child) {
849 lysc_node_free_(ctx, child);
850 }
851 LY_LIST_FOR_SAFE((struct lysc_node *)node->notifs, child_next, child) {
852 lysc_node_free_(ctx, child);
853 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100854 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci58d171e2018-11-23 13:50:55 +0100855 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100856}
857
858static void
859lysc_node_leaf_free(struct ly_ctx *ctx, struct lysc_node_leaf *node)
860{
Radek Krejci9a3823e2021-01-27 20:26:46 +0100861 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci58d171e2018-11-23 13:50:55 +0100862 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100863 if (node->type) {
864 lysc_type_free(ctx, node->type);
865 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100866 lydict_remove(ctx, node->units);
Radek Krejcia1911222019-07-22 17:24:50 +0200867 if (node->dflt) {
868 node->dflt->realtype->plugin->free(ctx, node->dflt);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200869 lysc_type_free(ctx, (struct lysc_type *)node->dflt->realtype);
Radek Krejcia1911222019-07-22 17:24:50 +0200870 free(node->dflt);
871 }
Radek Krejci19a96102018-11-15 13:38:09 +0100872}
873
Radek Krejci42452ac2018-11-28 17:09:52 +0100874static void
875lysc_node_leaflist_free(struct ly_ctx *ctx, struct lysc_node_leaflist *node)
876{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200877 LY_ARRAY_COUNT_TYPE u;
Radek Krejci42452ac2018-11-28 17:09:52 +0100878
Radek Krejci9a3823e2021-01-27 20:26:46 +0100879 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci42452ac2018-11-28 17:09:52 +0100880 FREE_ARRAY(ctx, node->musts, lysc_must_free);
881 if (node->type) {
882 lysc_type_free(ctx, node->type);
883 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100884 lydict_remove(ctx, node->units);
Radek Krejci42452ac2018-11-28 17:09:52 +0100885 LY_ARRAY_FOR(node->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +0200886 node->dflts[u]->realtype->plugin->free(ctx, node->dflts[u]);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200887 lysc_type_free(ctx, (struct lysc_type *)node->dflts[u]->realtype);
Radek Krejcia1911222019-07-22 17:24:50 +0200888 free(node->dflts[u]);
Radek Krejci42452ac2018-11-28 17:09:52 +0100889 }
890 LY_ARRAY_FREE(node->dflts);
891}
892
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100893static void
894lysc_node_list_free(struct ly_ctx *ctx, struct lysc_node_list *node)
895{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200896 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100897 struct lysc_node *child, *child_next;
898
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100899 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100900 lysc_node_free_(ctx, child);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100901 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100902 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100903 FREE_ARRAY(ctx, node->musts, lysc_must_free);
904
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100905 LY_ARRAY_FOR(node->uniques, u) {
906 LY_ARRAY_FREE(node->uniques[u]);
907 }
908 LY_ARRAY_FREE(node->uniques);
909
Radek Krejci2a9fc652021-01-22 17:44:34 +0100910 LY_LIST_FOR_SAFE((struct lysc_node *)node->actions, child_next, child) {
911 lysc_node_free_(ctx, child);
912 }
913 LY_LIST_FOR_SAFE((struct lysc_node *)node->notifs, child_next, child) {
914 lysc_node_free_(ctx, child);
915 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100916}
917
Radek Krejci056d0a82018-12-06 16:57:25 +0100918static void
919lysc_node_choice_free(struct ly_ctx *ctx, struct lysc_node_choice *node)
920{
921 struct lysc_node *child, *child_next;
922
Radek Krejci9a3823e2021-01-27 20:26:46 +0100923 FREE_ARRAY(ctx, node->when, lysc_when_free);
Michal Vasko20424b42020-08-31 12:29:38 +0200924 LY_LIST_FOR_SAFE((struct lysc_node *)node->cases, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100925 lysc_node_free_(ctx, child);
Michal Vasko20424b42020-08-31 12:29:38 +0200926 }
927}
928
929static void
930lysc_node_case_free(struct ly_ctx *ctx, struct lysc_node_case *node)
931{
932 struct lysc_node *child, *child_next;
933
Radek Krejci9a3823e2021-01-27 20:26:46 +0100934 FREE_ARRAY(ctx, node->when, lysc_when_free);
Michal Vasko20424b42020-08-31 12:29:38 +0200935 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100936 lysc_node_free_(ctx, child);
Radek Krejci056d0a82018-12-06 16:57:25 +0100937 }
Radek Krejci9800fb82018-12-13 14:26:23 +0100938}
Radek Krejci056d0a82018-12-06 16:57:25 +0100939
Radek Krejci9800fb82018-12-13 14:26:23 +0100940static void
941lysc_node_anydata_free(struct ly_ctx *ctx, struct lysc_node_anydata *node)
942{
Radek Krejci9a3823e2021-01-27 20:26:46 +0100943 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci9800fb82018-12-13 14:26:23 +0100944 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci056d0a82018-12-06 16:57:25 +0100945}
946
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100947static void
948lysc_node_free_(struct ly_ctx *ctx, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +0100949{
Radek Krejci2a9fc652021-01-22 17:44:34 +0100950 ly_bool inout = 0;
951
Radek Krejci19a96102018-11-15 13:38:09 +0100952 /* common part */
Michal Vaskoe180ed02021-02-05 16:31:20 +0100953 lydict_remove(ctx, node->name);
954 lydict_remove(ctx, node->dsc);
955 lydict_remove(ctx, node->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100956
957 /* nodetype-specific part */
Michal Vaskod989ba02020-08-24 10:59:24 +0200958 switch (node->nodetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100959 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +0200960 lysc_node_container_free(ctx, (struct lysc_node_container *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100961 break;
962 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200963 lysc_node_leaf_free(ctx, (struct lysc_node_leaf *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100964 break;
Radek Krejci42452ac2018-11-28 17:09:52 +0100965 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200966 lysc_node_leaflist_free(ctx, (struct lysc_node_leaflist *)node);
Radek Krejci42452ac2018-11-28 17:09:52 +0100967 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100968 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200969 lysc_node_list_free(ctx, (struct lysc_node_list *)node);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100970 break;
Radek Krejci056d0a82018-12-06 16:57:25 +0100971 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200972 lysc_node_choice_free(ctx, (struct lysc_node_choice *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +0100973 break;
974 case LYS_CASE:
Michal Vasko20424b42020-08-31 12:29:38 +0200975 lysc_node_case_free(ctx, (struct lysc_node_case *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +0100976 break;
Radek Krejci9800fb82018-12-13 14:26:23 +0100977 case LYS_ANYDATA:
978 case LYS_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +0200979 lysc_node_anydata_free(ctx, (struct lysc_node_anydata *)node);
Radek Krejci9800fb82018-12-13 14:26:23 +0100980 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100981 case LYS_RPC:
982 case LYS_ACTION:
983 lysc_node_action_free(ctx, (struct lysc_node_action *)node);
984 break;
985 case LYS_INPUT:
986 case LYS_OUTPUT:
987 lysc_node_action_inout_free(ctx, (struct lysc_node_action_inout *)node);
988 inout = 1;
989 break;
990 case LYS_NOTIF:
991 lysc_node_notif_free(ctx, (struct lysc_node_notif *)node);
992 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100993 default:
994 LOGINT(ctx);
995 }
996
Radek Krejci056d0a82018-12-06 16:57:25 +0100997 FREE_ARRAY(ctx, node->exts, lysc_ext_instance_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100998
999 if (!inout) {
1000 free(node);
1001 }
Radek Krejci19a96102018-11-15 13:38:09 +01001002}
1003
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001004void
1005lysc_node_free(struct ly_ctx *ctx, struct lysc_node *node, ly_bool unlink)
1006{
Michal Vaskoe02e7402021-07-23 08:29:28 +02001007 struct lysc_node *next, *iter, **child_p;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001008
Radek Krejci2a9fc652021-01-22 17:44:34 +01001009 if (node->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
Michal Vaskoe02e7402021-07-23 08:29:28 +02001010 /* inouts are part of actions and cannot be unlinked/freed separately, we can only free all the children */
1011 struct lysc_node_action_inout *inout = (struct lysc_node_action_inout *)node;
1012 LY_LIST_FOR_SAFE(inout->child, next, iter) {
1013 lysc_node_free_(ctx, iter);
1014 }
1015 inout->child = NULL;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001016 return;
1017 }
1018
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001019 if (unlink) {
1020 /* unlink from siblings */
1021 if (node->prev->next) {
1022 node->prev->next = node->next;
1023 }
1024 if (node->next) {
1025 node->next->prev = node->prev;
1026 } else {
1027 /* unlinking the last node */
1028 if (node->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01001029 if (node->nodetype == LYS_ACTION) {
1030 iter = (struct lysc_node *)lysc_node_actions(node->parent);
1031 } else if (node->nodetype == LYS_NOTIF) {
1032 iter = (struct lysc_node *)lysc_node_notifs(node->parent);
1033 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01001034 iter = (struct lysc_node *)lysc_node_child(node->parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001035 }
Michal Vasko539c4a62020-11-03 17:21:34 +01001036 LY_CHECK_ERR_RET(!iter, LOGINT(ctx), );
Radek Krejci2a9fc652021-01-22 17:44:34 +01001037 } else if (node->nodetype == LYS_RPC) {
1038 iter = (struct lysc_node *)node->module->compiled->rpcs;
1039 } else if (node->nodetype == LYS_NOTIF) {
1040 iter = (struct lysc_node *)node->module->compiled->notifs;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001041 } else {
1042 iter = node->module->compiled->data;
1043 }
1044 /* update the "last" pointer from the first node */
1045 iter->prev = node->prev;
1046 }
1047
1048 /* unlink from parent */
1049 if (node->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01001050 if (node->nodetype == LYS_ACTION) {
1051 child_p = (struct lysc_node **)lysc_node_actions_p(node->parent);
1052 } else if (node->nodetype == LYS_NOTIF) {
1053 child_p = (struct lysc_node **)lysc_node_notifs_p(node->parent);
1054 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01001055 child_p = lysc_node_child_p(node->parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001056 }
1057 } else if (node->nodetype == LYS_RPC) {
1058 child_p = (struct lysc_node **)&node->module->compiled->rpcs;
1059 } else if (node->nodetype == LYS_NOTIF) {
1060 child_p = (struct lysc_node **)&node->module->compiled->notifs;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001061 } else {
1062 child_p = &node->module->compiled->data;
1063 }
1064 if (child_p && (*child_p == node)) {
1065 /* the node is the first child */
1066 *child_p = node->next;
1067 }
1068 }
1069
1070 lysc_node_free_(ctx, node);
1071}
1072
Radek Krejci90ed21e2021-04-12 14:47:46 +02001073void
1074lysc_module_free(struct lysc_module *module)
Radek Krejci19a96102018-11-15 13:38:09 +01001075{
1076 struct ly_ctx *ctx;
1077 struct lysc_node *node, *node_next;
1078
Radek Krejci90ed21e2021-04-12 14:47:46 +02001079 if (!module) {
1080 return;
1081 }
1082
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001083 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01001084
Radek Krejci19a96102018-11-15 13:38:09 +01001085 LY_LIST_FOR_SAFE(module->data, node_next, node) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001086 lysc_node_free_(ctx, node);
Radek Krejci19a96102018-11-15 13:38:09 +01001087 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01001088 LY_LIST_FOR_SAFE((struct lysc_node *)module->rpcs, node_next, node) {
1089 lysc_node_free_(ctx, node);
1090 }
1091 LY_LIST_FOR_SAFE((struct lysc_node *)module->notifs, node_next, node) {
1092 lysc_node_free_(ctx, node);
1093 }
Radek Krejci19a96102018-11-15 13:38:09 +01001094 FREE_ARRAY(ctx, module->exts, lysc_ext_instance_free);
1095
1096 free(module);
1097}
1098
1099void
Michal Vasko4f9da5e2022-03-14 13:11:26 +01001100lys_module_free(struct lys_module *module, ly_bool remove_links)
Radek Krejci19a96102018-11-15 13:38:09 +01001101{
Michal Vasko4f9da5e2022-03-14 13:11:26 +01001102 LY_ARRAY_COUNT_TYPE u;
1103
Radek Krejci19a96102018-11-15 13:38:09 +01001104 if (!module) {
1105 return;
1106 }
Michal Vasko4f9da5e2022-03-14 13:11:26 +01001107
Michal Vaskoee757602021-06-10 14:38:19 +02001108 assert(!module->implemented);
1109 assert(!module->compiled);
Radek Krejci19a96102018-11-15 13:38:09 +01001110
Michal Vasko4f9da5e2022-03-14 13:11:26 +01001111 if (remove_links) {
1112 /* remove derived identity links */
1113 LY_ARRAY_FOR(module->identities, u) {
1114 lysc_ident_derived_unlink(&module->identities[u]);
1115 }
1116 }
Radek Krejci80d281e2020-09-14 17:42:54 +02001117 FREE_ARRAY(module->ctx, module->identities, lysc_ident_free);
Radek Krejci19a96102018-11-15 13:38:09 +01001118 lysp_module_free(module->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001119
Michal Vasko7f45cf22020-10-01 12:49:44 +02001120 LY_ARRAY_FREE(module->augmented_by);
1121 LY_ARRAY_FREE(module->deviated_by);
1122
Michal Vaskoe180ed02021-02-05 16:31:20 +01001123 lydict_remove(module->ctx, module->name);
1124 lydict_remove(module->ctx, module->revision);
1125 lydict_remove(module->ctx, module->ns);
1126 lydict_remove(module->ctx, module->prefix);
1127 lydict_remove(module->ctx, module->filepath);
1128 lydict_remove(module->ctx, module->org);
1129 lydict_remove(module->ctx, module->contact);
1130 lydict_remove(module->ctx, module->dsc);
1131 lydict_remove(module->ctx, module->ref);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001132
Radek Krejci19a96102018-11-15 13:38:09 +01001133 free(module);
1134}
Michal Vasko33ff9422020-07-03 09:50:39 +02001135
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001136LIBYANG_API_DEF void
Radek Krejci0b013302021-03-29 15:22:32 +02001137lyplg_ext_instance_substatements_free(struct ly_ctx *ctx, struct lysc_ext_substmt *substmts)
Radek Krejci38d85362019-09-05 16:26:38 +02001138{
Radek Krejci1b2eef82021-02-17 11:17:27 +01001139 LY_ARRAY_COUNT_TYPE u;
1140
1141 LY_ARRAY_FOR(substmts, u) {
Radek Krejci38d85362019-09-05 16:26:38 +02001142 if (!substmts[u].storage) {
1143 continue;
1144 }
1145
Michal Vaskod989ba02020-08-24 10:59:24 +02001146 switch (substmts[u].stmt) {
Radek Krejci6b88a462021-02-17 12:39:34 +01001147 case LY_STMT_ACTION:
1148 case LY_STMT_ANYDATA:
1149 case LY_STMT_ANYXML:
1150 case LY_STMT_CONTAINER:
1151 case LY_STMT_CHOICE:
1152 case LY_STMT_LEAF:
1153 case LY_STMT_LEAF_LIST:
1154 case LY_STMT_LIST:
1155 case LY_STMT_NOTIFICATION:
1156 case LY_STMT_RPC:
1157 case LY_STMT_USES: {
1158 struct lysc_node *child, *child_next;
1159
1160 LY_LIST_FOR_SAFE(*((struct lysc_node **)substmts[u].storage), child_next, child) {
1161 lysc_node_free_(ctx, child);
Radek Krejci38d85362019-09-05 16:26:38 +02001162 }
1163 break;
Radek Krejci6b88a462021-02-17 12:39:34 +01001164 }
1165 case LY_STMT_CONFIG:
1166 case LY_STMT_STATUS:
1167 /* nothing to do */
1168 break;
Radek Krejci1b2eef82021-02-17 11:17:27 +01001169 case LY_STMT_DESCRIPTION:
1170 case LY_STMT_REFERENCE:
Radek Krejci38d85362019-09-05 16:26:38 +02001171 case LY_STMT_UNITS:
1172 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1173 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +02001174 const char *str = *((const char **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001175 if (!str) {
1176 break;
1177 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001178 lydict_remove(ctx, str);
Radek Krejci38d85362019-09-05 16:26:38 +02001179 } else {
1180 /* multiple items */
Michal Vasko22df3f02020-08-24 13:29:22 +02001181 const char **strs = *((const char ***)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001182 if (!strs) {
1183 break;
1184 }
1185 FREE_STRINGS(ctx, strs);
1186 }
1187 break;
Radek Krejci38d85362019-09-05 16:26:38 +02001188 case LY_STMT_IF_FEATURE: {
Michal Vasko22df3f02020-08-24 13:29:22 +02001189 struct lysc_iffeature *iff = *((struct lysc_iffeature **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001190 if (!iff) {
1191 break;
1192 }
1193 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1194 /* single item */
1195 lysc_iffeature_free(ctx, iff);
1196 free(iff);
1197 } else {
1198 /* multiple items */
1199 FREE_ARRAY(ctx, iff, lysc_iffeature_free);
1200 }
1201 break;
Radek Krejci6b88a462021-02-17 12:39:34 +01001202 case LY_STMT_TYPE:
1203 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1204 /* single item */
1205 struct lysc_type *type = *((struct lysc_type **)substmts[u].storage);
1206 if (!type) {
1207 break;
1208 }
1209 lysc_type_free(ctx, type);
1210 } else {
1211 /* multiple items */
1212 struct lysc_type **types = *((struct lysc_type ***)substmts[u].storage);
1213 if (!types) {
1214 break;
1215 }
1216 FREE_ARRAY(ctx, types, lysc_type2_free);
1217 }
1218 break;
Radek Krejci38d85362019-09-05 16:26:38 +02001219 }
1220
Radek Krejci0f969882020-08-21 16:56:47 +02001221 /* TODO other statements */
Radek Krejci38d85362019-09-05 16:26:38 +02001222 default:
1223 LOGINT(ctx);
1224 }
1225 }
Radek Krejci1b2eef82021-02-17 11:17:27 +01001226
1227 LY_ARRAY_FREE(substmts);
Radek Krejci38d85362019-09-05 16:26:38 +02001228}
David Sedlákebd3acf2019-07-26 15:04:32 +02001229
1230void
Michal Vaskob36053d2020-03-26 15:49:30 +01001231yang_parser_ctx_free(struct lys_yang_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +02001232{
1233 if (ctx) {
aPiecek8d4e75d2021-06-24 14:47:06 +02001234 if (ctx->main_ctx == (struct lys_parser_ctx *)ctx) {
1235 ly_set_erase(&ctx->tpdfs_nodes, NULL);
1236 ly_set_erase(&ctx->grps_nodes, NULL);
1237 }
Michal Vasko8a67eff2021-12-07 14:04:47 +01001238 ly_set_rm_index(ctx->parsed_mods, ctx->parsed_mods->count - 1, NULL);
1239 if (!ctx->parsed_mods->count) {
1240 ly_set_free(ctx->parsed_mods, NULL);
1241 }
David Sedlákebd3acf2019-07-26 15:04:32 +02001242 free(ctx);
1243 }
1244}
1245
1246void
Michal Vaskob36053d2020-03-26 15:49:30 +01001247yin_parser_ctx_free(struct lys_yin_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +02001248{
1249 if (ctx) {
aPiecek8d4e75d2021-06-24 14:47:06 +02001250 if (ctx->main_ctx == (struct lys_parser_ctx *)ctx) {
1251 ly_set_erase(&ctx->tpdfs_nodes, NULL);
1252 ly_set_erase(&ctx->grps_nodes, NULL);
1253 }
Michal Vasko8a67eff2021-12-07 14:04:47 +01001254 ly_set_rm_index(ctx->parsed_mods, ctx->parsed_mods->count - 1, NULL);
1255 if (!ctx->parsed_mods->count) {
1256 ly_set_free(ctx->parsed_mods, NULL);
1257 }
Michal Vaskob36053d2020-03-26 15:49:30 +01001258 lyxml_ctx_free(ctx->xmlctx);
David Sedlákebd3acf2019-07-26 15:04:32 +02001259 free(ctx);
1260 }
1261}