From fb112c9a95b38f9b757ddf03d377951d705b7671 Mon Sep 17 00:00:00 2001 From: Ben Maurer Date: Thu, 11 Mar 2004 21:53:40 +0000 Subject: [PATCH] 2004-03-11 Ben Maurer * class.cs: Create a Constructor.CheckBase method that takes care of all validation type code. The method contains some code that was moved from Define. It also includes new code that checks for duplicate ctors. This fixes bug #55148. svn path=/trunk/mcs/; revision=23935 --- mcs/mcs/ChangeLog | 9 +++++ mcs/mcs/class.cs | 89 ++++++++++++++++++++++++++++++++++------------- 2 files changed, 73 insertions(+), 25 deletions(-) diff --git a/mcs/mcs/ChangeLog b/mcs/mcs/ChangeLog index 9e9c438de2d..4d6b1ae41c7 100755 --- a/mcs/mcs/ChangeLog +++ b/mcs/mcs/ChangeLog @@ -1,3 +1,12 @@ +2004-03-11 Ben Maurer + + * class.cs: Create a Constructor.CheckBase method that + takes care of all validation type code. The method + contains some code that was moved from Define. + + It also includes new code that checks for duplicate ctors. + This fixes bug #55148. + 2004-03-09 Joshua Tauberer * expression.cs (ArrayCreation): Fix: More than 6 nulls in diff --git a/mcs/mcs/class.cs b/mcs/mcs/class.cs index 51cba3849ed..0b2d9484b37 100755 --- a/mcs/mcs/class.cs +++ b/mcs/mcs/class.cs @@ -258,11 +258,8 @@ namespace Mono.CSharp { if (is_static){ have_static_constructor = true; - if (default_static_constructor != null){ - Console.WriteLine ("I have a static constructor already"); - Console.WriteLine (" " + default_static_constructor); + if (default_static_constructor != null) return AdditionResult.MethodExists; - } default_static_constructor = c; } else { @@ -2831,7 +2828,63 @@ namespace Mono.CSharp { (Initializer is ConstructorBaseInitializer) && (Initializer.Arguments == null); } + + protected override bool CheckBase (TypeContainer container) + { + base.CheckBase (container); + + // Check whether arguments were correct. + if (!DoDefineParameters ()) + return false; + + if ((ModFlags & Modifiers.STATIC) != 0) + return true; + + if (container is Struct && ParameterTypes.Length == 0) { + Report.Error (568, Location, + "Structs can not contain explicit parameterless " + + "constructors"); + return false; + } + + // + // Check in our class for dups + // + ArrayList ar = container.InstanceConstructors; + + // I don't think this is possible (there should be + // at least one, us). However, just in case: + if (ar == null) + return true; + + int arLen = ar.Count; + for (int i = 0; i < arLen; i++) { + Constructor m = (Constructor) ar [i]; + + // + // Check for a ctor with identical args + // + if (m == this || m.ConstructorBuilder == null || m.ParameterTypes.Length != ParameterTypes.Length) + continue; + for (int j = ParameterTypes.Length - 1; j >= 0; j --) + if (m.ParameterTypes [j] != ParameterTypes [j]) + goto next; + + Report.Error ( + 111, Location, + "Class `{0}' already contains a definition with the " + + "same return value and parameter types for constructor `{1}'", + container.Name, Name); + return false; + + next : + ; + } + + return true; + } + // // Creates the ConstructorBuilder // @@ -2839,21 +2892,10 @@ namespace Mono.CSharp { { MethodAttributes ca = (MethodAttributes.RTSpecialName | MethodAttributes.SpecialName); - - // Check if arguments were correct. - if (!DoDefineParameters ()) - return false; - - if ((ModFlags & Modifiers.STATIC) != 0){ + + if ((ModFlags & Modifiers.STATIC) != 0) { ca |= MethodAttributes.Static | MethodAttributes.Private; } else { - if (container is Struct && ParameterTypes.Length == 0){ - Report.Error ( - 568, Location, - "Structs can not contain explicit parameterless " + - "constructors"); - return false; - } ca |= MethodAttributes.HideBySig; if ((ModFlags & Modifiers.PUBLIC) != 0) @@ -2870,6 +2912,10 @@ namespace Mono.CSharp { else ca |= MethodAttributes.Private; } + + // Check if arguments were correct. + if (!CheckBase (container)) + return false; ConstructorBuilder = container.TypeBuilder.DefineConstructor ( ca, GetCallingConvention (container is Class), ParameterTypes); @@ -2880,14 +2926,7 @@ namespace Mono.CSharp { // // HACK because System.Reflection.Emit is lame // - if (!TypeManager.RegisterMethod (ConstructorBuilder, ParameterInfo, ParameterTypes)) { - Report.Error ( - 111, Location, - "Class `" +container.Name+ "' already contains a definition with the " + - "same return value and parameter types for constructor `" + Name - + "'"); - return false; - } + TypeManager.RegisterMethod (ConstructorBuilder, ParameterInfo, ParameterTypes); return true; } -- 2.25.1