public override bool Equals(object obj) { if (this == obj) { return(true); } ITypeReference reference = obj as ITypeReference; IMethodReference owner = obj as IMethodReference; IGenericArgument argument = obj as IGenericArgument; if (reference == null) { // If this is not a type reference, it might be an argument // of a generic function. if (argument != null && (owner == null || owner.GenericMethod != null)) { ITypeReference resolvedRef = argument.Resolve() as ITypeReference; if (resolvedRef == null) { return(false); } else { return(AreEqual(this, resolvedRef)); } } else { return(false); } } else { return(AreEqual(this, reference)); } }
private string GetName(bool fullname) { if (tref != null) { string name = tref.Name.Replace(".", "::").Replace("+", "::"); if (!fullname) { string special; if (tref.Namespace == "System" && specialTypeNames.TryGetValue(name, out special)) { name = special; } } ITypeCollection genericArguments = tref.GenericArguments; if (genericArguments.Count > 0) { System.Text.StringBuilder build = new System.Text.StringBuilder(name); build.Append('<'); bool first = true; foreach (IType type1 in genericArguments) { if (first) { first = false; } else { build.Append(", "); } build.Append(new TypeRef(type1).GetNameWithRef(fullname)); } build.Append('>'); name = build.ToString(); } return(fullname?RawNamespace + name:name); } IArrayType type2 = type as IArrayType; if (type2 != null) { string name = type2.Dimensions.Count <= 1?"":", " + type2.Dimensions.Count.ToString(); return("array<" + new TypeRef(type2.ElementType).GetNameWithRef(fullname) + name + ">"); } IPointerType type4 = type as IPointerType; if (type4 != null) { return(new TypeRef(type4.ElementType).GetNameWithRef(fullname) + "*"); } IReferenceType type3 = type as IReferenceType; if (type3 != null) { return(new TypeRef(type3.ElementType).GetNameWithRef(fullname) + "%"); } IOptionalModifier modifier2 = type as IOptionalModifier; if (modifier2 != null) { return(Modify(new TypeRef(modifier2.Modifier), new TypeRef(modifier2.ElementType), fullname, false)); } IRequiredModifier modifier = type as IRequiredModifier; if (modifier != null) { return(Modify(new TypeRef(modifier.Modifier), new TypeRef(modifier.ElementType), fullname, true)); } IGenericParameter parameter = type as IGenericParameter; if (parameter != null) { return(parameter.Name); } IGenericArgument argument = type as IGenericArgument; if (argument != null) { return(new TypeRef(argument.Resolve()).GetNameWithRef(fullname)); } IFunctionPointer fptr = type as IFunctionPointer; if (fptr != null) { System.Text.StringBuilder build = new System.Text.StringBuilder(); build.Append("("); build.Append(new TypeRef(fptr.ReturnType.Type).GetNameWithRef(fullname)); build.Append(" (*)("); bool first = true; foreach (IParameterDeclaration declaration in fptr.Parameters) { if (first) { first = false; } else { build.Append(", "); } build.Append(new TypeRef(declaration.ParameterType).GetNameWithRef(fullname)); } build.Append("))"); return(build.ToString()); } if (type is IValueTypeConstraint) { return("value class"); } else if (type is IDefaultConstructorConstraint) { return("<DefaultConstructorConstraint>"); } return("<不明な型>"); }
/// <summary> /// Use the generic argument providers to specialize a type /// </summary> /// <param name="type">The type to resolve</param> /// <param name="declaringType">The declaring type as a generic argument provider</param> /// <param name="method">The method as a generic argument provider</param> /// <returns></returns> public static IType GetType(IType type, IGenericArgumentProvider declaringType, IGenericArgumentProvider method) { ITypeReference itr = type as ITypeReference; if (itr != null) { if (itr.GenericType == null) { // The parameter is directly a non-generic type reference return(itr); } else { // The parameter is a reference to a generic type. Build an array of types of the // generic arguments by recursion IType[] ita = new IType[itr.GenericArguments.Count]; for (int i = 0; i < ita.Length; i++) { ita[i] = GetType(itr.GenericArguments[i], declaringType, method); } // Build the specialization (the 'instance') of the generic type ITypeReference itr2 = new XTypeInstanceReference(); itr2.GenericType = itr.GenericType; itr2.GenericArguments.AddRange(ita); ((ISettableTypeDeclaration)itr2).Declaration = itr.Resolve(); return(itr2); } } // Check to see if the parameter is an array IArrayType iat = type as IArrayType; if (iat != null) { // The parameter is an array type. Build an array type and recurse to get its element type IArrayType iat2 = new XArrayType(); iat2.ElementType = GetType(iat.ElementType, declaringType, method); iat2.DotNetType = iat.DotNetType; return(iat2); } // Check to see if the parameter is a pointer IPointerType ipt = type as IPointerType; if (ipt != null) { // The parameter is a pointer type. Build a pointer type and recurse to set its element type IPointerType ipt2 = new XPointerType(); ipt2.ElementType = GetType(ipt.ElementType, declaringType, method); return(ipt2); } // Check to see if the parameter is a reference IReferenceType irt = type as IReferenceType; if (irt != null) { // The parameter is a reference type. Build a reference type and recurse // to set its element type IReferenceType irt2 = new XReferenceType(); irt2.ElementType = GetType(irt.ElementType, declaringType, method); return(irt2); } // Check to see if the parameter has an optional modifier IOptionalModifier iom = type as IOptionalModifier; if (iom != null) { // The parameter has an optional modifier. Build an optional modifier type and recurse // to set its element type IOptionalModifier iom2 = new XOptionalModifier(); iom2.Modifier = (ITypeReference)GetType(iom.Modifier, declaringType, method); iom2.ElementType = GetType(iom.ElementType, declaringType, method); return(iom2); } // Check to see if the parameter has an required modifier IRequiredModifier irm = type as IRequiredModifier; if (irm != null) { // The parameter has a required modifier. Build a required modifier type and recurse // to set its element type IRequiredModifier irm2 = new XRequiredModifier(); irm2.Modifier = (ITypeReference)GetType(irm.Modifier, declaringType, method); irm2.ElementType = GetType(irm.ElementType, declaringType, method); return(irm2); } // Deal with generic parameters IGenericParameter igp = type as IGenericParameter; IMethodReference imr; if (igp != null) { itr = igp.Owner as ITypeReference; imr = igp.Owner as IMethodReference; ; if (itr == null && imr == null) { throw new NotSupportedException("A generic parameter must be owned by a method or type"); } if (itr == null) { if (method != null) { // Get the parameter type from the method instance return(method.GenericArguments[igp.Position]); } else { return(igp); } } else { if (declaringType != null) { // Get the parameter type from the declaring type return(declaringType.GenericArguments[igp.Position]); } else { return(igp); } } } // The only thing left is that the parameter is a generic argument IGenericArgument iga = type as IGenericArgument; if (iga == null) { throw new NotSupportedException("Unable to get the parameters type"); } IType it = iga.Resolve(); if (it == null || !(it is IGenericArgument) || (it is IGenericParameter)) { itr = iga.Owner as ITypeReference; imr = iga.Owner as IMethodReference; if (itr == null && imr == null) { throw new NotSupportedException(); } if (itr == null) { if (method != null) { IGenericArgument iga2 = new XGenericArgument(); iga2.Owner = method; iga2.Position = iga.Position; return(iga2); } else { return(iga); } } else { IGenericArgument iga2 = new XGenericArgument(); iga2.Owner = declaringType; iga2.Position = iga.Position; return(iga2); } } // Recurse return(GetType(it, declaringType, method)); }
public string WriteName(LanguageWriter w) { if (tref != null) { string name = tref.Name.Replace(".", "::").Replace("+", "::"); string special; if (tref.Namespace == "System" && specialTypeNames.TryGetValue(name, out special)) { name = special; } name = NameMangling.UnDecorateName(name); w.WriteReference(name, this.FullName, tref); ITypeCollection genericArguments = tref.GenericArguments; if (genericArguments.Count > 0) { w.Write("<"); bool first = true; foreach (IType type1 in genericArguments) { if (first) { first = false; } else { w.Write(", "); } new TypeRef(type1).WriteNameWithRef(w); } w.Write(">"); } return(name); } IArrayType type2 = type as IArrayType; if (type2 != null) { w.WriteKeyword("array"); w.Write("<"); new TypeRef(type2.ElementType).WriteNameWithRef(w); if (type2.Dimensions.Count > 1) { w.Write(", "); w.WriteAsLiteral(type2.Dimensions.Count); } w.Write(">"); } IPointerType type4 = type as IPointerType; if (type4 != null) { new TypeRef(type4.ElementType).WriteNameWithRef(w); w.Write("*"); } IReferenceType type3 = type as IReferenceType; if (type3 != null) { new TypeRef(type3.ElementType).WriteNameWithRef(w); w.Write("%"); } IOptionalModifier modifier2 = type as IOptionalModifier; if (modifier2 != null) { WriteModify(w, new TypeRef(modifier2.Modifier), new TypeRef(modifier2.ElementType), false); } IRequiredModifier modifier = type as IRequiredModifier; if (modifier != null) { WriteModify(w, new TypeRef(modifier.Modifier), new TypeRef(modifier.ElementType), true); } IFunctionPointer fptr = type as IFunctionPointer; if (fptr != null) { w.Write("("); new TypeRef(fptr.ReturnType.Type).WriteNameWithRef(w); w.Write(" (*)("); bool first = true; foreach (IParameterDeclaration declaration in fptr.Parameters) { if (first) { first = false; } else { w.Write(", "); } new TypeRef(declaration.ParameterType).WriteNameWithRef(w); } w.Write("))"); } IGenericParameter parameter = type as IGenericParameter; if (parameter != null) { w.WriteReference(parameter.Name, "/* ジェネリックパラメータ */ " + parameter.Name, null); } IGenericArgument argument = type as IGenericArgument; if (argument != null) { new TypeRef(argument.Resolve()).WriteNameWithRef(w); } if (type is IValueTypeConstraint) { w.WriteKeyword("value class"); } else if (type is IDefaultConstructorConstraint) { w.Write("<DefaultConstructorConstraint>"); } return("<不明な型>"); }