/// <summary> /// This function tells whether one of our base classes implements /// the given method (which turns out, it is valid to have an interface /// implementation in a base /// </summary> bool BaseImplements(TypeSpec iface_type, MethodSpec mi, out MethodSpec base_method) { base_method = null; var base_type = container.BaseType; // // Setup filter with no return type to give better error message // about mismatch at return type when the check bellow rejects them // var parameters = mi.Parameters; MethodSpec close_match = null; while (true) { var candidates = MemberCache.FindMembers(base_type, mi.Name, false); if (candidates == null) { base_method = close_match; return(false); } MethodSpec similar_candidate = null; foreach (var candidate in candidates) { if (candidate.Kind != MemberKind.Method) { continue; } if (candidate.Arity != mi.Arity) { continue; } var candidate_param = ((MethodSpec)candidate).Parameters; if (!TypeSpecComparer.Override.IsEqual(parameters.Types, candidate_param.Types)) { continue; } bool modifiers_match = true; for (int i = 0; i < parameters.Count; ++i) { // // First check exact ref/out match // if ((parameters.FixedParameters[i].ModFlags & Parameter.Modifier.RefOutMask) == (candidate_param.FixedParameters[i].ModFlags & Parameter.Modifier.RefOutMask)) { continue; } modifiers_match = false; // // Different in ref/out only // if ((parameters.FixedParameters[i].ModFlags & Parameter.Modifier.RefOutMask) != (candidate_param.FixedParameters[i].ModFlags & Parameter.Modifier.RefOutMask)) { if (similar_candidate == null) { if (!candidate.IsPublic) { break; } if (!TypeSpecComparer.Override.IsEqual(mi.ReturnType, ((MethodSpec)candidate).ReturnType)) { break; } // It's used for ref/out ambiguity overload check similar_candidate = (MethodSpec)candidate; } continue; } similar_candidate = null; break; } if (!modifiers_match) { continue; } // // From this point the candidate is used for detailed error reporting // because it's very close match to what we are looking for // var m = (MethodSpec)candidate; if (!m.IsPublic) { if (close_match == null) { close_match = m; } continue; } if (!TypeSpecComparer.Override.IsEqual(mi.ReturnType, m.ReturnType)) { if (close_match == null) { close_match = m; } continue; } base_method = m; if (mi.IsGeneric && !Method.CheckImplementingMethodConstraints(container, m, mi)) { return(true); } } if (base_method != null) { if (similar_candidate != null) { Report.SymbolRelatedToPreviousError(similar_candidate); Report.SymbolRelatedToPreviousError(mi); Report.SymbolRelatedToPreviousError(container); Report.Warning(1956, 1, ((MemberCore)base_method.MemberDefinition).Location, "The interface method `{0}' implementation is ambiguous between following methods: `{1}' and `{2}' in type `{3}'", mi.GetSignatureForError(), base_method.GetSignatureForError(), similar_candidate.GetSignatureForError(), container.GetSignatureForError()); } break; } base_type = candidates[0].DeclaringType.BaseType; if (base_type == null) { base_method = close_match; return(false); } } if (!base_method.IsVirtual) { #if STATIC var base_builder = base_method.GetMetaInfo() as MethodBuilder; if (base_builder != null) { // // We can avoid creating a proxy if base_method can be marked 'final virtual'. This can // be done for all methods from compiled assembly // base_builder.__SetAttributes(base_builder.Attributes | MethodAttributes.Virtual | MethodAttributes.Final | MethodAttributes.NewSlot); return(true); } #endif DefineProxy(iface_type, base_method, mi); } return(true); }
/// <summary> /// Verifies that any pending abstract methods or interface methods /// were implemented. /// </summary> public bool VerifyPendingMethods() { int top = pending_implementations.Length; bool errors = false; int i; for (i = 0; i < top; i++) { TypeSpec type = pending_implementations [i].type; bool base_implements_type = type.IsInterface && container.BaseType != null && container.BaseType.ImplementsInterface(type, false); for (int j = 0; j < pending_implementations [i].methods.Count; ++j) { var mi = pending_implementations[i].methods[j]; if (mi == null) { continue; } if (type.IsInterface) { var need_proxy = pending_implementations [i].need_proxy [j]; if (need_proxy != null) { DefineProxy(type, need_proxy, mi); continue; } if (pending_implementations [i].optional) { continue; } MethodSpec candidate = null; if (base_implements_type || BaseImplements(type, mi, out candidate)) { continue; } if (candidate == null) { MethodData md = pending_implementations [i].found [j]; if (md != null) { candidate = md.method.Spec; } } Report.SymbolRelatedToPreviousError(mi); if (candidate != null) { Report.SymbolRelatedToPreviousError(candidate); if (candidate.IsStatic) { Report.Error(736, container.Location, "`{0}' does not implement interface member `{1}' and the best implementing candidate `{2}' is static", container.GetSignatureForError(), mi.GetSignatureForError(), candidate.GetSignatureForError()); } else if ((candidate.Modifiers & Modifiers.PUBLIC) == 0) { Report.Error(737, container.Location, "`{0}' does not implement interface member `{1}' and the best implementing candidate `{2}' is not public", container.GetSignatureForError(), mi.GetSignatureForError(), candidate.GetSignatureForError()); } else { Report.Error(738, container.Location, "`{0}' does not implement interface member `{1}' and the best implementing candidate `{2}' return type `{3}' does not match interface member return type `{4}'", container.GetSignatureForError(), mi.GetSignatureForError(), candidate.GetSignatureForError(), candidate.ReturnType.GetSignatureForError(), mi.ReturnType.GetSignatureForError()); } } else { Report.Error(535, container.Location, "`{0}' does not implement interface member `{1}'", container.GetSignatureForError(), mi.GetSignatureForError()); } } else { Report.SymbolRelatedToPreviousError(mi); Report.Error(534, container.Location, "`{0}' does not implement inherited abstract member `{1}'", container.GetSignatureForError(), mi.GetSignatureForError()); } errors = true; } } return(errors); }
// // Factory method: if there are pending implementation methods, we return a PendingImplementation // object, otherwise we return null. // // Register method implementations are either abstract methods // flagged as such on the base class or interface methods // static public PendingImplementation GetPendingImplementations(TypeDefinition container) { TypeSpec b = container.BaseType; var missing_interfaces = GetMissingInterfaces(container); // // If we are implementing an abstract class, and we are not // ourselves abstract, and there are abstract methods (C# allows // abstract classes that have no abstract methods), then allocate // one slot. // // We also pre-compute the methods. // bool implementing_abstract = ((b != null) && b.IsAbstract && (container.ModFlags & Modifiers.ABSTRACT) == 0); MethodSpec[] abstract_methods = null; if (implementing_abstract) { var am = MemberCache.GetNotImplementedAbstractMethods(b); if (am == null) { implementing_abstract = false; } else { abstract_methods = new MethodSpec[am.Count]; am.CopyTo(abstract_methods, 0); } } int total = missing_interfaces.Length + (implementing_abstract ? 1 : 0); if (total == 0) { return(null); } var pending = new PendingImplementation(container, missing_interfaces, abstract_methods, total); // // check for inherited conflicting methods // foreach (var p in pending.pending_implementations) { // // It can happen for generic interfaces only // if (!p.type.IsGeneric) { continue; } // // CLR does not distinguishes between ref and out // for (int i = 0; i < p.methods.Count; ++i) { MethodSpec compared_method = p.methods[i]; if (compared_method.Parameters.IsEmpty) { continue; } for (int ii = i + 1; ii < p.methods.Count; ++ii) { MethodSpec tested_method = p.methods[ii]; if (compared_method.Name != tested_method.Name) { continue; } if (p.type != tested_method.DeclaringType) { continue; } if (!TypeSpecComparer.Override.IsSame(compared_method.Parameters.Types, tested_method.Parameters.Types)) { continue; } bool exact_match = true; bool ref_only_difference = false; var cp = compared_method.Parameters.FixedParameters; var tp = tested_method.Parameters.FixedParameters; for (int pi = 0; pi < cp.Length; ++pi) { // // First check exact modifiers match // if ((cp[pi].ModFlags & Parameter.Modifier.RefOutMask) == (tp[pi].ModFlags & Parameter.Modifier.RefOutMask)) { continue; } if (((cp[pi].ModFlags | tp[pi].ModFlags) & Parameter.Modifier.RefOutMask) == Parameter.Modifier.RefOutMask) { ref_only_difference = true; continue; } exact_match = false; break; } if (!exact_match || !ref_only_difference) { continue; } pending.Report.SymbolRelatedToPreviousError(compared_method); pending.Report.SymbolRelatedToPreviousError(tested_method); pending.Report.Error(767, container.Location, "Cannot implement interface `{0}' with the specified type parameters because it causes method `{1}' to differ on parameter modifiers only", p.type.GetDefinition().GetSignatureForError(), compared_method.GetSignatureForError()); break; } } } return(pending); }
/// <summary> /// This function tells whether one of our base classes implements /// the given method (which turns out, it is valid to have an interface /// implementation in a base /// </summary> bool BaseImplements(TypeSpec iface_type, MethodSpec mi, out MethodSpec base_method) { base_method = null; bool base_can_implement = true; TypeSpec lookup_type; // // Special handling for properties/indexers which cannot have accessors // implementing an interface found in different types (e.g. current and base) // if (mi.IsAccessor && container.Interfaces != null) { bool new_implementation = false; foreach (var iface in container.Interfaces) { if (TypeSpecComparer.IsEqual(iface, iface_type)) { new_implementation = true; break; } } if (new_implementation) { MemberFilter filter; if (mi.Parameters.Count > 1) { var indexer_params = mi.Name [0] == 'g' ? mi.Parameters : IndexerSpec.CreateParametersFromSetter(mi, mi.Parameters.Count - 1); filter = new MemberFilter(MemberCache.IndexerNameAlias, 0, MemberKind.Indexer, indexer_params, null); } else { var pname = mi.Name.Substring(4); filter = MemberFilter.Property(pname, null); } var prop = MemberCache.FindMember(container.CurrentType, filter, BindingRestriction.DeclaredOnly | BindingRestriction.InstanceOnly); if (prop != null && (prop.Modifiers & Modifiers.NEW) != 0) { base_can_implement = false; } } } if (base_can_implement) { lookup_type = container.BaseType; if (lookup_type.ImplementsInterface(iface_type, false)) { return(true); } } else { lookup_type = container.CurrentType; } // // Setup filter with no return type to give better error message // about mismatch at return type when the check bellow rejects them // var parameters = mi.Parameters; MethodSpec close_match = null; while (true) { var candidates = MemberCache.FindMembers(lookup_type, mi.Name, !base_can_implement); if (candidates == null) { base_method = close_match; return(false); } MethodSpec similar_candidate = null; foreach (var candidate in candidates) { if (candidate.Kind != MemberKind.Method) { continue; } if (candidate.Arity != mi.Arity) { continue; } var candidate_param = ((MethodSpec)candidate).Parameters; if (!TypeSpecComparer.Override.IsEqual(parameters.Types, candidate_param.Types)) { continue; } bool modifiers_match = true; for (int i = 0; i < parameters.Count; ++i) { // // First check exact ref/out match // if ((parameters.FixedParameters[i].ModFlags & Parameter.Modifier.RefOutMask) == (candidate_param.FixedParameters[i].ModFlags & Parameter.Modifier.RefOutMask)) { continue; } modifiers_match = false; // // Different in ref/out only // if ((parameters.FixedParameters[i].ModFlags & Parameter.Modifier.RefOutMask) != (candidate_param.FixedParameters[i].ModFlags & Parameter.Modifier.RefOutMask)) { if (similar_candidate == null) { if (!candidate.IsPublic) { break; } if (!TypeSpecComparer.Override.IsEqual(mi.ReturnType, ((MethodSpec)candidate).ReturnType)) { break; } // It's used for ref/out ambiguity overload check similar_candidate = (MethodSpec)candidate; } continue; } similar_candidate = null; break; } if (!modifiers_match) { continue; } // // From this point the candidate is used for detailed error reporting // because it's very close match to what we are looking for // var m = (MethodSpec)candidate; if (!m.IsPublic) { if (close_match == null) { close_match = m; } continue; } if (!TypeSpecComparer.Override.IsEqual(mi.ReturnType, m.ReturnType)) { if (close_match == null) { close_match = m; } continue; } base_method = m; if (mi.IsGeneric && !Method.CheckImplementingMethodConstraints(container, m, mi)) { return(true); } } if (base_method != null) { if (similar_candidate != null) { Report.SymbolRelatedToPreviousError(similar_candidate); Report.SymbolRelatedToPreviousError(mi); Report.SymbolRelatedToPreviousError(container); Report.Warning(1956, 1, ((MemberCore)base_method.MemberDefinition).Location, "The interface method `{0}' implementation is ambiguous between following methods: `{1}' and `{2}' in type `{3}'", mi.GetSignatureForError(), base_method.GetSignatureForError(), similar_candidate.GetSignatureForError(), container.GetSignatureForError()); } break; } if (!base_can_implement) { return(false); } lookup_type = candidates[0].DeclaringType.BaseType; if (lookup_type == null) { base_method = close_match; return(false); } } if (!base_method.IsVirtual) { #if STATIC var base_builder = base_method.GetMetaInfo() as MethodBuilder; if (base_builder != null) { // // We can avoid creating a proxy if base_method can be marked 'final virtual'. This can // be done for all methods from compiled assembly // base_builder.__SetAttributes(base_builder.Attributes | MethodAttributes.Virtual | MethodAttributes.Final | MethodAttributes.NewSlot); return(true); } #endif DefineProxy(iface_type, base_method, mi); } return(true); }