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
12 // Copyright 2011 Xamarin Inc.
16 using System.Collections.Generic;
17 using System.Security;
18 using System.Security.Permissions;
21 using Mono.CompilerServices.SymbolWriter;
22 using System.Runtime.CompilerServices;
25 using XmlElement = System.Object;
31 using MetaType = IKVM.Reflection.Type;
32 using SecurityType = System.Collections.Generic.List<IKVM.Reflection.Emit.CustomAttributeBuilder>;
33 using IKVM.Reflection;
34 using IKVM.Reflection.Emit;
36 using MetaType = System.Type;
37 using SecurityType = System.Collections.Generic.Dictionary<System.Security.Permissions.SecurityAction, System.Security.PermissionSet>;
38 using System.Reflection;
39 using System.Reflection.Emit;
42 namespace Mono.CSharp {
44 public abstract class MethodCore : InterfaceMemberBase, IParametersMember
46 protected ParametersCompiled parameters;
47 protected ToplevelBlock block;
48 protected MethodSpec spec;
50 public MethodCore (TypeDefinition parent, FullNamedExpression type, Modifiers mod, Modifiers allowed_mod,
51 MemberName name, Attributes attrs, ParametersCompiled parameters)
52 : base (parent, type, mod, allowed_mod, name, attrs)
54 this.parameters = parameters;
57 public override Variance ExpectedMemberTypeVariance {
59 return Variance.Covariant;
64 // Returns the System.Type array for the parameters of this method
66 public TypeSpec [] ParameterTypes {
68 return parameters.Types;
72 public ParametersCompiled ParameterInfo {
78 AParametersCollection IParametersMember.Parameters {
79 get { return parameters; }
82 public ToplevelBlock Block {
92 public CallingConventions CallingConventions {
94 CallingConventions cc = parameters.CallingConvention;
96 if ((ModFlags & Modifiers.STATIC) == 0)
97 cc |= CallingConventions.HasThis;
99 // FIXME: How is `ExplicitThis' used in C#?
105 protected override bool CheckOverrideAgainstBase (MemberSpec base_member)
107 bool res = base.CheckOverrideAgainstBase (base_member);
110 // Check that the permissions are not being changed
112 if (!CheckAccessModifiers (this, base_member)) {
113 Error_CannotChangeAccessModifiers (this, base_member);
120 protected override bool CheckBase ()
122 // Check whether arguments were correct.
123 if (!DefineParameters (parameters))
126 return base.CheckBase ();
130 // Represents header string for documentation comment.
132 public override string DocCommentHeader
137 public override void Emit ()
139 if ((ModFlags & Modifiers.COMPILER_GENERATED) == 0) {
140 parameters.CheckConstraints (this);
146 public override bool EnableOverloadChecks (MemberCore overload)
148 if (overload is MethodCore) {
149 caching_flags |= Flags.MethodOverloadsExist;
153 if (overload is AbstractPropertyEventMethod)
156 return base.EnableOverloadChecks (overload);
159 public override string GetSignatureForDocumentation ()
161 string s = base.GetSignatureForDocumentation ();
162 if (MemberName.Arity > 0)
163 s += "``" + MemberName.Arity.ToString ();
165 return s + parameters.GetSignatureForDocumentation ();
168 public MethodSpec Spec {
172 protected override bool VerifyClsCompliance ()
174 if (!base.VerifyClsCompliance ())
177 if (parameters.HasArglist) {
178 Report.Warning (3000, 1, Location, "Methods with variable arguments are not CLS-compliant");
181 if (member_type != null && !member_type.IsCLSCompliant ()) {
182 Report.Warning (3002, 1, Location, "Return type of `{0}' is not CLS-compliant",
183 GetSignatureForError ());
186 parameters.VerifyClsCompliance (this);
191 public interface IGenericMethodDefinition : IMemberDefinition
193 TypeParameterSpec[] TypeParameters { get; }
194 int TypeParametersCount { get; }
196 // MethodInfo MakeGenericMethod (TypeSpec[] targs);
199 public sealed class MethodSpec : MemberSpec, IParametersMember
201 MethodBase metaInfo, inflatedMetaInfo;
202 AParametersCollection parameters;
206 TypeParameterSpec[] constraints;
208 public MethodSpec (MemberKind kind, TypeSpec declaringType, IMemberDefinition details, TypeSpec returnType,
209 MethodBase info, AParametersCollection parameters, Modifiers modifiers)
210 : base (kind, declaringType, details, modifiers)
212 this.metaInfo = info;
213 this.parameters = parameters;
214 this.returnType = returnType;
219 public override int Arity {
221 return IsGeneric ? GenericDefinition.TypeParametersCount : 0;
225 public TypeParameterSpec[] Constraints {
227 if (constraints == null && IsGeneric)
228 constraints = GenericDefinition.TypeParameters;
234 public bool IsConstructor {
236 return Kind == MemberKind.Constructor;
240 public IGenericMethodDefinition GenericDefinition {
242 return (IGenericMethodDefinition) definition;
246 public bool IsExtensionMethod {
248 return IsStatic && parameters.HasExtensionMethodType;
252 public bool IsSealed {
254 return (Modifiers & Modifiers.SEALED) != 0;
258 // When is virtual or abstract
259 public bool IsVirtual {
261 return (Modifiers & (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE)) != 0;
265 public bool IsReservedMethod {
267 return Kind == MemberKind.Operator || IsAccessor;
271 TypeSpec IInterfaceMemberSpec.MemberType {
277 public AParametersCollection Parameters {
283 public TypeSpec ReturnType {
289 public TypeSpec[] TypeArguments {
297 public MethodSpec GetGenericMethodDefinition ()
299 if (!IsGeneric && !DeclaringType.IsGeneric)
302 return MemberCache.GetMember (declaringType, this);
305 public MethodBase GetMetaInfo ()
308 // inflatedMetaInfo is extra field needed for cases where we
309 // inflate method but another nested type can later inflate
310 // again (the cache would be build with inflated metaInfo) and
311 // TypeBuilder can work with method definitions only
313 if (inflatedMetaInfo == null) {
314 if ((state & StateFlags.PendingMetaInflate) != 0) {
315 var dt_meta = DeclaringType.GetMetaInfo ();
317 if (DeclaringType.IsTypeBuilder) {
319 inflatedMetaInfo = TypeBuilder.GetConstructor (dt_meta, (ConstructorInfo) metaInfo);
321 inflatedMetaInfo = TypeBuilder.GetMethod (dt_meta, (MethodInfo) metaInfo);
324 // it should not be reached
325 throw new NotImplementedException ();
327 inflatedMetaInfo = MethodInfo.GetMethodFromHandle (metaInfo.MethodHandle, dt_meta.TypeHandle);
331 state &= ~StateFlags.PendingMetaInflate;
333 inflatedMetaInfo = metaInfo;
337 if ((state & StateFlags.PendingMakeMethod) != 0) {
338 var sre_targs = new MetaType[targs.Length];
339 for (int i = 0; i < sre_targs.Length; ++i)
340 sre_targs[i] = targs[i].GetMetaInfo ();
342 inflatedMetaInfo = ((MethodInfo) inflatedMetaInfo).MakeGenericMethod (sre_targs);
343 state &= ~StateFlags.PendingMakeMethod;
346 return inflatedMetaInfo;
349 public override string GetSignatureForDocumentation ()
353 case MemberKind.Constructor:
356 case MemberKind.Method:
358 name = Name + "``" + Arity.ToString ();
368 name = DeclaringType.GetSignatureForDocumentation () + "." + name + parameters.GetSignatureForDocumentation ();
369 if (Kind == MemberKind.Operator) {
370 var op = Operator.GetType (Name).Value;
371 if (op == Operator.OpType.Explicit || op == Operator.OpType.Implicit) {
372 name += "~" + ReturnType.GetSignatureForDocumentation ();
379 public override string GetSignatureForError ()
383 name = DeclaringType.GetSignatureForError () + "." + DeclaringType.Name;
384 } else if (Kind == MemberKind.Operator) {
385 var op = Operator.GetType (Name).Value;
386 if (op == Operator.OpType.Implicit || op == Operator.OpType.Explicit) {
387 name = DeclaringType.GetSignatureForError () + "." + Operator.GetName (op) + " operator " + returnType.GetSignatureForError ();
389 name = DeclaringType.GetSignatureForError () + ".operator " + Operator.GetName (op);
391 } else if (IsAccessor) {
392 int split = Name.IndexOf ('_');
393 name = Name.Substring (split + 1);
394 var postfix = Name.Substring (0, split);
396 var pc = parameters.Count;
397 if (pc > 0 && postfix == "get") {
398 name = "this" + parameters.GetSignatureForError ("[", "]", pc);
399 } else if (pc > 1 && postfix == "set") {
400 name = "this" + parameters.GetSignatureForError ("[", "]", pc - 1);
404 return DeclaringType.GetSignatureForError () + "." + name + "." + postfix;
406 name = base.GetSignatureForError ();
408 name += "<" + TypeManager.CSharpName (targs) + ">";
410 name += "<" + TypeManager.CSharpName (GenericDefinition.TypeParameters) + ">";
413 return name + parameters.GetSignatureForError ();
416 public override MemberSpec InflateMember (TypeParameterInflator inflator)
418 var ms = (MethodSpec) base.InflateMember (inflator);
419 ms.inflatedMetaInfo = null;
420 ms.returnType = inflator.Inflate (returnType);
421 ms.parameters = parameters.Inflate (inflator);
423 ms.constraints = TypeParameterSpec.InflateConstraints (inflator, Constraints);
428 public MethodSpec MakeGenericMethod (IMemberContext context, params TypeSpec[] targs)
431 throw new ArgumentNullException ();
433 // if (generic_intances != null && generic_intances.TryGetValue (targs, out ginstance))
436 //if (generic_intances == null)
437 // generic_intances = new Dictionary<TypeSpec[], Method> (TypeSpecArrayComparer.Default);
439 var inflator = new TypeParameterInflator (context, DeclaringType, GenericDefinition.TypeParameters, targs);
441 var inflated = (MethodSpec) MemberwiseClone ();
442 inflated.declaringType = inflator.TypeInstance;
443 inflated.returnType = inflator.Inflate (returnType);
444 inflated.parameters = parameters.Inflate (inflator);
445 inflated.targs = targs;
446 inflated.constraints = TypeParameterSpec.InflateConstraints (inflator, constraints ?? GenericDefinition.TypeParameters);
447 inflated.state |= StateFlags.PendingMakeMethod;
449 // if (inflated.parent == null)
450 // inflated.parent = parent;
452 //generic_intances.Add (targs, inflated);
456 public MethodSpec Mutate (TypeParameterMutator mutator)
458 var targs = TypeArguments;
460 targs = mutator.Mutate (targs);
462 var decl = DeclaringType;
463 if (DeclaringType.IsGenericOrParentIsGeneric) {
464 decl = mutator.Mutate (decl);
467 if (targs == TypeArguments && decl == DeclaringType)
470 var ms = (MethodSpec) MemberwiseClone ();
471 if (decl != DeclaringType) {
472 ms.inflatedMetaInfo = null;
473 ms.declaringType = decl;
474 ms.state |= StateFlags.PendingMetaInflate;
479 ms.state |= StateFlags.PendingMakeMethod;
485 public override List<TypeSpec> ResolveMissingDependencies ()
487 var missing = returnType.ResolveMissingDependencies ();
488 foreach (var pt in parameters.Types) {
489 var m = pt.GetMissingDependencies ();
494 missing = new List<TypeSpec> ();
496 missing.AddRange (m);
502 public void SetMetaInfo (MethodInfo info)
504 if (this.metaInfo != null)
505 throw new InternalErrorException ("MetaInfo reset");
507 this.metaInfo = info;
511 public abstract class MethodOrOperator : MethodCore, IMethodData
513 public MethodBuilder MethodBuilder;
514 ReturnParameter return_attributes;
515 SecurityType declarative_security;
516 protected MethodData MethodData;
518 static readonly string[] attribute_targets = new string [] { "method", "return" };
520 protected MethodOrOperator (TypeDefinition parent, FullNamedExpression type, Modifiers mod, Modifiers allowed_mod, MemberName name,
521 Attributes attrs, ParametersCompiled parameters)
522 : base (parent, type, mod, allowed_mod, name, attrs, parameters)
526 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
528 if (a.Target == AttributeTargets.ReturnValue) {
529 if (return_attributes == null)
530 return_attributes = new ReturnParameter (this, MethodBuilder, Location);
532 return_attributes.ApplyAttributeBuilder (a, ctor, cdata, pa);
536 if (a.Type == pa.MethodImpl) {
537 if ((ModFlags & Modifiers.ASYNC) != 0 && (a.GetMethodImplOptions () & MethodImplOptions.Synchronized) != 0) {
538 Report.Error (4015, a.Location, "`{0}': Async methods cannot use `MethodImplOptions.Synchronized'",
539 GetSignatureForError ());
542 is_external_implementation = a.IsInternalCall ();
543 } else if (a.Type == pa.DllImport) {
544 const Modifiers extern_static = Modifiers.EXTERN | Modifiers.STATIC;
545 if ((ModFlags & extern_static) != extern_static) {
546 Report.Error (601, a.Location, "The DllImport attribute must be specified on a method marked `static' and `extern'");
548 is_external_implementation = true;
551 if (a.IsValidSecurityAttribute ()) {
552 a.ExtractSecurityPermissionSet (ctor, ref declarative_security);
556 if (MethodBuilder != null)
557 MethodBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
560 public override AttributeTargets AttributeTargets {
562 return AttributeTargets.Method;
566 protected override bool CheckForDuplications ()
568 return Parent.MemberCache.CheckExistingMembersOverloads (this, parameters);
571 public virtual EmitContext CreateEmitContext (ILGenerator ig, SourceMethodBuilder sourceMethod)
573 return new EmitContext (this, ig, MemberType, sourceMethod);
576 public override bool Define ()
585 if (this is Operator)
586 kind = MemberKind.Operator;
587 else if (this is Destructor)
588 kind = MemberKind.Destructor;
590 kind = MemberKind.Method;
592 if (IsPartialDefinition) {
593 caching_flags &= ~Flags.Excluded_Undetected;
594 caching_flags |= Flags.Excluded;
596 // Add to member cache only when a partial method implementation has not been found yet
597 if ((caching_flags & Flags.PartialDefinitionExists) == 0) {
598 // MethodBase mb = new PartialMethodDefinitionInfo (this);
600 spec = new MethodSpec (kind, Parent.Definition, this, ReturnType, null, parameters, ModFlags);
601 if (MemberName.Arity > 0) {
602 spec.IsGeneric = true;
604 // TODO: Have to move DefineMethod after Define (ideally to Emit)
605 throw new NotImplementedException ("Generic partial methods");
608 Parent.MemberCache.AddMember (spec);
614 MethodData = new MethodData (
615 this, ModFlags, flags, this, MethodBuilder, base_method);
617 if (!MethodData.Define (Parent.PartialContainer, GetFullName (MemberName)))
620 MethodBuilder = MethodData.MethodBuilder;
622 spec = new MethodSpec (kind, Parent.Definition, this, ReturnType, MethodBuilder, parameters, ModFlags);
623 if (MemberName.Arity > 0)
624 spec.IsGeneric = true;
626 Parent.MemberCache.AddMember (this, MethodBuilder.Name, spec);
631 protected override void DoMemberTypeIndependentChecks ()
633 base.DoMemberTypeIndependentChecks ();
635 CheckAbstractAndExtern (block != null);
637 if ((ModFlags & Modifiers.PARTIAL) != 0) {
638 for (int i = 0; i < parameters.Count; ++i) {
639 IParameterData p = parameters.FixedParameters [i];
640 if ((p.ModFlags & Parameter.Modifier.OUT) != 0) {
641 Report.Error (752, Location, "`{0}': A partial method parameters cannot use `out' modifier",
642 GetSignatureForError ());
645 if (p.HasDefaultValue && IsPartialImplementation)
646 ((Parameter) p).Warning_UselessOptionalParameter (Report);
651 protected override void DoMemberTypeDependentChecks ()
653 base.DoMemberTypeDependentChecks ();
655 if (MemberType.IsStatic) {
656 Error_StaticReturnType ();
660 public override void Emit ()
662 if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
663 Module.PredefinedAttributes.CompilerGenerated.EmitAttribute (MethodBuilder);
664 if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
665 Module.PredefinedAttributes.DebuggerHidden.EmitAttribute (MethodBuilder);
667 if (ReturnType.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
668 return_attributes = new ReturnParameter (this, MethodBuilder, Location);
669 Module.PredefinedAttributes.Dynamic.EmitAttribute (return_attributes.Builder);
670 } else if (ReturnType.HasDynamicElement) {
671 return_attributes = new ReturnParameter (this, MethodBuilder, Location);
672 Module.PredefinedAttributes.Dynamic.EmitAttribute (return_attributes.Builder, ReturnType, Location);
675 if (OptAttributes != null)
676 OptAttributes.Emit ();
678 if (declarative_security != null) {
679 foreach (var de in declarative_security) {
681 MethodBuilder.__AddDeclarativeSecurity (de);
683 MethodBuilder.AddDeclarativeSecurity (de.Key, de.Value);
688 if (type_expr != null)
689 ConstraintChecker.Check (this, member_type, type_expr.Location);
693 if (MethodData != null)
694 MethodData.Emit (Parent);
699 protected void Error_ConditionalAttributeIsNotValid ()
701 Report.Error (577, Location,
702 "Conditional not valid on `{0}' because it is a constructor, destructor, operator or explicit interface implementation",
703 GetSignatureForError ());
706 public bool IsPartialDefinition {
708 return (ModFlags & Modifiers.PARTIAL) != 0 && Block == null;
712 public bool IsPartialImplementation {
714 return (ModFlags & Modifiers.PARTIAL) != 0 && Block != null;
718 public override string[] ValidAttributeTargets {
720 return attribute_targets;
724 #region IMethodData Members
726 bool IMethodData.IsAccessor {
732 public TypeSpec ReturnType {
738 public MemberName MethodName {
745 /// Returns true if method has conditional attribute and the conditions is not defined (method is excluded).
747 public override string[] ConditionalConditions ()
749 if ((caching_flags & (Flags.Excluded_Undetected | Flags.Excluded)) == 0)
752 if ((ModFlags & Modifiers.PARTIAL) != 0 && (caching_flags & Flags.Excluded) != 0)
753 return new string [0];
755 caching_flags &= ~Flags.Excluded_Undetected;
758 if (base_method == null) {
759 if (OptAttributes == null)
762 Attribute[] attrs = OptAttributes.SearchMulti (Module.PredefinedAttributes.Conditional);
766 conditions = new string[attrs.Length];
767 for (int i = 0; i < conditions.Length; ++i)
768 conditions[i] = attrs[i].GetConditionalAttributeValue ();
770 conditions = base_method.MemberDefinition.ConditionalConditions();
773 if (conditions != null)
774 caching_flags |= Flags.Excluded;
781 public override void WriteDebugSymbol (MonoSymbolFile file)
783 if (MethodData != null)
784 MethodData.WriteDebugSymbol (file);
788 public class Method : MethodOrOperator, IGenericMethodDefinition
790 Method partialMethodImplementation;
792 public Method (TypeDefinition parent, FullNamedExpression return_type, Modifiers mod, MemberName name, ParametersCompiled parameters, Attributes attrs)
793 : base (parent, return_type, mod,
794 parent.PartialContainer.Kind == MemberKind.Interface ? AllowedModifiersInterface :
795 parent.PartialContainer.Kind == MemberKind.Struct ? AllowedModifiersStruct | Modifiers.ASYNC :
796 AllowedModifiersClass | Modifiers.ASYNC,
797 name, attrs, parameters)
801 protected Method (TypeDefinition parent, FullNamedExpression return_type, Modifiers mod, Modifiers amod,
802 MemberName name, ParametersCompiled parameters, Attributes attrs)
803 : base (parent, return_type, mod, amod, name, attrs, parameters)
809 public override TypeParameters CurrentTypeParameters {
811 return MemberName.TypeParameters;
815 public TypeParameterSpec[] TypeParameters {
817 return CurrentTypeParameters.Types;
821 public int TypeParametersCount {
823 return CurrentTypeParameters == null ? 0 : CurrentTypeParameters.Count;
829 public override void Accept (StructuralVisitor visitor)
831 visitor.Visit (this);
834 public static Method Create (TypeDefinition parent, FullNamedExpression returnType, Modifiers mod,
835 MemberName name, ParametersCompiled parameters, Attributes attrs, bool hasConstraints)
837 var m = new Method (parent, returnType, mod, name, parameters, attrs);
839 if (hasConstraints && ((mod & Modifiers.OVERRIDE) != 0 || m.IsExplicitImpl)) {
840 m.Report.Error (460, m.Location,
841 "`{0}': Cannot specify constraints for overrides and explicit interface implementation methods",
842 m.GetSignatureForError ());
845 if ((mod & Modifiers.PARTIAL) != 0) {
846 const Modifiers invalid_partial_mod = Modifiers.AccessibilityMask | Modifiers.ABSTRACT | Modifiers.EXTERN |
847 Modifiers.NEW | Modifiers.OVERRIDE | Modifiers.SEALED | Modifiers.VIRTUAL;
849 if ((mod & invalid_partial_mod) != 0) {
850 m.Report.Error (750, m.Location,
851 "A partial method cannot define access modifier or any of abstract, extern, new, override, sealed, or virtual modifiers");
852 mod &= ~invalid_partial_mod;
855 if ((parent.ModFlags & Modifiers.PARTIAL) == 0) {
856 m.Report.Error (751, m.Location,
857 "A partial method must be declared within a partial class or partial struct");
861 if ((mod & Modifiers.STATIC) == 0 && parameters.HasExtensionMethodType) {
862 m.Report.Error (1105, m.Location, "`{0}': Extension methods must be declared static",
863 m.GetSignatureForError ());
870 public override string GetSignatureForError()
872 return base.GetSignatureForError () + parameters.GetSignatureForError ();
875 void Error_DuplicateEntryPoint (Method b)
877 Report.Error (17, b.Location,
878 "Program `{0}' has more than one entry point defined: `{1}'",
879 b.Module.Builder.ScopeName, b.GetSignatureForError ());
884 if (ReturnType.Kind != MemberKind.Void && ReturnType.BuiltinType != BuiltinTypeSpec.Type.Int)
887 if (parameters.IsEmpty)
890 if (parameters.Count > 1)
893 var ac = parameters.Types [0] as ArrayContainer;
894 return ac != null && ac.Rank == 1 && ac.Element.BuiltinType == BuiltinTypeSpec.Type.String &&
895 (parameters[0].ModFlags & Parameter.Modifier.RefOutMask) == 0;
898 public override FullNamedExpression LookupNamespaceOrType (string name, int arity, LookupMode mode, Location loc)
901 var tp = CurrentTypeParameters;
903 TypeParameter t = tp.Find (name);
905 return new TypeParameterExpr (t, loc);
909 return base.LookupNamespaceOrType (name, arity, mode, loc);
912 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
914 if (a.Type == pa.Conditional) {
915 if (IsExplicitImpl) {
916 Error_ConditionalAttributeIsNotValid ();
920 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
921 Report.Error (243, Location, "Conditional not valid on `{0}' because it is an override method", GetSignatureForError ());
925 if (ReturnType.Kind != MemberKind.Void) {
926 Report.Error (578, Location, "Conditional not valid on `{0}' because its return type is not void", GetSignatureForError ());
931 Report.Error (582, Location, "Conditional not valid on interface members");
935 if (MethodData.implementing != null) {
936 Report.SymbolRelatedToPreviousError (MethodData.implementing.DeclaringType);
937 Report.Error (629, Location, "Conditional member `{0}' cannot implement interface member `{1}'",
938 GetSignatureForError (), TypeManager.CSharpSignature (MethodData.implementing));
942 for (int i = 0; i < parameters.Count; ++i) {
943 if ((parameters.FixedParameters [i].ModFlags & Parameter.Modifier.OUT) != 0) {
944 Report.Error (685, Location, "Conditional method `{0}' cannot have an out parameter", GetSignatureForError ());
950 if (a.Type == pa.Extension) {
951 a.Error_MisusedExtensionAttribute ();
955 base.ApplyAttributeBuilder (a, ctor, cdata, pa);
958 void CreateTypeParameters ()
960 var tparams = MemberName.TypeParameters;
961 string[] snames = new string[MemberName.Arity];
962 var parent_tparams = Parent.TypeParametersAll;
964 for (int i = 0; i < snames.Length; i++) {
965 string type_argument_name = tparams[i].MemberName.Name;
968 int idx = parameters.GetParameterIndexByName (type_argument_name);
972 b = new ToplevelBlock (Compiler, Location);
974 b.Error_AlreadyDeclaredTypeParameter (type_argument_name, parameters[i].Location);
977 INamedBlockVariable variable = null;
978 block.GetLocalName (type_argument_name, block, ref variable);
979 if (variable != null)
980 variable.Block.Error_AlreadyDeclaredTypeParameter (type_argument_name, variable.Location);
983 if (parent_tparams != null) {
984 var tp = parent_tparams.Find (type_argument_name);
986 tparams[i].WarningParentNameConflict (tp);
990 snames[i] = type_argument_name;
993 GenericTypeParameterBuilder[] gen_params = MethodBuilder.DefineGenericParameters (snames);
994 tparams.Define (gen_params, null, 0, Parent);
997 protected virtual void DefineTypeParameters ()
999 var tparams = CurrentTypeParameters;
1001 TypeParameterSpec[] base_tparams = null;
1002 TypeParameterSpec[] base_decl_tparams = TypeParameterSpec.EmptyTypes;
1003 TypeSpec[] base_targs = TypeSpec.EmptyTypes;
1004 if (((ModFlags & Modifiers.OVERRIDE) != 0 || IsExplicitImpl)) {
1005 if (base_method != null) {
1006 base_tparams = base_method.GenericDefinition.TypeParameters;
1008 if (base_method.DeclaringType.IsGeneric) {
1009 base_decl_tparams = base_method.DeclaringType.MemberDefinition.TypeParameters;
1011 var base_type_parent = CurrentType;
1012 while (base_type_parent.BaseType != base_method.DeclaringType) {
1013 base_type_parent = base_type_parent.BaseType;
1016 base_targs = base_type_parent.BaseType.TypeArguments;
1019 if (base_method.IsGeneric) {
1020 ObsoleteAttribute oa;
1021 foreach (var base_tp in base_tparams) {
1022 oa = base_tp.BaseType.GetAttributeObsolete ();
1024 AttributeTester.Report_ObsoleteMessage (oa, base_tp.BaseType.GetSignatureForError (), Location, Report);
1027 if (base_tp.InterfacesDefined != null) {
1028 foreach (var iface in base_tp.InterfacesDefined) {
1029 oa = iface.GetAttributeObsolete ();
1031 AttributeTester.Report_ObsoleteMessage (oa, iface.GetSignatureForError (), Location, Report);
1037 if (base_decl_tparams.Length != 0) {
1038 base_decl_tparams = base_decl_tparams.Concat (base_tparams).ToArray ();
1039 base_targs = base_targs.Concat (tparams.Types).ToArray ();
1041 base_decl_tparams = base_tparams;
1042 base_targs = tparams.Types;
1045 } else if (MethodData.implementing != null) {
1046 base_tparams = MethodData.implementing.GenericDefinition.TypeParameters;
1047 if (MethodData.implementing.DeclaringType.IsGeneric) {
1048 base_decl_tparams = MethodData.implementing.DeclaringType.MemberDefinition.TypeParameters;
1049 foreach (var iface in Parent.CurrentType.Interfaces) {
1050 if (iface == MethodData.implementing.DeclaringType) {
1051 base_targs = iface.TypeArguments;
1059 for (int i = 0; i < tparams.Count; ++i) {
1060 var tp = tparams[i];
1062 if (!tp.ResolveConstraints (this))
1066 // Copy base constraints for override/explicit methods
1068 if (base_tparams != null) {
1069 var base_tparam = base_tparams[i];
1070 var local_tparam = tp.Type;
1071 local_tparam.SpecialConstraint = base_tparam.SpecialConstraint;
1073 var inflator = new TypeParameterInflator (this, CurrentType, base_decl_tparams, base_targs);
1074 base_tparam.InflateConstraints (inflator, local_tparam);
1077 // Check all type argument constraints for possible collision or unification
1078 // introduced by inflating inherited constraints in this context
1080 // Conflict example:
1082 // class A<T> { virtual void Foo<U> () where U : class, T {} }
1083 // class B : A<int> { override void Foo<U> {} }
1085 var local_tparam_targs = local_tparam.TypeArguments;
1086 if (local_tparam_targs != null) {
1087 for (int ii = 0; ii < local_tparam_targs.Length; ++ii) {
1088 var ta = local_tparam_targs [ii];
1089 if (!ta.IsClass && !ta.IsStruct)
1092 TypeSpec[] unique_tparams = null;
1093 for (int iii = ii + 1; iii < local_tparam_targs.Length; ++iii) {
1095 // Remove any identical or unified constraint types
1097 var tparam_checked = local_tparam_targs[iii];
1098 if (TypeSpecComparer.IsEqual (ta, tparam_checked) || TypeSpec.IsBaseClass (ta, tparam_checked, false)) {
1099 unique_tparams = new TypeSpec[local_tparam_targs.Length - 1];
1100 Array.Copy (local_tparam_targs, 0, unique_tparams, 0, iii);
1101 Array.Copy (local_tparam_targs, iii + 1, unique_tparams, iii, local_tparam_targs.Length - iii - 1);
1102 } else if (!TypeSpec.IsBaseClass (tparam_checked, ta, false)) {
1103 Constraints.Error_ConflictingConstraints (this, local_tparam, ta, tparam_checked, Location);
1107 if (unique_tparams != null) {
1108 local_tparam_targs = unique_tparams;
1109 local_tparam.TypeArguments = local_tparam_targs;
1113 Constraints.CheckConflictingInheritedConstraint (local_tparam, ta, this, Location);
1121 if (base_tparams == null && MethodData != null && MethodData.implementing != null) {
1122 CheckImplementingMethodConstraints (Parent, spec, MethodData.implementing);
1126 public static bool CheckImplementingMethodConstraints (TypeContainer container, MethodSpec method, MethodSpec baseMethod)
1128 var tparams = method.Constraints;
1129 var base_tparams = baseMethod.Constraints;
1130 for (int i = 0; i < tparams.Length; ++i) {
1131 if (!tparams[i].HasSameConstraintsImplementation (base_tparams[i])) {
1132 container.Compiler.Report.SymbolRelatedToPreviousError (method);
1133 container.Compiler.Report.SymbolRelatedToPreviousError (baseMethod);
1135 // Using container location because the interface can be implemented
1137 container.Compiler.Report.Error (425, container.Location,
1138 "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",
1139 tparams[i].GetSignatureForError (), method.GetSignatureForError (),
1140 base_tparams[i].GetSignatureForError (), baseMethod.GetSignatureForError ());
1152 public override bool Define ()
1154 if (!base.Define ())
1157 if (member_type.Kind == MemberKind.Void && parameters.IsEmpty && MemberName.Arity == 0 && MemberName.Name == Destructor.MetadataName) {
1158 Report.Warning (465, 1, Location,
1159 "Introducing `Finalize' method can interfere with destructor invocation. Did you intend to declare a destructor?");
1162 if (partialMethodImplementation != null && IsPartialDefinition)
1163 MethodBuilder = partialMethodImplementation.MethodBuilder;
1165 if (Compiler.Settings.StdLib && ReturnType.IsSpecialRuntimeType) {
1166 Error1599 (Location, ReturnType, Report);
1170 if (CurrentTypeParameters == null) {
1171 if (base_method != null && !IsExplicitImpl) {
1172 if (parameters.Count == 1 && ParameterTypes[0].BuiltinType == BuiltinTypeSpec.Type.Object && MemberName.Name == "Equals")
1173 Parent.PartialContainer.Mark_HasEquals ();
1174 else if (parameters.IsEmpty && MemberName.Name == "GetHashCode")
1175 Parent.PartialContainer.Mark_HasGetHashCode ();
1179 DefineTypeParameters ();
1182 if (block != null) {
1183 if (block.IsIterator) {
1185 // Current method is turned into automatically generated
1186 // wrapper which creates an instance of iterator
1188 Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
1189 ModFlags |= Modifiers.DEBUGGER_HIDDEN;
1192 if ((ModFlags & Modifiers.ASYNC) != 0) {
1193 if (ReturnType.Kind != MemberKind.Void &&
1194 ReturnType != Module.PredefinedTypes.Task.TypeSpec &&
1195 !ReturnType.IsGenericTask) {
1196 Report.Error (1983, Location, "The return type of an async method must be void, Task, or Task<T>");
1199 AsyncInitializer.Create (this, block, parameters, Parent.PartialContainer, ReturnType, Location);
1200 ModFlags |= Modifiers.DEBUGGER_HIDDEN;
1204 if ((ModFlags & Modifiers.STATIC) == 0)
1207 if (parameters.HasExtensionMethodType) {
1208 if (Parent.PartialContainer.IsStatic && !Parent.IsGenericOrParentIsGeneric) {
1209 if (!Parent.IsTopLevel)
1210 Report.Error (1109, Location, "`{0}': Extension methods cannot be defined in a nested class",
1211 GetSignatureForError ());
1213 PredefinedAttribute pa = Module.PredefinedAttributes.Extension;
1214 if (!pa.IsDefined) {
1215 Report.Error (1110, Location,
1216 "`{0}': Extension methods require `System.Runtime.CompilerServices.ExtensionAttribute' type to be available. Are you missing an assembly reference?",
1217 GetSignatureForError ());
1220 ModFlags |= Modifiers.METHOD_EXTENSION;
1221 Parent.PartialContainer.ModFlags |= Modifiers.METHOD_EXTENSION;
1222 Spec.DeclaringType.SetExtensionMethodContainer ();
1223 Parent.Module.HasExtensionMethod = true;
1225 Report.Error (1106, Location, "`{0}': Extension methods must be defined in a non-generic static class",
1226 GetSignatureForError ());
1231 // This is used to track the Entry Point,
1233 var settings = Compiler.Settings;
1234 if (settings.NeedsEntryPoint && MemberName.Name == "Main" && (settings.MainClass == null || settings.MainClass == Parent.TypeBuilder.FullName)) {
1235 if (IsEntryPoint ()) {
1236 if (Parent.DeclaringAssembly.EntryPoint == null) {
1237 if (Parent.IsGenericOrParentIsGeneric || MemberName.IsGeneric) {
1238 Report.Warning (402, 4, Location, "`{0}': an entry point cannot be generic or in a generic type",
1239 GetSignatureForError ());
1240 } else if ((ModFlags & Modifiers.ASYNC) != 0) {
1241 Report.Error (4009, Location, "`{0}': an entry point cannot be async method",
1242 GetSignatureForError ());
1245 Parent.DeclaringAssembly.EntryPoint = this;
1248 Error_DuplicateEntryPoint (Parent.DeclaringAssembly.EntryPoint);
1249 Error_DuplicateEntryPoint (this);
1252 Report.Warning (28, 4, Location, "`{0}' has the wrong signature to be an entry point",
1253 GetSignatureForError ());
1263 public override void Emit ()
1266 if (IsPartialDefinition) {
1268 // Use partial method implementation builder for partial method declaration attributes
1270 if (partialMethodImplementation != null) {
1271 MethodBuilder = partialMethodImplementation.MethodBuilder;
1277 if ((ModFlags & Modifiers.PARTIAL) != 0 && (caching_flags & Flags.PartialDefinitionExists) == 0) {
1278 Report.Error (759, Location, "A partial method `{0}' implementation is missing a partial method declaration",
1279 GetSignatureForError ());
1282 if (CurrentTypeParameters != null) {
1283 for (int i = 0; i < CurrentTypeParameters.Count; ++i) {
1284 var tp = CurrentTypeParameters [i];
1285 tp.CheckGenericConstraints (false);
1292 if ((ModFlags & Modifiers.METHOD_EXTENSION) != 0)
1293 Module.PredefinedAttributes.Extension.EmitAttribute (MethodBuilder);
1295 Console.WriteLine ("Internal compiler error at {0}: exception caught while emitting {1}",
1296 Location, MethodBuilder);
1301 public override bool EnableOverloadChecks (MemberCore overload)
1303 if (overload is Indexer)
1306 return base.EnableOverloadChecks (overload);
1309 public static void Error1599 (Location loc, TypeSpec t, Report Report)
1311 Report.Error (1599, loc, "Method or delegate cannot return type `{0}'", TypeManager.CSharpName (t));
1314 protected override bool ResolveMemberType ()
1316 if (CurrentTypeParameters != null) {
1317 MethodBuilder = Parent.TypeBuilder.DefineMethod (GetFullName (MemberName), flags);
1318 CreateTypeParameters ();
1321 return base.ResolveMemberType ();
1324 public void SetPartialDefinition (Method methodDefinition)
1326 caching_flags |= Flags.PartialDefinitionExists;
1327 methodDefinition.partialMethodImplementation = this;
1329 // Ensure we are always using method declaration parameters
1330 for (int i = 0; i < methodDefinition.parameters.Count; ++i ) {
1331 parameters [i].Name = methodDefinition.parameters [i].Name;
1332 parameters [i].DefaultValue = methodDefinition.parameters [i].DefaultValue;
1335 if (methodDefinition.attributes == null)
1338 if (attributes == null) {
1339 attributes = methodDefinition.attributes;
1341 attributes.Attrs.AddRange (methodDefinition.attributes.Attrs);
1346 public abstract class ConstructorInitializer : ExpressionStatement
1348 Arguments argument_list;
1349 MethodSpec base_ctor;
1351 public ConstructorInitializer (Arguments argument_list, Location loc)
1353 this.argument_list = argument_list;
1357 public Arguments Arguments {
1359 return argument_list;
1363 public override bool ContainsEmitWithAwait ()
1365 throw new NotSupportedException ();
1368 public override Expression CreateExpressionTree (ResolveContext ec)
1370 throw new NotSupportedException ("ET");
1373 protected override Expression DoResolve (ResolveContext ec)
1375 eclass = ExprClass.Value;
1378 var caller_builder = (Constructor) ec.MemberContext;
1381 // Spec mandates that constructor initializer will not have `this' access
1383 using (ec.Set (ResolveContext.Options.BaseInitializer)) {
1384 if (argument_list != null) {
1386 argument_list.Resolve (ec, out dynamic);
1389 ec.Report.Error (1975, loc,
1390 "The constructor call cannot be dynamically dispatched within constructor initializer");
1396 type = ec.CurrentType;
1397 if (this is ConstructorBaseInitializer) {
1398 if (ec.CurrentType.BaseType == null)
1401 type = ec.CurrentType.BaseType;
1402 if (ec.CurrentType.IsStruct) {
1403 ec.Report.Error (522, loc,
1404 "`{0}': Struct constructors cannot call base constructors", caller_builder.GetSignatureForError ());
1409 // It is legal to have "this" initializers that take no arguments
1410 // in structs, they are just no-ops.
1412 // struct D { public D (int a) : this () {}
1414 if (ec.CurrentType.IsStruct && argument_list == null)
1418 base_ctor = ConstructorLookup (ec, type, ref argument_list, loc);
1421 // TODO MemberCache: Does it work for inflated types ?
1422 if (base_ctor == caller_builder.Spec){
1423 ec.Report.Error (516, loc, "Constructor `{0}' cannot call itself",
1424 caller_builder.GetSignatureForError ());
1430 public override void Emit (EmitContext ec)
1432 // It can be null for static initializers
1433 if (base_ctor == null)
1436 var call = new CallEmitter ();
1437 call.InstanceExpression = new CompilerGeneratedThis (type, loc);
1438 call.EmitPredefined (ec, base_ctor, argument_list);
1441 public override void EmitStatement (EmitContext ec)
1447 public class ConstructorBaseInitializer : ConstructorInitializer {
1448 public ConstructorBaseInitializer (Arguments argument_list, Location l) :
1449 base (argument_list, l)
1454 class GeneratedBaseInitializer: ConstructorBaseInitializer {
1455 public GeneratedBaseInitializer (Location loc):
1461 public class ConstructorThisInitializer : ConstructorInitializer {
1462 public ConstructorThisInitializer (Arguments argument_list, Location l) :
1463 base (argument_list, l)
1468 public class Constructor : MethodCore, IMethodData
1470 public ConstructorBuilder ConstructorBuilder;
1471 public ConstructorInitializer Initializer;
1472 SecurityType declarative_security;
1473 bool has_compliant_args;
1474 SourceMethodBuilder debug_builder;
1477 // Modifiers allowed for a constructor.
1479 public const Modifiers AllowedModifiers =
1481 Modifiers.PROTECTED |
1482 Modifiers.INTERNAL |
1488 static readonly string[] attribute_targets = new string [] { "method" };
1490 public static readonly string ConstructorName = ".ctor";
1491 public static readonly string TypeConstructorName = ".cctor";
1493 public Constructor (TypeDefinition parent, string name, Modifiers mod, Attributes attrs, ParametersCompiled args, Location loc)
1494 : base (parent, null, mod, AllowedModifiers, new MemberName (name, loc), attrs, args)
1498 public bool HasCompliantArgs {
1500 return has_compliant_args;
1504 public override AttributeTargets AttributeTargets {
1506 return AttributeTargets.Constructor;
1510 bool IMethodData.IsAccessor {
1517 // Returns true if this is a default constructor
1519 public bool IsDefault ()
1521 if ((ModFlags & Modifiers.STATIC) != 0)
1522 return parameters.IsEmpty;
1524 return parameters.IsEmpty &&
1525 (Initializer is ConstructorBaseInitializer) &&
1526 (Initializer.Arguments == null);
1529 public override void Accept (StructuralVisitor visitor)
1531 visitor.Visit (this);
1534 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
1536 if (a.IsValidSecurityAttribute ()) {
1537 a.ExtractSecurityPermissionSet (ctor, ref declarative_security);
1541 if (a.Type == pa.MethodImpl) {
1542 is_external_implementation = a.IsInternalCall ();
1545 ConstructorBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
1548 protected override bool CheckBase ()
1550 if ((ModFlags & Modifiers.STATIC) != 0) {
1551 if (!parameters.IsEmpty) {
1552 Report.Error (132, Location, "`{0}': The static constructor must be parameterless",
1553 GetSignatureForError ());
1557 if ((caching_flags & Flags.MethodOverloadsExist) != 0)
1558 Parent.MemberCache.CheckExistingMembersOverloads (this, parameters);
1560 // the rest can be ignored
1564 // Check whether arguments were correct.
1565 if (!DefineParameters (parameters))
1568 if ((caching_flags & Flags.MethodOverloadsExist) != 0)
1569 Parent.MemberCache.CheckExistingMembersOverloads (this, parameters);
1571 if (Parent.PartialContainer.Kind == MemberKind.Struct && parameters.IsEmpty) {
1572 Report.Error (568, Location,
1573 "Structs cannot contain explicit parameterless constructors");
1577 CheckProtectedModifier ();
1583 // Creates the ConstructorBuilder
1585 public override bool Define ()
1587 if (ConstructorBuilder != null)
1590 if (!CheckAbstractAndExtern (block != null))
1593 // Check if arguments were correct.
1597 var ca = ModifiersExtensions.MethodAttr (ModFlags) | MethodAttributes.RTSpecialName | MethodAttributes.SpecialName;
1599 ConstructorBuilder = Parent.TypeBuilder.DefineConstructor (
1600 ca, CallingConventions,
1601 parameters.GetMetaInfo ());
1603 spec = new MethodSpec (MemberKind.Constructor, Parent.Definition, this, Compiler.BuiltinTypes.Void, ConstructorBuilder, parameters, ModFlags);
1605 Parent.MemberCache.AddMember (spec);
1607 // It's here only to report an error
1608 if (block != null && block.IsIterator) {
1609 member_type = Compiler.BuiltinTypes.Void;
1610 Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
1619 public override void Emit ()
1621 if (Parent.PartialContainer.IsComImport) {
1622 if (!IsDefault ()) {
1623 Report.Error (669, Location, "`{0}': A class with the ComImport attribute cannot have a user-defined constructor",
1624 Parent.GetSignatureForError ());
1627 // Set as internal implementation and reset block data
1628 // to ensure no IL is generated
1629 ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall);
1633 if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
1634 Module.PredefinedAttributes.DebuggerHidden.EmitAttribute (ConstructorBuilder);
1636 if (OptAttributes != null)
1637 OptAttributes.Emit ();
1640 parameters.ApplyAttributes (this, ConstructorBuilder);
1643 BlockContext bc = new BlockContext (this, block, Compiler.BuiltinTypes.Void);
1644 bc.Set (ResolveContext.Options.ConstructorScope);
1647 // If we use a "this (...)" constructor initializer, then
1648 // do not emit field initializers, they are initialized in the other constructor
1650 if (!(Initializer is ConstructorThisInitializer))
1651 Parent.PartialContainer.ResolveFieldInitializers (bc);
1653 if (block != null) {
1655 if (Initializer == null) {
1656 if (Parent.PartialContainer.Kind == MemberKind.Struct) {
1658 // If this is a non-static `struct' constructor and doesn't have any
1659 // initializer, it must initialize all of the struct's fields.
1661 block.AddThisVariable (bc);
1662 } else if (Parent.PartialContainer.Kind == MemberKind.Class) {
1663 Initializer = new GeneratedBaseInitializer (Location);
1667 if (Initializer != null) {
1669 // mdb format does not support reqions. Try to workaround this by emitting the
1670 // sequence point at initializer. Any breakpoint at constructor header should
1671 // be adjusted to this sequence point as it's the next one which follows.
1673 block.AddScopeStatement (new StatementExpression (Initializer));
1677 if (block.Resolve (null, bc, this)) {
1678 debug_builder = Parent.CreateMethodSymbolEntry ();
1679 EmitContext ec = new EmitContext (this, ConstructorBuilder.GetILGenerator (), bc.ReturnType, debug_builder);
1680 ec.With (EmitContext.Options.ConstructorScope, true);
1686 if (declarative_security != null) {
1687 foreach (var de in declarative_security) {
1689 ConstructorBuilder.__AddDeclarativeSecurity (de);
1691 ConstructorBuilder.AddDeclarativeSecurity (de.Key, de.Value);
1699 protected override MemberSpec FindBaseMember (out MemberSpec bestCandidate, ref bool overrides)
1701 // Is never override
1702 bestCandidate = null;
1706 public override string GetCallerMemberName ()
1708 return IsStatic ? TypeConstructorName : ConstructorName;
1711 public override string GetSignatureForDocumentation ()
1713 return Parent.GetSignatureForDocumentation () + ".#ctor" + parameters.GetSignatureForDocumentation ();
1716 public override string GetSignatureForError()
1718 return base.GetSignatureForError () + parameters.GetSignatureForError ();
1721 public override string[] ValidAttributeTargets {
1723 return attribute_targets;
1727 protected override bool VerifyClsCompliance ()
1729 if (!base.VerifyClsCompliance () || !IsExposedFromAssembly ()) {
1733 if (!parameters.IsEmpty && Parent.Definition.IsAttribute) {
1734 foreach (TypeSpec param in parameters.Types) {
1735 if (param.IsArray) {
1741 has_compliant_args = true;
1745 public override void WriteDebugSymbol (MonoSymbolFile file)
1747 if (debug_builder == null)
1750 var token = ConstructorBuilder.GetToken ();
1751 int t = token.Token;
1753 if (token.IsPseudoToken)
1754 t = Module.Builder.ResolvePseudoToken (t);
1757 debug_builder.DefineMethod (file, t);
1760 #region IMethodData Members
1762 public MemberName MethodName {
1768 public TypeSpec ReturnType {
1774 EmitContext IMethodData.CreateEmitContext (ILGenerator ig, SourceMethodBuilder sourceMethod)
1776 throw new NotImplementedException ();
1783 /// Interface for MethodData class. Holds links to parent members to avoid member duplication.
1785 public interface IMethodData : IMemberContext
1787 CallingConventions CallingConventions { get; }
1788 Location Location { get; }
1789 MemberName MethodName { get; }
1790 TypeSpec ReturnType { get; }
1791 ParametersCompiled ParameterInfo { get; }
1792 MethodSpec Spec { get; }
1793 bool IsAccessor { get; }
1795 Attributes OptAttributes { get; }
1796 ToplevelBlock Block { get; set; }
1798 EmitContext CreateEmitContext (ILGenerator ig, SourceMethodBuilder sourceMethod);
1802 // Encapsulates most of the Method's state
1804 public class MethodData
1807 static FieldInfo methodbuilder_attrs_field;
1810 public readonly IMethodData method;
1813 // Are we implementing an interface ?
1815 public MethodSpec implementing;
1820 protected InterfaceMemberBase member;
1821 protected Modifiers modifiers;
1822 protected MethodAttributes flags;
1823 protected TypeSpec declaring_type;
1824 protected MethodSpec parent_method;
1825 SourceMethodBuilder debug_builder;
1827 MethodBuilder builder;
1828 public MethodBuilder MethodBuilder {
1834 public TypeSpec DeclaringType {
1836 return declaring_type;
1840 public MethodData (InterfaceMemberBase member,
1841 Modifiers modifiers, MethodAttributes flags, IMethodData method)
1843 this.member = member;
1844 this.modifiers = modifiers;
1847 this.method = method;
1850 public MethodData (InterfaceMemberBase member,
1851 Modifiers modifiers, MethodAttributes flags,
1852 IMethodData method, MethodBuilder builder,
1853 MethodSpec parent_method)
1854 : this (member, modifiers, flags, method)
1856 this.builder = builder;
1857 this.parent_method = parent_method;
1860 public bool Define (TypeDefinition container, string method_full_name)
1862 PendingImplementation pending = container.PendingImplementations;
1863 MethodSpec ambig_iface_method;
1864 bool optional = false;
1866 if (pending != null) {
1867 implementing = pending.IsInterfaceMethod (method.MethodName, member.InterfaceType, this, out ambig_iface_method, ref optional);
1869 if (member.InterfaceType != null) {
1870 if (implementing == null) {
1871 if (member is PropertyBase) {
1872 container.Compiler.Report.Error (550, method.Location,
1873 "`{0}' is an accessor not found in interface member `{1}{2}'",
1874 method.GetSignatureForError (), TypeManager.CSharpName (member.InterfaceType),
1875 member.GetSignatureForError ().Substring (member.GetSignatureForError ().LastIndexOf ('.')));
1878 container.Compiler.Report.Error (539, method.Location,
1879 "`{0}.{1}' in explicit interface declaration is not a member of interface",
1880 TypeManager.CSharpName (member.InterfaceType), member.ShortName);
1884 if (implementing.IsAccessor && !method.IsAccessor) {
1885 container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
1886 container.Compiler.Report.Error (683, method.Location,
1887 "`{0}' explicit method implementation cannot implement `{1}' because it is an accessor",
1888 member.GetSignatureForError (), TypeManager.CSharpSignature (implementing));
1892 if (implementing != null) {
1893 if (!method.IsAccessor) {
1894 if (implementing.IsAccessor) {
1895 container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
1896 container.Compiler.Report.Error (470, method.Location,
1897 "Method `{0}' cannot implement interface accessor `{1}'",
1898 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing));
1900 } else if (implementing.DeclaringType.IsInterface) {
1901 if (!implementing.IsAccessor) {
1902 container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
1903 container.Compiler.Report.Error (686, method.Location,
1904 "Accessor `{0}' cannot implement interface member `{1}' for type `{2}'. Use an explicit interface implementation",
1905 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing), container.GetSignatureForError ());
1907 PropertyBase.PropertyMethod pm = method as PropertyBase.PropertyMethod;
1908 if (pm != null && pm.HasCustomAccessModifier && (pm.ModFlags & Modifiers.PUBLIC) == 0) {
1909 container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
1910 container.Compiler.Report.Error (277, method.Location,
1911 "Accessor `{0}' must be declared public to implement interface member `{1}'",
1912 method.GetSignatureForError (), implementing.GetSignatureForError ());
1919 ambig_iface_method = null;
1923 // For implicit implementations, make sure we are public, for
1924 // explicit implementations, make sure we are private.
1926 if (implementing != null){
1927 if (member.IsExplicitImpl) {
1928 if (method.ParameterInfo.HasParams && !implementing.Parameters.HasParams) {
1929 container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
1930 container.Compiler.Report.Error (466, method.Location,
1931 "`{0}': the explicit interface implementation cannot introduce the params modifier",
1932 method.GetSignatureForError ());
1935 if (ambig_iface_method != null) {
1936 container.Compiler.Report.SymbolRelatedToPreviousError (ambig_iface_method);
1937 container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
1938 container.Compiler.Report.Warning (473, 2, method.Location,
1939 "Explicit interface implementation `{0}' matches more than one interface member. Consider using a non-explicit implementation instead",
1940 method.GetSignatureForError ());
1944 // Setting implementin to null inside this block will trigger a more
1945 // verbose error reporting for missing interface implementations
1947 if (implementing.DeclaringType.IsInterface) {
1949 // If this is an interface method implementation,
1950 // check for public accessibility
1952 if ((flags & MethodAttributes.MemberAccessMask) != MethodAttributes.Public) {
1953 implementing = null;
1954 } else if (optional && (container.Interfaces == null || Array.IndexOf (container.Interfaces, implementing.DeclaringType) < 0)) {
1956 // We are not implementing interface when base class already implemented it
1958 implementing = null;
1960 } else if ((flags & MethodAttributes.MemberAccessMask) == MethodAttributes.Private) {
1961 // We may never be private.
1962 implementing = null;
1964 } else if ((modifiers & Modifiers.OVERRIDE) == 0) {
1966 // We may be protected if we're overriding something.
1968 implementing = null;
1973 // Static is not allowed
1975 if ((modifiers & Modifiers.STATIC) != 0){
1976 implementing = null;
1981 // If implementing is still valid, set flags
1983 if (implementing != null){
1985 // When implementing interface methods, set NewSlot
1986 // unless, we are overwriting a method.
1988 if ((modifiers & Modifiers.OVERRIDE) == 0 && implementing.DeclaringType.IsInterface) {
1989 flags |= MethodAttributes.NewSlot;
1992 flags |= MethodAttributes.Virtual | MethodAttributes.HideBySig;
1994 // Set Final unless we're virtual, abstract or already overriding a method.
1995 if ((modifiers & (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE)) == 0)
1996 flags |= MethodAttributes.Final;
1999 // clear the pending implementation flag (requires explicit methods to be defined first)
2001 pending.ImplementMethod (method.MethodName,
2002 member.InterfaceType, this, member.IsExplicitImpl, out ambig_iface_method, ref optional);
2005 // Update indexer accessor name to match implementing abstract accessor
2007 if (!implementing.DeclaringType.IsInterface && !member.IsExplicitImpl && implementing.IsAccessor)
2008 method_full_name = implementing.MemberDefinition.Name;
2011 DefineMethodBuilder (container, method_full_name, method.ParameterInfo);
2013 if (builder == null)
2016 // if (container.CurrentType != null)
2017 // declaring_type = container.CurrentType;
2019 declaring_type = container.Definition;
2021 if (implementing != null && member.IsExplicitImpl) {
2022 container.TypeBuilder.DefineMethodOverride (builder, (MethodInfo) implementing.GetMetaInfo ());
2030 /// Create the MethodBuilder for the method
2032 void DefineMethodBuilder (TypeDefinition container, string method_name, ParametersCompiled param)
2034 var return_type = method.ReturnType.GetMetaInfo ();
2035 var p_types = param.GetMetaInfo ();
2037 if (builder == null) {
2038 builder = container.TypeBuilder.DefineMethod (
2039 method_name, flags, method.CallingConventions,
2040 return_type, p_types);
2045 // Generic method has been already defined to resolve method parameters
2046 // correctly when they use type parameters
2048 builder.SetParameters (p_types);
2049 builder.SetReturnType (return_type);
2050 if (builder.Attributes != flags) {
2052 builder.__SetAttributes (flags);
2055 if (methodbuilder_attrs_field == null)
2056 methodbuilder_attrs_field = typeof (MethodBuilder).GetField ("attrs", BindingFlags.NonPublic | BindingFlags.Instance);
2057 methodbuilder_attrs_field.SetValue (builder, flags);
2059 container.Compiler.Report.RuntimeMissingSupport (method.Location, "Generic method MethodAttributes");
2068 public void Emit (TypeDefinition parent)
2070 var mc = (IMemberContext) method;
2072 method.ParameterInfo.ApplyAttributes (mc, MethodBuilder);
2074 ToplevelBlock block = method.Block;
2075 if (block != null) {
2076 BlockContext bc = new BlockContext (mc, block, method.ReturnType);
2077 if (block.Resolve (null, bc, method)) {
2078 debug_builder = member.Parent.CreateMethodSymbolEntry ();
2079 EmitContext ec = method.CreateEmitContext (MethodBuilder.GetILGenerator (), debug_builder);
2086 public void WriteDebugSymbol (MonoSymbolFile file)
2088 if (debug_builder == null)
2091 var token = builder.GetToken ();
2092 int t = token.Token;
2094 if (token.IsPseudoToken)
2095 t = member.Module.Builder.ResolvePseudoToken (t);
2098 debug_builder.DefineMethod (file, t);
2102 public class Destructor : MethodOrOperator
2104 const Modifiers AllowedModifiers =
2108 static readonly string[] attribute_targets = new string [] { "method" };
2110 public static readonly string MetadataName = "Finalize";
2112 public Destructor (TypeDefinition parent, Modifiers mod, ParametersCompiled parameters, Attributes attrs, Location l)
2113 : base (parent, null, mod, AllowedModifiers, new MemberName (MetadataName, l), attrs, parameters)
2115 ModFlags &= ~Modifiers.PRIVATE;
2116 ModFlags |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2119 public override void Accept (StructuralVisitor visitor)
2121 visitor.Visit (this);
2124 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
2126 if (a.Type == pa.Conditional) {
2127 Error_ConditionalAttributeIsNotValid ();
2131 base.ApplyAttributeBuilder (a, ctor, cdata, pa);
2134 protected override bool CheckBase ()
2136 // Don't check base, destructors have special syntax
2140 public override void Emit()
2142 var base_type = Parent.PartialContainer.BaseType;
2143 if (base_type != null && Block != null) {
2144 var base_dtor = MemberCache.FindMember (base_type,
2145 new MemberFilter (MetadataName, 0, MemberKind.Destructor, null, null), BindingRestriction.InstanceOnly) as MethodSpec;
2147 if (base_dtor == null)
2148 throw new NotImplementedException ();
2150 MethodGroupExpr method_expr = MethodGroupExpr.CreatePredefined (base_dtor, base_type, Location);
2151 method_expr.InstanceExpression = new BaseThis (base_type, Location);
2153 var try_block = new ExplicitBlock (block, block.StartLocation, block.EndLocation) {
2154 IsCompilerGenerated = true
2156 var finaly_block = new ExplicitBlock (block, Location, Location) {
2157 IsCompilerGenerated = true
2161 // 0-size arguments to avoid CS0250 error
2162 // TODO: Should use AddScopeStatement or something else which emits correct
2165 finaly_block.AddStatement (new StatementExpression (new Invocation (method_expr, new Arguments (0)), Location.Null));
2167 var tf = new TryFinally (try_block, finaly_block, Location);
2168 block.WrapIntoDestructor (tf, try_block);
2174 public override string GetSignatureForError ()
2176 return Parent.GetSignatureForError () + ".~" + Parent.MemberName.Name + "()";
2179 protected override bool ResolveMemberType ()
2181 member_type = Compiler.BuiltinTypes.Void;
2185 public override string[] ValidAttributeTargets {
2187 return attribute_targets;
2192 // Ooouh Martin, templates are missing here.
2193 // When it will be possible move here a lot of child code and template method type.
2194 public abstract class AbstractPropertyEventMethod : MemberCore, IMethodData {
2195 protected MethodData method_data;
2196 protected ToplevelBlock block;
2197 protected SecurityType declarative_security;
2199 protected readonly string prefix;
2201 ReturnParameter return_attributes;
2203 public AbstractPropertyEventMethod (InterfaceMemberBase member, string prefix, Attributes attrs, Location loc)
2204 : base (member.Parent, SetupName (prefix, member, loc), attrs)
2206 this.prefix = prefix;
2209 static MemberName SetupName (string prefix, InterfaceMemberBase member, Location loc)
2211 return new MemberName (member.MemberName.Left, prefix + member.ShortName, member.MemberName.ExplicitInterface, loc);
2214 public void UpdateName (InterfaceMemberBase member)
2216 SetMemberName (SetupName (prefix, member, Location));
2219 #region IMethodData Members
2221 public ToplevelBlock Block {
2231 public CallingConventions CallingConventions {
2233 return CallingConventions.Standard;
2237 public EmitContext CreateEmitContext (ILGenerator ig, SourceMethodBuilder sourceMethod)
2239 return new EmitContext (this, ig, ReturnType, sourceMethod);
2242 public bool IsAccessor {
2248 public MemberName MethodName {
2254 public TypeSpec[] ParameterTypes {
2256 return ParameterInfo.Types;
2260 public abstract ParametersCompiled ParameterInfo { get ; }
2261 public abstract TypeSpec ReturnType { get; }
2265 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
2267 if (a.Type == pa.CLSCompliant || a.Type == pa.Obsolete || a.Type == pa.Conditional) {
2268 Report.Error (1667, a.Location,
2269 "Attribute `{0}' is not valid on property or event accessors. It is valid on `{1}' declarations only",
2270 TypeManager.CSharpName (a.Type), a.GetValidTargets ());
2274 if (a.IsValidSecurityAttribute ()) {
2275 a.ExtractSecurityPermissionSet (ctor, ref declarative_security);
2279 if (a.Target == AttributeTargets.Method) {
2280 method_data.MethodBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
2284 if (a.Target == AttributeTargets.ReturnValue) {
2285 if (return_attributes == null)
2286 return_attributes = new ReturnParameter (this, method_data.MethodBuilder, Location);
2288 return_attributes.ApplyAttributeBuilder (a, ctor, cdata, pa);
2292 ApplyToExtraTarget (a, ctor, cdata, pa);
2295 protected virtual void ApplyToExtraTarget (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
2297 throw new NotSupportedException ("You forgot to define special attribute target handling");
2300 // It is not supported for the accessors
2301 public sealed override bool Define()
2303 throw new NotSupportedException ();
2306 public virtual void Emit (TypeDefinition parent)
2308 method_data.Emit (parent);
2310 if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
2311 Module.PredefinedAttributes.CompilerGenerated.EmitAttribute (method_data.MethodBuilder);
2312 if (((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0))
2313 Module.PredefinedAttributes.DebuggerHidden.EmitAttribute (method_data.MethodBuilder);
2315 if (ReturnType.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
2316 return_attributes = new ReturnParameter (this, method_data.MethodBuilder, Location);
2317 Module.PredefinedAttributes.Dynamic.EmitAttribute (return_attributes.Builder);
2318 } else if (ReturnType.HasDynamicElement) {
2319 return_attributes = new ReturnParameter (this, method_data.MethodBuilder, Location);
2320 Module.PredefinedAttributes.Dynamic.EmitAttribute (return_attributes.Builder, ReturnType, Location);
2323 if (OptAttributes != null)
2324 OptAttributes.Emit ();
2326 if (declarative_security != null) {
2327 foreach (var de in declarative_security) {
2329 method_data.MethodBuilder.__AddDeclarativeSecurity (de);
2331 method_data.MethodBuilder.AddDeclarativeSecurity (de.Key, de.Value);
2339 public override bool EnableOverloadChecks (MemberCore overload)
2341 if (overload is MethodCore) {
2342 caching_flags |= Flags.MethodOverloadsExist;
2346 // This can only happen with indexers and it will
2347 // be catched as indexer difference
2348 if (overload is AbstractPropertyEventMethod)
2354 public override string GetCallerMemberName ()
2356 return base.GetCallerMemberName ().Substring (prefix.Length);
2359 public override string GetSignatureForDocumentation ()
2361 // should not be called
2362 throw new NotSupportedException ();
2365 public override bool IsClsComplianceRequired()
2370 public override void WriteDebugSymbol (MonoSymbolFile file)
2372 if (method_data != null)
2373 method_data.WriteDebugSymbol (file);
2376 public MethodSpec Spec { get; protected set; }
2379 // Represents header string for documentation comment.
2381 public override string DocCommentHeader {
2382 get { throw new InvalidOperationException ("Unexpected attempt to get doc comment from " + this.GetType () + "."); }
2386 public class Operator : MethodOrOperator {
2388 const Modifiers AllowedModifiers =
2394 public enum OpType : byte {
2404 // Unary and Binary operators
2427 // Implicit and Explicit
2431 // Just because of enum
2435 public readonly OpType OperatorType;
2437 static readonly string [] [] names;
2441 names = new string[(int)OpType.TOP][];
2442 names [(int) OpType.LogicalNot] = new string [] { "!", "op_LogicalNot" };
2443 names [(int) OpType.OnesComplement] = new string [] { "~", "op_OnesComplement" };
2444 names [(int) OpType.Increment] = new string [] { "++", "op_Increment" };
2445 names [(int) OpType.Decrement] = new string [] { "--", "op_Decrement" };
2446 names [(int) OpType.True] = new string [] { "true", "op_True" };
2447 names [(int) OpType.False] = new string [] { "false", "op_False" };
2448 names [(int) OpType.Addition] = new string [] { "+", "op_Addition" };
2449 names [(int) OpType.Subtraction] = new string [] { "-", "op_Subtraction" };
2450 names [(int) OpType.UnaryPlus] = new string [] { "+", "op_UnaryPlus" };
2451 names [(int) OpType.UnaryNegation] = new string [] { "-", "op_UnaryNegation" };
2452 names [(int) OpType.Multiply] = new string [] { "*", "op_Multiply" };
2453 names [(int) OpType.Division] = new string [] { "/", "op_Division" };
2454 names [(int) OpType.Modulus] = new string [] { "%", "op_Modulus" };
2455 names [(int) OpType.BitwiseAnd] = new string [] { "&", "op_BitwiseAnd" };
2456 names [(int) OpType.BitwiseOr] = new string [] { "|", "op_BitwiseOr" };
2457 names [(int) OpType.ExclusiveOr] = new string [] { "^", "op_ExclusiveOr" };
2458 names [(int) OpType.LeftShift] = new string [] { "<<", "op_LeftShift" };
2459 names [(int) OpType.RightShift] = new string [] { ">>", "op_RightShift" };
2460 names [(int) OpType.Equality] = new string [] { "==", "op_Equality" };
2461 names [(int) OpType.Inequality] = new string [] { "!=", "op_Inequality" };
2462 names [(int) OpType.GreaterThan] = new string [] { ">", "op_GreaterThan" };
2463 names [(int) OpType.LessThan] = new string [] { "<", "op_LessThan" };
2464 names [(int) OpType.GreaterThanOrEqual] = new string [] { ">=", "op_GreaterThanOrEqual" };
2465 names [(int) OpType.LessThanOrEqual] = new string [] { "<=", "op_LessThanOrEqual" };
2466 names [(int) OpType.Implicit] = new string [] { "implicit", "op_Implicit" };
2467 names [(int) OpType.Explicit] = new string [] { "explicit", "op_Explicit" };
2470 public Operator (TypeDefinition parent, OpType type, FullNamedExpression ret_type, Modifiers mod_flags, ParametersCompiled parameters,
2471 ToplevelBlock block, Attributes attrs, Location loc)
2472 : base (parent, ret_type, mod_flags, AllowedModifiers, new MemberName (GetMetadataName (type), loc), attrs, parameters)
2474 OperatorType = type;
2478 public override void Accept (StructuralVisitor visitor)
2480 visitor.Visit (this);
2483 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
2485 if (a.Type == pa.Conditional) {
2486 Error_ConditionalAttributeIsNotValid ();
2490 base.ApplyAttributeBuilder (a, ctor, cdata, pa);
2493 public override bool Define ()
2495 const Modifiers RequiredModifiers = Modifiers.PUBLIC | Modifiers.STATIC;
2496 if ((ModFlags & RequiredModifiers) != RequiredModifiers){
2497 Report.Error (558, Location, "User-defined operator `{0}' must be declared static and public", GetSignatureForError ());
2500 if (!base.Define ())
2503 if (block != null && block.IsIterator) {
2505 // Current method is turned into automatically generated
2506 // wrapper which creates an instance of iterator
2508 Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
2509 ModFlags |= Modifiers.DEBUGGER_HIDDEN;
2512 // imlicit and explicit operator of same types are not allowed
2513 if (OperatorType == OpType.Explicit)
2514 Parent.MemberCache.CheckExistingMembersOverloads (this, GetMetadataName (OpType.Implicit), parameters);
2515 else if (OperatorType == OpType.Implicit)
2516 Parent.MemberCache.CheckExistingMembersOverloads (this, GetMetadataName (OpType.Explicit), parameters);
2518 TypeSpec declaring_type = Parent.CurrentType;
2519 TypeSpec return_type = MemberType;
2520 TypeSpec first_arg_type = ParameterTypes [0];
2522 TypeSpec first_arg_type_unwrap = first_arg_type;
2523 if (first_arg_type.IsNullableType)
2524 first_arg_type_unwrap = Nullable.NullableInfo.GetUnderlyingType (first_arg_type);
2526 TypeSpec return_type_unwrap = return_type;
2527 if (return_type.IsNullableType)
2528 return_type_unwrap = Nullable.NullableInfo.GetUnderlyingType (return_type);
2531 // Rules for conversion operators
2533 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
2534 if (first_arg_type_unwrap == return_type_unwrap && first_arg_type_unwrap == declaring_type) {
2535 Report.Error (555, Location,
2536 "User-defined operator cannot take an object of the enclosing type and convert to an object of the enclosing type");
2541 if (declaring_type == return_type || declaring_type == return_type_unwrap) {
2542 conv_type = first_arg_type;
2543 } else if (declaring_type == first_arg_type || declaring_type == first_arg_type_unwrap) {
2544 conv_type = return_type;
2546 Report.Error (556, Location,
2547 "User-defined conversion must convert to or from the enclosing type");
2551 if (conv_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
2552 Report.Error (1964, Location,
2553 "User-defined conversion `{0}' cannot convert to or from the dynamic type",
2554 GetSignatureForError ());
2559 if (conv_type.IsInterface) {
2560 Report.Error (552, Location, "User-defined conversion `{0}' cannot convert to or from an interface type",
2561 GetSignatureForError ());
2565 if (conv_type.IsClass) {
2566 if (TypeSpec.IsBaseClass (declaring_type, conv_type, true)) {
2567 Report.Error (553, Location, "User-defined conversion `{0}' cannot convert to or from a base class",
2568 GetSignatureForError ());
2572 if (TypeSpec.IsBaseClass (conv_type, declaring_type, false)) {
2573 Report.Error (554, Location, "User-defined conversion `{0}' cannot convert to or from a derived class",
2574 GetSignatureForError ());
2578 } else if (OperatorType == OpType.LeftShift || OperatorType == OpType.RightShift) {
2579 if (first_arg_type != declaring_type || parameters.Types[1].BuiltinType != BuiltinTypeSpec.Type.Int) {
2580 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");
2583 } else if (parameters.Count == 1) {
2584 // Checks for Unary operators
2586 if (OperatorType == OpType.Increment || OperatorType == OpType.Decrement) {
2587 if (return_type != declaring_type && !TypeSpec.IsBaseClass (return_type, declaring_type, false)) {
2588 Report.Error (448, Location,
2589 "The return type for ++ or -- operator must be the containing type or derived from the containing type");
2592 if (first_arg_type != declaring_type) {
2594 559, Location, "The parameter type for ++ or -- operator must be the containing type");
2599 if (first_arg_type_unwrap != declaring_type) {
2600 Report.Error (562, Location,
2601 "The parameter type of a unary operator must be the containing type");
2605 if (OperatorType == OpType.True || OperatorType == OpType.False) {
2606 if (return_type.BuiltinType != BuiltinTypeSpec.Type.Bool) {
2609 "The return type of operator True or False " +
2615 } else if (first_arg_type_unwrap != declaring_type) {
2616 // Checks for Binary operators
2618 var second_arg_type = ParameterTypes[1];
2619 if (second_arg_type.IsNullableType)
2620 second_arg_type = Nullable.NullableInfo.GetUnderlyingType (second_arg_type);
2622 if (second_arg_type != declaring_type) {
2623 Report.Error (563, Location,
2624 "One of the parameters of a binary operator must be the containing type");
2632 protected override bool ResolveMemberType ()
2634 if (!base.ResolveMemberType ())
2637 flags |= MethodAttributes.SpecialName | MethodAttributes.HideBySig;
2641 protected override MemberSpec FindBaseMember (out MemberSpec bestCandidate, ref bool overrides)
2643 // Operator cannot be override
2644 bestCandidate = null;
2648 public static string GetName (OpType ot)
2650 return names [(int) ot] [0];
2653 public static string GetName (string metadata_name)
2655 for (int i = 0; i < names.Length; ++i) {
2656 if (names [i] [1] == metadata_name)
2657 return names [i] [0];
2662 public static string GetMetadataName (OpType ot)
2664 return names [(int) ot] [1];
2667 public static string GetMetadataName (string name)
2669 for (int i = 0; i < names.Length; ++i) {
2670 if (names [i] [0] == name)
2671 return names [i] [1];
2676 public static OpType? GetType (string metadata_name)
2678 for (int i = 0; i < names.Length; ++i) {
2679 if (names[i][1] == metadata_name)
2686 public OpType GetMatchingOperator ()
2688 switch (OperatorType) {
2689 case OpType.Equality:
2690 return OpType.Inequality;
2691 case OpType.Inequality:
2692 return OpType.Equality;
2694 return OpType.False;
2697 case OpType.GreaterThan:
2698 return OpType.LessThan;
2699 case OpType.LessThan:
2700 return OpType.GreaterThan;
2701 case OpType.GreaterThanOrEqual:
2702 return OpType.LessThanOrEqual;
2703 case OpType.LessThanOrEqual:
2704 return OpType.GreaterThanOrEqual;
2710 public override string GetSignatureForDocumentation ()
2712 string s = base.GetSignatureForDocumentation ();
2713 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
2714 s = s + "~" + ReturnType.GetSignatureForDocumentation ();
2720 public override string GetSignatureForError ()
2722 StringBuilder sb = new StringBuilder ();
2723 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
2724 sb.AppendFormat ("{0}.{1} operator {2}",
2725 Parent.GetSignatureForError (), GetName (OperatorType),
2726 member_type == null ? type_expr.GetSignatureForError () : member_type.GetSignatureForError ());
2729 sb.AppendFormat ("{0}.operator {1}", Parent.GetSignatureForError (), GetName (OperatorType));
2732 sb.Append (parameters.GetSignatureForError ());
2733 return sb.ToString ();