public IndifierInfo GetIndifierInfo(string name, string name_space = "", EIndifierFlag flag = EIndifierFlag.IF_All) { IndifierInfo info = new IndifierInfo(); //在指定的命名空间查找标示符 if (!string.IsNullOrEmpty(name_space)) { DB_Type type = Finder.FindType(name_space + "." + name); if (type != null) { info.is_type = true; info.type = type; return(info); } return(null); } //查找本地变量 foreach (var v in stackLocalVariables) { if (v.ContainsKey(name)) { info.is_var = true; info.type = v[name]; return(info); } } //泛型参数 if (currentMethod != null) { foreach (var gd in currentMethod.method_generic_parameter_definitions) { if (gd.type_name == name) { info.is_method_type_parameter = true; Metadata.DB_Type constraintType = Finder.FindType(gd.constraint, this); info.type = Metadata.DB_Type.MakeGenericParameterType(constraintType, name); return(info); } } } //查找成员变量 if (currentType != null) { if ((flag & EIndifierFlag.IF_Local) != 0) { if (currentType.FindField(name, this) != null) { info.is_field = true; info.type = GetType(currentType.FindField(name, this).typeName); return(info); } if (currentType.FindProperty(name, this) != null) { info.is_property = true; info.type = GetType(currentType.FindProperty(name, this).typeName); return(info); } if (currentType.FindEvent(name, this) != null) { info.is_event = true; info.type = GetType(currentType.FindEvent(name, this).typeName); return(info); } } if ((flag & EIndifierFlag.IF_Method) != 0) { List <DB_Member> methods = currentType.FindMethod(name, this); if (methods.Count > 0) { info.is_method = true; info.methods = methods; return(info); } } if ((flag & EIndifierFlag.IF_Type) != 0) { //查找泛型 if (currentType.is_generic_type_definition) { foreach (var gd in currentType.generic_parameter_definitions) { if (gd.type_name == name) { info.is_class_type_parameter = true; Metadata.DB_Type constraintType = Finder.FindType(gd.constraint, this); info.type = Metadata.DB_Type.MakeGenericParameterType(constraintType, name); return(info); } } } else if (currentType.is_generic_type) { //泛型实例,已经被替换了参数,所以无需查找 } //当前命名空间查找 foreach (var nsName in currentType.usingNamespace) { DB_Type type = FindTypeInNamespace(name, nsName); if (type != null) { info.is_type = true; info.type = type; return(info); } } } } if ((flag & EIndifierFlag.IF_Type) != 0) { //当前命名空间查找 if (!string.IsNullOrEmpty(outNamespace)) { DB_Type type = FindTypeInNamespace(name, outNamespace); if (type != null) { info.is_type = true; info.type = type; return(info); } } foreach (var nsName in usingNamespace) { DB_Type type = FindTypeInNamespace(name, nsName); if (type != null) { info.is_type = true; info.type = type; return(info); } } } return(null); }
public DB_Type GetExpType(Expression.Exp exp, Expression.Exp outer = null) { if (exp is Metadata.Expression.ConstExp) { Metadata.Expression.ConstExp e = exp as Metadata.Expression.ConstExp; int int_v; if (int.TryParse(e.value, out int_v)) { return(GetType("System.Int32")); } long long_v; if (long.TryParse(e.value, out long_v)) { return(GetType("System.Int64")); } bool b_v; if (bool.TryParse(e.value, out b_v)) { return(GetType("System.Boolean")); } float single_v; if (float.TryParse(e.value, out single_v)) { return(GetType("System.Single")); } double double_v; if (double.TryParse(e.value, out double_v)) { return(GetType("System.Double")); } if (e.value == "null") { return(GetType("System.Object")); } if (e.value.StartsWith("'")) { return(GetType("System.Char")); } return(GetType("System.String")); } else if (exp is Metadata.Expression.FieldExp) { Metadata.Expression.FieldExp e = exp as Metadata.Expression.FieldExp; Metadata.DB_Type caller_type = GetExpType(e.Caller); if (outer is MethodExp) { MethodExp methodExp = outer as MethodExp; List <Metadata.DB_Type> argTypes = new List <Metadata.DB_Type>(); foreach (var t in methodExp.Args) { argTypes.Add(GetExpType(t)); } Metadata.DB_Member member = caller_type.FindMethod(e.Name, argTypes, this); return(GetType(member.typeName)); } else { DB_Member field = caller_type.FindField(e.Name, this); if (field == null) { field = caller_type.FindProperty(e.Name, this); } if (field == null) { return(null); } return(GetType(field.typeName)); } } else if (exp is Metadata.Expression.IndifierExp) { Metadata.Expression.IndifierExp e = exp as Metadata.Expression.IndifierExp; IndifierInfo info = GetIndifierInfo(e.Name); //if(outer is MethodExp) //{ // MethodExp methodExp = outer as MethodExp; // List<Metadata.DB_Type> argTypes = new List<Metadata.DB_Type>(); // foreach (var t in methodExp.Args) // { // argTypes.Add(GetExpType(t)); // } // foreach(var m in info.methods) // { // if(m.MatchingParameter(argTypes,this)) // { // return GetType(m.typeName); // } // } //} //else { return(info.type); } } else if (exp is Metadata.Expression.MethodExp) { Metadata.Expression.MethodExp me = exp as Metadata.Expression.MethodExp; List <Metadata.DB_Type> argTypes = new List <Metadata.DB_Type>(); foreach (var t in me.Args) { argTypes.Add(GetExpType(t)); } if (me.Caller is FieldExp) { FieldExp fe = me.Caller as FieldExp; Metadata.DB_Type caller_type = GetExpType(fe.Caller, fe); return(GetType(caller_type.FindMethod(fe.Name, argTypes, this).typeName)); } else if (me.Caller is IndifierExp) { IndifierExp ie = me.Caller as IndifierExp; IndifierInfo info = GetIndifierInfo(ie.Name); if (info.is_method) { foreach (var m in info.methods) { if (m.MatchingParameter(argTypes, this)) { return(GetType(m.typeName)); } } } else { return(info.type); } } } else if (exp is Metadata.Expression.ObjectCreateExp) { Metadata.Expression.ObjectCreateExp e = exp as Metadata.Expression.ObjectCreateExp; return(GetType(e.Type)); } else if (exp is Metadata.Expression.BaseExp) { return(GetType(currentType.base_type)); } else if (exp is Metadata.Expression.ThisExp) { return(currentType); } else if (exp is Metadata.Expression.BinaryExpressionSyntax) { Metadata.Expression.BinaryExpressionSyntax me = exp as Metadata.Expression.BinaryExpressionSyntax; Metadata.DB_Type caller_type = GetExpType(me.Left); List <Metadata.DB_Type> argTypes = new List <Metadata.DB_Type>(); argTypes.Add(GetExpType(me.Right)); Metadata.DB_Member member = caller_type.FindMethod(me.OperatorToken, argTypes, this); return(GetType(member.typeName)); } else if (exp is Metadata.Expression.PostfixUnaryExpressionSyntax) { Metadata.Expression.PostfixUnaryExpressionSyntax me = exp as Metadata.Expression.PostfixUnaryExpressionSyntax; Metadata.DB_Type caller_type = GetExpType(me.Operand); List <Metadata.DB_Type> argTypes = new List <Metadata.DB_Type>(); argTypes.Add(caller_type); Metadata.DB_Member member = caller_type.FindMethod(me.OperatorToken, argTypes, this); return(GetType(member.typeName)); } else if (exp is Metadata.Expression.PrefixUnaryExpressionSyntax) { Metadata.Expression.PrefixUnaryExpressionSyntax me = exp as Metadata.Expression.PrefixUnaryExpressionSyntax; Metadata.DB_Type caller_type = GetExpType(me.Operand); List <Metadata.DB_Type> argTypes = new List <Metadata.DB_Type>(); argTypes.Add(caller_type); Metadata.DB_Member member = caller_type.FindMethod(me.OperatorToken, argTypes, this); return(GetType(member.typeName)); } else if (exp is Metadata.Expression.ParenthesizedExpressionSyntax) { Metadata.Expression.ParenthesizedExpressionSyntax pes = exp as Metadata.Expression.ParenthesizedExpressionSyntax; return(GetExpType(pes.exp)); } else if (exp is Metadata.Expression.ElementAccessExp) { Metadata.Expression.ElementAccessExp eae = exp as Metadata.Expression.ElementAccessExp; Metadata.DB_Type caller_type = GetExpType(eae.exp); List <Metadata.DB_Type> argTypes = new List <Metadata.DB_Type>(); foreach (var a in eae.args) { argTypes.Add(GetExpType(a)); } return(GetType(caller_type.FindProperty("Index", this).type)); //string methodName = ""; //if(outer is Metadata.Expression.AssignmentExpressionSyntax && ((Metadata.Expression.AssignmentExpressionSyntax)outer).Left == exp) //{ // methodName = "set_Index"; //} //else //{ // methodName = "get_Index"; //} //Metadata.DB_Member member = caller_type.FindMethod(methodName, argTypes, this); //return GetType(member.typeName); } else { Console.Error.WriteLine("无法确定表达式类型 " + exp.GetType().Name); } return(null); }