@@ -5208,24 +5208,29 @@ void GDScriptAnalyzer::reduce_unary_op(GDScriptParser::UnaryOpNode *p_unary_op)
5208
5208
}
5209
5209
5210
5210
Variant GDScriptAnalyzer::make_expression_reduced_value (GDScriptParser::ExpressionNode *p_expression, bool &is_reduced) {
5211
- Variant value;
5212
-
5213
5211
if (p_expression == nullptr ) {
5214
- return value ;
5212
+ return Variant () ;
5215
5213
}
5216
5214
5217
5215
if (p_expression->is_constant ) {
5218
5216
is_reduced = true ;
5219
- value = p_expression->reduced_value ;
5220
- } else if (p_expression->type == GDScriptParser::Node::ARRAY) {
5221
- value = make_array_reduced_value (static_cast <GDScriptParser::ArrayNode *>(p_expression), is_reduced);
5222
- } else if (p_expression->type == GDScriptParser::Node::DICTIONARY) {
5223
- value = make_dictionary_reduced_value (static_cast <GDScriptParser::DictionaryNode *>(p_expression), is_reduced);
5224
- } else if (p_expression->type == GDScriptParser::Node::SUBSCRIPT) {
5225
- value = make_subscript_reduced_value (static_cast <GDScriptParser::SubscriptNode *>(p_expression), is_reduced);
5217
+ return p_expression->reduced_value ;
5226
5218
}
5227
5219
5228
- return value;
5220
+ switch (p_expression->type ) {
5221
+ case GDScriptParser::Node::ARRAY:
5222
+ return make_array_reduced_value (static_cast <GDScriptParser::ArrayNode *>(p_expression), is_reduced);
5223
+ case GDScriptParser::Node::DICTIONARY:
5224
+ return make_dictionary_reduced_value (static_cast <GDScriptParser::DictionaryNode *>(p_expression), is_reduced);
5225
+ case GDScriptParser::Node::SUBSCRIPT:
5226
+ return make_subscript_reduced_value (static_cast <GDScriptParser::SubscriptNode *>(p_expression), is_reduced);
5227
+ case GDScriptParser::Node::CALL:
5228
+ return make_call_reduced_value (static_cast <GDScriptParser::CallNode *>(p_expression), is_reduced);
5229
+ default :
5230
+ break ;
5231
+ }
5232
+
5233
+ return Variant ();
5229
5234
}
5230
5235
5231
5236
Variant GDScriptAnalyzer::make_array_reduced_value (GDScriptParser::ArrayNode *p_array, bool &is_reduced) {
@@ -5317,6 +5322,53 @@ Variant GDScriptAnalyzer::make_subscript_reduced_value(GDScriptParser::Subscript
5317
5322
}
5318
5323
}
5319
5324
5325
+ Variant GDScriptAnalyzer::make_call_reduced_value (GDScriptParser::CallNode *p_call, bool &is_reduced) {
5326
+ if (p_call->get_callee_type () == GDScriptParser::Node::IDENTIFIER) {
5327
+ Variant::Type type = Variant::NIL;
5328
+ if (p_call->function_name == SNAME (" Array" )) {
5329
+ type = Variant::ARRAY;
5330
+ } else if (p_call->function_name == SNAME (" Dictionary" )) {
5331
+ type = Variant::DICTIONARY;
5332
+ } else {
5333
+ return Variant ();
5334
+ }
5335
+
5336
+ Vector<Variant> args;
5337
+ args.resize (p_call->arguments .size ());
5338
+ const Variant **argptrs = (const Variant **)alloca (sizeof (const Variant **) * args.size ());
5339
+ for (int i = 0 ; i < p_call->arguments .size (); i++) {
5340
+ bool is_arg_value_reduced = false ;
5341
+ Variant arg_value = make_expression_reduced_value (p_call->arguments [i], is_arg_value_reduced);
5342
+ if (!is_arg_value_reduced) {
5343
+ return Variant ();
5344
+ }
5345
+ args.write [i] = arg_value;
5346
+ argptrs[i] = &args[i];
5347
+ }
5348
+
5349
+ Variant result;
5350
+ Callable::CallError ce;
5351
+ Variant::construct (type, result, argptrs, args.size (), ce);
5352
+ if (ce.error ) {
5353
+ push_error (vformat (R"( Failed to construct "%s".)" , Variant::get_type_name (type)), p_call);
5354
+ return Variant ();
5355
+ }
5356
+
5357
+ if (type == Variant::ARRAY) {
5358
+ Array array = result;
5359
+ array.make_read_only ();
5360
+ } else if (type == Variant::DICTIONARY) {
5361
+ Dictionary dictionary = result;
5362
+ dictionary.make_read_only ();
5363
+ }
5364
+
5365
+ is_reduced = true ;
5366
+ return result;
5367
+ }
5368
+
5369
+ return Variant ();
5370
+ }
5371
+
5320
5372
Array GDScriptAnalyzer::make_array_from_element_datatype (const GDScriptParser::DataType &p_element_datatype, const GDScriptParser::Node *p_source_node) {
5321
5373
Array array;
5322
5374
0 commit comments