2 // method.cs: Method based declarations
4 // Authors: Miguel de Icaza (miguel@gnu.org)
5 // Martin Baulig (martin@ximian.com)
6 // Marek Safar (marek.safar@gmail.com)
8 // Dual licensed under the terms of the MIT X11 or GNU GPL
10 // Copyright 2001, 2002, 2003 Ximian, Inc (http://www.ximian.com)
11 // Copyright 2004-2008 Novell, Inc
15 using System.Collections.Generic;
16 using System.Reflection;
17 using System.Reflection.Emit;
18 using System.Runtime.CompilerServices;
19 using System.Runtime.InteropServices;
20 using System.Security;
21 using System.Security.Permissions;
26 using XmlElement = System.Object;
31 using Mono.CompilerServices.SymbolWriter;
33 namespace Mono.CSharp {
35 public abstract class MethodCore : InterfaceMemberBase, IParametersMember
37 protected ParametersCompiled parameters;
38 protected ToplevelBlock block;
39 protected MethodSpec spec;
41 public MethodCore (DeclSpace parent, GenericMethod generic,
42 FullNamedExpression type, Modifiers mod, Modifiers allowed_mod,
43 MemberName name, Attributes attrs, ParametersCompiled parameters)
44 : base (parent, generic, type, mod, allowed_mod, name, attrs)
46 this.parameters = parameters;
50 // Returns the System.Type array for the parameters of this method
52 public TypeSpec [] ParameterTypes {
54 return parameters.Types;
58 public ParametersCompiled ParameterInfo {
64 AParametersCollection IParametersMember.Parameters {
65 get { return parameters; }
68 public ToplevelBlock Block {
78 public CallingConventions CallingConventions {
80 CallingConventions cc = parameters.CallingConvention;
82 if ((ModFlags & Modifiers.STATIC) == 0)
83 cc |= CallingConventions.HasThis;
85 // FIXME: How is `ExplicitThis' used in C#?
91 protected override bool CheckOverrideAgainstBase (MemberSpec base_member)
93 bool res = base.CheckOverrideAgainstBase (base_member);
96 // Check that the permissions are not being changed
98 if (!CheckAccessModifiers (this, base_member)) {
99 Error_CannotChangeAccessModifiers (this, base_member);
106 protected override bool CheckBase ()
108 // Check whether arguments were correct.
109 if (!DefineParameters (parameters))
112 return base.CheckBase ();
116 // Returns a string that represents the signature for this
117 // member which should be used in XML documentation.
119 public override string GetDocCommentName (DeclSpace ds)
121 return DocUtil.GetMethodDocCommentName (this, parameters, ds);
125 // Raised (and passed an XmlElement that contains the comment)
126 // when GenerateDocComment is writing documentation expectedly.
128 // FIXME: with a few effort, it could be done with XmlReader,
129 // that means removal of DOM use.
131 internal override void OnGenerateDocComment (XmlElement el)
133 DocUtil.OnMethodGenerateDocComment (this, el, Report);
137 // Represents header string for documentation comment.
139 public override string DocCommentHeader
144 public override bool EnableOverloadChecks (MemberCore overload)
146 if (overload is MethodCore) {
147 caching_flags |= Flags.MethodOverloadsExist;
151 if (overload is AbstractPropertyEventMethod)
154 return base.EnableOverloadChecks (overload);
157 public MethodSpec Spec {
161 protected override bool VerifyClsCompliance ()
163 if (!base.VerifyClsCompliance ())
166 if (parameters.HasArglist) {
167 Report.Warning (3000, 1, Location, "Methods with variable arguments are not CLS-compliant");
170 if (member_type != null && !member_type.IsCLSCompliant ()) {
171 Report.Warning (3002, 1, Location, "Return type of `{0}' is not CLS-compliant",
172 GetSignatureForError ());
175 parameters.VerifyClsCompliance (this);
180 public interface IGenericMethodDefinition : IMemberDefinition
182 TypeParameterSpec[] TypeParameters { get; }
183 int TypeParametersCount { get; }
185 // MethodInfo MakeGenericMethod (TypeSpec[] targs);
188 public class MethodSpec : MemberSpec, IParametersMember
191 AParametersCollection parameters;
195 TypeParameterSpec[] constraints;
197 public MethodSpec (MemberKind kind, TypeSpec declaringType, IMemberDefinition details, TypeSpec returnType,
198 MethodBase info, AParametersCollection parameters, Modifiers modifiers)
199 : base (kind, declaringType, details, modifiers)
201 this.metaInfo = info;
202 this.parameters = parameters;
203 this.returnType = returnType;
208 public override int Arity {
210 return IsGeneric ? GenericDefinition.TypeParametersCount : 0;
214 public TypeParameterSpec[] Constraints {
216 if (constraints == null && IsGeneric)
217 constraints = GenericDefinition.TypeParameters;
223 public bool IsConstructor {
225 return Kind == MemberKind.Constructor;
229 public IGenericMethodDefinition GenericDefinition {
231 return (IGenericMethodDefinition) definition;
235 public bool IsExtensionMethod {
237 return IsStatic && parameters.HasExtensionMethodType;
241 public bool IsSealed {
243 return (Modifiers & Modifiers.SEALED) != 0;
247 // When is virtual or abstract
248 public bool IsVirtual {
250 return (Modifiers & (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE)) != 0;
254 public bool IsReservedMethod {
256 return Kind == MemberKind.Operator || IsAccessor;
260 TypeSpec IInterfaceMemberSpec.MemberType {
266 public AParametersCollection Parameters {
272 public TypeSpec ReturnType {
278 public TypeSpec[] TypeArguments {
286 public MethodSpec GetGenericMethodDefinition ()
288 if (!IsGeneric && !DeclaringType.IsGeneric)
291 return MemberCache.GetMember (declaringType, this);
294 public MethodBase GetMetaInfo ()
296 if ((state & StateFlags.PendingMetaInflate) != 0) {
297 if (DeclaringType.IsTypeBuilder) {
299 metaInfo = TypeBuilder.GetConstructor (DeclaringType.GetMetaInfo (), (ConstructorInfo) metaInfo);
301 metaInfo = TypeBuilder.GetMethod (DeclaringType.GetMetaInfo (), (MethodInfo) metaInfo);
303 metaInfo = MethodInfo.GetMethodFromHandle (metaInfo.MethodHandle, DeclaringType.GetMetaInfo ().TypeHandle);
306 state &= ~StateFlags.PendingMetaInflate;
309 if ((state & StateFlags.PendingMakeMethod) != 0) {
310 var sre_targs = new Type[targs.Length];
311 for (int i = 0; i < sre_targs.Length; ++i)
312 sre_targs[i] = targs[i].GetMetaInfo ();
314 metaInfo = ((MethodInfo) metaInfo).MakeGenericMethod (sre_targs);
315 state &= ~StateFlags.PendingMakeMethod;
321 public override string GetSignatureForError ()
325 name = DeclaringType.GetSignatureForError () + "." + DeclaringType.Name;
326 } else if (Kind == MemberKind.Operator) {
327 var op = Operator.GetType (Name).Value;
328 if (op == Operator.OpType.Implicit || op == Operator.OpType.Explicit) {
329 name = DeclaringType.GetSignatureForError () + "." + Operator.GetName (op) + " operator " + returnType.GetSignatureForError ();
331 name = DeclaringType.GetSignatureForError () + ".operator " + Operator.GetName (op);
333 } else if (IsAccessor) {
334 int split = Name.IndexOf ('_');
335 name = Name.Substring (split + 1);
336 var postfix = Name.Substring (0, split);
338 var pc = parameters.Count;
339 if (pc > 0 && postfix == "get") {
340 name = "this" + parameters.GetSignatureForError ("[", "]", pc);
341 } else if (pc > 1 && postfix == "set") {
342 name = "this" + parameters.GetSignatureForError ("[", "]", pc - 1);
346 return DeclaringType.GetSignatureForError () + "." + name + "." + postfix;
348 name = base.GetSignatureForError ();
350 name += "<" + TypeManager.CSharpName (targs) + ">";
352 name += "<" + TypeManager.CSharpName (GenericDefinition.TypeParameters) + ">";
355 return name + parameters.GetSignatureForError ();
358 public override MemberSpec InflateMember (TypeParameterInflator inflator)
360 var ms = (MethodSpec) base.InflateMember (inflator);
361 ms.returnType = inflator.Inflate (returnType);
362 ms.parameters = parameters.Inflate (inflator);
364 ms.constraints = TypeParameterSpec.InflateConstraints (inflator, Constraints);
369 public MethodSpec MakeGenericMethod (params TypeSpec[] targs)
372 throw new ArgumentNullException ();
374 // if (generic_intances != null && generic_intances.TryGetValue (targs, out ginstance))
377 //if (generic_intances == null)
378 // generic_intances = new Dictionary<TypeSpec[], Method> (TypeSpecArrayComparer.Default);
380 var inflator = new TypeParameterInflator (DeclaringType, GenericDefinition.TypeParameters, targs);
382 var inflated = (MethodSpec) MemberwiseClone ();
383 inflated.declaringType = inflator.TypeInstance;
384 inflated.returnType = inflator.Inflate (returnType);
385 inflated.parameters = parameters.Inflate (inflator);
386 inflated.targs = targs;
387 inflated.constraints = TypeParameterSpec.InflateConstraints (inflator, constraints ?? GenericDefinition.TypeParameters);
388 inflated.state |= StateFlags.PendingMakeMethod;
390 // if (inflated.parent == null)
391 // inflated.parent = parent;
393 //generic_intances.Add (targs, inflated);
397 public MethodSpec Mutate (TypeParameterMutator mutator)
399 var targs = TypeArguments;
401 targs = mutator.Mutate (targs);
403 var decl = DeclaringType;
404 if (DeclaringType.IsGenericOrParentIsGeneric) {
405 decl = mutator.Mutate (decl);
408 if (targs == TypeArguments && decl == DeclaringType)
411 var ms = (MethodSpec) MemberwiseClone ();
412 if (decl != DeclaringType) {
413 // Gets back MethodInfo in case of metaInfo was inflated
414 ms.metaInfo = MemberCache.GetMember (TypeParameterMutator.GetMemberDeclaringType (DeclaringType), this).metaInfo;
416 ms.declaringType = decl;
417 ms.state |= StateFlags.PendingMetaInflate;
422 ms.state |= StateFlags.PendingMakeMethod;
428 public void SetMetaInfo (MethodInfo info)
430 if (this.metaInfo != null)
431 throw new InternalErrorException ("MetaInfo reset");
433 this.metaInfo = info;
437 public abstract class MethodOrOperator : MethodCore, IMethodData
439 public MethodBuilder MethodBuilder;
440 ReturnParameter return_attributes;
441 Dictionary<SecurityAction, PermissionSet> declarative_security;
442 protected MethodData MethodData;
444 static string[] attribute_targets = new string [] { "method", "return" };
446 protected MethodOrOperator (DeclSpace parent, GenericMethod generic, FullNamedExpression type, Modifiers mod,
447 Modifiers allowed_mod, MemberName name,
448 Attributes attrs, ParametersCompiled parameters)
449 : base (parent, generic, type, mod, allowed_mod, name,
454 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
456 if (a.Target == AttributeTargets.ReturnValue) {
457 if (return_attributes == null)
458 return_attributes = new ReturnParameter (this, MethodBuilder, Location);
460 return_attributes.ApplyAttributeBuilder (a, ctor, cdata, pa);
464 if (a.IsInternalMethodImplAttribute) {
465 is_external_implementation = true;
468 if (a.Type == pa.DllImport) {
469 const Modifiers extern_static = Modifiers.EXTERN | Modifiers.STATIC;
470 if ((ModFlags & extern_static) != extern_static) {
471 Report.Error (601, a.Location, "The DllImport attribute must be specified on a method marked `static' and `extern'");
473 is_external_implementation = true;
476 if (a.IsValidSecurityAttribute ()) {
477 if (declarative_security == null)
478 declarative_security = new Dictionary<SecurityAction, PermissionSet> ();
479 a.ExtractSecurityPermissionSet (declarative_security);
483 if (MethodBuilder != null)
484 MethodBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
487 public override AttributeTargets AttributeTargets {
489 return AttributeTargets.Method;
493 protected override bool CheckForDuplications ()
495 return Parent.MemberCache.CheckExistingMembersOverloads (this, parameters);
498 public virtual EmitContext CreateEmitContext (ILGenerator ig)
500 return new EmitContext (this, ig, MemberType);
503 public override bool Define ()
512 if (this is Operator)
513 kind = MemberKind.Operator;
514 else if (this is Destructor)
515 kind = MemberKind.Destructor;
517 kind = MemberKind.Method;
519 if (IsPartialDefinition) {
520 caching_flags &= ~Flags.Excluded_Undetected;
521 caching_flags |= Flags.Excluded;
523 // Add to member cache only when a partial method implementation has not been found yet
524 if ((caching_flags & Flags.PartialDefinitionExists) == 0) {
525 // MethodBase mb = new PartialMethodDefinitionInfo (this);
527 spec = new MethodSpec (kind, Parent.Definition, this, ReturnType, null, parameters, ModFlags);
528 Parent.MemberCache.AddMember (spec);
534 MethodData = new MethodData (
535 this, ModFlags, flags, this, MethodBuilder, GenericMethod, base_method);
537 if (!MethodData.Define (Parent.PartialContainer, GetFullName (MemberName), Report))
540 MethodBuilder = MethodData.MethodBuilder;
542 spec = new MethodSpec (kind, Parent.Definition, this, ReturnType, MethodBuilder, parameters, ModFlags);
543 if (MemberName.Arity > 0)
544 spec.IsGeneric = true;
546 Parent.MemberCache.AddMember (this, MethodBuilder.Name, spec);
551 protected override void DoMemberTypeIndependentChecks ()
553 base.DoMemberTypeIndependentChecks ();
555 CheckAbstractAndExtern (block != null);
557 if ((ModFlags & Modifiers.PARTIAL) != 0) {
558 for (int i = 0; i < parameters.Count; ++i) {
559 IParameterData p = parameters.FixedParameters [i];
560 if (p.ModFlags == Parameter.Modifier.OUT) {
561 Report.Error (752, Location, "`{0}': A partial method parameters cannot use `out' modifier",
562 GetSignatureForError ());
565 if (p.HasDefaultValue && IsPartialImplementation)
566 ((Parameter) p).Warning_UselessOptionalParameter (Report);
571 protected override void DoMemberTypeDependentChecks ()
573 base.DoMemberTypeDependentChecks ();
575 if (MemberType.IsStatic) {
576 Error_StaticReturnType ();
580 public override void Emit ()
582 if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
583 Compiler.PredefinedAttributes.CompilerGenerated.EmitAttribute (MethodBuilder);
584 if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
585 Compiler.PredefinedAttributes.DebuggerHidden.EmitAttribute (MethodBuilder);
587 if (ReturnType == InternalType.Dynamic) {
588 return_attributes = new ReturnParameter (this, MethodBuilder, Location);
589 Compiler.PredefinedAttributes.Dynamic.EmitAttribute (return_attributes.Builder);
590 } else if (ReturnType.HasDynamicElement) {
591 return_attributes = new ReturnParameter (this, MethodBuilder, Location);
592 Compiler.PredefinedAttributes.Dynamic.EmitAttribute (return_attributes.Builder, ReturnType);
595 if (OptAttributes != null)
596 OptAttributes.Emit ();
598 if (declarative_security != null) {
599 foreach (var de in declarative_security) {
600 MethodBuilder.AddDeclarativeSecurity (de.Key, de.Value);
604 if (MethodData != null)
605 MethodData.Emit (Parent);
613 protected void Error_ConditionalAttributeIsNotValid ()
615 Report.Error (577, Location,
616 "Conditional not valid on `{0}' because it is a constructor, destructor, operator or explicit interface implementation",
617 GetSignatureForError ());
620 public bool IsPartialDefinition {
622 return (ModFlags & Modifiers.PARTIAL) != 0 && Block == null;
626 public bool IsPartialImplementation {
628 return (ModFlags & Modifiers.PARTIAL) != 0 && Block != null;
632 public override string[] ValidAttributeTargets {
634 return attribute_targets;
638 #region IMethodData Members
640 public TypeSpec ReturnType {
646 public MemberName MethodName {
653 /// Returns true if method has conditional attribute and the conditions is not defined (method is excluded).
655 public override string[] ConditionalConditions ()
657 if ((caching_flags & (Flags.Excluded_Undetected | Flags.Excluded)) == 0)
660 if ((ModFlags & Modifiers.PARTIAL) != 0 && (caching_flags & Flags.Excluded) != 0)
661 return new string [0];
663 caching_flags &= ~Flags.Excluded_Undetected;
666 if (base_method == null) {
667 if (OptAttributes == null)
670 Attribute[] attrs = OptAttributes.SearchMulti (Compiler.PredefinedAttributes.Conditional);
674 conditions = new string[attrs.Length];
675 for (int i = 0; i < conditions.Length; ++i)
676 conditions[i] = attrs[i].GetConditionalAttributeValue ();
678 conditions = base_method.MemberDefinition.ConditionalConditions();
681 if (conditions != null)
682 caching_flags |= Flags.Excluded;
687 GenericMethod IMethodData.GenericMethod {
689 return GenericMethod;
693 public virtual void EmitExtraSymbolInfo (SourceMethod source)
700 public class SourceMethod : IMethodDef
703 SourceMethodBuilder builder;
705 protected SourceMethod (DeclSpace parent, MethodBase method, ICompileUnit file)
707 this.method = method;
709 builder = SymbolWriter.OpenMethod (file, parent.NamespaceEntry.SymbolFileID, this);
713 get { return method.Name; }
718 if (method is MethodBuilder)
719 return ((MethodBuilder) method).GetToken ().Token;
720 else if (method is ConstructorBuilder)
721 return ((ConstructorBuilder) method).GetToken ().Token;
723 throw new NotSupportedException ();
727 public void CloseMethod ()
729 SymbolWriter.CloseMethod ();
732 public void SetRealMethodName (string name)
735 builder.SetRealMethodName (name);
738 public static SourceMethod Create (DeclSpace parent, MethodBase method, Block block)
740 if (!SymbolWriter.HasSymbolWriter)
745 Location start_loc = block.StartLocation;
746 if (start_loc.IsNull)
749 ICompileUnit compile_unit = start_loc.CompilationUnit;
750 if (compile_unit == null)
753 return new SourceMethod (parent, method, compile_unit);
757 public class Method : MethodOrOperator, IGenericMethodDefinition
759 Method partialMethodImplementation;
761 public Method (DeclSpace parent, GenericMethod generic,
762 FullNamedExpression return_type, Modifiers mod,
763 MemberName name, ParametersCompiled parameters, Attributes attrs)
764 : base (parent, generic, return_type, mod,
765 parent.PartialContainer.Kind == MemberKind.Interface ? AllowedModifiersInterface :
766 parent.PartialContainer.Kind == MemberKind.Struct ? AllowedModifiersStruct :
767 AllowedModifiersClass,
768 name, attrs, parameters)
772 protected Method (DeclSpace parent, FullNamedExpression return_type, Modifiers mod, Modifiers amod,
773 MemberName name, ParametersCompiled parameters, Attributes attrs)
774 : base (parent, null, return_type, mod, amod, name, attrs, parameters)
780 public override TypeParameter[] CurrentTypeParameters {
782 if (GenericMethod != null)
783 return GenericMethod.CurrentTypeParameters;
789 public override bool HasUnresolvedConstraints {
791 if (CurrentTypeParameters == null)
794 // When overriding base method constraints are fetched from
795 // base method but to find it we have to resolve parameters
796 // to find exact base method match
797 if (IsExplicitImpl || (ModFlags & Modifiers.OVERRIDE) != 0)
798 return base_method == null;
800 // Even for non-override generic method constraints check has to be
801 // delayed after all constraints are resolved
806 public TypeParameterSpec[] TypeParameters {
809 return CurrentTypeParameters.Select (l => l.Type).ToArray ();
813 public int TypeParametersCount {
815 return CurrentTypeParameters == null ? 0 : CurrentTypeParameters.Length;
821 public override string GetSignatureForError()
823 return base.GetSignatureForError () + parameters.GetSignatureForError ();
826 void Error_DuplicateEntryPoint (Method b)
828 Report.Error (17, b.Location,
829 "Program `{0}' has more than one entry point defined: `{1}'",
830 CodeGen.FileName, b.GetSignatureForError ());
835 if (ReturnType != TypeManager.void_type &&
836 ReturnType != TypeManager.int32_type)
839 if (parameters.IsEmpty)
842 if (parameters.Count > 1)
845 var ac = parameters.Types [0] as ArrayContainer;
846 return ac != null && ac.Rank == 1 && ac.Element == TypeManager.string_type &&
847 (parameters[0].ModFlags & ~Parameter.Modifier.PARAMS) == Parameter.Modifier.NONE;
850 public override FullNamedExpression LookupNamespaceOrType (string name, int arity, Location loc, bool ignore_cs0104)
853 TypeParameter[] tp = CurrentTypeParameters;
855 TypeParameter t = TypeParameter.FindTypeParameter (tp, name);
857 return new TypeParameterExpr (t, loc);
861 return base.LookupNamespaceOrType (name, arity, loc, ignore_cs0104);
864 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
866 if (a.Type == pa.Conditional) {
867 if (IsExplicitImpl) {
868 Error_ConditionalAttributeIsNotValid ();
872 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
873 Report.Error (243, Location, "Conditional not valid on `{0}' because it is an override method", GetSignatureForError ());
877 if (ReturnType != TypeManager.void_type) {
878 Report.Error (578, Location, "Conditional not valid on `{0}' because its return type is not void", GetSignatureForError ());
883 Report.Error (582, Location, "Conditional not valid on interface members");
887 if (MethodData.implementing != null) {
888 Report.SymbolRelatedToPreviousError (MethodData.implementing.DeclaringType);
889 Report.Error (629, Location, "Conditional member `{0}' cannot implement interface member `{1}'",
890 GetSignatureForError (), TypeManager.CSharpSignature (MethodData.implementing));
894 for (int i = 0; i < parameters.Count; ++i) {
895 if (parameters.FixedParameters [i].ModFlags == Parameter.Modifier.OUT) {
896 Report.Error (685, Location, "Conditional method `{0}' cannot have an out parameter", GetSignatureForError ());
902 if (a.Type == pa.Extension) {
903 a.Error_MisusedExtensionAttribute ();
907 base.ApplyAttributeBuilder (a, ctor, cdata, pa);
910 protected virtual void DefineTypeParameters ()
912 var tparams = CurrentTypeParameters;
914 TypeParameterSpec[] base_tparams = null;
915 TypeParameterSpec[] base_decl_tparams = TypeParameterSpec.EmptyTypes;
916 TypeSpec[] base_targs = TypeSpec.EmptyTypes;
917 if (((ModFlags & Modifiers.OVERRIDE) != 0 || IsExplicitImpl)) {
918 if (base_method != null) {
919 base_tparams = base_method.GenericDefinition.TypeParameters;
921 if (base_method.DeclaringType.IsGeneric) {
922 base_decl_tparams = base_method.DeclaringType.MemberDefinition.TypeParameters;
923 base_targs = Parent.BaseType.TypeArguments;
926 if (base_method.IsGeneric) {
927 if (base_decl_tparams.Length != 0) {
928 base_decl_tparams = base_decl_tparams.Concat (base_tparams).ToArray ();
929 base_targs = base_targs.Concat (tparams.Select<TypeParameter, TypeSpec> (l => l.Type)).ToArray ();
931 base_decl_tparams = base_tparams;
932 base_targs = tparams.Select (l => l.Type).ToArray ();
935 } else if (MethodData.implementing != null) {
936 base_tparams = MethodData.implementing.GenericDefinition.TypeParameters;
937 if (MethodData.implementing.DeclaringType.IsGeneric) {
938 base_decl_tparams = MethodData.implementing.DeclaringType.MemberDefinition.TypeParameters;
939 foreach (var iface in Parent.CurrentType.Interfaces) {
940 if (iface == MethodData.implementing.DeclaringType) {
941 base_targs = iface.TypeArguments;
949 for (int i = 0; i < tparams.Length; ++i) {
952 if (!tp.ResolveConstraints (this))
956 // Copy base constraints for override/explicit methods
958 if (base_tparams != null) {
959 var base_tparam = base_tparams[i];
960 var local_tparam = tp.Type;
961 local_tparam.SpecialConstraint = base_tparam.SpecialConstraint;
963 var inflator = new TypeParameterInflator (CurrentType, base_decl_tparams, base_targs);
964 base_tparam.InflateConstraints (inflator, local_tparam);
967 // Check all type argument constraints for possible collision
968 // introduced by inflating inherited constraints in this context
972 // class A<T> { virtual void Foo<U> () where U : class, T {} }
973 // class B : A<int> { override void Foo<U> {} }
975 var local_tparam_targs = local_tparam.TypeArguments;
976 if (local_tparam_targs != null) {
977 for (int ii = 0; ii < local_tparam_targs.Length; ++ii) {
978 var ta = local_tparam_targs [ii];
979 if (!ta.IsClass && !ta.IsStruct)
982 if (Constraints.CheckConflictingInheritedConstraint (local_tparam, ta, this, Location)) {
983 local_tparam.ChangeTypeArgumentToBaseType (ii);
991 if (MethodData.implementing != null) {
992 var base_tp = MethodData.implementing.Constraints[i];
993 if (!tp.Type.HasSameConstraintsImplementation (base_tp)) {
994 Report.SymbolRelatedToPreviousError (MethodData.implementing);
995 Report.Error (425, Location,
996 "The constraints for type parameter `{0}' of method `{1}' must match the constraints for type parameter `{2}' of interface method `{3}'. Consider using an explicit interface implementation instead",
997 tp.GetSignatureForError (), GetSignatureForError (), base_tp.GetSignatureForError (), MethodData.implementing.GetSignatureForError ());
1006 public override bool Define ()
1008 if (type_expr.Type == TypeManager.void_type && parameters.IsEmpty && MemberName.Arity == 0 && MemberName.Name == Destructor.MetadataName) {
1009 Report.Warning (465, 1, Location, "Introducing `Finalize' method can interfere with destructor invocation. Did you intend to declare a destructor?");
1012 if (!base.Define ())
1015 if (partialMethodImplementation != null && IsPartialDefinition)
1016 MethodBuilder = partialMethodImplementation.MethodBuilder;
1018 if (RootContext.StdLib && TypeManager.IsSpecialType (ReturnType)) {
1019 Error1599 (Location, ReturnType, Report);
1023 if (CurrentTypeParameters == null) {
1024 if (base_method != null) {
1025 if (parameters.Count == 1 && ParameterTypes[0] == TypeManager.object_type && Name == "Equals")
1026 Parent.PartialContainer.Mark_HasEquals ();
1027 else if (parameters.IsEmpty && Name == "GetHashCode")
1028 Parent.PartialContainer.Mark_HasGetHashCode ();
1032 DefineTypeParameters ();
1035 if (block != null && block.IsIterator) {
1037 // Current method is turned into automatically generated
1038 // wrapper which creates an instance of iterator
1040 Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags, Compiler);
1041 ModFlags |= Modifiers.DEBUGGER_HIDDEN;
1044 if ((ModFlags & Modifiers.STATIC) == 0)
1047 if (parameters.HasExtensionMethodType) {
1048 if (Parent.PartialContainer.IsStatic && !Parent.IsGeneric) {
1049 if (!Parent.IsTopLevel)
1050 Report.Error (1109, Location, "`{0}': Extension methods cannot be defined in a nested class",
1051 GetSignatureForError ());
1053 PredefinedAttribute pa = Compiler.PredefinedAttributes.Extension;
1054 if (!pa.IsDefined) {
1055 Report.Error (1110, Location,
1056 "`{0}': Extension methods cannot be declared without a reference to System.Core.dll assembly. Add the assembly reference or remove `this' modifer from the first parameter",
1057 GetSignatureForError ());
1060 ModFlags |= Modifiers.METHOD_EXTENSION;
1061 Parent.PartialContainer.ModFlags |= Modifiers.METHOD_EXTENSION;
1062 Spec.DeclaringType.SetExtensionMethodContainer ();
1063 CodeGen.Assembly.HasExtensionMethods = true;
1065 Report.Error (1106, Location, "`{0}': Extension methods must be defined in a non-generic static class",
1066 GetSignatureForError ());
1071 // This is used to track the Entry Point,
1073 if (RootContext.NeedsEntryPoint &&
1075 (RootContext.MainClass == null ||
1076 RootContext.MainClass == Parent.TypeBuilder.FullName)){
1077 if (IsEntryPoint ()) {
1079 if (RootContext.EntryPoint == null) {
1080 if (Parent.IsGeneric || MemberName.IsGeneric) {
1081 Report.Warning (402, 4, Location, "`{0}': an entry point cannot be generic or in a generic type",
1082 GetSignatureForError ());
1085 RootContext.EntryPoint = this;
1088 Error_DuplicateEntryPoint (RootContext.EntryPoint);
1089 Error_DuplicateEntryPoint (this);
1092 Report.Warning (28, 4, Location, "`{0}' has the wrong signature to be an entry point",
1093 GetSignatureForError ());
1103 public override void Emit ()
1106 if (IsPartialDefinition) {
1108 // Use partial method implementation builder for partial method declaration attributes
1110 if (partialMethodImplementation != null) {
1111 MethodBuilder = partialMethodImplementation.MethodBuilder;
1117 if ((ModFlags & Modifiers.PARTIAL) != 0 && (caching_flags & Flags.PartialDefinitionExists) == 0) {
1118 Report.Error (759, Location, "A partial method `{0}' implementation is missing a partial method declaration",
1119 GetSignatureForError ());
1122 if (CurrentTypeParameters != null) {
1123 var ge = type_expr as GenericTypeExpr;
1125 ge.CheckConstraints (this);
1127 foreach (Parameter p in parameters.FixedParameters) {
1128 ge = p.TypeExpression as GenericTypeExpr;
1130 ge.CheckConstraints (this);
1133 for (int i = 0; i < CurrentTypeParameters.Length; ++i) {
1134 var tp = CurrentTypeParameters [i];
1135 tp.CheckGenericConstraints ();
1142 if ((ModFlags & Modifiers.METHOD_EXTENSION) != 0)
1143 Compiler.PredefinedAttributes.Extension.EmitAttribute (MethodBuilder);
1145 Console.WriteLine ("Internal compiler error at {0}: exception caught while emitting {1}",
1146 Location, MethodBuilder);
1151 public override bool EnableOverloadChecks (MemberCore overload)
1153 // TODO: It can be deleted when members will be defined in correct order
1154 if (overload is Operator)
1155 return overload.EnableOverloadChecks (this);
1157 if (overload is Indexer)
1160 return base.EnableOverloadChecks (overload);
1163 public static void Error1599 (Location loc, TypeSpec t, Report Report)
1165 Report.Error (1599, loc, "Method or delegate cannot return type `{0}'", TypeManager.CSharpName (t));
1168 protected override bool ResolveMemberType ()
1170 if (GenericMethod != null) {
1171 MethodBuilder = Parent.TypeBuilder.DefineMethod (GetFullName (MemberName), flags);
1172 if (!GenericMethod.Define (this))
1176 return base.ResolveMemberType ();
1179 public void SetPartialDefinition (Method methodDefinition)
1181 caching_flags |= Flags.PartialDefinitionExists;
1182 methodDefinition.partialMethodImplementation = this;
1184 // Ensure we are always using method declaration parameters
1185 for (int i = 0; i < methodDefinition.parameters.Count; ++i ) {
1186 parameters [i].Name = methodDefinition.parameters [i].Name;
1187 parameters [i].DefaultValue = methodDefinition.parameters [i].DefaultValue;
1190 if (methodDefinition.attributes == null)
1193 if (attributes == null) {
1194 attributes = methodDefinition.attributes;
1196 attributes.Attrs.AddRange (methodDefinition.attributes.Attrs);
1201 public abstract class ConstructorInitializer : ExpressionStatement
1203 Arguments argument_list;
1204 MethodSpec base_ctor;
1206 public ConstructorInitializer (Arguments argument_list, Location loc)
1208 this.argument_list = argument_list;
1212 public Arguments Arguments {
1214 return argument_list;
1218 public override Expression CreateExpressionTree (ResolveContext ec)
1220 throw new NotSupportedException ("ET");
1223 protected override Expression DoResolve (ResolveContext ec)
1225 eclass = ExprClass.Value;
1228 var caller_builder = (Constructor) ec.MemberContext;
1230 if (argument_list != null) {
1234 // Spec mandates that constructor initializer will not have `this' access
1236 using (ec.Set (ResolveContext.Options.BaseInitializer)) {
1237 argument_list.Resolve (ec, out dynamic);
1241 ec.Report.Error (1975, loc,
1242 "The constructor call cannot be dynamically dispatched within constructor initializer");
1248 type = ec.CurrentType;
1249 if (this is ConstructorBaseInitializer) {
1250 if (ec.CurrentType.BaseType == null)
1253 type = ec.CurrentType.BaseType;
1254 if (ec.CurrentType.IsStruct) {
1255 ec.Report.Error (522, loc,
1256 "`{0}': Struct constructors cannot call base constructors", caller_builder.GetSignatureForError ());
1261 // It is legal to have "this" initializers that take no arguments
1262 // in structs, they are just no-ops.
1264 // struct D { public D (int a) : this () {}
1266 if (TypeManager.IsStruct (ec.CurrentType) && argument_list == null)
1270 base_ctor = ConstructorLookup (ec, type, ref argument_list, loc);
1272 // TODO MemberCache: Does it work for inflated types ?
1273 if (base_ctor == caller_builder.Spec){
1274 ec.Report.Error (516, loc, "Constructor `{0}' cannot call itself",
1275 caller_builder.GetSignatureForError ());
1281 public override void Emit (EmitContext ec)
1283 // It can be null for static initializers
1284 if (base_ctor == null)
1289 Invocation.EmitCall (ec, new CompilerGeneratedThis (type, loc), base_ctor, argument_list, loc);
1292 public override void EmitStatement (EmitContext ec)
1298 public class ConstructorBaseInitializer : ConstructorInitializer {
1299 public ConstructorBaseInitializer (Arguments argument_list, Location l) :
1300 base (argument_list, l)
1305 class GeneratedBaseInitializer: ConstructorBaseInitializer {
1306 public GeneratedBaseInitializer (Location loc):
1312 public class ConstructorThisInitializer : ConstructorInitializer {
1313 public ConstructorThisInitializer (Arguments argument_list, Location l) :
1314 base (argument_list, l)
1319 public class Constructor : MethodCore, IMethodData {
1320 public ConstructorBuilder ConstructorBuilder;
1321 public ConstructorInitializer Initializer;
1322 Dictionary<SecurityAction, PermissionSet> declarative_security;
1323 bool has_compliant_args;
1326 // Modifiers allowed for a constructor.
1328 public const Modifiers AllowedModifiers =
1330 Modifiers.PROTECTED |
1331 Modifiers.INTERNAL |
1337 static readonly string[] attribute_targets = new string [] { "method" };
1340 // The spec claims that static is not permitted, but
1341 // my very own code has static constructors.
1343 public Constructor (DeclSpace parent, string name, Modifiers mod, Attributes attrs, ParametersCompiled args,
1344 ConstructorInitializer init, Location loc)
1345 : base (parent, null, null, mod, AllowedModifiers,
1346 new MemberName (name, loc), attrs, args)
1351 public bool HasCompliantArgs {
1352 get { return has_compliant_args; }
1355 public override AttributeTargets AttributeTargets {
1356 get { return AttributeTargets.Constructor; }
1360 // Returns true if this is a default constructor
1362 public bool IsDefault ()
1364 if ((ModFlags & Modifiers.STATIC) != 0)
1365 return parameters.IsEmpty;
1367 return parameters.IsEmpty &&
1368 (Initializer is ConstructorBaseInitializer) &&
1369 (Initializer.Arguments == null);
1372 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
1374 if (a.IsValidSecurityAttribute ()) {
1375 if (declarative_security == null) {
1376 declarative_security = new Dictionary<SecurityAction, PermissionSet> ();
1378 a.ExtractSecurityPermissionSet (declarative_security);
1382 if (a.IsInternalMethodImplAttribute) {
1383 is_external_implementation = true;
1386 ConstructorBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
1389 protected override bool CheckBase ()
1391 if ((ModFlags & Modifiers.STATIC) != 0) {
1392 if (!parameters.IsEmpty) {
1393 Report.Error (132, Location, "`{0}': The static constructor must be parameterless",
1394 GetSignatureForError ());
1398 // the rest can be ignored
1402 // Check whether arguments were correct.
1403 if (!DefineParameters (parameters))
1406 if ((caching_flags & Flags.MethodOverloadsExist) != 0)
1407 Parent.MemberCache.CheckExistingMembersOverloads (this, parameters);
1409 if (Parent.PartialContainer.Kind == MemberKind.Struct && parameters.IsEmpty) {
1410 Report.Error (568, Location,
1411 "Structs cannot contain explicit parameterless constructors");
1415 CheckProtectedModifier ();
1421 // Creates the ConstructorBuilder
1423 public override bool Define ()
1425 if (ConstructorBuilder != null)
1428 var ca = MethodAttributes.RTSpecialName | MethodAttributes.SpecialName;
1430 if ((ModFlags & Modifiers.STATIC) != 0) {
1431 ca |= MethodAttributes.Static | MethodAttributes.Private;
1433 ca |= ModifiersExtensions.MethodAttr (ModFlags);
1436 if (!CheckAbstractAndExtern (block != null))
1439 // Check if arguments were correct.
1443 ConstructorBuilder = Parent.TypeBuilder.DefineConstructor (
1444 ca, CallingConventions,
1445 parameters.GetMetaInfo ());
1447 spec = new MethodSpec (MemberKind.Constructor, Parent.Definition, this, TypeManager.void_type, ConstructorBuilder, parameters, ModFlags);
1449 Parent.MemberCache.AddMember (spec);
1451 // It's here only to report an error
1452 if (block != null && block.IsIterator) {
1453 member_type = TypeManager.void_type;
1454 Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags, Compiler);
1463 public override void Emit ()
1465 if (Parent.PartialContainer.IsComImport) {
1466 if (!IsDefault ()) {
1467 Report.Error (669, Location, "`{0}': A class with the ComImport attribute cannot have a user-defined constructor",
1468 Parent.GetSignatureForError ());
1470 ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall);
1473 if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
1474 Compiler.PredefinedAttributes.DebuggerHidden.EmitAttribute (ConstructorBuilder);
1476 if (OptAttributes != null)
1477 OptAttributes.Emit ();
1482 // If we use a "this (...)" constructor initializer, then
1483 // do not emit field initializers, they are initialized in the other constructor
1485 bool emit_field_initializers = ((ModFlags & Modifiers.STATIC) != 0) ||
1486 !(Initializer is ConstructorThisInitializer);
1488 BlockContext bc = new BlockContext (this, block, TypeManager.void_type);
1489 bc.Set (ResolveContext.Options.ConstructorScope);
1491 if (emit_field_initializers)
1492 Parent.PartialContainer.ResolveFieldInitializers (bc);
1494 if (block != null) {
1495 // If this is a non-static `struct' constructor and doesn't have any
1496 // initializer, it must initialize all of the struct's fields.
1497 if ((Parent.PartialContainer.Kind == MemberKind.Struct) &&
1498 ((ModFlags & Modifiers.STATIC) == 0) && (Initializer == null))
1499 block.AddThisVariable (bc, Parent, Location);
1501 if (block != null && (ModFlags & Modifiers.STATIC) == 0){
1502 if (Parent.PartialContainer.Kind == MemberKind.Class && Initializer == null)
1503 Initializer = new GeneratedBaseInitializer (Location);
1505 if (Initializer != null) {
1506 block.AddScopeStatement (new StatementExpression (Initializer));
1511 parameters.ApplyAttributes (this, ConstructorBuilder);
1513 SourceMethod source = SourceMethod.Create (Parent, ConstructorBuilder, block);
1515 if (block != null) {
1516 if (block.Resolve (null, bc, this)) {
1517 EmitContext ec = new EmitContext (this, ConstructorBuilder.GetILGenerator (), bc.ReturnType);
1518 ec.With (EmitContext.Options.ConstructorScope, true);
1520 if (!ec.HasReturnLabel && bc.HasReturnLabel) {
1521 ec.ReturnLabel = bc.ReturnLabel;
1522 ec.HasReturnLabel = true;
1530 source.CloseMethod ();
1532 if (declarative_security != null) {
1533 foreach (var de in declarative_security) {
1534 ConstructorBuilder.AddDeclarativeSecurity (de.Key, de.Value);
1541 protected override MemberSpec FindBaseMember (out MemberSpec bestCandidate)
1543 // Is never override
1544 bestCandidate = null;
1548 public override string GetSignatureForError()
1550 return base.GetSignatureForError () + parameters.GetSignatureForError ();
1553 public override string[] ValidAttributeTargets {
1555 return attribute_targets;
1559 protected override bool VerifyClsCompliance ()
1561 if (!base.VerifyClsCompliance () || !IsExposedFromAssembly ()) {
1565 if (!parameters.IsEmpty && Parent.Definition.IsAttribute) {
1566 foreach (TypeSpec param in parameters.Types) {
1567 if (param.IsArray) {
1573 has_compliant_args = true;
1577 #region IMethodData Members
1579 public MemberName MethodName {
1585 public TypeSpec ReturnType {
1591 public EmitContext CreateEmitContext (ILGenerator ig)
1593 throw new NotImplementedException ();
1596 public bool IsExcluded()
1601 GenericMethod IMethodData.GenericMethod {
1607 void IMethodData.EmitExtraSymbolInfo (SourceMethod source)
1614 /// Interface for MethodData class. Holds links to parent members to avoid member duplication.
1616 public interface IMethodData
1618 CallingConventions CallingConventions { get; }
1619 Location Location { get; }
1620 MemberName MethodName { get; }
1621 TypeSpec ReturnType { get; }
1622 GenericMethod GenericMethod { get; }
1623 ParametersCompiled ParameterInfo { get; }
1624 MethodSpec Spec { get; }
1626 Attributes OptAttributes { get; }
1627 ToplevelBlock Block { get; set; }
1629 EmitContext CreateEmitContext (ILGenerator ig);
1630 string GetSignatureForError ();
1631 void EmitExtraSymbolInfo (SourceMethod source);
1635 // Encapsulates most of the Method's state
1637 public class MethodData {
1638 static FieldInfo methodbuilder_attrs_field;
1639 public readonly IMethodData method;
1641 public readonly GenericMethod GenericMethod;
1644 // Are we implementing an interface ?
1646 public MethodSpec implementing;
1651 protected InterfaceMemberBase member;
1652 protected Modifiers modifiers;
1653 protected MethodAttributes flags;
1654 protected TypeSpec declaring_type;
1655 protected MethodSpec parent_method;
1657 MethodBuilder builder;
1658 public MethodBuilder MethodBuilder {
1664 public TypeSpec DeclaringType {
1666 return declaring_type;
1670 public MethodData (InterfaceMemberBase member,
1671 Modifiers modifiers, MethodAttributes flags, IMethodData method)
1673 this.member = member;
1674 this.modifiers = modifiers;
1677 this.method = method;
1680 public MethodData (InterfaceMemberBase member,
1681 Modifiers modifiers, MethodAttributes flags,
1682 IMethodData method, MethodBuilder builder,
1683 GenericMethod generic, MethodSpec parent_method)
1684 : this (member, modifiers, flags, method)
1686 this.builder = builder;
1687 this.GenericMethod = generic;
1688 this.parent_method = parent_method;
1691 public bool Define (DeclSpace parent, string method_full_name, Report Report)
1693 TypeContainer container = parent.PartialContainer;
1695 PendingImplementation pending = container.PendingImplementations;
1696 if (pending != null){
1697 implementing = pending.IsInterfaceMethod (method.MethodName, member.InterfaceType, this);
1699 if (member.InterfaceType != null){
1700 if (implementing == null){
1701 if (member is PropertyBase) {
1702 Report.Error (550, method.Location, "`{0}' is an accessor not found in interface member `{1}{2}'",
1703 method.GetSignatureForError (), TypeManager.CSharpName (member.InterfaceType),
1704 member.GetSignatureForError ().Substring (member.GetSignatureForError ().LastIndexOf ('.')));
1707 Report.Error (539, method.Location,
1708 "`{0}.{1}' in explicit interface declaration is not a member of interface",
1709 TypeManager.CSharpName (member.InterfaceType), member.ShortName);
1713 if (implementing.IsAccessor && !(method is AbstractPropertyEventMethod)) {
1714 Report.SymbolRelatedToPreviousError (implementing);
1715 Report.Error (683, method.Location, "`{0}' explicit method implementation cannot implement `{1}' because it is an accessor",
1716 member.GetSignatureForError (), TypeManager.CSharpSignature (implementing));
1720 if (implementing != null) {
1721 AbstractPropertyEventMethod prop_method = method as AbstractPropertyEventMethod;
1722 if (prop_method == null) {
1723 if (implementing.IsAccessor) {
1724 Report.SymbolRelatedToPreviousError (implementing);
1725 Report.Error (470, method.Location, "Method `{0}' cannot implement interface accessor `{1}'",
1726 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing));
1728 } else if (implementing.DeclaringType.IsInterface) {
1729 if (!implementing.IsAccessor) {
1730 Report.SymbolRelatedToPreviousError (implementing);
1731 Report.Error (686, method.Location, "Accessor `{0}' cannot implement interface member `{1}' for type `{2}'. Use an explicit interface implementation",
1732 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing), container.GetSignatureForError ());
1734 PropertyBase.PropertyMethod pm = prop_method as PropertyBase.PropertyMethod;
1735 if (pm != null && pm.HasCustomAccessModifier && (pm.ModFlags & Modifiers.PUBLIC) == 0) {
1736 Report.SymbolRelatedToPreviousError (implementing);
1737 Report.Error (277, method.Location, "Accessor `{0}' must be declared public to implement interface member `{1}'",
1738 method.GetSignatureForError (), implementing.GetSignatureForError ());
1747 // For implicit implementations, make sure we are public, for
1748 // explicit implementations, make sure we are private.
1750 if (implementing != null){
1752 // Setting null inside this block will trigger a more
1753 // verbose error reporting for missing interface implementations
1755 // The "candidate" function has been flagged already
1756 // but it wont get cleared
1758 if (member.IsExplicitImpl){
1759 if (method.ParameterInfo.HasParams && !implementing.Parameters.HasParams) {
1760 Report.SymbolRelatedToPreviousError (implementing);
1761 Report.Error (466, method.Location, "`{0}': the explicit interface implementation cannot introduce the params modifier",
1762 method.GetSignatureForError ());
1765 if (implementing.DeclaringType.IsInterface) {
1767 // If this is an interface method implementation,
1768 // check for public accessibility
1770 if ((flags & MethodAttributes.MemberAccessMask) != MethodAttributes.Public)
1772 implementing = null;
1774 } else if ((flags & MethodAttributes.MemberAccessMask) == MethodAttributes.Private){
1775 // We may never be private.
1776 implementing = null;
1778 } else if ((modifiers & Modifiers.OVERRIDE) == 0){
1780 // We may be protected if we're overriding something.
1782 implementing = null;
1787 // Static is not allowed
1789 if ((modifiers & Modifiers.STATIC) != 0){
1790 implementing = null;
1795 // If implementing is still valid, set flags
1797 if (implementing != null){
1799 // When implementing interface methods, set NewSlot
1800 // unless, we are overwriting a method.
1802 if (implementing.DeclaringType.IsInterface){
1803 if ((modifiers & Modifiers.OVERRIDE) == 0)
1804 flags |= MethodAttributes.NewSlot;
1807 flags |= MethodAttributes.Virtual | MethodAttributes.HideBySig;
1809 // Set Final unless we're virtual, abstract or already overriding a method.
1810 if ((modifiers & (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE)) == 0)
1811 flags |= MethodAttributes.Final;
1814 // clear the pending implementation flag (requires explicit methods to be defined first)
1816 parent.PartialContainer.PendingImplementations.ImplementMethod (method.MethodName,
1817 member.InterfaceType, this, member.IsExplicitImpl);
1820 // Update indexer accessor name to match implementing abstract accessor
1822 if (!implementing.DeclaringType.IsInterface && !member.IsExplicitImpl && implementing.IsAccessor)
1823 method_full_name = implementing.MemberDefinition.Name;
1826 DefineMethodBuilder (container, method_full_name, method.ParameterInfo);
1828 if (builder == null)
1831 // if (container.CurrentType != null)
1832 // declaring_type = container.CurrentType;
1834 declaring_type = container.Definition;
1836 if (implementing != null && member.IsExplicitImpl) {
1837 container.TypeBuilder.DefineMethodOverride (builder, (MethodInfo) implementing.GetMetaInfo ());
1845 /// Create the MethodBuilder for the method
1847 void DefineMethodBuilder (TypeContainer container, string method_name, ParametersCompiled param)
1849 var return_type = method.ReturnType.GetMetaInfo ();
1850 var p_types = param.GetMetaInfo ();
1852 if (builder == null) {
1853 builder = container.TypeBuilder.DefineMethod (
1854 method_name, flags, method.CallingConventions,
1855 return_type, p_types);
1860 // Generic method has been already defined to resolve method parameters
1861 // correctly when they use type parameters
1863 builder.SetParameters (p_types);
1864 builder.SetReturnType (return_type);
1865 if (builder.Attributes != flags) {
1867 if (methodbuilder_attrs_field == null)
1868 methodbuilder_attrs_field = typeof (MethodBuilder).GetField ("attrs", BindingFlags.NonPublic | BindingFlags.Instance);
1869 methodbuilder_attrs_field.SetValue (builder, flags);
1871 container.Compiler.Report.RuntimeMissingSupport (method.Location, "Generic method MethodAttributes");
1879 public void Emit (DeclSpace parent)
1881 if (GenericMethod != null)
1882 GenericMethod.EmitAttributes ();
1884 var mc = (IMemberContext) method;
1886 method.ParameterInfo.ApplyAttributes (mc, MethodBuilder);
1888 SourceMethod source = SourceMethod.Create (parent, MethodBuilder, method.Block);
1890 ToplevelBlock block = method.Block;
1891 if (block != null) {
1892 BlockContext bc = new BlockContext (mc, block, method.ReturnType);
1893 if (block.Resolve (null, bc, method)) {
1894 EmitContext ec = method.CreateEmitContext (MethodBuilder.GetILGenerator ());
1895 if (!ec.HasReturnLabel && bc.HasReturnLabel) {
1896 ec.ReturnLabel = bc.ReturnLabel;
1897 ec.HasReturnLabel = true;
1904 if (source != null) {
1905 method.EmitExtraSymbolInfo (source);
1906 source.CloseMethod ();
1911 public class Destructor : MethodOrOperator
1913 const Modifiers AllowedModifiers =
1917 static readonly string[] attribute_targets = new string [] { "method" };
1919 public static readonly string MetadataName = "Finalize";
1921 public Destructor (DeclSpace parent, Modifiers mod, ParametersCompiled parameters, Attributes attrs, Location l)
1922 : base (parent, null, null, mod, AllowedModifiers,
1923 new MemberName (MetadataName, l), attrs, parameters)
1925 ModFlags &= ~Modifiers.PRIVATE;
1926 ModFlags |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
1929 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
1931 if (a.Type == pa.Conditional) {
1932 Error_ConditionalAttributeIsNotValid ();
1936 base.ApplyAttributeBuilder (a, ctor, cdata, pa);
1939 protected override bool CheckBase ()
1941 // Don't check base, destructors have special syntax
1945 public override void Emit()
1947 var base_type = Parent.PartialContainer.BaseType;
1948 if (base_type != null && Block != null) {
1949 var base_dtor = MemberCache.FindMember (base_type,
1950 new MemberFilter (MetadataName, 0, MemberKind.Destructor, null, null), BindingRestriction.InstanceOnly) as MethodSpec;
1952 if (base_dtor == null)
1953 throw new NotImplementedException ();
1955 MethodGroupExpr method_expr = MethodGroupExpr.CreatePredefined (base_dtor, base_type, Location);
1956 method_expr.InstanceExpression = new BaseThis (base_type, Location);
1958 var try_block = new ExplicitBlock (block, block.StartLocation, block.EndLocation);
1959 var finaly_block = new ExplicitBlock (block, Location, Location);
1962 // 0-size arguments to avoid CS0250 error
1963 // TODO: Should use AddScopeStatement or something else which emits correct
1966 finaly_block.AddStatement (new StatementExpression (new Invocation (method_expr, new Arguments (0))));
1968 var tf = new TryFinally (try_block, finaly_block, Location);
1969 block.WrapIntoDestructor (tf, try_block);
1975 public override string GetSignatureForError ()
1977 return Parent.GetSignatureForError () + ".~" + Parent.MemberName.Name + "()";
1980 protected override bool ResolveMemberType ()
1982 member_type = TypeManager.void_type;
1986 public override string[] ValidAttributeTargets {
1988 return attribute_targets;
1993 // Ooouh Martin, templates are missing here.
1994 // When it will be possible move here a lot of child code and template method type.
1995 public abstract class AbstractPropertyEventMethod : MemberCore, IMethodData {
1996 protected MethodData method_data;
1997 protected ToplevelBlock block;
1998 protected Dictionary<SecurityAction, PermissionSet> declarative_security;
2000 protected readonly string prefix;
2002 ReturnParameter return_attributes;
2004 public AbstractPropertyEventMethod (InterfaceMemberBase member, string prefix, Attributes attrs, Location loc)
2005 : base (member.Parent, SetupName (prefix, member, loc), attrs)
2007 this.prefix = prefix;
2010 static MemberName SetupName (string prefix, InterfaceMemberBase member, Location loc)
2012 return new MemberName (member.MemberName.Left, prefix + member.ShortName, loc);
2015 public void UpdateName (InterfaceMemberBase member)
2017 SetMemberName (SetupName (prefix, member, Location));
2020 #region IMethodData Members
2022 public ToplevelBlock Block {
2032 public CallingConventions CallingConventions {
2034 return CallingConventions.Standard;
2038 public EmitContext CreateEmitContext (ILGenerator ig)
2040 return new EmitContext (this, ig, ReturnType);
2043 public bool IsExcluded ()
2048 GenericMethod IMethodData.GenericMethod {
2054 public MemberName MethodName {
2060 public TypeSpec[] ParameterTypes {
2062 return ParameterInfo.Types;
2066 public abstract ParametersCompiled ParameterInfo { get ; }
2067 public abstract TypeSpec ReturnType { get; }
2071 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
2073 if (a.Type == pa.CLSCompliant || a.Type == pa.Obsolete || a.Type == pa.Conditional) {
2074 Report.Error (1667, a.Location,
2075 "Attribute `{0}' is not valid on property or event accessors. It is valid on `{1}' declarations only",
2076 TypeManager.CSharpName (a.Type), a.GetValidTargets ());
2080 if (a.IsValidSecurityAttribute ()) {
2081 if (declarative_security == null)
2082 declarative_security = new Dictionary<SecurityAction, PermissionSet> ();
2083 a.ExtractSecurityPermissionSet (declarative_security);
2087 if (a.Target == AttributeTargets.Method) {
2088 method_data.MethodBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
2092 if (a.Target == AttributeTargets.ReturnValue) {
2093 if (return_attributes == null)
2094 return_attributes = new ReturnParameter (this, method_data.MethodBuilder, Location);
2096 return_attributes.ApplyAttributeBuilder (a, ctor, cdata, pa);
2100 ApplyToExtraTarget (a, ctor, cdata, pa);
2103 protected virtual void ApplyToExtraTarget (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
2105 throw new NotSupportedException ("You forgot to define special attribute target handling");
2108 // It is not supported for the accessors
2109 public sealed override bool Define()
2111 throw new NotSupportedException ();
2114 public virtual void Emit (DeclSpace parent)
2116 method_data.Emit (parent);
2118 if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
2119 Compiler.PredefinedAttributes.CompilerGenerated.EmitAttribute (method_data.MethodBuilder);
2120 if (((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0))
2121 Compiler.PredefinedAttributes.DebuggerHidden.EmitAttribute (method_data.MethodBuilder);
2123 if (ReturnType == InternalType.Dynamic) {
2124 return_attributes = new ReturnParameter (this, method_data.MethodBuilder, Location);
2125 Compiler.PredefinedAttributes.Dynamic.EmitAttribute (return_attributes.Builder);
2126 } else if (ReturnType.HasDynamicElement) {
2127 return_attributes = new ReturnParameter (this, method_data.MethodBuilder, Location);
2128 Compiler.PredefinedAttributes.Dynamic.EmitAttribute (return_attributes.Builder, ReturnType);
2131 if (OptAttributes != null)
2132 OptAttributes.Emit ();
2134 if (declarative_security != null) {
2135 foreach (var de in declarative_security) {
2136 method_data.MethodBuilder.AddDeclarativeSecurity (de.Key, de.Value);
2143 public override bool EnableOverloadChecks (MemberCore overload)
2145 if (overload is MethodCore) {
2146 caching_flags |= Flags.MethodOverloadsExist;
2150 // This can only happen with indexers and it will
2151 // be catched as indexer difference
2152 if (overload is AbstractPropertyEventMethod)
2158 public override bool IsClsComplianceRequired()
2163 public MethodSpec Spec { get; protected set; }
2166 // Represents header string for documentation comment.
2168 public override string DocCommentHeader {
2169 get { throw new InvalidOperationException ("Unexpected attempt to get doc comment from " + this.GetType () + "."); }
2172 void IMethodData.EmitExtraSymbolInfo (SourceMethod source)
2176 public class Operator : MethodOrOperator {
2178 const Modifiers AllowedModifiers =
2184 public enum OpType : byte {
2194 // Unary and Binary operators
2217 // Implicit and Explicit
2221 // Just because of enum
2225 public readonly OpType OperatorType;
2227 static readonly string [] [] names;
2231 names = new string[(int)OpType.TOP][];
2232 names [(int) OpType.LogicalNot] = new string [] { "!", "op_LogicalNot" };
2233 names [(int) OpType.OnesComplement] = new string [] { "~", "op_OnesComplement" };
2234 names [(int) OpType.Increment] = new string [] { "++", "op_Increment" };
2235 names [(int) OpType.Decrement] = new string [] { "--", "op_Decrement" };
2236 names [(int) OpType.True] = new string [] { "true", "op_True" };
2237 names [(int) OpType.False] = new string [] { "false", "op_False" };
2238 names [(int) OpType.Addition] = new string [] { "+", "op_Addition" };
2239 names [(int) OpType.Subtraction] = new string [] { "-", "op_Subtraction" };
2240 names [(int) OpType.UnaryPlus] = new string [] { "+", "op_UnaryPlus" };
2241 names [(int) OpType.UnaryNegation] = new string [] { "-", "op_UnaryNegation" };
2242 names [(int) OpType.Multiply] = new string [] { "*", "op_Multiply" };
2243 names [(int) OpType.Division] = new string [] { "/", "op_Division" };
2244 names [(int) OpType.Modulus] = new string [] { "%", "op_Modulus" };
2245 names [(int) OpType.BitwiseAnd] = new string [] { "&", "op_BitwiseAnd" };
2246 names [(int) OpType.BitwiseOr] = new string [] { "|", "op_BitwiseOr" };
2247 names [(int) OpType.ExclusiveOr] = new string [] { "^", "op_ExclusiveOr" };
2248 names [(int) OpType.LeftShift] = new string [] { "<<", "op_LeftShift" };
2249 names [(int) OpType.RightShift] = new string [] { ">>", "op_RightShift" };
2250 names [(int) OpType.Equality] = new string [] { "==", "op_Equality" };
2251 names [(int) OpType.Inequality] = new string [] { "!=", "op_Inequality" };
2252 names [(int) OpType.GreaterThan] = new string [] { ">", "op_GreaterThan" };
2253 names [(int) OpType.LessThan] = new string [] { "<", "op_LessThan" };
2254 names [(int) OpType.GreaterThanOrEqual] = new string [] { ">=", "op_GreaterThanOrEqual" };
2255 names [(int) OpType.LessThanOrEqual] = new string [] { "<=", "op_LessThanOrEqual" };
2256 names [(int) OpType.Implicit] = new string [] { "implicit", "op_Implicit" };
2257 names [(int) OpType.Explicit] = new string [] { "explicit", "op_Explicit" };
2260 public Operator (DeclSpace parent, OpType type, FullNamedExpression ret_type,
2261 Modifiers mod_flags, ParametersCompiled parameters,
2262 ToplevelBlock block, Attributes attrs, Location loc)
2263 : base (parent, null, ret_type, mod_flags, AllowedModifiers,
2264 new MemberName (GetMetadataName (type), loc), attrs, parameters)
2266 OperatorType = type;
2270 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
2272 if (a.Type == pa.Conditional) {
2273 Error_ConditionalAttributeIsNotValid ();
2277 base.ApplyAttributeBuilder (a, ctor, cdata, pa);
2280 public override bool Define ()
2282 const Modifiers RequiredModifiers = Modifiers.PUBLIC | Modifiers.STATIC;
2283 if ((ModFlags & RequiredModifiers) != RequiredModifiers){
2284 Report.Error (558, Location, "User-defined operator `{0}' must be declared static and public", GetSignatureForError ());
2287 if (!base.Define ())
2290 if (block != null && block.IsIterator) {
2292 // Current method is turned into automatically generated
2293 // wrapper which creates an instance of iterator
2295 Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags, Compiler);
2296 ModFlags |= Modifiers.DEBUGGER_HIDDEN;
2299 // imlicit and explicit operator of same types are not allowed
2300 if (OperatorType == OpType.Explicit)
2301 Parent.MemberCache.CheckExistingMembersOverloads (this, GetMetadataName (OpType.Implicit), parameters);
2302 else if (OperatorType == OpType.Implicit)
2303 Parent.MemberCache.CheckExistingMembersOverloads (this, GetMetadataName (OpType.Explicit), parameters);
2305 TypeSpec declaring_type = Parent.CurrentType;
2306 TypeSpec return_type = MemberType;
2307 TypeSpec first_arg_type = ParameterTypes [0];
2309 TypeSpec first_arg_type_unwrap = first_arg_type;
2310 if (TypeManager.IsNullableType (first_arg_type))
2311 first_arg_type_unwrap = TypeManager.GetTypeArguments (first_arg_type) [0];
2313 TypeSpec return_type_unwrap = return_type;
2314 if (TypeManager.IsNullableType (return_type))
2315 return_type_unwrap = TypeManager.GetTypeArguments (return_type) [0];
2318 // Rules for conversion operators
2320 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
2321 if (first_arg_type_unwrap == return_type_unwrap && first_arg_type_unwrap == declaring_type) {
2322 Report.Error (555, Location,
2323 "User-defined operator cannot take an object of the enclosing type and convert to an object of the enclosing type");
2328 if (declaring_type == return_type || declaring_type == return_type_unwrap) {
2329 conv_type = first_arg_type;
2330 } else if (declaring_type == first_arg_type || declaring_type == first_arg_type_unwrap) {
2331 conv_type = return_type;
2333 Report.Error (556, Location,
2334 "User-defined conversion must convert to or from the enclosing type");
2338 if (conv_type == InternalType.Dynamic) {
2339 Report.Error (1964, Location,
2340 "User-defined conversion `{0}' cannot convert to or from the dynamic type",
2341 GetSignatureForError ());
2346 if (conv_type.IsInterface) {
2347 Report.Error (552, Location, "User-defined conversion `{0}' cannot convert to or from an interface type",
2348 GetSignatureForError ());
2352 if (conv_type.IsClass) {
2353 if (TypeSpec.IsBaseClass (declaring_type, conv_type, true)) {
2354 Report.Error (553, Location, "User-defined conversion `{0}' cannot convert to or from a base class",
2355 GetSignatureForError ());
2359 if (TypeSpec.IsBaseClass (conv_type, declaring_type, false)) {
2360 Report.Error (554, Location, "User-defined conversion `{0}' cannot convert to or from a derived class",
2361 GetSignatureForError ());
2365 } else if (OperatorType == OpType.LeftShift || OperatorType == OpType.RightShift) {
2366 if (first_arg_type != declaring_type || parameters.Types[1] != TypeManager.int32_type) {
2367 Report.Error (564, Location, "Overloaded shift operator must have the type of the first operand be the containing type, and the type of the second operand must be int");
2370 } else if (parameters.Count == 1) {
2371 // Checks for Unary operators
2373 if (OperatorType == OpType.Increment || OperatorType == OpType.Decrement) {
2374 if (return_type != declaring_type && !TypeSpec.IsBaseClass (return_type, declaring_type, false)) {
2375 Report.Error (448, Location,
2376 "The return type for ++ or -- operator must be the containing type or derived from the containing type");
2379 if (first_arg_type != declaring_type) {
2381 559, Location, "The parameter type for ++ or -- operator must be the containing type");
2386 if (first_arg_type_unwrap != declaring_type) {
2387 Report.Error (562, Location,
2388 "The parameter type of a unary operator must be the containing type");
2392 if (OperatorType == OpType.True || OperatorType == OpType.False) {
2393 if (return_type != TypeManager.bool_type) {
2396 "The return type of operator True or False " +
2402 } else if (first_arg_type_unwrap != declaring_type) {
2403 // Checks for Binary operators
2405 var second_arg_type = ParameterTypes[1];
2406 if (TypeManager.IsNullableType (second_arg_type))
2407 second_arg_type = TypeManager.GetTypeArguments (second_arg_type)[0];
2409 if (second_arg_type != declaring_type) {
2410 Report.Error (563, Location,
2411 "One of the parameters of a binary operator must be the containing type");
2419 protected override bool ResolveMemberType ()
2421 if (!base.ResolveMemberType ())
2424 flags |= MethodAttributes.SpecialName | MethodAttributes.HideBySig;
2428 protected override MemberSpec FindBaseMember (out MemberSpec bestCandidate)
2430 // Operator cannot be override
2431 bestCandidate = null;
2435 public static string GetName (OpType ot)
2437 return names [(int) ot] [0];
2440 public static string GetName (string metadata_name)
2442 for (int i = 0; i < names.Length; ++i) {
2443 if (names [i] [1] == metadata_name)
2444 return names [i] [0];
2449 public static string GetMetadataName (OpType ot)
2451 return names [(int) ot] [1];
2454 public static string GetMetadataName (string name)
2456 for (int i = 0; i < names.Length; ++i) {
2457 if (names [i] [0] == name)
2458 return names [i] [1];
2463 public static OpType? GetType (string metadata_name)
2465 for (int i = 0; i < names.Length; ++i) {
2466 if (names[i][1] == metadata_name)
2473 public OpType GetMatchingOperator ()
2475 switch (OperatorType) {
2476 case OpType.Equality:
2477 return OpType.Inequality;
2478 case OpType.Inequality:
2479 return OpType.Equality;
2481 return OpType.False;
2484 case OpType.GreaterThan:
2485 return OpType.LessThan;
2486 case OpType.LessThan:
2487 return OpType.GreaterThan;
2488 case OpType.GreaterThanOrEqual:
2489 return OpType.LessThanOrEqual;
2490 case OpType.LessThanOrEqual:
2491 return OpType.GreaterThanOrEqual;
2497 public override string GetSignatureForError ()
2499 StringBuilder sb = new StringBuilder ();
2500 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
2501 sb.AppendFormat ("{0}.{1} operator {2}",
2502 Parent.GetSignatureForError (), GetName (OperatorType), type_expr.GetSignatureForError ());
2505 sb.AppendFormat ("{0}.operator {1}", Parent.GetSignatureForError (), GetName (OperatorType));
2508 sb.Append (parameters.GetSignatureForError ());
2509 return sb.ToString ();