libamxo  4.3.4
Object Definition Language (ODL) parsing
amxo_parser_dm_func_utils.c
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** SPDX-License-Identifier: BSD-2-Clause-Patent
4 **
5 ** SPDX-FileCopyrightText: Copyright (c) 2023 SoftAtHome
6 **
7 ** Redistribution and use in source and binary forms, with or without modification,
8 ** are permitted provided that the following conditions are met:
9 **
10 ** 1. Redistributions of source code must retain the above copyright notice,
11 ** this list of conditions and the following disclaimer.
12 **
13 ** 2. Redistributions in binary form must reproduce the above copyright notice,
14 ** this list of conditions and the following disclaimer in the documentation
15 ** and/or other materials provided with the distribution.
16 **
17 ** Subject to the terms and conditions of this license, each copyright holder
18 ** and contributor hereby grants to those receiving rights under this license
19 ** a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable
20 ** (except for failure to satisfy the conditions of this license) patent license
21 ** to make, have made, use, offer to sell, sell, import, and otherwise transfer
22 ** this software, where such license applies only to those patent claims, already
23 ** acquired or hereafter acquired, licensable by such copyright holder or contributor
24 ** that are necessarily infringed by:
25 **
26 ** (a) their Contribution(s) (the licensed copyrights of copyright holders and
27 ** non-copyrightable additions of contributors, in source or binary form) alone;
28 ** or
29 **
30 ** (b) combination of their Contribution(s) with the work of authorship to which
31 ** such Contribution(s) was added by such copyright holder or contributor, if,
32 ** at the time the Contribution is added, such addition causes such combination
33 ** to be necessarily infringed. The patent license shall not apply to any other
34 ** combinations which include the Contribution.
35 **
36 ** Except as expressly stated above, no rights or licenses from any copyright
37 ** holder or contributor is granted under this license, whether expressly, by
38 ** implication, estoppel or otherwise.
39 **
40 ** DISCLAIMER
41 **
42 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
46 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
51 ** USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 **
53 ****************************************************************************/
54 #ifndef _GNU_SOURCE
55 #define _GNU_SOURCE
56 #endif
57 
58 #include "amxo_parser_priv.h"
59 #include "amxo_parser.tab.h"
60 #include "amxo_parser_hooks_priv.h"
61 
62 static int64_t amxo_attr_2_func_attr(int64_t attributes) {
63  int64_t func_attrs = 0;
64  if(SET_BIT(attr_private) & attributes) {
65  func_attrs |= SET_BIT(amxd_fattr_private);
66  }
67  if(SET_BIT(attr_protected) & attributes) {
68  func_attrs |= SET_BIT(amxd_fattr_protected);
69  }
70  if(SET_BIT(attr_template) & attributes) {
71  func_attrs |= SET_BIT(amxd_fattr_template);
72  }
73  if(SET_BIT(attr_instance) & attributes) {
74  func_attrs |= SET_BIT(amxd_fattr_instance);
75  }
76  if(SET_BIT(attr_asynchronous) & attributes) {
77  func_attrs |= SET_BIT(amxd_fattr_async);
78  }
79  return func_attrs;
80 }
81 
82 static int64_t amxo_attr_2_arg_attr(int64_t attributes) {
83  int64_t arg_attrs = 0;
84  if(SET_BIT(attr_in) & attributes) {
85  arg_attrs |= SET_BIT(amxd_aattr_in);
86  }
87  if(SET_BIT(attr_out) & attributes) {
88  arg_attrs |= SET_BIT(amxd_aattr_out);
89  }
90  if(SET_BIT(attr_mandatory) & attributes) {
91  arg_attrs |= SET_BIT(amxd_aattr_mandatory);
92  }
93  if(SET_BIT(attr_strict) & attributes) {
94  arg_attrs |= SET_BIT(amxd_aattr_strict);
95  }
96  return arg_attrs;
97 }
98 
100  const char* name,
101  int64_t attr_bitmask,
102  uint32_t type) {
103  amxd_function_t* func = NULL;
104  amxd_function_t* orig_func = NULL;
105  int64_t fattrs = amxo_attr_2_func_attr(attr_bitmask);
106  int retval = -1;
107  amxc_string_t res_name;
108  amxc_string_init(&res_name, 0);
109 
110  if(amxc_string_set_resolved(&res_name, name, &pctx->config) > 0) {
111  name = amxc_string_get(&res_name, 0);
112  }
113 
114  orig_func = amxd_object_get_function(pctx->object, name);
115 
116  pctx->status = amxd_function_new(&func, name, type, NULL);
117  if(pctx->status != amxd_status_ok) {
118  amxo_parser_msg(pctx, "Failed to create function %s", name);
119  goto exit;
120  }
121 
122  if(amxd_object_get_type(pctx->object) == amxd_object_instance) {
123  fattrs |= SET_BIT(amxd_fattr_instance);
124  }
125 
126  amxd_function_set_attrs(func, fattrs, true);
127 
128  if(orig_func != NULL) {
129  if(amxd_function_get_owner(orig_func) == pctx->object) {
130  amxd_function_delete(&orig_func);
131  }
132  amxo_parser_msg(pctx, "Overriding function %s", name);
133  pctx->status = amxd_object_add_function(pctx->object, func);
134  retval = 1;
135  } else {
136  pctx->status = amxd_object_add_function(pctx->object, func);
137  retval = 0;
138  }
139 
140  amxo_hooks_add_func(pctx, name, fattrs, type);
141 
142  pctx->func = func;
143 
144 exit:
145  amxc_string_clean(&res_name);
146  return retval;
147 }
148 
149 bool amxo_parser_set_function_flags(amxo_parser_t* pctx, amxc_var_t* flags) {
150  const amxc_htable_t* ht_flags = NULL;
151 
152  when_null(flags, exit);
153  when_true(amxc_var_type_of(flags) != AMXC_VAR_ID_HTABLE, exit);
154 
155  ht_flags = amxc_var_constcast(amxc_htable_t, flags);
156  amxc_htable_for_each(it, ht_flags) {
157  const char* flag_name = amxc_htable_it_get_key(it);
158  amxc_var_t* flag = amxc_var_from_htable_it(it);
159  if(amxc_var_dyncast(bool, flag)) {
160  amxd_function_set_flag(pctx->func, flag_name);
161  } else {
162  amxd_function_unset_flag(pctx->func, flag_name);
163  }
164  }
165 
166  amxc_var_delete(&flags);
167 
168 exit:
169  return true;
170 }
171 
173  amxd_object_fn_t fn = NULL;
174  amxo_hooks_end_func(pctx);
175  fn = (amxd_object_fn_t) pctx->resolved_fn;
176  amxd_function_set_impl(pctx->func, fn);
177  amxc_string_delete(&pctx->resolved_fn_name);
178  pctx->func = NULL;
179  pctx->resolved_fn = NULL;
180 }
181 
183  const char* name,
184  int64_t attr_bitmask,
185  uint32_t type,
186  amxc_var_t* def_value) {
187  bool retval = false;
188  int64_t aattrs = amxo_attr_2_arg_attr(attr_bitmask);
189  if(!IS_BIT_SET(aattrs, amxd_aattr_in) &&
190  !IS_BIT_SET(aattrs, amxd_aattr_out)) {
191  aattrs |= 1 << amxd_aattr_in;
192  }
193 
194  amxo_hooks_add_func_arg(pctx, name, aattrs, type, def_value);
195 
196  pctx->status = amxd_function_new_arg(pctx->func, name, type, def_value);
197  if(pctx->status != amxd_status_ok) {
198  amxo_parser_msg(pctx, "Failed to create/add function argument %s", name);
199  goto exit;
200  }
201 
202  amxd_function_arg_set_attrs(pctx->func, name, aattrs, true);
203  retval = true;
204 
205 exit:
206  return retval;
207 }
int amxo_parser_push_func(amxo_parser_t *pctx, const char *name, int64_t attr_bitmask, uint32_t type)
bool amxo_parser_set_function_flags(amxo_parser_t *pctx, amxc_var_t *flags)
static int64_t amxo_attr_2_arg_attr(int64_t attributes)
static int64_t amxo_attr_2_func_attr(int64_t attributes)
void amxo_parser_pop_func(amxo_parser_t *pctx)
bool amxo_parser_add_arg(amxo_parser_t *pctx, const char *name, int64_t attr_bitmask, uint32_t type, amxc_var_t *def_value)
PRIVATE void amxo_hooks_add_func(amxo_parser_t *parser, const char *name, int64_t attr_bitmask, uint32_t type)
PRIVATE void amxo_hooks_add_func_arg(amxo_parser_t *parser, const char *name, int64_t attr_bitmask, uint32_t type, amxc_var_t *def_value)
PRIVATE void amxo_hooks_end_func(amxo_parser_t *parser)
@ attr_instance
@ attr_protected
@ attr_asynchronous
@ attr_template
@ attr_mandatory
@ attr_private
@ attr_strict
@ attr_in
@ attr_out
PRIVATE void amxo_parser_msg(amxo_parser_t *parser, const char *format,...) __attribute__((format(printf
The ODL parser structure.
Definition: amxo_types.h:245
amxo_fn_ptr_t resolved_fn
Definition: amxo_types.h:266
amxc_var_t config
Definition: amxo_types.h:250
amxc_string_t * resolved_fn_name
Definition: amxo_types.h:267
amxd_status_t status
Definition: amxo_types.h:258
amxd_object_t * object
Definition: amxo_types.h:262
amxd_function_t * func
Definition: amxo_types.h:264