@@ -96,16 +96,16 @@ bool ApplyRule::AddTargetedRule(const ApplyRule::Ptr& rule, const String& target
96
96
*
97
97
* @returns Whether the given assign filter is like above.
98
98
*/
99
- bool ApplyRule::GetTargetHosts (Expression* assignFilter, std::vector<const String *>& hosts)
99
+ bool ApplyRule::GetTargetHosts (Expression* assignFilter, std::vector<const String *>& hosts, const Dictionary:: Ptr & constants )
100
100
{
101
101
auto lor (dynamic_cast <LogicalOrExpression*>(assignFilter));
102
102
103
103
if (lor) {
104
- return GetTargetHosts (lor->GetOperand1 ().get (), hosts)
105
- && GetTargetHosts (lor->GetOperand2 ().get (), hosts);
104
+ return GetTargetHosts (lor->GetOperand1 ().get (), hosts, constants )
105
+ && GetTargetHosts (lor->GetOperand2 ().get (), hosts, constants );
106
106
}
107
107
108
- auto name (GetComparedName (assignFilter, " host" ));
108
+ auto name (GetComparedName (assignFilter, " host" , constants ));
109
109
110
110
if (name) {
111
111
hosts.emplace_back (name);
@@ -124,16 +124,16 @@ bool ApplyRule::GetTargetHosts(Expression* assignFilter, std::vector<const Strin
124
124
*
125
125
* @returns Whether the given assign filter is like above.
126
126
*/
127
- bool ApplyRule::GetTargetServices (Expression* assignFilter, std::vector<std::pair<const String *, const String *>>& services)
127
+ bool ApplyRule::GetTargetServices (Expression* assignFilter, std::vector<std::pair<const String *, const String *>>& services, const Dictionary:: Ptr & constants )
128
128
{
129
129
auto lor (dynamic_cast <LogicalOrExpression*>(assignFilter));
130
130
131
131
if (lor) {
132
- return GetTargetServices (lor->GetOperand1 ().get (), services)
133
- && GetTargetServices (lor->GetOperand2 ().get (), services);
132
+ return GetTargetServices (lor->GetOperand1 ().get (), services, constants )
133
+ && GetTargetServices (lor->GetOperand2 ().get (), services, constants );
134
134
}
135
135
136
- auto service (GetTargetService (assignFilter));
136
+ auto service (GetTargetService (assignFilter, constants ));
137
137
138
138
if (service.first ) {
139
139
services.emplace_back (service);
@@ -152,7 +152,7 @@ bool ApplyRule::GetTargetServices(Expression* assignFilter, std::vector<std::pai
152
152
*
153
153
* @returns {host, service} on success and {nullptr, nullptr} on failure.
154
154
*/
155
- std::pair<const String *, const String *> ApplyRule::GetTargetService (Expression* assignFilter)
155
+ std::pair<const String *, const String *> ApplyRule::GetTargetService (Expression* assignFilter, const Dictionary:: Ptr & constants )
156
156
{
157
157
auto land (dynamic_cast <LogicalAndExpression*>(assignFilter));
158
158
@@ -162,15 +162,15 @@ std::pair<const String *, const String *> ApplyRule::GetTargetService(Expression
162
162
163
163
auto op1 (land->GetOperand1 ().get ());
164
164
auto op2 (land->GetOperand2 ().get ());
165
- auto host (GetComparedName (op1, " host" ));
165
+ auto host (GetComparedName (op1, " host" , constants ));
166
166
167
167
if (!host) {
168
168
std::swap (op1, op2);
169
- host = GetComparedName (op1, " host" );
169
+ host = GetComparedName (op1, " host" , constants );
170
170
}
171
171
172
172
if (host) {
173
- auto service (GetComparedName (op2, " service" ));
173
+ auto service (GetComparedName (op2, " service" , constants ));
174
174
175
175
if (service) {
176
176
return {host, service};
@@ -189,7 +189,7 @@ std::pair<const String *, const String *> ApplyRule::GetTargetService(Expression
189
189
*
190
190
* @returns The object name on success and nullptr on failure.
191
191
*/
192
- const String * ApplyRule::GetComparedName (Expression* assignFilter, const char * lcType)
192
+ const String * ApplyRule::GetComparedName (Expression* assignFilter, const char * lcType, const Dictionary:: Ptr & constants )
193
193
{
194
194
auto eq (dynamic_cast <EqualExpression*>(assignFilter));
195
195
@@ -200,12 +200,12 @@ const String * ApplyRule::GetComparedName(Expression* assignFilter, const char *
200
200
auto op1 (eq->GetOperand1 ().get ());
201
201
auto op2 (eq->GetOperand2 ().get ());
202
202
203
- if (IsNameIndexer (op1, lcType)) {
204
- return GetLiteralStringValue (op2);
203
+ if (IsNameIndexer (op1, lcType, constants )) {
204
+ return GetConstString (op2, constants );
205
205
}
206
206
207
- if (IsNameIndexer (op2, lcType)) {
208
- return GetLiteralStringValue (op1);
207
+ if (IsNameIndexer (op2, lcType, constants )) {
208
+ return GetConstString (op1, constants );
209
209
}
210
210
211
211
return nullptr ;
@@ -214,7 +214,7 @@ const String * ApplyRule::GetComparedName(Expression* assignFilter, const char *
214
214
/* *
215
215
* @returns Whether the given expression is like $lcType$.name.
216
216
*/
217
- bool ApplyRule::IsNameIndexer (Expression* exp, const char * lcType)
217
+ bool ApplyRule::IsNameIndexer (Expression* exp, const char * lcType, const Dictionary:: Ptr & constants )
218
218
{
219
219
auto ixr (dynamic_cast <IndexerExpression*>(exp ));
220
220
@@ -228,27 +228,39 @@ bool ApplyRule::IsNameIndexer(Expression* exp, const char * lcType)
228
228
return false ;
229
229
}
230
230
231
- auto val (GetLiteralStringValue (ixr->GetOperand2 ().get ()));
231
+ auto val (GetConstString (ixr->GetOperand2 ().get (), constants ));
232
232
233
233
return val && *val == " name" ;
234
234
}
235
235
236
236
/* *
237
- * @returns If the given expression is a string literal, the string . nullptr on failure.
237
+ * @returns If the given expression is a constant string, its address . nullptr on failure.
238
238
*/
239
- const String * ApplyRule::GetLiteralStringValue (Expression* exp)
239
+ const String * ApplyRule::GetConstString (Expression* exp, const Dictionary::Ptr & constants)
240
+ {
241
+ auto cnst (GetConst (exp , constants));
242
+
243
+ return cnst && cnst->IsString () ? &cnst->Get <String>() : nullptr ;
244
+ }
245
+
246
+ /* *
247
+ * @returns If the given expression is a constant, its address. nullptr on failure.
248
+ */
249
+ const Value * ApplyRule::GetConst (Expression* exp, const Dictionary::Ptr & constants)
240
250
{
241
251
auto lit (dynamic_cast <LiteralExpression*>(exp ));
242
252
243
- if (! lit) {
244
- return nullptr ;
253
+ if (lit) {
254
+ return &lit-> GetValue () ;
245
255
}
246
256
247
- auto & val (lit->GetValue ());
257
+ if (constants) {
258
+ auto var (dynamic_cast <VariableExpression*>(exp ));
248
259
249
- if (!val.IsString ()) {
250
- return nullptr ;
260
+ if (var) {
261
+ return constants->GetRef (var->GetVariable ());
262
+ }
251
263
}
252
264
253
- return &val. Get <String>() ;
265
+ return nullptr ;
254
266
}
0 commit comments