private static List <ident> GetTemplateParametersTypeDependsOn(type_node type) { if (type.generic_function_container != null) { return(new List <ident> { new ident(type.name) }); } var typeRef = type as ref_type_node; if (typeRef != null) { return(GetTemplateParametersTypeDependsOn(typeRef.pointed_type)); } var typeIi = type.get_internal_interface(internal_interface_kind.unsized_array_interface) as array_internal_interface; if (typeIi != null) { return(GetTemplateParametersTypeDependsOn(typeIi.element_type)); } if (type.type_special_kind == type_special_kind.set_type) { return(GetTemplateParametersTypeDependsOn(type.element_type)); } if (type.IsDelegate) { var dii = type.get_internal_interface(internal_interface_kind.delegate_interface) as delegate_internal_interface; var res = new List <ident>(); if (dii != null) { var paramCount = dii.parameters.Count; for (var i = 0; i < paramCount; i++) { res.AddRange(GetTemplateParametersTypeDependsOn(dii.parameters[i].type)); } } return(res); } if (type.is_generic_type_instance) { var pcount = type.instance_params.Count; var res = new List <ident>(); for (var i = 0; i < pcount; i++) { res.AddRange(GetTemplateParametersTypeDependsOn(type.instance_params[i])); } return(res); } return(new List <ident>()); }
/// <summary> /// Вывод типа параметров лямбд и типа возвращаемого значения при присваивании лямбды переменной /// </summary> public static void InferTypesFromVarStmt(type_node leftType, function_lambda_definition lambdaDef, syntax_tree_visitor visitor, Operators op = Operators.Undefined) { if (lambdaDef == null) { return; } if (leftType != null) { delegate_internal_interface dii_left = (delegate_internal_interface)leftType.get_internal_interface(internal_interface_kind.delegate_interface); if (dii_left == null) { if (leftType != SystemLibrary.SystemLibrary.system_delegate_type) { if (op != Operators.Undefined) { var sil = leftType.find_in_type(name_reflector.get_name(op)); if (sil != null && sil.Count > 0) { foreach (SymbolInfo si in sil) { if (si.sym_info is function_node) { function_node fn = si.sym_info as function_node; if (fn.parameters.Count == 2) { dii_left = (delegate_internal_interface)fn.parameters[1].type.get_internal_interface(internal_interface_kind.delegate_interface); if (dii_left != null) { break; } if (fn.parameters[1].type.is_generic_parameter) { compiled_type_node ctn = leftType as compiled_type_node; common_type_node ctn2 = leftType as common_type_node; if (ctn != null && ctn.is_generic_type_instance && fn.parameters[0].type.is_generic_type_instance && ctn.original_generic == fn.parameters[0].type.original_generic) { dii_left = (delegate_internal_interface)ctn.generic_params[0].get_internal_interface(internal_interface_kind.delegate_interface); if (dii_left != null) { break; } } if (ctn2 != null && ctn2.is_generic_type_instance && ctn2.instance_params.Count > 0 && fn.parameters[0].type.is_generic_type_instance && ctn2.original_generic == fn.parameters[0].type.original_generic) { dii_left = (delegate_internal_interface)ctn2.instance_params[0].get_internal_interface(internal_interface_kind.delegate_interface); if (dii_left != null) { break; } } } } } } } } if (dii_left == null) { visitor.AddError(visitor.get_location(lambdaDef), "ILLEGAL_LAMBDA_VARIABLE_TYPE"); } } else { return; } } int leftTypeParamsNumber = dii_left.parameters.Count; int lambdaDefParamsCount = 0; if (lambdaDef.formal_parameters != null && lambdaDef.formal_parameters.params_list.Count != 0) { for (int i = 0; i < lambdaDef.formal_parameters.params_list.Count; i++) { lambdaDefParamsCount += lambdaDef.formal_parameters.params_list[i].idents.idents.Count; } if (lambdaDefParamsCount != leftTypeParamsNumber) { visitor.AddError(visitor.get_location(lambdaDef), "ILLEGAL_LAMBDA_PARAMETERS_NUMBER"); } bool flag = true; SyntaxTree.formal_parameters lambdaDefParamsTypes = new formal_parameters(); for (int i = 0; i < lambdaDef.formal_parameters.params_list.Count; i++) { for (int j = 0; j < lambdaDef.formal_parameters.params_list[i].idents.idents.Count; j++) { var param = new SyntaxTree.typed_parameters(); param.idents = new ident_list(); param.idents.Add(lambdaDef.formal_parameters.params_list[i].idents.idents[j]); param.vars_type = lambdaDef.formal_parameters.params_list[i].vars_type; param.source_context = lambdaDef.formal_parameters.source_context; lambdaDefParamsTypes.Add(param); } } for (int i = 0; i < leftTypeParamsNumber && flag; i++) { if (lambdaDefParamsTypes.params_list[i].vars_type is SyntaxTree.lambda_inferred_type) { if ((lambdaDefParamsTypes.params_list[i].vars_type as SyntaxTree.lambda_inferred_type).real_type is lambda_any_type_node) { var curLeftParType = dii_left.parameters[i].type; lambdaDefParamsTypes.params_list[i].vars_type = new SyntaxTree.lambda_inferred_type(); (lambdaDefParamsTypes.params_list[i].vars_type as SyntaxTree.lambda_inferred_type).real_type = curLeftParType; continue; } } var lambdaPar = visitor.convert_strong(lambdaDefParamsTypes.params_list[i].vars_type); if (!convertion_data_and_alghoritms.eq_type_nodes(dii_left.parameters[i].type, lambdaPar)) { visitor.AddError(visitor.get_location(lambdaDef), "ILLEGAL_LAMBDA_VARIABLE_TYPE"); } } lambdaDef.formal_parameters = lambdaDefParamsTypes; } if (lambdaDef.return_type != null && lambdaDef.return_type is lambda_inferred_type) { if (dii_left.return_value_type != null) { (lambdaDef.return_type as lambda_inferred_type).real_type = dii_left.return_value_type; } else // SSM 23/07/16 - попытка бороться с var p: Shape->() := a->a.Print() { // lambdaDef.usedkeyword == 1 // function var b = lambdaDef.usedkeyword == 0 && TryConvertFuncLambdaBodyWithMethodCallToProcLambdaBody(lambdaDef); // пытаться конвертировать только если мы явно не указали, что это функция if (!b) { visitor.AddError(visitor.get_location(lambdaDef), "UNABLE_TO_CONVERT_FUNCTIONAL_TYPE_TO_PROCEDURAL_TYPE"); } } } } else { if (lambdaDef.formal_parameters != null) { for (int i = 0; i < lambdaDef.formal_parameters.params_list.Count; i++) { if (lambdaDef.formal_parameters.params_list[i].vars_type is lambda_inferred_type) { visitor.AddError(visitor.get_location(lambdaDef), "IMPOSSIBLE_TO_INFER_TYPES_IN_LAMBDA"); } } } } }
/// <summary> /// Вывод типа параметров лямбд и типа возвращаемого значения при присваивании лямбды переменной /// </summary> public static void InferTypesFromVarStmt(type_node leftType, function_lambda_definition lambdaDef, syntax_tree_visitor visitor) { if (lambdaDef == null) { return; } if (leftType != null) { delegate_internal_interface dii_left = (delegate_internal_interface)leftType.get_internal_interface(internal_interface_kind.delegate_interface); if (dii_left == null) { if (leftType != SystemLibrary.SystemLibrary.system_delegate_type) { visitor.AddError(visitor.get_location(lambdaDef), "ILLEGAL_LAMBDA_VARIABLE_TYPE"); } else { return; } } int leftTypeParamsNumber = dii_left.parameters.Count; int lambdaDefParamsCount = 0; if (lambdaDef.formal_parameters != null && lambdaDef.formal_parameters.params_list.Count != 0) { for (int i = 0; i < lambdaDef.formal_parameters.params_list.Count; i++) { lambdaDefParamsCount += lambdaDef.formal_parameters.params_list[i].idents.idents.Count; } if (lambdaDefParamsCount != leftTypeParamsNumber) { visitor.AddError(visitor.get_location(lambdaDef), "ILLEGAL_LAMBDA_PARAMETERS_NUMBER"); } bool flag = true; SyntaxTree.formal_parameters lambdaDefParamsTypes = new formal_parameters(); for (int i = 0; i < lambdaDef.formal_parameters.params_list.Count; i++) { for (int j = 0; j < lambdaDef.formal_parameters.params_list[i].idents.idents.Count; j++) { var param = new SyntaxTree.typed_parameters(); param.idents = new ident_list(); param.idents.Add(lambdaDef.formal_parameters.params_list[i].idents.idents[j]); param.vars_type = lambdaDef.formal_parameters.params_list[i].vars_type; param.source_context = lambdaDef.formal_parameters.source_context; lambdaDefParamsTypes.Add(param); } } for (int i = 0; i < leftTypeParamsNumber && flag; i++) { if (lambdaDefParamsTypes.params_list[i].vars_type is SyntaxTree.lambda_inferred_type) { if ((lambdaDefParamsTypes.params_list[i].vars_type as SyntaxTree.lambda_inferred_type).real_type is lambda_any_type_node) { var curLeftParType = dii_left.parameters[i].type; lambdaDefParamsTypes.params_list[i].vars_type = new SyntaxTree.lambda_inferred_type(); (lambdaDefParamsTypes.params_list[i].vars_type as SyntaxTree.lambda_inferred_type).real_type = curLeftParType; continue; } } var lambdaPar = visitor.convert_strong(lambdaDefParamsTypes.params_list[i].vars_type); if (!convertion_data_and_alghoritms.eq_type_nodes(dii_left.parameters[i].type, lambdaPar)) { visitor.AddError(visitor.get_location(lambdaDef), "ILLEGAL_LAMBDA_VARIABLE_TYPE"); } } lambdaDef.formal_parameters = lambdaDefParamsTypes; } if (lambdaDef.return_type != null && lambdaDef.return_type is lambda_inferred_type) { if (dii_left.return_value_type != null) { (lambdaDef.return_type as lambda_inferred_type).real_type = dii_left.return_value_type; } else // SSM 23/07/16 - попытка бороться с var p: Shape->() := a->a.Print() { var b = TryConvertFuncLambdaBodyWithMethodCallToProcLambdaBody(lambdaDef); if (!b) { throw new SimpleSemanticError(visitor.get_location(lambdaDef), "UNABLE_TO_CONVERT_FUNCTIONAL_TYPE_TO_PROCEDURAL_TYPE"); } } } } else { if (lambdaDef.formal_parameters != null) { for (int i = 0; i < lambdaDef.formal_parameters.params_list.Count; i++) { if (lambdaDef.formal_parameters.params_list[i].vars_type is lambda_inferred_type) { visitor.AddError(visitor.get_location(lambdaDef), "IMPOSSIBLE_TO_INFER_TYPES_IN_LAMBDA"); } } } } }
public expression_node convert_delegates_to_delegates(location call_location, expression_node[] parameters) { if (parameters.Length != 1) { throw new PascalABCCompiler.TreeConverter.CompilerInternalError("Invalid delegates convertion"); } delegate_internal_interface dii_to = (delegate_internal_interface)_to.get_internal_interface(internal_interface_kind.delegate_interface); delegate_internal_interface dii = (delegate_internal_interface)parameters[0].type.get_internal_interface(internal_interface_kind.delegate_interface); expression_node pr = parameters[0]; base_function_call ifnotnull = null; if (_to.semantic_node_type == semantic_node_type.compiled_type_node) { ifnotnull = new compiled_constructor_call((compiled_constructor_node)dii_to.constructor, call_location); } else { ifnotnull = new common_constructor_call((common_method_node)dii_to.constructor, call_location); } //ccc = new common_constructor_call(dii_to.constructor, call_location); expression_node par = null; if (parameters[0].type.semantic_node_type == semantic_node_type.compiled_type_node) { par = new compiled_function_call((compiled_function_node)dii.invoke_method, parameters[0], call_location); } else { par = new common_method_call((common_method_node)dii.invoke_method, parameters[0], call_location); } ifnotnull.parameters.AddElement(par); null_const_node ncn = new null_const_node(_to, call_location); null_const_node ncn2 = new null_const_node(_to, call_location); PascalABCCompiler.TreeConverter.SymbolInfo si = pr.type.find_first_in_type(PascalABCCompiler.TreeConverter.compiler_string_consts.eq_name); basic_function_node fn = si.sym_info as basic_function_node; expression_node condition = null; if (fn != null) { basic_function_call condition_bfc = new basic_function_call(fn, call_location); condition_bfc.parameters.AddElement(pr); condition_bfc.parameters.AddElement(ncn); condition = condition_bfc; } else if (si.sym_info is compiled_function_node) { compiled_static_method_call condition_cfc = new compiled_static_method_call(si.sym_info as compiled_function_node, call_location); condition_cfc.parameters.AddElement(pr); condition_cfc.parameters.AddElement(ncn); condition = condition_cfc; } question_colon_expression qce = new question_colon_expression(condition, ncn2, ifnotnull, call_location); return(qce); }
//TODO: Возможно стоит если пересечение типов найдено в откомпилированных типах, добавлять к нашим структурам и не искать повторно. public static possible_type_convertions get_convertions(type_node from, type_node to, bool is_implicit) { possible_type_convertions ret = new possible_type_convertions(); ret.first = null; ret.second = null; if ((from == null) || (to == null)) { return(ret); } type_intersection_node tin_from = from.get_type_intersection(to); type_intersection_node tin_to = to.get_type_intersection(from); if (tin_from != null) { if (tin_from.this_to_another != null) { if ((!is_implicit) || (!(tin_from.this_to_another.is_explicit))) { add_conversion(ret, tin_from.this_to_another.convertion_method, from, to); } } } if (tin_to != null) { if (tin_to.another_to_this != null) { if ((!is_implicit) || (!(tin_to.another_to_this.is_explicit))) { add_conversion(ret, tin_to.another_to_this.convertion_method, from, to); } } } if (ret.second != null) { return(ret); } if (is_derived(to, from) || (from.IsInterface && to == SystemLibrary.SystemLibrary.object_type) || from.is_generic_type_instance && to == SystemLibrary.SystemLibrary.object_type) { add_conversion(ret, TreeConverter.convertion_data_and_alghoritms.get_empty_conversion(from, to, true), from, to); //add_conversion(ret, SystemLibrary.SystemLibrary.empty_method, from, to); } if (ret.second != null) { return(ret); } wrapped_type ctn_to = to as wrapped_type; wrapped_type ctn_from = from as wrapped_type; if (ctn_to != null) { function_node fnode1 = null; fnode1 = ctn_to.get_implicit_conversion_from(from); add_conversion(ret, fnode1, from, to); if (ret.second != null) { return(ret); } fnode1 = null; if (!is_implicit) { fnode1 = ctn_to.get_explicit_conversion_from(from); } add_conversion(ret, fnode1, from, to); if (ret.second != null) { return(ret); } } if (ctn_from != null) { function_node fnode2 = null; fnode2 = ctn_from.get_implicit_conversion_to(to); add_conversion(ret, fnode2, from, to); if (ret.second != null) { return(ret); } fnode2 = null; if (!is_implicit) { fnode2 = ctn_from.get_explicit_conversion_to(to); } add_conversion(ret, fnode2, from, to); if (ret.second != null) { return(ret); } } //TODO: Вот это должно быть в каком нибудь другом месте. internal_interface ii = from.get_internal_interface(internal_interface_kind.delegate_interface); if (ii != null) { delegate_internal_interface dii = (delegate_internal_interface)ii; internal_interface to_ii = to.get_internal_interface(internal_interface_kind.delegate_interface); if (to_ii != null) { delegate_internal_interface to_dii = (delegate_internal_interface)to_ii; if (dii.parameters.Count == to_dii.parameters.Count) { //ms100 error fixed (DS) bool eq = TreeConverter.convertion_data_and_alghoritms.function_eq_params_and_result(dii.invoke_method, to_dii.invoke_method); if (eq) { delegate_to_delegate_type_converter dtdtc = new delegate_to_delegate_type_converter(to); add_conversion(ret, new convert_types_function_node(dtdtc.convert_delegates_to_delegates, false), from, to); } } } if (dii.parameters.Count == 0) { if (dii.return_value_type == to) { add_conversion(ret, new convert_types_function_node(convert_delegate_to_return_value_type, true), from, to); } else { possible_type_convertions ptcc = get_convertions(dii.return_value_type, to); if ((ptcc.first != null) && (ptcc.first.convertion_method != null)) { delegate_type_converter dtc = new delegate_type_converter(ptcc.first.convertion_method); add_conversion(ret, new convert_types_function_node(dtc.convert_delegate_to_return_value_type_with_convertion, false), from, to); } if ((ptcc.second != null) && (ptcc.second.convertion_method != null)) { delegate_type_converter dtc = new delegate_type_converter(ptcc.second.convertion_method); add_conversion(ret, new convert_types_function_node(dtc.convert_delegate_to_return_value_type_with_convertion, false), from, to); } } } } return(ret); }