string GetTypeSpecifier(Definition.TypeSpecifier t) { if (t is Definition.SimpleType) { var t2 = (Definition.SimpleType)t; return(string.Format("{0}{1}", t2.Namespace == null || t2.Namespace == "" ? "" : t2.Namespace + ".", t2.TypeName)); } else if (t is Definition.ArrayType) { var t2 = (Definition.ArrayType)t; return(string.Format("{0}[]", GetTypeSpecifier(t2.BaseType))); } else if (t is Definition.GenericType) { var t2 = (Definition.GenericType)t; return(string.Format("{0}<{1}>", GetTypeSpecifier(t2.OuterType), string.Join(", ", t2.InnerType.ConvertAll(GetTypeSpecifier)))); } else if (t is Definition.NullableType) { var t2 = (Definition.NullableType)t; return(string.Format("Optional<{0}>", GetTypeSpecifier(t2.BaseType))); } else if (t is Definition.GenericTypenameType) { var t2 = (Definition.GenericTypenameType)t; return(t2.Name); } else { throw new NotImplementedException("unknown type " + Enum.GetName(t.GetType(), t)); } }
void Edit(Func <object, Tuple <bool, object> > func, ref Definition.TypeSpecifier arr) { var r = func(arr); if (r.Item2 != null) { arr = (Definition.TypeSpecifier)r.Item2; if (!r.Item1) { return; } } var typeSpecifier = arr; if (typeSpecifier == null) { return; } else if (typeSpecifier is Definition.SimpleType) { return; } else if (typeSpecifier is Definition.ArrayType) { var src = typeSpecifier as Definition.ArrayType; Definition.TypeSpecifier t = src.BaseType; Edit(func, ref t); src.BaseType = (Definition.SimpleType)t; } else if (typeSpecifier is Definition.GenericType) { var src = typeSpecifier as Definition.GenericType; { Definition.TypeSpecifier t = src.OuterType; Edit(func, ref t); src.OuterType = (Definition.SimpleType)t; } for (int i = 0; i < src.InnerType.Count(); i++) { Definition.TypeSpecifier t = src.InnerType[i]; Edit(func, ref t); src.InnerType[i] = t; } } else if (typeSpecifier is Definition.GenericTypenameType) { } else { throw new Exception(); } }
Definition.TypeSpecifier ConvertTypeName(Definition.TypeSpecifier typeSpecifier) { if (typeSpecifier is Definition.SimpleType) { var src = typeSpecifier as Definition.SimpleType; var dst = new Definition.SimpleType(); string srcType = GetTypeString(src.Namespace, src.TypeName); if (typeConverter.ContainsKey(srcType)) { var dstType = typeConverter[srcType]; dst.Namespace = dstType.Item1; dst.TypeName = dstType.Item2; dst.TypeKind = src.TypeKind; } else { dst.TypeName = src.TypeName; dst.Namespace = src.Namespace; dst.TypeKind = src.TypeKind; } return(dst); } else if (typeSpecifier is Definition.ArrayType) { var src = typeSpecifier as Definition.ArrayType; var dst = new Definition.ArrayType(); dst.BaseType = ConvertTypeName(src.BaseType) as Definition.SimpleType; return(dst); } else if (typeSpecifier is Definition.GenericType) { var src = typeSpecifier as Definition.GenericType; var dst = new Definition.GenericType(); dst.OuterType = ConvertTypeName(src.OuterType) as Definition.SimpleType; dst.InnerType = src.InnerType.Select(_ => ConvertTypeName(_)).OfType <Definition.TypeSpecifier>().ToList(); return(dst); } return(typeSpecifier); }