blob: da2f1e2fc7ca710c5b29bd1a2aac3b9e2f00c093 [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
Radek Krejcie7b95092019-05-15 11:03:07 +020015#include <stdlib.h>
16
Radek Krejci535ea9f2020-05-29 16:01:05 +020017#include "common.h"
Michal Vasko69730152020-10-09 16:30:07 +020018#include "compat.h"
Radek Krejci77114102021-03-10 15:21:57 +010019#include "dict.h"
Radek Krejci47fab892020-11-05 17:02:41 +010020#include "log.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include "plugins_exts.h"
22#include "plugins_types.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include "tree.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include "tree_data.h"
Michal Vaskofc2cd072021-02-24 13:17:17 +010025#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010026#include "tree_edit.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010028#include "tree_schema_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020029#include "xml.h"
Radek Krejci19a96102018-11-15 13:38:09 +010030#include "xpath.h"
31
Radek Krejci2a9fc652021-01-22 17:44:34 +010032void lysp_grp_free(struct ly_ctx *ctx, struct lysp_node_grp *grp);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010033static void lysc_node_free_(struct ly_ctx *ctx, struct lysc_node *node);
Radek Krejci19a96102018-11-15 13:38:09 +010034
35static void
36lysp_stmt_free(struct ly_ctx *ctx, struct lysp_stmt *stmt)
37{
38 struct lysp_stmt *child, *next;
39
Michal Vaskoe180ed02021-02-05 16:31:20 +010040 lydict_remove(ctx, stmt->stmt);
41 lydict_remove(ctx, stmt->arg);
Michal Vaskofc2cd072021-02-24 13:17:17 +010042 ly_free_prefix_data(stmt->format, stmt->prefix_data);
Radek Krejci19a96102018-11-15 13:38:09 +010043
44 LY_LIST_FOR_SAFE(stmt->child, next, child) {
45 lysp_stmt_free(ctx, child);
46 }
47
48 free(stmt);
49}
50
Radek Krejci2d7a47b2019-05-16 13:34:10 +020051void
Radek Krejci19a96102018-11-15 13:38:09 +010052lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext)
53{
54 struct lysp_stmt *stmt, *next;
aPiecek60d9d672021-04-27 15:49:57 +020055 struct lysp_node *node, *next_node;
Radek Krejci19a96102018-11-15 13:38:09 +010056
Michal Vaskoe180ed02021-02-05 16:31:20 +010057 lydict_remove(ctx, ext->name);
58 lydict_remove(ctx, ext->argument);
Michal Vaskofc2cd072021-02-24 13:17:17 +010059 ly_free_prefix_data(ext->format, ext->prefix_data);
aPiecek60d9d672021-04-27 15:49:57 +020060 LY_LIST_FOR_SAFE(ext->parsed, next_node, node) {
61 lysp_node_free(ctx, node);
62 }
Radek Krejci19a96102018-11-15 13:38:09 +010063
64 LY_LIST_FOR_SAFE(ext->child, next, stmt) {
65 lysp_stmt_free(ctx, stmt);
66 }
67}
68
David Sedlák298ff6d2019-07-26 14:29:03 +020069void
Radek Krejci19a96102018-11-15 13:38:09 +010070lysp_import_free(struct ly_ctx *ctx, struct lysp_import *import)
71{
72 /* imported module is freed directly from the context's list */
Michal Vaskoe180ed02021-02-05 16:31:20 +010073 lydict_remove(ctx, import->name);
74 lydict_remove(ctx, import->prefix);
75 lydict_remove(ctx, import->dsc);
76 lydict_remove(ctx, import->ref);
Radek Krejci19a96102018-11-15 13:38:09 +010077 FREE_ARRAY(ctx, import->exts, lysp_ext_instance_free);
78}
79
Radek Krejci771928a2021-01-19 13:42:36 +010080/**
81 * @brief Common function to erase include record in main module and submodule.
82 *
83 * There is a difference since the main module is expected to have the complete list if the included submodules and
84 * the parsed submodule is shared with any include in a submodule. Therefore, the referenced submodules in the include
85 * record are freed only from main module's records.
86 *
87 * @param[in] ctx libyang context
88 * @param[in] include The include record to be erased, the record itself is not freed.
89 * @param[in] main_module Flag to get know if the include record is placed in main module so also the referenced submodule
90 * is supposed to be freed.
91 */
92static void
93lysp_include_free_(struct ly_ctx *ctx, struct lysp_include *include, ly_bool main_module)
Radek Krejci19a96102018-11-15 13:38:09 +010094{
Radek Krejci771928a2021-01-19 13:42:36 +010095 if (main_module && include->submodule) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +020096 lysp_module_free((struct lysp_module *)include->submodule);
Radek Krejci19a96102018-11-15 13:38:09 +010097 }
Michal Vaskoe180ed02021-02-05 16:31:20 +010098 lydict_remove(ctx, include->name);
99 lydict_remove(ctx, include->dsc);
100 lydict_remove(ctx, include->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100101 FREE_ARRAY(ctx, include->exts, lysp_ext_instance_free);
102}
103
David Sedlákaa854b02019-07-22 14:17:10 +0200104void
Radek Krejci771928a2021-01-19 13:42:36 +0100105lysp_include_free_submodule(struct ly_ctx *ctx, struct lysp_include *include)
106{
107 return lysp_include_free_(ctx, include, 0);
108}
109
110void
111lysp_include_free(struct ly_ctx *ctx, struct lysp_include *include)
112{
113 return lysp_include_free_(ctx, include, 1);
114}
115
116void
Radek Krejci19a96102018-11-15 13:38:09 +0100117lysp_revision_free(struct ly_ctx *ctx, struct lysp_revision *rev)
118{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100119 lydict_remove(ctx, rev->dsc);
120 lydict_remove(ctx, rev->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100121 FREE_ARRAY(ctx, rev->exts, lysp_ext_instance_free);
122}
123
David Sedlák986cb412019-07-04 13:10:11 +0200124void
Radek Krejci19a96102018-11-15 13:38:09 +0100125lysp_ext_free(struct ly_ctx *ctx, struct lysp_ext *ext)
126{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100127 lydict_remove(ctx, ext->name);
Radek Krejci9f87b0c2021-03-05 14:45:26 +0100128 lydict_remove(ctx, ext->argname);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100129 lydict_remove(ctx, ext->dsc);
130 lydict_remove(ctx, ext->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100131 FREE_ARRAY(ctx, ext->exts, lysp_ext_instance_free);
Michal Vasko5fe75f12020-03-02 13:52:37 +0100132 if (ext->compiled) {
133 lysc_extension_free(ctx, &ext->compiled);
134 }
Radek Krejci19a96102018-11-15 13:38:09 +0100135}
136
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200137void
Radek Krejci19a96102018-11-15 13:38:09 +0100138lysp_feature_free(struct ly_ctx *ctx, struct lysp_feature *feat)
139{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100140 lydict_remove(ctx, feat->name);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200141 FREE_ARRAY(ctx, feat->iffeatures, lysp_qname_free);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100142 FREE_ARRAY(ctx, feat->iffeatures_c, lysc_iffeature_free);
143 LY_ARRAY_FREE(feat->depfeatures);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100144 lydict_remove(ctx, feat->dsc);
145 lydict_remove(ctx, feat->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100146 FREE_ARRAY(ctx, feat->exts, lysp_ext_instance_free);
147}
148
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200149void
Radek Krejci19a96102018-11-15 13:38:09 +0100150lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident)
151{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100152 lydict_remove(ctx, ident->name);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200153 FREE_ARRAY(ctx, ident->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100154 FREE_STRINGS(ctx, ident->bases);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100155 lydict_remove(ctx, ident->dsc);
156 lydict_remove(ctx, ident->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100157 FREE_ARRAY(ctx, ident->exts, lysp_ext_instance_free);
158}
159
Michal Vasko7f45cf22020-10-01 12:49:44 +0200160void
Radek Krejci19a96102018-11-15 13:38:09 +0100161lysp_restr_free(struct ly_ctx *ctx, struct lysp_restr *restr)
162{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100163 lydict_remove(ctx, restr->arg.str);
164 lydict_remove(ctx, restr->emsg);
165 lydict_remove(ctx, restr->eapptag);
166 lydict_remove(ctx, restr->dsc);
167 lydict_remove(ctx, restr->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100168 FREE_ARRAY(ctx, restr->exts, lysp_ext_instance_free);
169}
170
171static void
172lysp_type_enum_free(struct ly_ctx *ctx, struct lysp_type_enum *item)
173{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100174 lydict_remove(ctx, item->name);
175 lydict_remove(ctx, item->dsc);
176 lydict_remove(ctx, item->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200177 FREE_ARRAY(ctx, item->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100178 FREE_ARRAY(ctx, item->exts, lysp_ext_instance_free);
179}
180
Radek Krejcicdfecd92018-11-26 11:27:32 +0100181void lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type);
Michal Vasko004d3152020-06-11 19:59:22 +0200182
David Sedlák32488102019-07-15 17:44:10 +0200183void
Radek Krejci19a96102018-11-15 13:38:09 +0100184lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type)
185{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100186 lydict_remove(ctx, type->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100187 FREE_MEMBER(ctx, type->range, lysp_restr_free);
188 FREE_MEMBER(ctx, type->length, lysp_restr_free);
189 FREE_ARRAY(ctx, type->patterns, lysp_restr_free);
190 FREE_ARRAY(ctx, type->enums, lysp_type_enum_free);
191 FREE_ARRAY(ctx, type->bits, lysp_type_enum_free);
Michal Vasko004d3152020-06-11 19:59:22 +0200192 lyxp_expr_free(ctx, type->path);
Radek Krejci19a96102018-11-15 13:38:09 +0100193 FREE_STRINGS(ctx, type->bases);
194 FREE_ARRAY(ctx, type->types, lysp_type_free);
195 FREE_ARRAY(ctx, type->exts, lysp_ext_instance_free);
196 if (type->compiled) {
197 lysc_type_free(ctx, type->compiled);
198 }
199}
200
David Sedlák04e17b22019-07-19 15:29:48 +0200201void
Radek Krejci19a96102018-11-15 13:38:09 +0100202lysp_tpdf_free(struct ly_ctx *ctx, struct lysp_tpdf *tpdf)
203{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100204 lydict_remove(ctx, tpdf->name);
205 lydict_remove(ctx, tpdf->units);
206 lydict_remove(ctx, tpdf->dflt.str);
207 lydict_remove(ctx, tpdf->dsc);
208 lydict_remove(ctx, tpdf->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100209 FREE_ARRAY(ctx, tpdf->exts, lysp_ext_instance_free);
210
211 lysp_type_free(ctx, &tpdf->type);
212
213}
214
Radek Krejcif538ce52019-03-05 10:46:14 +0100215void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100216lysp_grp_free(struct ly_ctx *ctx, struct lysp_node_grp *grp)
Radek Krejci19a96102018-11-15 13:38:09 +0100217{
218 struct lysp_node *node, *next;
219
Radek Krejci19a96102018-11-15 13:38:09 +0100220 FREE_ARRAY(ctx, grp->typedefs, lysp_tpdf_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100221 LY_LIST_FOR_SAFE((struct lysp_node *)grp->groupings, next, node) {
222 lysp_node_free(ctx, node);
223 }
Radek Krejci01180ac2021-01-27 08:48:22 +0100224 LY_LIST_FOR_SAFE(grp->child, next, node) {
Radek Krejci19a96102018-11-15 13:38:09 +0100225 lysp_node_free(ctx, node);
226 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100227 LY_LIST_FOR_SAFE((struct lysp_node *)grp->actions, next, node) {
228 lysp_node_free(ctx, node);
229 }
230 LY_LIST_FOR_SAFE((struct lysp_node *)grp->notifs, next, node) {
231 lysp_node_free(ctx, node);
232 }
Radek Krejci19a96102018-11-15 13:38:09 +0100233}
234
Radek Krejcif09e4e82019-06-14 15:08:11 +0200235void
Radek Krejci19a96102018-11-15 13:38:09 +0100236lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when)
237{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100238 lydict_remove(ctx, when->cond);
239 lydict_remove(ctx, when->dsc);
240 lydict_remove(ctx, when->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100241 FREE_ARRAY(ctx, when->exts, lysp_ext_instance_free);
242}
243
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200244void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100245lysp_augment_free(struct ly_ctx *ctx, struct lysp_node_augment *augment)
Radek Krejci19a96102018-11-15 13:38:09 +0100246{
247 struct lysp_node *node, *next;
248
Radek Krejci19a96102018-11-15 13:38:09 +0100249 LY_LIST_FOR_SAFE(augment->child, next, node) {
250 lysp_node_free(ctx, node);
251 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100252 LY_LIST_FOR_SAFE((struct lysp_node *)augment->actions, next, node) {
253 lysp_node_free(ctx, node);
254 }
255 LY_LIST_FOR_SAFE((struct lysp_node *)augment->notifs, next, node) {
256 lysp_node_free(ctx, node);
257 }
Radek Krejci19a96102018-11-15 13:38:09 +0100258}
259
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200260void
Michal Vasko7f45cf22020-10-01 12:49:44 +0200261lysp_qname_free(struct ly_ctx *ctx, struct lysp_qname *qname)
262{
263 if (qname) {
Michal Vaskoe180ed02021-02-05 16:31:20 +0100264 lydict_remove(ctx, qname->str);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200265 }
266}
267
268void
Radek Krejci19a96102018-11-15 13:38:09 +0100269lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d)
270{
Michal Vasko22df3f02020-08-24 13:29:22 +0200271 struct lysp_deviate_add *add = (struct lysp_deviate_add *)d;
272 struct lysp_deviate_rpl *rpl = (struct lysp_deviate_rpl *)d;
Radek Krejci19a96102018-11-15 13:38:09 +0100273
274 FREE_ARRAY(ctx, d->exts, lysp_ext_instance_free);
Michal Vaskod989ba02020-08-24 10:59:24 +0200275 switch (d->mod) {
Radek Krejci19a96102018-11-15 13:38:09 +0100276 case LYS_DEV_NOT_SUPPORTED:
277 /* nothing to do */
278 break;
279 case LYS_DEV_ADD:
280 case LYS_DEV_DELETE: /* compatible for dynamically allocated data */
Michal Vaskoe180ed02021-02-05 16:31:20 +0100281 lydict_remove(ctx, add->units);
Radek Krejci19a96102018-11-15 13:38:09 +0100282 FREE_ARRAY(ctx, add->musts, lysp_restr_free);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200283 FREE_ARRAY(ctx, add->uniques, lysp_qname_free);
284 FREE_ARRAY(ctx, add->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100285 break;
286 case LYS_DEV_REPLACE:
287 FREE_MEMBER(ctx, rpl->type, lysp_type_free);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100288 lydict_remove(ctx, rpl->units);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200289 lysp_qname_free(ctx, &rpl->dflt);
Radek Krejci19a96102018-11-15 13:38:09 +0100290 break;
291 default:
292 LOGINT(ctx);
293 break;
294 }
295}
296
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200297void
Radek Krejci19a96102018-11-15 13:38:09 +0100298lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev)
299{
300 struct lysp_deviate *next, *iter;
301
Michal Vaskoe180ed02021-02-05 16:31:20 +0100302 lydict_remove(ctx, dev->nodeid);
303 lydict_remove(ctx, dev->dsc);
304 lydict_remove(ctx, dev->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100305 LY_LIST_FOR_SAFE(dev->deviates, next, iter) {
306 lysp_deviate_free(ctx, iter);
307 free(iter);
308 }
309 FREE_ARRAY(ctx, dev->exts, lysp_ext_instance_free);
310}
311
David Sedlákd2d676a2019-07-22 11:28:19 +0200312void
Radek Krejci19a96102018-11-15 13:38:09 +0100313lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref)
314{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100315 lydict_remove(ctx, ref->nodeid);
316 lydict_remove(ctx, ref->dsc);
317 lydict_remove(ctx, ref->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200318 FREE_ARRAY(ctx, ref->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100319 FREE_ARRAY(ctx, ref->musts, lysp_restr_free);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100320 lydict_remove(ctx, ref->presence);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200321 FREE_ARRAY(ctx, ref->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100322 FREE_ARRAY(ctx, ref->exts, lysp_ext_instance_free);
323}
324
Radek Krejci2d7a47b2019-05-16 13:34:10 +0200325void
Radek Krejci19a96102018-11-15 13:38:09 +0100326lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node)
327{
328 struct lysp_node *child, *next;
Michal Vasko856ea972021-06-09 09:44:30 +0200329 struct lysp_node_container *cont;
330 struct lysp_node_leaf *leaf;
331 struct lysp_node_leaflist *llist;
332 struct lysp_node_list *list;
333 struct lysp_node_choice *choice;
334 struct lysp_node_case *cas;
335 struct lysp_node_uses *uses;
336 struct lysp_node_action *act;
337 struct lysp_node_action_inout *inout;
338 struct lysp_node_notif *notif;
Radek Krejci9a3823e2021-01-27 20:26:46 +0100339 struct lysp_restr *musts = lysp_node_musts(node);
340 struct lysp_when *when = lysp_node_when(node);
Radek Krejci19a96102018-11-15 13:38:09 +0100341
Michal Vaskoe180ed02021-02-05 16:31:20 +0100342 lydict_remove(ctx, node->name);
343 lydict_remove(ctx, node->dsc);
344 lydict_remove(ctx, node->ref);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200345 FREE_ARRAY(ctx, node->iffeatures, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100346 FREE_ARRAY(ctx, node->exts, lysp_ext_instance_free);
347
Radek Krejci9a3823e2021-01-27 20:26:46 +0100348 FREE_MEMBER(ctx, when, lysp_when_free);
349 FREE_ARRAY(ctx, musts, lysp_restr_free);
350
Michal Vaskod989ba02020-08-24 10:59:24 +0200351 switch (node->nodetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100352 case LYS_CONTAINER:
Michal Vasko856ea972021-06-09 09:44:30 +0200353 cont = (struct lysp_node_container *)node;
354
355 lydict_remove(ctx, cont->presence);
356 FREE_ARRAY(ctx, cont->typedefs, lysp_tpdf_free);
357 if (cont->groupings) {
358 LY_LIST_FOR_SAFE(&cont->groupings->node, next, child) {
359 lysp_node_free(ctx, child);
360 }
361 }
362 LY_LIST_FOR_SAFE(cont->child, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100363 lysp_node_free(ctx, child);
364 }
Michal Vasko856ea972021-06-09 09:44:30 +0200365 if (cont->actions) {
366 LY_LIST_FOR_SAFE(&cont->actions->node, next, child) {
367 lysp_node_free(ctx, child);
368 }
Radek Krejci19a96102018-11-15 13:38:09 +0100369 }
Michal Vasko856ea972021-06-09 09:44:30 +0200370 if (cont->notifs) {
371 LY_LIST_FOR_SAFE(&cont->notifs->node, next, child) {
372 lysp_node_free(ctx, child);
373 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100374 }
Radek Krejci19a96102018-11-15 13:38:09 +0100375 break;
376 case LYS_LEAF:
Michal Vasko856ea972021-06-09 09:44:30 +0200377 leaf = (struct lysp_node_leaf *)node;
378
379 lysp_type_free(ctx, &leaf->type);
380 lydict_remove(ctx, leaf->units);
381 lydict_remove(ctx, leaf->dflt.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100382 break;
383 case LYS_LEAFLIST:
Michal Vasko856ea972021-06-09 09:44:30 +0200384 llist = (struct lysp_node_leaflist *)node;
385
386 lysp_type_free(ctx, &llist->type);
387 lydict_remove(ctx, llist->units);
388 FREE_ARRAY(ctx, llist->dflts, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100389 break;
390 case LYS_LIST:
Michal Vasko856ea972021-06-09 09:44:30 +0200391 list = (struct lysp_node_list *)node;
392
393 lydict_remove(ctx, list->key);
394 FREE_ARRAY(ctx, list->typedefs, lysp_tpdf_free);
395 if (list->groupings) {
396 LY_LIST_FOR_SAFE(&list->groupings->node, next, child) {
397 lysp_node_free(ctx, child);
398 }
399 }
400 LY_LIST_FOR_SAFE(list->child, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100401 lysp_node_free(ctx, child);
402 }
Michal Vasko856ea972021-06-09 09:44:30 +0200403 if (list->actions) {
404 LY_LIST_FOR_SAFE(&list->actions->node, next, child) {
405 lysp_node_free(ctx, child);
406 }
Radek Krejci19a96102018-11-15 13:38:09 +0100407 }
Michal Vasko856ea972021-06-09 09:44:30 +0200408 if (list->notifs) {
409 LY_LIST_FOR_SAFE(&list->notifs->node, next, child) {
410 lysp_node_free(ctx, child);
411 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100412 }
Michal Vasko856ea972021-06-09 09:44:30 +0200413 FREE_ARRAY(ctx, list->uniques, lysp_qname_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100414 break;
415 case LYS_CHOICE:
Michal Vasko856ea972021-06-09 09:44:30 +0200416 choice = (struct lysp_node_choice *)node;
417
418 LY_LIST_FOR_SAFE(choice->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100419 lysp_node_free(ctx, child);
420 }
Michal Vasko856ea972021-06-09 09:44:30 +0200421 lydict_remove(ctx, choice->dflt.str);
Radek Krejci19a96102018-11-15 13:38:09 +0100422 break;
423 case LYS_CASE:
Michal Vasko856ea972021-06-09 09:44:30 +0200424 cas = (struct lysp_node_case *)node;
425
426 LY_LIST_FOR_SAFE(cas->child, next, child) {
Radek Krejci19a96102018-11-15 13:38:09 +0100427 lysp_node_free(ctx, child);
428 }
429 break;
430 case LYS_ANYDATA:
431 case LYS_ANYXML:
Radek Krejci9a3823e2021-01-27 20:26:46 +0100432 /* nothing special to do */
Radek Krejci19a96102018-11-15 13:38:09 +0100433 break;
434 case LYS_USES:
Michal Vasko856ea972021-06-09 09:44:30 +0200435 uses = (struct lysp_node_uses *)node;
436
437 FREE_ARRAY(ctx, uses->refines, lysp_refine_free);
438 if (uses->augments) {
439 LY_LIST_FOR_SAFE(&uses->augments->node, next, child) {
440 lysp_node_free(ctx, child);
441 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100442 }
443 break;
444 case LYS_RPC:
445 case LYS_ACTION:
Michal Vasko856ea972021-06-09 09:44:30 +0200446 act = (struct lysp_node_action *)node;
447
448 FREE_ARRAY(ctx, act->typedefs, lysp_tpdf_free);
449 if (act->groupings) {
450 LY_LIST_FOR_SAFE(&act->groupings->node, next, child) {
451 lysp_node_free(ctx, child);
452 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100453 }
Michal Vasko856ea972021-06-09 09:44:30 +0200454 if (act->input.nodetype) {
455 lysp_node_free(ctx, &act->input.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100456 }
Michal Vasko856ea972021-06-09 09:44:30 +0200457 if (act->output.nodetype) {
458 lysp_node_free(ctx, &act->output.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100459 }
460 break;
461 case LYS_INPUT:
462 case LYS_OUTPUT:
Michal Vasko856ea972021-06-09 09:44:30 +0200463 inout = (struct lysp_node_action_inout *)node;
464
465 FREE_ARRAY(ctx, inout->typedefs, lysp_tpdf_free);
466 if (inout->groupings) {
467 LY_LIST_FOR_SAFE(&inout->groupings->node, next, child) {
468 lysp_node_free(ctx, child);
469 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100470 }
Michal Vasko856ea972021-06-09 09:44:30 +0200471 LY_LIST_FOR_SAFE(inout->child, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100472 lysp_node_free(ctx, child);
473 }
474 /* do not free the node, it is never standalone but part of the action node */
475 return;
476 case LYS_NOTIF:
Michal Vasko856ea972021-06-09 09:44:30 +0200477 notif = (struct lysp_node_notif *)node;
478
479 FREE_ARRAY(ctx, notif->typedefs, lysp_tpdf_free);
480 if (notif->groupings) {
481 LY_LIST_FOR_SAFE(&notif->groupings->node, next, child) {
482 lysp_node_free(ctx, child);
483 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100484 }
Michal Vasko856ea972021-06-09 09:44:30 +0200485 LY_LIST_FOR_SAFE(notif->child, next, child) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100486 lysp_node_free(ctx, child);
487 }
488 break;
489 case LYS_GROUPING:
490 lysp_grp_free(ctx, (struct lysp_node_grp *)node);
491 break;
492 case LYS_AUGMENT:
Radek Krejci9a3823e2021-01-27 20:26:46 +0100493 lysp_augment_free(ctx, ((struct lysp_node_augment *)node));
Radek Krejci19a96102018-11-15 13:38:09 +0100494 break;
495 default:
496 LOGINT(ctx);
497 }
498
499 free(node);
500}
501
Radek Krejci15f10ab2020-11-03 14:14:14 +0100502void
Radek Krejci19a96102018-11-15 13:38:09 +0100503lysp_module_free(struct lysp_module *module)
504{
505 struct ly_ctx *ctx;
506 struct lysp_node *node, *next;
507
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100508 if (!module) {
509 return;
510 }
511 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100512
513 FREE_ARRAY(ctx, module->imports, lysp_import_free);
Radek Krejci771928a2021-01-19 13:42:36 +0100514 FREE_ARRAY(ctx, module->includes, module->is_submod ? lysp_include_free_submodule : lysp_include_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100515
Radek Krejci19a96102018-11-15 13:38:09 +0100516 FREE_ARRAY(ctx, module->revs, lysp_revision_free);
517 FREE_ARRAY(ctx, module->extensions, lysp_ext_free);
518 FREE_ARRAY(ctx, module->features, lysp_feature_free);
519 FREE_ARRAY(ctx, module->identities, lysp_ident_free);
520 FREE_ARRAY(ctx, module->typedefs, lysp_tpdf_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100521 LY_LIST_FOR_SAFE((struct lysp_node *)module->groupings, next, node) {
522 lysp_node_free(ctx, node);
523 }
Radek Krejci19a96102018-11-15 13:38:09 +0100524 LY_LIST_FOR_SAFE(module->data, next, node) {
525 lysp_node_free(ctx, node);
526 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100527 LY_LIST_FOR_SAFE((struct lysp_node *)module->augments, next, node) {
528 lysp_node_free(ctx, node);
529 }
530 LY_LIST_FOR_SAFE((struct lysp_node *)module->rpcs, next, node) {
531 lysp_node_free(ctx, node);
532 }
533 LY_LIST_FOR_SAFE((struct lysp_node *)module->notifs, next, node) {
534 lysp_node_free(ctx, node);
535 }
Radek Krejci19a96102018-11-15 13:38:09 +0100536 FREE_ARRAY(ctx, module->deviations, lysp_deviation_free);
537 FREE_ARRAY(ctx, module->exts, lysp_ext_instance_free);
538
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200539 if (module->is_submod) {
540 struct lysp_submodule *submod = (struct lysp_submodule *)module;
541
Michal Vaskoe180ed02021-02-05 16:31:20 +0100542 lydict_remove(ctx, submod->name);
543 lydict_remove(ctx, submod->filepath);
544 lydict_remove(ctx, submod->prefix);
545 lydict_remove(ctx, submod->org);
546 lydict_remove(ctx, submod->contact);
547 lydict_remove(ctx, submod->dsc);
548 lydict_remove(ctx, submod->ref);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200549 }
550
Radek Krejci19a96102018-11-15 13:38:09 +0100551 free(module);
552}
553
Radek Krejci0af46292019-01-11 16:02:31 +0100554void
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100555lysc_extension_free(struct ly_ctx *ctx, struct lysc_ext **ext)
556{
557 if (--(*ext)->refcount) {
558 return;
559 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100560 lydict_remove(ctx, (*ext)->name);
Radek Krejci9f87b0c2021-03-05 14:45:26 +0100561 lydict_remove(ctx, (*ext)->argname);
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100562 FREE_ARRAY(ctx, (*ext)->exts, lysc_ext_instance_free);
563 free(*ext);
Radek Krejci720d2612021-03-03 19:44:22 +0100564 *ext = NULL;
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100565}
566
567void
Radek Krejci19a96102018-11-15 13:38:09 +0100568lysc_ext_instance_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
569{
fredganebc50572019-10-31 15:39:23 +0800570 if (ext->def && ext->def->plugin && ext->def->plugin->free) {
Radek Krejci38d85362019-09-05 16:26:38 +0200571 ext->def->plugin->free(ctx, ext);
572 }
Juraj Vijtiuk4a19ab02020-03-03 13:50:14 +0100573 if (ext->def) {
574 lysc_extension_free(ctx, &ext->def);
575 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100576 lydict_remove(ctx, ext->argument);
Radek Krejci0935f412019-08-20 16:15:18 +0200577 FREE_ARRAY(ctx, ext->exts, lysc_ext_instance_free);
578}
579
580void
Radek Krejci19a96102018-11-15 13:38:09 +0100581lysc_iffeature_free(struct ly_ctx *UNUSED(ctx), struct lysc_iffeature *iff)
582{
583 LY_ARRAY_FREE(iff->features);
584 free(iff->expr);
585}
586
587static void
Radek Krejci00b874b2019-02-12 10:54:50 +0100588lysc_when_free(struct ly_ctx *ctx, struct lysc_when **w)
Radek Krejci58d171e2018-11-23 13:50:55 +0100589{
Radek Krejci00b874b2019-02-12 10:54:50 +0100590 if (--(*w)->refcount) {
591 return;
592 }
593 lyxp_expr_free(ctx, (*w)->cond);
Radek Krejci8df109d2021-04-23 12:19:08 +0200594 ly_free_prefix_data(LY_VALUE_SCHEMA_RESOLVED, (*w)->prefixes);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100595 lydict_remove(ctx, (*w)->dsc);
596 lydict_remove(ctx, (*w)->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +0100597 FREE_ARRAY(ctx, (*w)->exts, lysc_ext_instance_free);
598 free(*w);
Radek Krejci58d171e2018-11-23 13:50:55 +0100599}
600
Radek Krejciccd20f12019-02-15 14:12:27 +0100601void
Radek Krejci58d171e2018-11-23 13:50:55 +0100602lysc_must_free(struct ly_ctx *ctx, struct lysc_must *must)
603{
604 lyxp_expr_free(ctx, must->cond);
Radek Krejci8df109d2021-04-23 12:19:08 +0200605 ly_free_prefix_data(LY_VALUE_SCHEMA_RESOLVED, must->prefixes);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100606 lydict_remove(ctx, must->emsg);
607 lydict_remove(ctx, must->eapptag);
608 lydict_remove(ctx, must->dsc);
609 lydict_remove(ctx, must->ref);
Radek Krejci58d171e2018-11-23 13:50:55 +0100610 FREE_ARRAY(ctx, must->exts, lysc_ext_instance_free);
611}
612
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100613void
Radek Krejci19a96102018-11-15 13:38:09 +0100614lysc_ident_free(struct ly_ctx *ctx, struct lysc_ident *ident)
615{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100616 lydict_remove(ctx, ident->name);
617 lydict_remove(ctx, ident->dsc);
618 lydict_remove(ctx, ident->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100619 LY_ARRAY_FREE(ident->derived);
620 FREE_ARRAY(ctx, ident->exts, lysc_ext_instance_free);
621}
622
Radek Krejci19a96102018-11-15 13:38:09 +0100623static void
624lysc_range_free(struct ly_ctx *ctx, struct lysc_range *range)
625{
626 LY_ARRAY_FREE(range->parts);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100627 lydict_remove(ctx, range->eapptag);
628 lydict_remove(ctx, range->emsg);
629 lydict_remove(ctx, range->dsc);
630 lydict_remove(ctx, range->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100631 FREE_ARRAY(ctx, range->exts, lysc_ext_instance_free);
632}
633
634static void
635lysc_pattern_free(struct ly_ctx *ctx, struct lysc_pattern **pattern)
636{
637 if (--(*pattern)->refcount) {
638 return;
639 }
Radek Krejci54579462019-04-30 12:47:06 +0200640 pcre2_code_free((*pattern)->code);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100641 lydict_remove(ctx, (*pattern)->expr);
642 lydict_remove(ctx, (*pattern)->eapptag);
643 lydict_remove(ctx, (*pattern)->emsg);
644 lydict_remove(ctx, (*pattern)->dsc);
645 lydict_remove(ctx, (*pattern)->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100646 FREE_ARRAY(ctx, (*pattern)->exts, lysc_ext_instance_free);
647 free(*pattern);
648}
649
650static void
Radek Krejci693262f2019-04-29 15:23:20 +0200651lysc_enum_item_free(struct ly_ctx *ctx, struct lysc_type_bitenum_item *item)
Radek Krejci19a96102018-11-15 13:38:09 +0100652{
Michal Vaskoe180ed02021-02-05 16:31:20 +0100653 lydict_remove(ctx, item->name);
654 lydict_remove(ctx, item->dsc);
655 lydict_remove(ctx, item->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100656 FREE_ARRAY(ctx, item->exts, lysc_ext_instance_free);
657}
658
Radek Krejcia3045382018-11-22 14:30:31 +0100659static void
660lysc_type2_free(struct ly_ctx *ctx, struct lysc_type **type)
661{
662 lysc_type_free(ctx, *type);
663}
Radek Krejci0f969882020-08-21 16:56:47 +0200664
Radek Krejcicdfecd92018-11-26 11:27:32 +0100665void
Radek Krejci19a96102018-11-15 13:38:09 +0100666lysc_type_free(struct ly_ctx *ctx, struct lysc_type *type)
667{
668 if (--type->refcount) {
669 return;
670 }
Radek Krejcib915ac92020-08-14 23:31:04 +0200671
Michal Vaskod989ba02020-08-24 10:59:24 +0200672 switch (type->basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100673 case LY_TYPE_BINARY:
Michal Vasko22df3f02020-08-24 13:29:22 +0200674 FREE_MEMBER(ctx, ((struct lysc_type_bin *)type)->length, lysc_range_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100675 break;
676 case LY_TYPE_BITS:
Michal Vasko22df3f02020-08-24 13:29:22 +0200677 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 +0100678 break;
Radek Krejci6cba4292018-11-15 17:33:29 +0100679 case LY_TYPE_DEC64:
Michal Vasko22df3f02020-08-24 13:29:22 +0200680 FREE_MEMBER(ctx, ((struct lysc_type_dec *)type)->range, lysc_range_free);
Radek Krejci6cba4292018-11-15 17:33:29 +0100681 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100682 case LY_TYPE_STRING:
Michal Vasko22df3f02020-08-24 13:29:22 +0200683 FREE_MEMBER(ctx, ((struct lysc_type_str *)type)->length, lysc_range_free);
684 FREE_ARRAY(ctx, ((struct lysc_type_str *)type)->patterns, lysc_pattern_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100685 break;
686 case LY_TYPE_ENUM:
Michal Vasko22df3f02020-08-24 13:29:22 +0200687 FREE_ARRAY(ctx, ((struct lysc_type_enum *)type)->enums, lysc_enum_item_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100688 break;
689 case LY_TYPE_INT8:
690 case LY_TYPE_UINT8:
691 case LY_TYPE_INT16:
692 case LY_TYPE_UINT16:
693 case LY_TYPE_INT32:
694 case LY_TYPE_UINT32:
695 case LY_TYPE_INT64:
696 case LY_TYPE_UINT64:
Michal Vasko22df3f02020-08-24 13:29:22 +0200697 FREE_MEMBER(ctx, ((struct lysc_type_num *)type)->range, lysc_range_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100698 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100699 case LY_TYPE_IDENT:
Michal Vasko22df3f02020-08-24 13:29:22 +0200700 LY_ARRAY_FREE(((struct lysc_type_identityref *)type)->bases);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100701 break;
Radek Krejcia3045382018-11-22 14:30:31 +0100702 case LY_TYPE_UNION:
Michal Vasko22df3f02020-08-24 13:29:22 +0200703 FREE_ARRAY(ctx, ((struct lysc_type_union *)type)->types, lysc_type2_free);
Radek Krejcia3045382018-11-22 14:30:31 +0100704 break;
705 case LY_TYPE_LEAFREF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200706 lyxp_expr_free(ctx, ((struct lysc_type_leafref *)type)->path);
Radek Krejci8df109d2021-04-23 12:19:08 +0200707 ly_free_prefix_data(LY_VALUE_SCHEMA_RESOLVED, ((struct lysc_type_leafref *)type)->prefixes);
Radek Krejcia3045382018-11-22 14:30:31 +0100708 break;
Radek Krejci16c0f822018-11-16 10:46:10 +0100709 case LY_TYPE_INST:
Radek Krejci19a96102018-11-15 13:38:09 +0100710 case LY_TYPE_BOOL:
711 case LY_TYPE_EMPTY:
Radek Krejci43699232018-11-23 14:59:46 +0100712 case LY_TYPE_UNKNOWN:
Radek Krejci19a96102018-11-15 13:38:09 +0100713 /* nothing to do */
714 break;
715 }
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200716
717 FREE_ARRAY(ctx, type->exts, lysc_ext_instance_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100718 free(type);
719}
720
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100721void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100722lysc_node_action_inout_free(struct ly_ctx *ctx, struct lysc_node_action_inout *inout)
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100723{
724 struct lysc_node *child, *child_next;
725
Radek Krejcif538ce52019-03-05 10:46:14 +0100726 FREE_ARRAY(ctx, inout->musts, lysc_must_free);
Radek Krejci01180ac2021-01-27 08:48:22 +0100727 LY_LIST_FOR_SAFE(inout->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100728 lysc_node_free_(ctx, child);
Radek Krejcif538ce52019-03-05 10:46:14 +0100729 }
730}
731
732void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100733lysc_node_action_free(struct ly_ctx *ctx, struct lysc_node_action *action)
Radek Krejcif538ce52019-03-05 10:46:14 +0100734{
Michal Vasko37a0fe62021-02-03 09:53:04 +0100735 FREE_ARRAY(ctx, action->when, lysc_when_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100736 if (action->input.nodetype) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +0100737 lysc_node_free_(ctx, &action->input.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100738 }
739 if (action->output.nodetype) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +0100740 lysc_node_free_(ctx, &action->output.node);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100741 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100742}
743
Radek Krejcifc11bd72019-04-11 16:00:05 +0200744void
Radek Krejci2a9fc652021-01-22 17:44:34 +0100745lysc_node_notif_free(struct ly_ctx *ctx, struct lysc_node_notif *notif)
Radek Krejcifc11bd72019-04-11 16:00:05 +0200746{
747 struct lysc_node *child, *child_next;
748
Michal Vasko37a0fe62021-02-03 09:53:04 +0100749 FREE_ARRAY(ctx, notif->when, lysc_when_free);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200750 FREE_ARRAY(ctx, notif->musts, lysc_must_free);
Radek Krejci01180ac2021-01-27 08:48:22 +0100751 LY_LIST_FOR_SAFE(notif->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100752 lysc_node_free_(ctx, child);
Radek Krejcifc11bd72019-04-11 16:00:05 +0200753 }
754}
755
Radek Krejcif2de0ed2019-05-02 14:13:18 +0200756void
Radek Krejci19a96102018-11-15 13:38:09 +0100757lysc_node_container_free(struct ly_ctx *ctx, struct lysc_node_container *node)
758{
759 struct lysc_node *child, *child_next;
760
761 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100762 lysc_node_free_(ctx, child);
Radek Krejci19a96102018-11-15 13:38:09 +0100763 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100764 LY_LIST_FOR_SAFE((struct lysc_node *)node->actions, child_next, child) {
765 lysc_node_free_(ctx, child);
766 }
767 LY_LIST_FOR_SAFE((struct lysc_node *)node->notifs, child_next, child) {
768 lysc_node_free_(ctx, child);
769 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100770 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci58d171e2018-11-23 13:50:55 +0100771 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100772}
773
774static void
775lysc_node_leaf_free(struct ly_ctx *ctx, struct lysc_node_leaf *node)
776{
Radek Krejci9a3823e2021-01-27 20:26:46 +0100777 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci58d171e2018-11-23 13:50:55 +0100778 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci19a96102018-11-15 13:38:09 +0100779 if (node->type) {
780 lysc_type_free(ctx, node->type);
781 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100782 lydict_remove(ctx, node->units);
Radek Krejcia1911222019-07-22 17:24:50 +0200783 if (node->dflt) {
784 node->dflt->realtype->plugin->free(ctx, node->dflt);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200785 lysc_type_free(ctx, (struct lysc_type *)node->dflt->realtype);
Radek Krejcia1911222019-07-22 17:24:50 +0200786 free(node->dflt);
787 }
Radek Krejci19a96102018-11-15 13:38:09 +0100788}
789
Radek Krejci42452ac2018-11-28 17:09:52 +0100790static void
791lysc_node_leaflist_free(struct ly_ctx *ctx, struct lysc_node_leaflist *node)
792{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200793 LY_ARRAY_COUNT_TYPE u;
Radek Krejci42452ac2018-11-28 17:09:52 +0100794
Radek Krejci9a3823e2021-01-27 20:26:46 +0100795 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci42452ac2018-11-28 17:09:52 +0100796 FREE_ARRAY(ctx, node->musts, lysc_must_free);
797 if (node->type) {
798 lysc_type_free(ctx, node->type);
799 }
Michal Vaskoe180ed02021-02-05 16:31:20 +0100800 lydict_remove(ctx, node->units);
Radek Krejci42452ac2018-11-28 17:09:52 +0100801 LY_ARRAY_FOR(node->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +0200802 node->dflts[u]->realtype->plugin->free(ctx, node->dflts[u]);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200803 lysc_type_free(ctx, (struct lysc_type *)node->dflts[u]->realtype);
Radek Krejcia1911222019-07-22 17:24:50 +0200804 free(node->dflts[u]);
Radek Krejci42452ac2018-11-28 17:09:52 +0100805 }
806 LY_ARRAY_FREE(node->dflts);
807}
808
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100809static void
810lysc_node_list_free(struct ly_ctx *ctx, struct lysc_node_list *node)
811{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200812 LY_ARRAY_COUNT_TYPE u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100813 struct lysc_node *child, *child_next;
814
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100815 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100816 lysc_node_free_(ctx, child);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100817 }
Radek Krejci9a3823e2021-01-27 20:26:46 +0100818 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100819 FREE_ARRAY(ctx, node->musts, lysc_must_free);
820
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100821 LY_ARRAY_FOR(node->uniques, u) {
822 LY_ARRAY_FREE(node->uniques[u]);
823 }
824 LY_ARRAY_FREE(node->uniques);
825
Radek Krejci2a9fc652021-01-22 17:44:34 +0100826 LY_LIST_FOR_SAFE((struct lysc_node *)node->actions, child_next, child) {
827 lysc_node_free_(ctx, child);
828 }
829 LY_LIST_FOR_SAFE((struct lysc_node *)node->notifs, child_next, child) {
830 lysc_node_free_(ctx, child);
831 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100832}
833
Radek Krejci056d0a82018-12-06 16:57:25 +0100834static void
835lysc_node_choice_free(struct ly_ctx *ctx, struct lysc_node_choice *node)
836{
837 struct lysc_node *child, *child_next;
838
Radek Krejci9a3823e2021-01-27 20:26:46 +0100839 FREE_ARRAY(ctx, node->when, lysc_when_free);
Michal Vasko20424b42020-08-31 12:29:38 +0200840 LY_LIST_FOR_SAFE((struct lysc_node *)node->cases, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100841 lysc_node_free_(ctx, child);
Michal Vasko20424b42020-08-31 12:29:38 +0200842 }
843}
844
845static void
846lysc_node_case_free(struct ly_ctx *ctx, struct lysc_node_case *node)
847{
848 struct lysc_node *child, *child_next;
849
Radek Krejci9a3823e2021-01-27 20:26:46 +0100850 FREE_ARRAY(ctx, node->when, lysc_when_free);
Michal Vasko20424b42020-08-31 12:29:38 +0200851 LY_LIST_FOR_SAFE(node->child, child_next, child) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100852 lysc_node_free_(ctx, child);
Radek Krejci056d0a82018-12-06 16:57:25 +0100853 }
Radek Krejci9800fb82018-12-13 14:26:23 +0100854}
Radek Krejci056d0a82018-12-06 16:57:25 +0100855
Radek Krejci9800fb82018-12-13 14:26:23 +0100856static void
857lysc_node_anydata_free(struct ly_ctx *ctx, struct lysc_node_anydata *node)
858{
Radek Krejci9a3823e2021-01-27 20:26:46 +0100859 FREE_ARRAY(ctx, node->when, lysc_when_free);
Radek Krejci9800fb82018-12-13 14:26:23 +0100860 FREE_ARRAY(ctx, node->musts, lysc_must_free);
Radek Krejci056d0a82018-12-06 16:57:25 +0100861}
862
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100863static void
864lysc_node_free_(struct ly_ctx *ctx, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +0100865{
Radek Krejci2a9fc652021-01-22 17:44:34 +0100866 ly_bool inout = 0;
867
Radek Krejci19a96102018-11-15 13:38:09 +0100868 /* common part */
Michal Vaskoe180ed02021-02-05 16:31:20 +0100869 lydict_remove(ctx, node->name);
870 lydict_remove(ctx, node->dsc);
871 lydict_remove(ctx, node->ref);
Radek Krejci19a96102018-11-15 13:38:09 +0100872
873 /* nodetype-specific part */
Michal Vaskod989ba02020-08-24 10:59:24 +0200874 switch (node->nodetype) {
Radek Krejci19a96102018-11-15 13:38:09 +0100875 case LYS_CONTAINER:
Michal Vasko22df3f02020-08-24 13:29:22 +0200876 lysc_node_container_free(ctx, (struct lysc_node_container *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100877 break;
878 case LYS_LEAF:
Michal Vasko22df3f02020-08-24 13:29:22 +0200879 lysc_node_leaf_free(ctx, (struct lysc_node_leaf *)node);
Radek Krejci19a96102018-11-15 13:38:09 +0100880 break;
Radek Krejci42452ac2018-11-28 17:09:52 +0100881 case LYS_LEAFLIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200882 lysc_node_leaflist_free(ctx, (struct lysc_node_leaflist *)node);
Radek Krejci42452ac2018-11-28 17:09:52 +0100883 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100884 case LYS_LIST:
Michal Vasko22df3f02020-08-24 13:29:22 +0200885 lysc_node_list_free(ctx, (struct lysc_node_list *)node);
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100886 break;
Radek Krejci056d0a82018-12-06 16:57:25 +0100887 case LYS_CHOICE:
Michal Vasko22df3f02020-08-24 13:29:22 +0200888 lysc_node_choice_free(ctx, (struct lysc_node_choice *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +0100889 break;
890 case LYS_CASE:
Michal Vasko20424b42020-08-31 12:29:38 +0200891 lysc_node_case_free(ctx, (struct lysc_node_case *)node);
Radek Krejci056d0a82018-12-06 16:57:25 +0100892 break;
Radek Krejci9800fb82018-12-13 14:26:23 +0100893 case LYS_ANYDATA:
894 case LYS_ANYXML:
Michal Vasko22df3f02020-08-24 13:29:22 +0200895 lysc_node_anydata_free(ctx, (struct lysc_node_anydata *)node);
Radek Krejci9800fb82018-12-13 14:26:23 +0100896 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100897 case LYS_RPC:
898 case LYS_ACTION:
899 lysc_node_action_free(ctx, (struct lysc_node_action *)node);
900 break;
901 case LYS_INPUT:
902 case LYS_OUTPUT:
903 lysc_node_action_inout_free(ctx, (struct lysc_node_action_inout *)node);
904 inout = 1;
905 break;
906 case LYS_NOTIF:
907 lysc_node_notif_free(ctx, (struct lysc_node_notif *)node);
908 break;
Radek Krejci19a96102018-11-15 13:38:09 +0100909 default:
910 LOGINT(ctx);
911 }
912
Radek Krejci056d0a82018-12-06 16:57:25 +0100913 FREE_ARRAY(ctx, node->exts, lysc_ext_instance_free);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100914
915 if (!inout) {
916 free(node);
917 }
Radek Krejci19a96102018-11-15 13:38:09 +0100918}
919
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100920void
921lysc_node_free(struct ly_ctx *ctx, struct lysc_node *node, ly_bool unlink)
922{
923 struct lysc_node *iter, **child_p;
924
Radek Krejci2a9fc652021-01-22 17:44:34 +0100925 if (node->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
926 /* nothing to do - inouts are part of actions and cannot be unlinked/freed separately */
927 return;
928 }
929
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100930 if (unlink) {
931 /* unlink from siblings */
932 if (node->prev->next) {
933 node->prev->next = node->next;
934 }
935 if (node->next) {
936 node->next->prev = node->prev;
937 } else {
938 /* unlinking the last node */
939 if (node->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100940 if (node->nodetype == LYS_ACTION) {
941 iter = (struct lysc_node *)lysc_node_actions(node->parent);
942 } else if (node->nodetype == LYS_NOTIF) {
943 iter = (struct lysc_node *)lysc_node_notifs(node->parent);
944 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +0100945 iter = (struct lysc_node *)lysc_node_child(node->parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100946 }
Michal Vasko539c4a62020-11-03 17:21:34 +0100947 LY_CHECK_ERR_RET(!iter, LOGINT(ctx), );
Radek Krejci2a9fc652021-01-22 17:44:34 +0100948 } else if (node->nodetype == LYS_RPC) {
949 iter = (struct lysc_node *)node->module->compiled->rpcs;
950 } else if (node->nodetype == LYS_NOTIF) {
951 iter = (struct lysc_node *)node->module->compiled->notifs;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100952 } else {
953 iter = node->module->compiled->data;
954 }
955 /* update the "last" pointer from the first node */
956 iter->prev = node->prev;
957 }
958
959 /* unlink from parent */
960 if (node->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +0100961 if (node->nodetype == LYS_ACTION) {
962 child_p = (struct lysc_node **)lysc_node_actions_p(node->parent);
963 } else if (node->nodetype == LYS_NOTIF) {
964 child_p = (struct lysc_node **)lysc_node_notifs_p(node->parent);
965 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +0100966 child_p = lysc_node_child_p(node->parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100967 }
968 } else if (node->nodetype == LYS_RPC) {
969 child_p = (struct lysc_node **)&node->module->compiled->rpcs;
970 } else if (node->nodetype == LYS_NOTIF) {
971 child_p = (struct lysc_node **)&node->module->compiled->notifs;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100972 } else {
973 child_p = &node->module->compiled->data;
974 }
975 if (child_p && (*child_p == node)) {
976 /* the node is the first child */
977 *child_p = node->next;
978 }
979 }
980
981 lysc_node_free_(ctx, node);
982}
983
Radek Krejci90ed21e2021-04-12 14:47:46 +0200984void
985lysc_module_free(struct lysc_module *module)
Radek Krejci19a96102018-11-15 13:38:09 +0100986{
987 struct ly_ctx *ctx;
988 struct lysc_node *node, *node_next;
989
Radek Krejci90ed21e2021-04-12 14:47:46 +0200990 if (!module) {
991 return;
992 }
993
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100994 ctx = module->mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +0100995
Radek Krejci19a96102018-11-15 13:38:09 +0100996 LY_LIST_FOR_SAFE(module->data, node_next, node) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100997 lysc_node_free_(ctx, node);
Radek Krejci19a96102018-11-15 13:38:09 +0100998 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100999 LY_LIST_FOR_SAFE((struct lysc_node *)module->rpcs, node_next, node) {
1000 lysc_node_free_(ctx, node);
1001 }
1002 LY_LIST_FOR_SAFE((struct lysc_node *)module->notifs, node_next, node) {
1003 lysc_node_free_(ctx, node);
1004 }
Radek Krejci19a96102018-11-15 13:38:09 +01001005 FREE_ARRAY(ctx, module->exts, lysc_ext_instance_free);
1006
1007 free(module);
1008}
1009
1010void
Radek Krejci90ed21e2021-04-12 14:47:46 +02001011lys_module_free(struct lys_module *module)
Radek Krejci19a96102018-11-15 13:38:09 +01001012{
1013 if (!module) {
1014 return;
1015 }
1016
Radek Krejci90ed21e2021-04-12 14:47:46 +02001017 lysc_module_free(module->compiled);
Radek Krejci80d281e2020-09-14 17:42:54 +02001018 FREE_ARRAY(module->ctx, module->identities, lysc_ident_free);
Radek Krejci19a96102018-11-15 13:38:09 +01001019 lysp_module_free(module->parsed);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001020
Michal Vasko7f45cf22020-10-01 12:49:44 +02001021 LY_ARRAY_FREE(module->augmented_by);
1022 LY_ARRAY_FREE(module->deviated_by);
1023
Michal Vaskoe180ed02021-02-05 16:31:20 +01001024 lydict_remove(module->ctx, module->name);
1025 lydict_remove(module->ctx, module->revision);
1026 lydict_remove(module->ctx, module->ns);
1027 lydict_remove(module->ctx, module->prefix);
1028 lydict_remove(module->ctx, module->filepath);
1029 lydict_remove(module->ctx, module->org);
1030 lydict_remove(module->ctx, module->contact);
1031 lydict_remove(module->ctx, module->dsc);
1032 lydict_remove(module->ctx, module->ref);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001033
Radek Krejci19a96102018-11-15 13:38:09 +01001034 free(module);
1035}
Michal Vasko33ff9422020-07-03 09:50:39 +02001036
Radek Krejci38d85362019-09-05 16:26:38 +02001037API void
Radek Krejci0b013302021-03-29 15:22:32 +02001038lyplg_ext_instance_substatements_free(struct ly_ctx *ctx, struct lysc_ext_substmt *substmts)
Radek Krejci38d85362019-09-05 16:26:38 +02001039{
Radek Krejci1b2eef82021-02-17 11:17:27 +01001040 LY_ARRAY_COUNT_TYPE u;
1041
1042 LY_ARRAY_FOR(substmts, u) {
Radek Krejci38d85362019-09-05 16:26:38 +02001043 if (!substmts[u].storage) {
1044 continue;
1045 }
1046
Michal Vaskod989ba02020-08-24 10:59:24 +02001047 switch (substmts[u].stmt) {
Radek Krejci6b88a462021-02-17 12:39:34 +01001048 case LY_STMT_ACTION:
1049 case LY_STMT_ANYDATA:
1050 case LY_STMT_ANYXML:
1051 case LY_STMT_CONTAINER:
1052 case LY_STMT_CHOICE:
1053 case LY_STMT_LEAF:
1054 case LY_STMT_LEAF_LIST:
1055 case LY_STMT_LIST:
1056 case LY_STMT_NOTIFICATION:
1057 case LY_STMT_RPC:
1058 case LY_STMT_USES: {
1059 struct lysc_node *child, *child_next;
1060
1061 LY_LIST_FOR_SAFE(*((struct lysc_node **)substmts[u].storage), child_next, child) {
1062 lysc_node_free_(ctx, child);
Radek Krejci38d85362019-09-05 16:26:38 +02001063 }
1064 break;
Radek Krejci6b88a462021-02-17 12:39:34 +01001065 }
1066 case LY_STMT_CONFIG:
1067 case LY_STMT_STATUS:
1068 /* nothing to do */
1069 break;
Radek Krejci1b2eef82021-02-17 11:17:27 +01001070 case LY_STMT_DESCRIPTION:
1071 case LY_STMT_REFERENCE:
Radek Krejci38d85362019-09-05 16:26:38 +02001072 case LY_STMT_UNITS:
1073 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1074 /* single item */
Michal Vasko22df3f02020-08-24 13:29:22 +02001075 const char *str = *((const char **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001076 if (!str) {
1077 break;
1078 }
Michal Vaskoe180ed02021-02-05 16:31:20 +01001079 lydict_remove(ctx, str);
Radek Krejci38d85362019-09-05 16:26:38 +02001080 } else {
1081 /* multiple items */
Michal Vasko22df3f02020-08-24 13:29:22 +02001082 const char **strs = *((const char ***)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001083 if (!strs) {
1084 break;
1085 }
1086 FREE_STRINGS(ctx, strs);
1087 }
1088 break;
Radek Krejci38d85362019-09-05 16:26:38 +02001089 case LY_STMT_IF_FEATURE: {
Michal Vasko22df3f02020-08-24 13:29:22 +02001090 struct lysc_iffeature *iff = *((struct lysc_iffeature **)substmts[u].storage);
Radek Krejci38d85362019-09-05 16:26:38 +02001091 if (!iff) {
1092 break;
1093 }
1094 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1095 /* single item */
1096 lysc_iffeature_free(ctx, iff);
1097 free(iff);
1098 } else {
1099 /* multiple items */
1100 FREE_ARRAY(ctx, iff, lysc_iffeature_free);
1101 }
1102 break;
Radek Krejci6b88a462021-02-17 12:39:34 +01001103 case LY_STMT_TYPE:
1104 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
1105 /* single item */
1106 struct lysc_type *type = *((struct lysc_type **)substmts[u].storage);
1107 if (!type) {
1108 break;
1109 }
1110 lysc_type_free(ctx, type);
1111 } else {
1112 /* multiple items */
1113 struct lysc_type **types = *((struct lysc_type ***)substmts[u].storage);
1114 if (!types) {
1115 break;
1116 }
1117 FREE_ARRAY(ctx, types, lysc_type2_free);
1118 }
1119 break;
Radek Krejci38d85362019-09-05 16:26:38 +02001120 }
1121
Radek Krejci0f969882020-08-21 16:56:47 +02001122 /* TODO other statements */
Radek Krejci38d85362019-09-05 16:26:38 +02001123 default:
1124 LOGINT(ctx);
1125 }
1126 }
Radek Krejci1b2eef82021-02-17 11:17:27 +01001127
1128 LY_ARRAY_FREE(substmts);
Radek Krejci38d85362019-09-05 16:26:38 +02001129}
David Sedlákebd3acf2019-07-26 15:04:32 +02001130
1131void
Michal Vaskob36053d2020-03-26 15:49:30 +01001132yang_parser_ctx_free(struct lys_yang_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +02001133{
1134 if (ctx) {
1135 free(ctx);
1136 }
1137}
1138
1139void
Michal Vaskob36053d2020-03-26 15:49:30 +01001140yin_parser_ctx_free(struct lys_yin_parser_ctx *ctx)
David Sedlákebd3acf2019-07-26 15:04:32 +02001141{
1142 if (ctx) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001143 lyxml_ctx_free(ctx->xmlctx);
David Sedlákebd3acf2019-07-26 15:04:32 +02001144 free(ctx);
1145 }
1146}