public override ABnfGuessError GuessTypes(out List <ABnfGuess> guess_list) { guess_list = new List <ABnfGuess>(); m_getter_list = null; m_setter_list = null; m_class_guess = null; var error = CalcResolve(out List <ABnfElement> result_list, out ABnfGuess pre_type); if (error != null) { return(error); } foreach (var result in result_list) { ABnfGuess guess = null; if (result is ALittleScriptClassVarDecElement) { error = result.GuessType(out guess); if (error != null) { return(error); } if (m_class_guess != null && guess is ALittleScriptGuessTemplate) { if (!m_class_guess.template_map.TryGetValue(guess.GetValueWithoutConst(), out ABnfGuess guess_template)) { for (int i = 0; i < m_class_guess.template_list.Count; ++i) { guess_template = m_class_guess.template_list[i]; if (guess_template.GetValueWithoutConst() == guess.GetValueWithoutConst()) { break; } } } if (guess_template != null) { if (guess.is_const && !guess_template.is_const) { guess_template = guess_template.Clone(); guess_template.is_const = true; guess_template.UpdateValue(); } guess = guess_template; } } } else if (result is ALittleScriptStructVarDecElement) { error = result.GuessType(out guess); if (error != null) { return(error); } } else if (result is ALittleScriptEnumVarDecElement) { error = result.GuessType(out guess); if (error != null) { return(error); } } else if (result is ALittleScriptMethodNameDecElement) { error = result.GuessType(out guess); if (error != null) { return(error); } // 如果前一个数据是const,那么调用的函数也必须是const if (pre_type != null && pre_type.is_const) { var guess_functor = guess as ALittleScriptGuessFunctor; if (guess_functor != null && !guess_functor.const_modifier) { return(new ABnfGuessError(m_element, "请使用带Const修饰的函数")); } } if (result.GetParent() is ALittleScriptClassGetterDecElement) { if (m_getter_list != null && m_getter_list.IndexOf(result) >= 0 && guess is ALittleScriptGuessFunctor) { guess = ((ALittleScriptGuessFunctor)guess).return_list[0]; } } else if (result.GetParent() is ALittleScriptClassSetterDecElement) { if (m_setter_list != null && m_setter_list.IndexOf(result) >= 0 && guess is ALittleScriptGuessFunctor) { guess = ((ALittleScriptGuessFunctor)guess).param_list[1]; } } error = ReplaceTemplate(guess, out guess); if (error != null) { return(error); } } else if (result is ALittleScriptVarAssignNameDecElement) { error = result.GuessType(out guess); if (error != null) { return(error); } } else if (result is ALittleScriptEnumNameDecElement) { error = result.GuessType(out ABnfGuess enum_guess); if (error != null) { return(error); } if (!(enum_guess is ALittleScriptGuessEnum)) { return(new ABnfGuessError(m_element, "ALittleEnumNameDec.guessType的结果不是ALittleGuessEnum")); } var enum_guess_enum = enum_guess as ALittleScriptGuessEnum; var info = new ALittleScriptGuessEnumName(enum_guess_enum.namespace_name, enum_guess_enum.enum_name, result as ALittleScriptEnumNameDecElement); info.UpdateValue(); guess = info; } else if (result is ALittleScriptStructNameDecElement) { error = result.GuessType(out ABnfGuess struct_guess); if (error != null) { return(error); } if (!(struct_guess is ALittleScriptGuessStruct)) { return(new ABnfGuessError(m_element, "ALittleStructNameDec.guessType的结果不是ALittleGuessStruct")); } var struct_guess_struct = struct_guess as ALittleScriptGuessStruct; var info = new ALittleScriptGuessStructName(struct_guess_struct.namespace_name, struct_guess_struct.struct_name, result as ALittleScriptStructNameDecElement); info.UpdateValue(); guess = info; } else if (result is ALittleScriptClassNameDecElement) { error = result.GuessType(out ABnfGuess class_guess); if (error != null) { return(error); } if (!(class_guess is ALittleScriptGuessClass)) { return(new ABnfGuessError(m_element, "ALittleClassNameDec.guessType的结果不是ALittleGuessClass")); } var class_guess_class = class_guess as ALittleScriptGuessClass; if (class_guess_class.template_list.Count > 0) { return(new ABnfGuessError(m_element, "模板类" + class_guess_class.GetValue() + "不能直接使用")); } var info = new ALittleScriptGuessClassName(class_guess_class.namespace_name, class_guess_class.class_name, result as ALittleScriptClassNameDecElement); info.UpdateValue(); guess = info; } if (guess != null) { if (pre_type != null && pre_type.is_const && !guess.is_const) { if (guess is ALittleScriptGuessPrimitive) { var guess_value = guess.GetValue(); ALittleScriptIndex.inst.sPrimitiveGuessMap.TryGetValue("const " + guess.GetValue(), out guess); if (guess == null) { return(new ABnfGuessError(m_element, "找不到const " + guess_value)); } } else { guess = guess.Clone(); guess.is_const = true; guess.UpdateValue(); } } guess_list.Add(guess); } } m_getter_list = null; m_setter_list = null; m_class_guess = null; return(null); }
private ABnfGuessError AnalysisTemplate(Dictionary <string, ABnfGuess> fill_map, ABnfGuess left_guess, ABnfElement right_src, ABnfGuess right_guess, bool assign_or_call) { // 如果值等于null,那么可以赋值 if (right_guess.GetValue() == "null") { return(null); } // const是否可以赋值给非const if (assign_or_call) { if (left_guess.is_const && !right_guess.is_const) { return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ", 不能是:" + right_guess.GetValue())); } } else { // 如果不是基本变量类型(排除any),基本都是值传递,函数调用时就不用检查const if (!(left_guess is ALittleScriptGuessPrimitive) || left_guess.GetValueWithoutConst() == "any") { if (!left_guess.is_const && right_guess.is_const) { return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ", 不能是:" + right_guess.GetValue())); } } } // 如果任何一方是any,那么就认为可以相等 if (left_guess is ALittleScriptGuessAny) { return(null); } if (left_guess is ALittleScriptGuessPrimitive || left_guess is ALittleScriptGuessStruct) { return(ALittleScriptOp.GuessTypeEqual(left_guess, right_src, right_guess, assign_or_call, false)); } if (left_guess is ALittleScriptGuessMap) { if (!(right_guess is ALittleScriptGuessMap)) { return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue())); } var error = AnalysisTemplate(fill_map, ((ALittleScriptGuessMap)left_guess).key_type, right_src, ((ALittleScriptGuessMap)right_guess).key_type, false); if (error != null) { return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue())); } error = AnalysisTemplate(fill_map, ((ALittleScriptGuessMap)left_guess).value_type, right_src, ((ALittleScriptGuessMap)right_guess).value_type, false); if (error != null) { return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue())); } return(null); } if (left_guess is ALittleScriptGuessList) { if (!(right_guess is ALittleScriptGuessList)) { return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue())); } var error = AnalysisTemplate(fill_map, ((ALittleScriptGuessList)left_guess).sub_type, right_src, ((ALittleScriptGuessList)right_guess).sub_type, false); if (error != null) { return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue())); } return(null); } if (left_guess is ALittleScriptGuessFunctor) { if (!(right_guess is ALittleScriptGuessFunctor)) { return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue())); } ALittleScriptGuessFunctor left_guess_functor = (ALittleScriptGuessFunctor)left_guess; ALittleScriptGuessFunctor right_guess_functor = (ALittleScriptGuessFunctor)right_guess; if (left_guess_functor.param_list.Count != right_guess_functor.param_list.Count || left_guess_functor.param_nullable_list.Count != right_guess_functor.param_nullable_list.Count || left_guess_functor.return_list.Count != right_guess_functor.return_list.Count || left_guess_functor.template_param_list.Count != right_guess_functor.template_param_list.Count || left_guess_functor.await_modifier != right_guess_functor.await_modifier || left_guess_functor.proto == null && right_guess_functor.proto != null || left_guess_functor.proto != null && right_guess_functor.proto == null || (left_guess_functor.proto != null && left_guess_functor.proto != right_guess_functor.proto) || left_guess_functor.param_tail == null && right_guess_functor.param_tail != null || left_guess_functor.param_tail != null && right_guess_functor.param_tail == null || left_guess_functor.return_tail == null && right_guess_functor.return_tail != null || left_guess_functor.return_tail != null && right_guess_functor.return_tail == null ) { return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue())); } for (int i = 0; i < left_guess_functor.template_param_list.Count; ++i) { var error = AnalysisTemplate(fill_map, left_guess_functor.template_param_list[i], right_src, right_guess_functor.template_param_list[i], false); if (error != null) { return(error); } } for (int i = 0; i < left_guess_functor.param_list.Count; ++i) { var error = AnalysisTemplate(fill_map, left_guess_functor.param_list[i], right_src, right_guess_functor.param_list[i], false); if (error != null) { return(error); } } for (int i = 0; i < left_guess_functor.param_nullable_list.Count; ++i) { if (left_guess_functor.param_nullable_list[i] != right_guess_functor.param_nullable_list[i]) { return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue())); } } for (int i = 0; i < left_guess_functor.return_list.Count; ++i) { var error = AnalysisTemplate(fill_map, left_guess_functor.return_list[i], right_src, right_guess_functor.return_list[i], false); if (error != null) { return(error); } } return(null); } if (left_guess is ALittleScriptGuessClass) { if (right_guess is ALittleScriptGuessTemplate) { right_guess = (right_guess as ALittleScriptGuessTemplate).template_extends; } if (!(right_guess is ALittleScriptGuessClass)) { return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue())); } if (left_guess.GetValue() == right_guess.GetValue()) { return(null); } var error = ALittleScriptUtility.IsClassSuper((left_guess as ALittleScriptGuessClass).class_dec, right_guess.GetValue(), out bool result); if (error != null) { return(error); } if (result) { return(null); } error = ALittleScriptUtility.IsClassSuper((right_guess as ALittleScriptGuessClass).class_dec, left_guess.GetValue(), out result); if (error != null) { return(error); } if (result) { return(null); } return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue())); } if (left_guess is ALittleScriptGuessTemplate) { var left_guess_template = left_guess as ALittleScriptGuessTemplate; // 查看模板是否已经被填充,那么就按填充的检查 if (fill_map.TryGetValue(left_guess_template.GetValue(), out ABnfGuess fill_guess)) { return(ALittleScriptOp.GuessTypeEqual(fill_guess, right_src, right_guess, false, false)); } // 处理还未填充 if (left_guess_template.template_extends != null) { var error = AnalysisTemplate(fill_map, left_guess_template.template_extends, right_src, right_guess, false); if (error != null) { return(error); } if (right_guess.is_const) { right_guess = right_guess.Clone(); right_guess.is_const = false; right_guess.UpdateValue(); } fill_map.Add(left_guess_template.GetValueWithoutConst(), right_guess); return(null); } else if (left_guess_template.is_class) { if (right_guess is ALittleScriptGuessClass) { if (right_guess.is_const) { right_guess = right_guess.Clone(); right_guess.is_const = false; right_guess.UpdateValue(); } fill_map.Add(left_guess_template.GetValueWithoutConst(), right_guess); return(null); } else if (right_guess is ALittleScriptGuessTemplate) { var right_guess_template = right_guess as ALittleScriptGuessTemplate; if (right_guess_template.template_extends is ALittleScriptGuessClass || right_guess_template.is_class) { if (right_guess.is_const) { right_guess = right_guess.Clone(); right_guess.is_const = false; right_guess.UpdateValue(); } fill_map.Add(right_guess_template.GetValueWithoutConst(), right_guess); return(null); } } return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue())); } else if (left_guess_template.is_struct) { if (right_guess is ALittleScriptGuessStruct) { if (right_guess.is_const) { right_guess = right_guess.Clone(); right_guess.is_const = false; right_guess.UpdateValue(); } fill_map.Add(left_guess_template.GetValueWithoutConst(), right_guess); return(null); } else if (right_guess is ALittleScriptGuessTemplate) { var right_guess_template = right_guess as ALittleScriptGuessTemplate; if (right_guess_template.template_extends is ALittleScriptGuessStruct || right_guess_template.is_struct) { if (right_guess.is_const) { right_guess = right_guess.Clone(); right_guess.is_const = false; right_guess.UpdateValue(); } fill_map.Add(left_guess_template.GetValue(), right_guess); return(null); } } return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue())); } if (right_guess.is_const) { right_guess = right_guess.Clone(); right_guess.is_const = false; right_guess.UpdateValue(); } fill_map.Add(left_guess_template.GetValueWithoutConst(), right_guess); return(null); } return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue())); }
private ABnfGuessError CalcResolve(out List <ABnfElement> result_list, out ABnfGuess pre_type) { pre_type = null; result_list = new List <ABnfElement>(); if (m_key.Length == 0) { return(null); } // 获取父节点 var property_value_dot_id = m_element.GetParent() as ALittleScriptPropertyValueDotIdElement; var property_value_suffix = property_value_dot_id.GetParent() as ALittleScriptPropertyValueSuffixElement; var property_value = property_value_suffix.GetParent() as ALittleScriptPropertyValueElement; var property_value_first_type = property_value.GetPropertyValueFirstType(); var suffix_list = property_value.GetPropertyValueSuffixList(); // 获取所在位置 int index = suffix_list.IndexOf(property_value_suffix); if (index == -1) { return(null); } ABnfGuessError error = null; if (index == 0) { error = property_value_first_type.GuessType(out pre_type); } else { error = suffix_list[index - 1].GuessType(out pre_type); } if (error != null) { return(error); } // 判断当前后缀是否是最后一个后缀 ALittleScriptPropertyValueSuffixElement next_suffix = null; if (index + 1 < suffix_list.Count) { next_suffix = suffix_list[index + 1]; } bool is_const = false; if (pre_type != null) { is_const = pre_type.is_const; } if (pre_type is ALittleScriptGuessTemplate) { pre_type = (pre_type as ALittleScriptGuessTemplate).template_extends; if (pre_type != null && is_const && !pre_type.is_const) { pre_type = pre_type.Clone(); pre_type.is_const = true; pre_type.UpdateValue(); } } if (pre_type == null) { return(null); } // 处理类的实例对象 if (pre_type is ALittleScriptGuessClass) { m_class_guess = pre_type as ALittleScriptGuessClass; var class_dec = m_class_guess.class_dec; // 计算当前元素对这个类的访问权限 int access_level = ALittleScriptUtility.CalcAccessLevelByTargetClassDecForElement(m_element, class_dec); // 所有成员变量 var class_var_dec_list = new List <ABnfElement>(); ALittleScriptUtility.FindClassAttrList(class_dec, access_level, ALittleScriptUtility.ClassAttrType.VAR, m_key, class_var_dec_list, 100); foreach (var class_var_dec in class_var_dec_list) { result_list.Add(class_var_dec); } var class_method_name_dec_list = new List <ABnfElement>(); // 在当前情况下,只有当前property_value在等号的左边,并且是最后一个属性才是setter,否则都是getter if (next_suffix == null && property_value.GetParent() is ALittleScriptOpAssignExprElement) { m_setter_list = new List <ABnfElement>(); ALittleScriptUtility.FindClassAttrList(class_dec, access_level, ALittleScriptUtility.ClassAttrType.SETTER, m_key, m_setter_list, 100); class_method_name_dec_list.AddRange(m_setter_list); } else { m_getter_list = new List <ABnfElement>(); ALittleScriptUtility.FindClassAttrList(class_dec, access_level, ALittleScriptUtility.ClassAttrType.GETTER, m_key, m_getter_list, 100); class_method_name_dec_list.AddRange(m_getter_list); } // 所有成员函数 ALittleScriptUtility.FindClassAttrList(class_dec, access_level, ALittleScriptUtility.ClassAttrType.FUN, m_key, class_method_name_dec_list, 100); // 添加函数名元素 class_method_name_dec_list = ALittleScriptUtility.FilterSameName(class_method_name_dec_list); foreach (var class_method_name_dec in class_method_name_dec_list) { result_list.Add(class_method_name_dec); } // 处理结构体的实例对象 } else if (pre_type is ALittleScriptGuessStruct) { var struct_dec = ((ALittleScriptGuessStruct)pre_type).struct_dec; var struct_var_dec_list = new List <ALittleScriptStructVarDecElement>(); // 所有成员变量 ALittleScriptUtility.FindStructVarDecList(struct_dec, m_key, struct_var_dec_list, 100); foreach (var struct_var_dec in struct_var_dec_list) { result_list.Add(struct_var_dec); } // 比如 ALittleName.XXX } else if (pre_type is ALittleScriptGuessNamespaceName) { var namespace_name_dec = ((ALittleScriptGuessNamespaceName)pre_type).namespace_name_dec; string namespace_name = namespace_name_dec.GetElementText(); // 所有枚举名 var enum_name_dec_list = ALittleScriptIndex.inst.FindALittleNameDecList( ALittleScriptUtility.ABnfElementType.ENUM_NAME, m_element.GetFile(), namespace_name, m_key, true); foreach (var enum_name_dec in enum_name_dec_list) { result_list.Add(enum_name_dec); } // 所有全局函数 var method_name_dec_list = ALittleScriptIndex.inst.FindALittleNameDecList( ALittleScriptUtility.ABnfElementType.GLOBAL_METHOD, m_element.GetFile(), namespace_name, m_key, true); foreach (var method_name_dec in method_name_dec_list) { result_list.Add(method_name_dec); } // 所有类名 var class_name_dec_list = ALittleScriptIndex.inst.FindALittleNameDecList( ALittleScriptUtility.ABnfElementType.CLASS_NAME, m_element.GetFile(), namespace_name, m_key, true); foreach (var class_name_dec in class_name_dec_list) { result_list.Add(class_name_dec); } // 所有结构体名 var struct_name_dec_list = ALittleScriptIndex.inst.FindALittleNameDecList( ALittleScriptUtility.ABnfElementType.STRUCT_NAME, m_element.GetFile(), namespace_name, m_key, true); foreach (var struct_name_dec in struct_name_dec_list) { result_list.Add(struct_name_dec); } // 所有单例 var instance_name_dec_list = ALittleScriptIndex.inst.FindALittleNameDecList( ALittleScriptUtility.ABnfElementType.INSTANCE_NAME, m_element.GetFile(), namespace_name, m_key, false); foreach (var instance_name_dec in instance_name_dec_list) { result_list.Add(instance_name_dec); } // 比如 AClassName.XXX } else if (pre_type is ALittleScriptGuessClassName) { var class_name_dec = ((ALittleScriptGuessClassName)pre_type).class_name_dec; var class_dec = class_name_dec.GetParent() as ALittleScriptClassDecElement; // 计算当前元素对这个类的访问权限 int access_level = ALittleScriptUtility.CalcAccessLevelByTargetClassDecForElement(m_element, class_dec); // 所有静态函数 var class_method_name_dec_list = new List <ABnfElement>(); ALittleScriptUtility.FindClassAttrList(class_dec, access_level, ALittleScriptUtility.ClassAttrType.STATIC, m_key, class_method_name_dec_list, 100); // 如果后面那个是MethodCall,并且有两个参数的是setter,是一个参数的是getter,否则两个都不是 if (next_suffix != null) { var method_call_stat = next_suffix.GetPropertyValueMethodCall(); if (method_call_stat != null) { int paramCount = method_call_stat.GetValueStatList().Count; if (paramCount == 1) { // 所有getter ALittleScriptUtility.FindClassAttrList(class_dec, access_level, ALittleScriptUtility.ClassAttrType.GETTER, m_key, class_method_name_dec_list, 100); } else if (paramCount == 2) { // 所有setter ALittleScriptUtility.FindClassAttrList(class_dec, access_level, ALittleScriptUtility.ClassAttrType.SETTER, m_key, class_method_name_dec_list, 100); } } } // 所有成员函数 ALittleScriptUtility.FindClassAttrList(class_dec, access_level, ALittleScriptUtility.ClassAttrType.FUN, m_key, class_method_name_dec_list, 100); class_method_name_dec_list = ALittleScriptUtility.FilterSameName(class_method_name_dec_list); foreach (var class_method_name_dec in class_method_name_dec_list) { result_list.Add(class_method_name_dec); } // 比如 AEnumName.XXX } else if (pre_type is ALittleScriptGuessEnumName) { // 所有枚举字段 var enum_name_dec = ((ALittleScriptGuessEnumName)pre_type).enum_name_dec; var enum_dec = enum_name_dec.GetParent() as ALittleScriptEnumDecElement; var var_dec_list = new List <ALittleScriptEnumVarDecElement>(); ALittleScriptUtility.FindEnumVarDecList(enum_dec, m_key, var_dec_list); foreach (var var_name_dec in var_dec_list) { result_list.Add(var_name_dec); } } return(null); }