Zigbee Protocol Controller 1.6.0
attribute_mapper_built_in_functions.hpp
Go to the documentation of this file.
1/******************************************************************************
2 * # License
3 * <b>Copyright 2022 Silicon Laboratories Inc. www.silabs.com</b>
4 ******************************************************************************
5 * The licensor of this software is Silicon Laboratories Inc. Your use of this
6 * software is governed by the terms of Silicon Labs Master Software License
7 * Agreement (MSLA) available at
8 * www.silabs.com/about-us/legal/master-software-license-agreement. This
9 * software is distributed to you in Source Code format and is governed by the
10 * sections of the MSLA applicable to Source Code.
11 *
12 *****************************************************************************/
13
14#ifndef ATTRIBUTE_MAPPER_BUILT_IN_FUNCTIONS_HPP
15#define ATTRIBUTE_MAPPER_BUILT_IN_FUNCTIONS_HPP
16
18
19#include <boost/optional.hpp>
20#include <map>
21#include <string>
22#include <vector>
23#include <functional>
24
25#include "sl_log.h"
26
27namespace ast
28{
29// Define the result type. Here we keep the boost::optional<T> from attribute_mapper_ast_eval.hpp
30template<typename T> using eval_result_type = boost::optional<T>;
31template<typename T> using built_in_function
32 = std::function<eval_result_type<T>(std::vector<eval_result_type<T>> &)>;
33template<typename T> using mapper_function_map
34 = std::map<std::string, built_in_function<T>>;
35
41template<typename T> const mapper_function_map<T> &get_built_in_functions();
42
47{
48 public:
49 bool operator()(const nil) const
50 {
51 return false;
52 }
53
54 bool operator()(unsigned int) const
55 {
56 return false;
57 }
58
59 bool operator()(float) const
60 {
61 return false;
62 }
63
64 bool operator()(const attribute &a) const
65 {
66 bool result = false;
67 for (auto path_element: a.attribute_path) {
68 result |= boost::apply_visitor(*this, path_element);
69 }
70 return result;
71 }
72
73 bool operator()(const attribute_path_subscript &subscript) const
74 {
75 bool result = false;
76 result |= boost::apply_visitor(*this, subscript.identifier);
77 result |= boost::apply_visitor(*this, subscript.index);
78 return result;
79 }
80
81 bool operator()(const operand &operand) const
82 {
83 return boost::apply_visitor(*this, operand);
84 }
85
86 bool operator()(const signed_ &x) const
87 {
88 return boost::apply_visitor(*this, x.operand_);
89 }
90
91 bool operator()(const condition &x) const
92 {
93 bool result = false;
94 result |= boost::apply_visitor(*this, x.cond_value);
95 result |= boost::apply_visitor(*this, x.cond_true);
96 result |= boost::apply_visitor(*this, x.cond_false);
97 return result;
98 }
99
100 bool operator()(const operation &x) const
101 {
102 return boost::apply_visitor(*this, x.operand_);
103 }
104
105 bool operator()(const expression &x) const
106 {
107 bool result = boost::apply_visitor(*this, x.first);
108 for (const auto &terms: x.rest) {
109 result |= (*this)(terms);
110 }
111 return result;
112 }
113
114 bool operator()(const assignment &a) const
115 {
116 // We assume no functions on the left hand side of assignments.
117 bool result = (*this)(a.rhs);
118 return result;
119 }
120
121 bool operator()(const function_invokation &f) const
122 {
123 const auto &functions = get_built_in_functions<float>();
124 if (functions.count(f.function_name) == 0) {
125 sl_log_error("attribute_mapper_built_in_function_name_check",
126 "Unknown function (%s) found in AST tree",
127 f.function_name.c_str());
128 return true;
129 }
130
131 // Check for the function arguments, they may be functions themselves.
132 for (auto &argument: f.arguments) {
133 if (true == (*this)(argument)) {
134 return true;
135 }
136 }
137
138 return false;
139 }
140
141 bool operator()(const scope &x) const
142 {
143 bool result = false;
144 for (auto &a: x.assignments) {
145 result |= (*this)(a);
146 }
147 return result;
148 }
149
150 bool operator()(const ast_tree &ast) const
151 {
152 bool result = false;
153 for (const auto &ast_element: ast) {
154 result |= boost::apply_visitor(*this, ast_element);
155 }
156 return result;
157 }
158};
159
160} // namespace ast
161
162#ifdef __cplusplus
163extern "C" {
164#endif
165
166#ifdef __cplusplus
167}
168#endif
169
170#endif //ATTRIBUTE_MAPPER_BUILT_IN_FUNCTIONS_HPP
Class that evaluates if we have unknown functions in the AST.
Definition: attribute_mapper_built_in_functions.hpp:47
bool operator()(const signed_ &x) const
Definition: attribute_mapper_built_in_functions.hpp:86
bool operator()(const operation &x) const
Definition: attribute_mapper_built_in_functions.hpp:100
bool operator()(const function_invokation &f) const
Definition: attribute_mapper_built_in_functions.hpp:121
bool operator()(const scope &x) const
Definition: attribute_mapper_built_in_functions.hpp:141
bool operator()(const operand &operand) const
Definition: attribute_mapper_built_in_functions.hpp:81
bool operator()(const nil) const
Definition: attribute_mapper_built_in_functions.hpp:49
bool operator()(unsigned int) const
Definition: attribute_mapper_built_in_functions.hpp:54
bool operator()(const assignment &a) const
Definition: attribute_mapper_built_in_functions.hpp:114
bool operator()(const expression &x) const
Definition: attribute_mapper_built_in_functions.hpp:105
bool operator()(const attribute_path_subscript &subscript) const
Definition: attribute_mapper_built_in_functions.hpp:73
bool operator()(const ast_tree &ast) const
Definition: attribute_mapper_built_in_functions.hpp:150
bool operator()(const condition &x) const
Definition: attribute_mapper_built_in_functions.hpp:91
bool operator()(const attribute &a) const
Definition: attribute_mapper_built_in_functions.hpp:64
bool operator()(float) const
Definition: attribute_mapper_built_in_functions.hpp:59
#define sl_log_error(tag, fmtstr,...)
Definition: sl_log.h:145
Definition: attribute_mapper_ast.hpp:39
const mapper_function_map< T > & get_built_in_functions()
Gets the map of built-in functions, containing names and pointers.
Definition: attribute_mapper_built_in_functions.cpp:254
std::function< eval_result_type< T >(std::vector< eval_result_type< T > > &)> built_in_function
Definition: attribute_mapper_built_in_functions.hpp:32
x3::variant< ast::nil, uint32_t, float, x3::forward_ast< attribute >, x3::forward_ast< signed_ >, x3::forward_ast< expression >, x3::forward_ast< condition >, x3::forward_ast< function_invokation > > operand
Operands.
Definition: attribute_mapper_ast.hpp:95
boost::optional< T > eval_result_type
Definition: attribute_mapper_built_in_functions.hpp:30
std::map< std::string, built_in_function< T > > mapper_function_map
Definition: attribute_mapper_built_in_functions.hpp:34
Assignment.
Definition: attribute_mapper_ast.hpp:193
expression rhs
right hand side of the assignment
Definition: attribute_mapper_ast.hpp:200
Definition: attribute_mapper_ast.hpp:53
Attribute path subscript.
Definition: attribute_mapper_ast.hpp:160
operand identifier
Definition: attribute_mapper_ast.hpp:161
operand index
Definition: attribute_mapper_ast.hpp:162
Attribute.
Definition: attribute_mapper_ast.hpp:177
std::vector< attribute_path_element > attribute_path
Definition: attribute_mapper_ast.hpp:179
Condition.
Definition: attribute_mapper_ast.hpp:147
operand cond_true
Truth value.
Definition: attribute_mapper_ast.hpp:149
operand cond_false
False value.
Definition: attribute_mapper_ast.hpp:150
operand cond_value
Selector value.
Definition: attribute_mapper_ast.hpp:148
Expression.
Definition: attribute_mapper_ast.hpp:134
operand first
Definition: attribute_mapper_ast.hpp:135
std::list< operation > rest
Definition: attribute_mapper_ast.hpp:136
Built-in function invokation Example : fn_min_value(r'3, 5, d'5 or 20)
Definition: attribute_mapper_ast.hpp:209
std::vector< expression > arguments
Definition: attribute_mapper_ast.hpp:211
std::string function_name
Definition: attribute_mapper_ast.hpp:210
Definition: attribute_mapper_ast.hpp:42
Operation An operation consists of an operator and an right hand side operand. When operations are ev...
Definition: attribute_mapper_ast.hpp:123
operand operand_
right hand side operand
Definition: attribute_mapper_ast.hpp:125
Scope.
Definition: attribute_mapper_ast.hpp:234
std::vector< assignment > assignments
Definition: attribute_mapper_ast.hpp:237
Uniary signed operand.
Definition: attribute_mapper_ast.hpp:111
operand operand_
Definition: attribute_mapper_ast.hpp:113