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 : IMethodDefinition
193 TypeParameterSpec[] TypeParameters { get; }
194 int TypeParametersCount { get; }
196 // MethodInfo MakeGenericMethod (TypeSpec[] targs);
199 public sealed class MethodSpec : MemberSpec, IParametersMember
201 MethodBase inflatedMetaInfo;
202 AParametersCollection parameters;
206 TypeParameterSpec[] constraints;
208 public MethodSpec (MemberKind kind, TypeSpec declaringType, IMethodDefinition details, TypeSpec returnType,
209 AParametersCollection parameters, Modifiers modifiers)
210 : base (kind, declaringType, details, modifiers)
212 this.parameters = parameters;
213 this.returnType = returnType;
218 public override int Arity {
220 return IsGeneric ? GenericDefinition.TypeParametersCount : 0;
224 public TypeParameterSpec[] Constraints {
226 if (constraints == null && IsGeneric)
227 constraints = GenericDefinition.TypeParameters;
233 public bool IsConstructor {
235 return Kind == MemberKind.Constructor;
239 public new IMethodDefinition MemberDefinition {
241 return (IMethodDefinition) definition;
245 public IGenericMethodDefinition GenericDefinition {
247 return (IGenericMethodDefinition) definition;
251 public bool IsAsync {
253 return (Modifiers & Modifiers.ASYNC) != 0;
257 public bool IsExtensionMethod {
259 return IsStatic && parameters.HasExtensionMethodType;
263 public bool IsSealed {
265 return (Modifiers & Modifiers.SEALED) != 0;
269 // When is virtual or abstract
270 public bool IsVirtual {
272 return (Modifiers & (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE)) != 0;
276 public bool IsReservedMethod {
278 return Kind == MemberKind.Operator || IsAccessor;
282 TypeSpec IInterfaceMemberSpec.MemberType {
288 public AParametersCollection Parameters {
294 public TypeSpec ReturnType {
300 public TypeSpec[] TypeArguments {
308 public MethodSpec GetGenericMethodDefinition ()
310 if (!IsGeneric && !DeclaringType.IsGeneric)
313 return MemberCache.GetMember (declaringType, this);
316 public MethodBase GetMetaInfo ()
319 // inflatedMetaInfo is extra field needed for cases where we
320 // inflate method but another nested type can later inflate
321 // again (the cache would be build with inflated metaInfo) and
322 // TypeBuilder can work with method definitions only
324 if (inflatedMetaInfo == null) {
325 if ((state & StateFlags.PendingMetaInflate) != 0) {
326 var dt_meta = DeclaringType.GetMetaInfo ();
328 if (DeclaringType.IsTypeBuilder) {
330 inflatedMetaInfo = TypeBuilder.GetConstructor (dt_meta, (ConstructorInfo) MemberDefinition.Metadata);
332 inflatedMetaInfo = TypeBuilder.GetMethod (dt_meta, (MethodInfo) MemberDefinition.Metadata);
335 // it should not be reached
336 throw new NotImplementedException ();
338 inflatedMetaInfo = MethodInfo.GetMethodFromHandle (MemberDefinition.Metadata.MethodHandle, dt_meta.TypeHandle);
342 state &= ~StateFlags.PendingMetaInflate;
344 inflatedMetaInfo = MemberDefinition.Metadata;
348 if ((state & StateFlags.PendingMakeMethod) != 0) {
349 var sre_targs = new MetaType[targs.Length];
350 for (int i = 0; i < sre_targs.Length; ++i)
351 sre_targs[i] = targs[i].GetMetaInfo ();
353 inflatedMetaInfo = ((MethodInfo) inflatedMetaInfo).MakeGenericMethod (sre_targs);
354 state &= ~StateFlags.PendingMakeMethod;
357 return inflatedMetaInfo;
360 public override string GetSignatureForDocumentation ()
364 case MemberKind.Constructor:
367 case MemberKind.Method:
369 name = Name + "``" + Arity.ToString ();
379 name = DeclaringType.GetSignatureForDocumentation () + "." + name + parameters.GetSignatureForDocumentation ();
380 if (Kind == MemberKind.Operator) {
381 var op = Operator.GetType (Name).Value;
382 if (op == Operator.OpType.Explicit || op == Operator.OpType.Implicit) {
383 name += "~" + ReturnType.GetSignatureForDocumentation ();
390 public override string GetSignatureForError ()
394 name = DeclaringType.GetSignatureForError () + "." + DeclaringType.Name;
395 } else if (Kind == MemberKind.Operator) {
396 var op = Operator.GetType (Name).Value;
397 if (op == Operator.OpType.Implicit || op == Operator.OpType.Explicit) {
398 name = DeclaringType.GetSignatureForError () + "." + Operator.GetName (op) + " operator " + returnType.GetSignatureForError ();
400 name = DeclaringType.GetSignatureForError () + ".operator " + Operator.GetName (op);
402 } else if (IsAccessor) {
403 int split = Name.IndexOf ('_');
404 name = Name.Substring (split + 1);
405 var postfix = Name.Substring (0, split);
407 var pc = parameters.Count;
408 if (pc > 0 && postfix == "get") {
409 name = "this" + parameters.GetSignatureForError ("[", "]", pc);
410 } else if (pc > 1 && postfix == "set") {
411 name = "this" + parameters.GetSignatureForError ("[", "]", pc - 1);
415 return DeclaringType.GetSignatureForError () + "." + name + "." + postfix;
417 name = base.GetSignatureForError ();
419 name += "<" + TypeManager.CSharpName (targs) + ">";
421 name += "<" + TypeManager.CSharpName (GenericDefinition.TypeParameters) + ">";
424 return name + parameters.GetSignatureForError ();
427 public override MemberSpec InflateMember (TypeParameterInflator inflator)
429 var ms = (MethodSpec) base.InflateMember (inflator);
430 ms.inflatedMetaInfo = null;
431 ms.returnType = inflator.Inflate (returnType);
432 ms.parameters = parameters.Inflate (inflator);
434 ms.constraints = TypeParameterSpec.InflateConstraints (inflator, Constraints);
439 public MethodSpec MakeGenericMethod (IMemberContext context, params TypeSpec[] targs)
442 throw new ArgumentNullException ();
444 // if (generic_intances != null && generic_intances.TryGetValue (targs, out ginstance))
447 //if (generic_intances == null)
448 // generic_intances = new Dictionary<TypeSpec[], Method> (TypeSpecArrayComparer.Default);
450 var inflator = new TypeParameterInflator (context, DeclaringType, GenericDefinition.TypeParameters, targs);
452 var inflated = (MethodSpec) MemberwiseClone ();
453 inflated.declaringType = inflator.TypeInstance;
454 inflated.returnType = inflator.Inflate (returnType);
455 inflated.parameters = parameters.Inflate (inflator);
456 inflated.targs = targs;
457 inflated.constraints = TypeParameterSpec.InflateConstraints (inflator, constraints ?? GenericDefinition.TypeParameters);
458 inflated.state |= StateFlags.PendingMakeMethod;
460 // if (inflated.parent == null)
461 // inflated.parent = parent;
463 //generic_intances.Add (targs, inflated);
467 public MethodSpec Mutate (TypeParameterMutator mutator)
469 var targs = TypeArguments;
471 targs = mutator.Mutate (targs);
473 var decl = DeclaringType;
474 if (DeclaringType.IsGenericOrParentIsGeneric) {
475 decl = mutator.Mutate (decl);
478 if (targs == TypeArguments && decl == DeclaringType)
481 var ms = (MethodSpec) MemberwiseClone ();
482 if (decl != DeclaringType) {
483 ms.inflatedMetaInfo = null;
484 ms.declaringType = decl;
485 ms.state |= StateFlags.PendingMetaInflate;
490 ms.state |= StateFlags.PendingMakeMethod;
496 public override List<TypeSpec> ResolveMissingDependencies ()
498 var missing = returnType.ResolveMissingDependencies ();
499 foreach (var pt in parameters.Types) {
500 var m = pt.GetMissingDependencies ();
505 missing = new List<TypeSpec> ();
507 missing.AddRange (m);
511 foreach (var tp in GenericDefinition.TypeParameters) {
512 var m = tp.GetMissingDependencies ();
518 missing = new List<TypeSpec> ();
520 missing.AddRange (m);
528 public abstract class MethodOrOperator : MethodCore, IMethodData, IMethodDefinition
530 ReturnParameter return_attributes;
531 SecurityType declarative_security;
532 protected MethodData MethodData;
534 static readonly string[] attribute_targets = new string [] { "method", "return" };
536 protected MethodOrOperator (TypeDefinition parent, FullNamedExpression type, Modifiers mod, Modifiers allowed_mod, MemberName name,
537 Attributes attrs, ParametersCompiled parameters)
538 : base (parent, type, mod, allowed_mod, name, attrs, parameters)
542 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
544 if (a.Target == AttributeTargets.ReturnValue) {
545 if (return_attributes == null)
546 return_attributes = new ReturnParameter (this, MethodBuilder, Location);
548 return_attributes.ApplyAttributeBuilder (a, ctor, cdata, pa);
552 if (a.Type == pa.MethodImpl) {
553 if ((ModFlags & Modifiers.ASYNC) != 0 && (a.GetMethodImplOptions () & MethodImplOptions.Synchronized) != 0) {
554 Report.Error (4015, a.Location, "`{0}': Async methods cannot use `MethodImplOptions.Synchronized'",
555 GetSignatureForError ());
558 is_external_implementation = a.IsInternalCall ();
559 } else if (a.Type == pa.DllImport) {
560 const Modifiers extern_static = Modifiers.EXTERN | Modifiers.STATIC;
561 if ((ModFlags & extern_static) != extern_static) {
562 Report.Error (601, a.Location, "The DllImport attribute must be specified on a method marked `static' and `extern'");
564 is_external_implementation = true;
567 if (a.IsValidSecurityAttribute ()) {
568 a.ExtractSecurityPermissionSet (ctor, ref declarative_security);
572 if (MethodBuilder != null)
573 MethodBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
576 public override AttributeTargets AttributeTargets {
578 return AttributeTargets.Method;
582 MethodBase IMethodDefinition.Metadata {
584 return MethodData.MethodBuilder;
588 // TODO: Remove and use MethodData abstraction
589 public MethodBuilder MethodBuilder {
591 return MethodData.MethodBuilder;
595 protected override bool CheckForDuplications ()
597 return Parent.MemberCache.CheckExistingMembersOverloads (this, parameters);
600 public virtual EmitContext CreateEmitContext (ILGenerator ig, SourceMethodBuilder sourceMethod)
602 return new EmitContext (this, ig, MemberType, sourceMethod);
605 public override bool Define ()
614 if (this is Operator)
615 kind = MemberKind.Operator;
616 else if (this is Destructor)
617 kind = MemberKind.Destructor;
619 kind = MemberKind.Method;
621 string explicit_name;
623 if (IsPartialDefinition) {
624 caching_flags &= ~Flags.Excluded_Undetected;
625 caching_flags |= Flags.Excluded;
627 // Add to member cache only when a partial method implementation has not been found yet
628 if ((caching_flags & Flags.PartialDefinitionExists) != 0)
634 explicit_name = null;
636 MethodData = new MethodData (this, ModFlags, flags, this, base_method);
638 if (!MethodData.Define (Parent.PartialContainer, GetFullName (MemberName)))
641 explicit_name = MethodData.MetadataName;
644 spec = new MethodSpec (kind, Parent.Definition, this, ReturnType, parameters, ModFlags);
645 if (MemberName.Arity > 0)
646 spec.IsGeneric = true;
648 Parent.MemberCache.AddMember (this, explicit_name, spec);
653 protected override void DoMemberTypeIndependentChecks ()
655 base.DoMemberTypeIndependentChecks ();
657 CheckAbstractAndExtern (block != null);
659 if ((ModFlags & Modifiers.PARTIAL) != 0) {
660 for (int i = 0; i < parameters.Count; ++i) {
661 IParameterData p = parameters.FixedParameters [i];
662 if ((p.ModFlags & Parameter.Modifier.OUT) != 0) {
663 Report.Error (752, Location, "`{0}': A partial method parameters cannot use `out' modifier",
664 GetSignatureForError ());
667 if (p.HasDefaultValue && IsPartialImplementation)
668 ((Parameter) p).Warning_UselessOptionalParameter (Report);
673 protected override void DoMemberTypeDependentChecks ()
675 base.DoMemberTypeDependentChecks ();
677 if (MemberType.IsStatic) {
678 Error_StaticReturnType ();
682 public override void Emit ()
684 if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
685 Module.PredefinedAttributes.CompilerGenerated.EmitAttribute (MethodBuilder);
686 if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
687 Module.PredefinedAttributes.DebuggerHidden.EmitAttribute (MethodBuilder);
689 if (ReturnType.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
690 return_attributes = new ReturnParameter (this, MethodBuilder, Location);
691 Module.PredefinedAttributes.Dynamic.EmitAttribute (return_attributes.Builder);
692 } else if (ReturnType.HasDynamicElement) {
693 return_attributes = new ReturnParameter (this, MethodBuilder, Location);
694 Module.PredefinedAttributes.Dynamic.EmitAttribute (return_attributes.Builder, ReturnType, Location);
697 if (OptAttributes != null)
698 OptAttributes.Emit ();
700 if (declarative_security != null) {
701 foreach (var de in declarative_security) {
703 MethodBuilder.__AddDeclarativeSecurity (de);
705 MethodBuilder.AddDeclarativeSecurity (de.Key, de.Value);
710 if (type_expr != null)
711 ConstraintChecker.Check (this, member_type, type_expr.Location);
715 if (MethodData != null)
716 MethodData.Emit (Parent);
721 protected void Error_ConditionalAttributeIsNotValid ()
723 Report.Error (577, Location,
724 "Conditional not valid on `{0}' because it is a constructor, destructor, operator or explicit interface implementation",
725 GetSignatureForError ());
728 public bool IsPartialDefinition {
730 return (ModFlags & Modifiers.PARTIAL) != 0 && Block == null;
734 public bool IsPartialImplementation {
736 return (ModFlags & Modifiers.PARTIAL) != 0 && Block != null;
740 public override string[] ValidAttributeTargets {
742 return attribute_targets;
746 #region IMethodData Members
748 bool IMethodData.IsAccessor {
754 public TypeSpec ReturnType {
760 public MemberName MethodName {
767 /// Returns true if method has conditional attribute and the conditions is not defined (method is excluded).
769 public override string[] ConditionalConditions ()
771 if ((caching_flags & (Flags.Excluded_Undetected | Flags.Excluded)) == 0)
774 if ((ModFlags & Modifiers.PARTIAL) != 0 && (caching_flags & Flags.Excluded) != 0)
775 return new string [0];
777 caching_flags &= ~Flags.Excluded_Undetected;
780 if (base_method == null) {
781 if (OptAttributes == null)
784 Attribute[] attrs = OptAttributes.SearchMulti (Module.PredefinedAttributes.Conditional);
788 conditions = new string[attrs.Length];
789 for (int i = 0; i < conditions.Length; ++i)
790 conditions[i] = attrs[i].GetConditionalAttributeValue ();
792 conditions = base_method.MemberDefinition.ConditionalConditions();
795 if (conditions != null)
796 caching_flags |= Flags.Excluded;
803 public virtual void PrepareEmit ()
805 var mb = MethodData.DefineMethodBuilder (Parent);
807 if (CurrentTypeParameters != null) {
808 string[] gnames = new string[CurrentTypeParameters.Count];
809 for (int i = 0; i < gnames.Length; ++i) {
810 gnames[i] = CurrentTypeParameters[i].Name;
813 var gen_params = MethodBuilder.DefineGenericParameters (gnames);
815 for (int i = 0; i < CurrentTypeParameters.Count; ++i) {
816 var tp = CurrentTypeParameters[i];
818 tp.Define (gen_params[i]);
823 // Generic method has been already defined to resolve method parameters
824 // correctly when they use type parameters
826 mb.SetParameters (parameters.GetMetaInfo ());
827 mb.SetReturnType (ReturnType.GetMetaInfo ());
830 public override void WriteDebugSymbol (MonoSymbolFile file)
832 if (MethodData != null)
833 MethodData.WriteDebugSymbol (file);
837 public class Method : MethodOrOperator, IGenericMethodDefinition
839 Method partialMethodImplementation;
841 public Method (TypeDefinition parent, FullNamedExpression return_type, Modifiers mod, MemberName name, ParametersCompiled parameters, Attributes attrs)
842 : base (parent, return_type, mod,
843 parent.PartialContainer.Kind == MemberKind.Interface ? AllowedModifiersInterface :
844 parent.PartialContainer.Kind == MemberKind.Struct ? AllowedModifiersStruct | Modifiers.ASYNC :
845 AllowedModifiersClass | Modifiers.ASYNC,
846 name, attrs, parameters)
850 protected Method (TypeDefinition parent, FullNamedExpression return_type, Modifiers mod, Modifiers amod,
851 MemberName name, ParametersCompiled parameters, Attributes attrs)
852 : base (parent, return_type, mod, amod, name, attrs, parameters)
858 public override TypeParameters CurrentTypeParameters {
860 return MemberName.TypeParameters;
864 public TypeParameterSpec[] TypeParameters {
866 return CurrentTypeParameters.Types;
870 public int TypeParametersCount {
872 return CurrentTypeParameters == null ? 0 : CurrentTypeParameters.Count;
878 public override void Accept (StructuralVisitor visitor)
880 visitor.Visit (this);
883 public static Method Create (TypeDefinition parent, FullNamedExpression returnType, Modifiers mod,
884 MemberName name, ParametersCompiled parameters, Attributes attrs)
886 var m = new Method (parent, returnType, mod, name, parameters, attrs);
888 if ((mod & Modifiers.PARTIAL) != 0) {
889 const Modifiers invalid_partial_mod = Modifiers.AccessibilityMask | Modifiers.ABSTRACT | Modifiers.EXTERN |
890 Modifiers.NEW | Modifiers.OVERRIDE | Modifiers.SEALED | Modifiers.VIRTUAL;
892 if ((mod & invalid_partial_mod) != 0) {
893 m.Report.Error (750, m.Location,
894 "A partial method cannot define access modifier or any of abstract, extern, new, override, sealed, or virtual modifiers");
895 mod &= ~invalid_partial_mod;
898 if ((parent.ModFlags & Modifiers.PARTIAL) == 0) {
899 m.Report.Error (751, m.Location,
900 "A partial method must be declared within a partial class or partial struct");
904 if ((mod & Modifiers.STATIC) == 0 && parameters.HasExtensionMethodType) {
905 m.Report.Error (1105, m.Location, "`{0}': Extension methods must be declared static",
906 m.GetSignatureForError ());
913 public override string GetSignatureForError()
915 return base.GetSignatureForError () + parameters.GetSignatureForError ();
918 void Error_DuplicateEntryPoint (Method b)
920 Report.Error (17, b.Location,
921 "Program `{0}' has more than one entry point defined: `{1}'",
922 b.Module.Builder.ScopeName, b.GetSignatureForError ());
927 if (ReturnType.Kind != MemberKind.Void && ReturnType.BuiltinType != BuiltinTypeSpec.Type.Int)
930 if (parameters.IsEmpty)
933 if (parameters.Count > 1)
936 var ac = parameters.Types [0] as ArrayContainer;
937 return ac != null && ac.Rank == 1 && ac.Element.BuiltinType == BuiltinTypeSpec.Type.String &&
938 (parameters[0].ModFlags & Parameter.Modifier.RefOutMask) == 0;
941 public override FullNamedExpression LookupNamespaceOrType (string name, int arity, LookupMode mode, Location loc)
944 var tp = CurrentTypeParameters;
946 TypeParameter t = tp.Find (name);
948 return new TypeParameterExpr (t, loc);
952 return base.LookupNamespaceOrType (name, arity, mode, loc);
955 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
957 if (a.Type == pa.Conditional) {
958 if (IsExplicitImpl) {
959 Error_ConditionalAttributeIsNotValid ();
963 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
964 Report.Error (243, Location, "Conditional not valid on `{0}' because it is an override method", GetSignatureForError ());
968 if (ReturnType.Kind != MemberKind.Void) {
969 Report.Error (578, Location, "Conditional not valid on `{0}' because its return type is not void", GetSignatureForError ());
974 Report.Error (582, Location, "Conditional not valid on interface members");
978 if (MethodData.implementing != null) {
979 Report.SymbolRelatedToPreviousError (MethodData.implementing.DeclaringType);
980 Report.Error (629, Location, "Conditional member `{0}' cannot implement interface member `{1}'",
981 GetSignatureForError (), TypeManager.CSharpSignature (MethodData.implementing));
985 for (int i = 0; i < parameters.Count; ++i) {
986 if ((parameters.FixedParameters [i].ModFlags & Parameter.Modifier.OUT) != 0) {
987 Report.Error (685, Location, "Conditional method `{0}' cannot have an out parameter", GetSignatureForError ());
993 if (a.Type == pa.Extension) {
994 a.Error_MisusedExtensionAttribute ();
998 base.ApplyAttributeBuilder (a, ctor, cdata, pa);
1001 void CreateTypeParameters ()
1003 var tparams = MemberName.TypeParameters;
1004 var parent_tparams = Parent.TypeParametersAll;
1006 for (int i = 0; i < MemberName.Arity; i++) {
1007 string type_argument_name = tparams[i].MemberName.Name;
1009 if (block == null) {
1010 int idx = parameters.GetParameterIndexByName (type_argument_name);
1014 b = new ToplevelBlock (Compiler, Location);
1016 b.Error_AlreadyDeclaredTypeParameter (type_argument_name, parameters[i].Location);
1019 INamedBlockVariable variable = null;
1020 block.GetLocalName (type_argument_name, block, ref variable);
1021 if (variable != null)
1022 variable.Block.Error_AlreadyDeclaredTypeParameter (type_argument_name, variable.Location);
1025 if (parent_tparams != null) {
1026 var tp = parent_tparams.Find (type_argument_name);
1028 tparams[i].WarningParentNameConflict (tp);
1033 tparams.Create (null, 0, Parent);
1036 protected virtual void DefineTypeParameters ()
1038 var tparams = CurrentTypeParameters;
1040 TypeParameterSpec[] base_tparams = null;
1041 TypeParameterSpec[] base_decl_tparams = TypeParameterSpec.EmptyTypes;
1042 TypeSpec[] base_targs = TypeSpec.EmptyTypes;
1043 if (((ModFlags & Modifiers.OVERRIDE) != 0 || IsExplicitImpl)) {
1044 MethodSpec base_override = base_method ?? MethodData.implementing;
1046 if (base_override != null) {
1047 base_tparams = base_override.GenericDefinition.TypeParameters;
1049 if (base_override.DeclaringType.IsGeneric) {
1050 base_decl_tparams = base_override.DeclaringType.MemberDefinition.TypeParameters;
1052 if (base_method != null) {
1053 var base_type_parent = CurrentType;
1054 while (base_type_parent.BaseType != base_override.DeclaringType) {
1055 base_type_parent = base_type_parent.BaseType;
1058 base_targs = base_type_parent.BaseType.TypeArguments;
1060 foreach (var iface in Parent.CurrentType.Interfaces) {
1061 if (iface == base_override.DeclaringType) {
1062 base_targs = iface.TypeArguments;
1069 if (base_override.IsGeneric) {
1070 ObsoleteAttribute oa;
1071 foreach (var base_tp in base_tparams) {
1072 oa = base_tp.BaseType.GetAttributeObsolete ();
1074 AttributeTester.Report_ObsoleteMessage (oa, base_tp.BaseType.GetSignatureForError (), Location, Report);
1077 if (base_tp.InterfacesDefined != null) {
1078 foreach (var iface in base_tp.InterfacesDefined) {
1079 oa = iface.GetAttributeObsolete ();
1081 AttributeTester.Report_ObsoleteMessage (oa, iface.GetSignatureForError (), Location, Report);
1087 if (base_decl_tparams.Length != 0) {
1088 base_decl_tparams = base_decl_tparams.Concat (base_tparams).ToArray ();
1089 base_targs = base_targs.Concat (tparams.Types).ToArray ();
1091 base_decl_tparams = base_tparams;
1092 base_targs = tparams.Types;
1098 for (int i = 0; i < tparams.Count; ++i) {
1099 var tp = tparams[i];
1101 if (!tp.ResolveConstraints (this))
1105 // Copy base constraints for override/explicit methods
1107 if (base_tparams != null) {
1108 var base_tparam = base_tparams[i];
1109 var local_tparam = tp.Type;
1110 local_tparam.SpecialConstraint = base_tparam.SpecialConstraint;
1112 var inflator = new TypeParameterInflator (this, CurrentType, base_decl_tparams, base_targs);
1113 base_tparam.InflateConstraints (inflator, local_tparam);
1116 // Check all type argument constraints for possible collision or unification
1117 // introduced by inflating inherited constraints in this context
1119 // Conflict example:
1121 // class A<T> { virtual void Foo<U> () where U : class, T {} }
1122 // class B : A<int> { override void Foo<U> {} }
1124 var local_tparam_targs = local_tparam.TypeArguments;
1125 if (local_tparam_targs != null) {
1126 for (int ii = 0; ii < local_tparam_targs.Length; ++ii) {
1127 var ta = local_tparam_targs [ii];
1128 if (!ta.IsClass && !ta.IsStruct)
1131 TypeSpec[] unique_tparams = null;
1132 for (int iii = ii + 1; iii < local_tparam_targs.Length; ++iii) {
1134 // Remove any identical or unified constraint types
1136 var tparam_checked = local_tparam_targs[iii];
1137 if (TypeSpecComparer.IsEqual (ta, tparam_checked) || TypeSpec.IsBaseClass (ta, tparam_checked, false)) {
1138 unique_tparams = new TypeSpec[local_tparam_targs.Length - 1];
1139 Array.Copy (local_tparam_targs, 0, unique_tparams, 0, iii);
1140 Array.Copy (local_tparam_targs, iii + 1, unique_tparams, iii, local_tparam_targs.Length - iii - 1);
1141 } else if (!TypeSpec.IsBaseClass (tparam_checked, ta, false)) {
1142 Constraints.Error_ConflictingConstraints (this, local_tparam, ta, tparam_checked, Location);
1146 if (unique_tparams != null) {
1147 local_tparam_targs = unique_tparams;
1148 local_tparam.TypeArguments = local_tparam_targs;
1152 Constraints.CheckConflictingInheritedConstraint (local_tparam, ta, this, Location);
1158 if (base_tparams == null && MethodData != null && MethodData.implementing != null) {
1159 CheckImplementingMethodConstraints (Parent, spec, MethodData.implementing);
1163 public static bool CheckImplementingMethodConstraints (TypeContainer container, MethodSpec method, MethodSpec baseMethod)
1165 var tparams = method.Constraints;
1166 var base_tparams = baseMethod.Constraints;
1167 for (int i = 0; i < tparams.Length; ++i) {
1168 if (!tparams[i].HasSameConstraintsImplementation (base_tparams[i])) {
1169 container.Compiler.Report.SymbolRelatedToPreviousError (method);
1170 container.Compiler.Report.SymbolRelatedToPreviousError (baseMethod);
1172 // Using container location because the interface can be implemented
1174 container.Compiler.Report.Error (425, container.Location,
1175 "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",
1176 tparams[i].GetSignatureForError (), method.GetSignatureForError (),
1177 base_tparams[i].GetSignatureForError (), baseMethod.GetSignatureForError ());
1189 public override bool Define ()
1191 if (!base.Define ())
1194 if (member_type.Kind == MemberKind.Void && parameters.IsEmpty && MemberName.Arity == 0 && MemberName.Name == Destructor.MetadataName) {
1195 Report.Warning (465, 1, Location,
1196 "Introducing `Finalize' method can interfere with destructor invocation. Did you intend to declare a destructor?");
1199 if (Compiler.Settings.StdLib && ReturnType.IsSpecialRuntimeType) {
1200 Error1599 (Location, ReturnType, Report);
1204 if (CurrentTypeParameters == null) {
1205 if (base_method != null && !IsExplicitImpl) {
1206 if (parameters.Count == 1 && ParameterTypes[0].BuiltinType == BuiltinTypeSpec.Type.Object && MemberName.Name == "Equals")
1207 Parent.PartialContainer.Mark_HasEquals ();
1208 else if (parameters.IsEmpty && MemberName.Name == "GetHashCode")
1209 Parent.PartialContainer.Mark_HasGetHashCode ();
1213 DefineTypeParameters ();
1216 if (block != null) {
1217 if (block.IsIterator) {
1219 // Current method is turned into automatically generated
1220 // wrapper which creates an instance of iterator
1222 Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
1223 ModFlags |= Modifiers.DEBUGGER_HIDDEN;
1226 if ((ModFlags & Modifiers.ASYNC) != 0) {
1227 if (ReturnType.Kind != MemberKind.Void &&
1228 ReturnType != Module.PredefinedTypes.Task.TypeSpec &&
1229 !ReturnType.IsGenericTask) {
1230 Report.Error (1983, Location, "The return type of an async method must be void, Task, or Task<T>");
1233 block = (ToplevelBlock) block.ConvertToAsyncTask (this, Parent.PartialContainer, parameters, ReturnType, null, Location);
1234 ModFlags |= Modifiers.DEBUGGER_HIDDEN;
1237 if (Compiler.Settings.WriteMetadataOnly)
1241 if ((ModFlags & Modifiers.STATIC) == 0)
1244 if (parameters.HasExtensionMethodType) {
1245 if (Parent.PartialContainer.IsStatic && !Parent.IsGenericOrParentIsGeneric) {
1246 if (!Parent.IsTopLevel)
1247 Report.Error (1109, Location, "`{0}': Extension methods cannot be defined in a nested class",
1248 GetSignatureForError ());
1250 PredefinedAttribute pa = Module.PredefinedAttributes.Extension;
1251 if (!pa.IsDefined) {
1252 Report.Error (1110, Location,
1253 "`{0}': Extension methods require `System.Runtime.CompilerServices.ExtensionAttribute' type to be available. Are you missing an assembly reference?",
1254 GetSignatureForError ());
1257 ModFlags |= Modifiers.METHOD_EXTENSION;
1258 Parent.PartialContainer.ModFlags |= Modifiers.METHOD_EXTENSION;
1259 Spec.DeclaringType.SetExtensionMethodContainer ();
1260 Parent.Module.HasExtensionMethod = true;
1262 Report.Error (1106, Location, "`{0}': Extension methods must be defined in a non-generic static class",
1263 GetSignatureForError ());
1268 // This is used to track the Entry Point,
1270 var settings = Compiler.Settings;
1271 if (settings.NeedsEntryPoint && MemberName.Name == "Main" && (settings.MainClass == null || settings.MainClass == Parent.TypeBuilder.FullName)) {
1272 if (IsEntryPoint ()) {
1273 if (Parent.DeclaringAssembly.EntryPoint == null) {
1274 if (Parent.IsGenericOrParentIsGeneric || MemberName.IsGeneric) {
1275 Report.Warning (402, 4, Location, "`{0}': an entry point cannot be generic or in a generic type",
1276 GetSignatureForError ());
1277 } else if ((ModFlags & Modifiers.ASYNC) != 0) {
1278 Report.Error (4009, Location, "`{0}': an entry point cannot be async method",
1279 GetSignatureForError ());
1282 Parent.DeclaringAssembly.EntryPoint = this;
1285 Error_DuplicateEntryPoint (Parent.DeclaringAssembly.EntryPoint);
1286 Error_DuplicateEntryPoint (this);
1289 Report.Warning (28, 4, Location, "`{0}' has the wrong signature to be an entry point",
1290 GetSignatureForError ());
1297 public override void PrepareEmit ()
1299 if (IsPartialDefinition) {
1301 // Use partial method implementation builder for partial method declaration attributes
1303 if (partialMethodImplementation != null) {
1304 MethodData = partialMethodImplementation.MethodData;
1310 base.PrepareEmit ();
1316 public override void Emit ()
1319 if (IsPartialDefinition) {
1320 if (partialMethodImplementation != null && CurrentTypeParameters != null) {
1321 CurrentTypeParameters.CheckPartialConstraints (partialMethodImplementation);
1327 if ((ModFlags & Modifiers.PARTIAL) != 0 && (caching_flags & Flags.PartialDefinitionExists) == 0) {
1328 Report.Error (759, Location, "A partial method `{0}' implementation is missing a partial method declaration",
1329 GetSignatureForError ());
1332 if (CurrentTypeParameters != null) {
1333 for (int i = 0; i < CurrentTypeParameters.Count; ++i) {
1334 var tp = CurrentTypeParameters [i];
1336 tp.CheckGenericConstraints (false);
1342 MethodData.DefineOverride (Parent);
1344 if (block != null && block.StateMachine is AsyncTaskStorey) {
1345 var psm = Module.PredefinedAttributes.AsyncStateMachine;
1347 psm.EmitAttribute (MethodBuilder, block.StateMachine);
1350 if ((ModFlags & Modifiers.METHOD_EXTENSION) != 0)
1351 Module.PredefinedAttributes.Extension.EmitAttribute (MethodBuilder);
1354 } catch (Exception e) {
1355 throw new InternalErrorException (this, e);
1359 public override bool EnableOverloadChecks (MemberCore overload)
1361 if (overload is Indexer)
1364 return base.EnableOverloadChecks (overload);
1367 public static void Error1599 (Location loc, TypeSpec t, Report Report)
1369 Report.Error (1599, loc, "Method or delegate cannot return type `{0}'", t.GetSignatureForError ());
1372 protected override bool ResolveMemberType ()
1374 if (CurrentTypeParameters != null) {
1375 CreateTypeParameters ();
1378 return base.ResolveMemberType ();
1381 public void SetPartialDefinition (Method methodDefinition)
1383 caching_flags |= Flags.PartialDefinitionExists;
1384 methodDefinition.partialMethodImplementation = this;
1386 // Ensure we are always using method declaration parameters
1387 for (int i = 0; i < methodDefinition.parameters.Count; ++i ) {
1388 parameters [i].Name = methodDefinition.parameters [i].Name;
1389 parameters [i].DefaultValue = methodDefinition.parameters [i].DefaultValue;
1392 if (methodDefinition.attributes == null)
1395 if (attributes == null) {
1396 attributes = methodDefinition.attributes;
1398 attributes.Attrs.AddRange (methodDefinition.attributes.Attrs);
1403 public abstract class ConstructorInitializer : ExpressionStatement
1405 Arguments argument_list;
1406 MethodSpec base_ctor;
1408 public ConstructorInitializer (Arguments argument_list, Location loc)
1410 this.argument_list = argument_list;
1414 public Arguments Arguments {
1416 return argument_list;
1420 public override bool ContainsEmitWithAwait ()
1422 throw new NotSupportedException ();
1425 public override Expression CreateExpressionTree (ResolveContext ec)
1427 throw new NotSupportedException ("ET");
1430 protected override Expression DoResolve (ResolveContext ec)
1432 eclass = ExprClass.Value;
1435 var caller_builder = (Constructor) ec.MemberContext;
1438 // Spec mandates that constructor initializer will not have `this' access
1440 using (ec.Set (ResolveContext.Options.BaseInitializer)) {
1441 if (argument_list != null) {
1443 argument_list.Resolve (ec, out dynamic);
1446 ec.Report.Error (1975, loc,
1447 "The constructor call cannot be dynamically dispatched within constructor initializer");
1453 type = ec.CurrentType;
1454 if (this is ConstructorBaseInitializer) {
1455 if (ec.CurrentType.BaseType == null)
1458 type = ec.CurrentType.BaseType;
1459 if (ec.CurrentType.IsStruct) {
1460 ec.Report.Error (522, loc,
1461 "`{0}': Struct constructors cannot call base constructors", caller_builder.GetSignatureForError ());
1466 // It is legal to have "this" initializers that take no arguments
1467 // in structs, they are just no-ops.
1469 // struct D { public D (int a) : this () {}
1471 if (ec.CurrentType.IsStruct && argument_list == null)
1475 base_ctor = ConstructorLookup (ec, type, ref argument_list, loc);
1478 if (base_ctor != null && base_ctor.MemberDefinition == caller_builder.Spec.MemberDefinition) {
1479 ec.Report.Error (516, loc, "Constructor `{0}' cannot call itself",
1480 caller_builder.GetSignatureForError ());
1486 public override void Emit (EmitContext ec)
1488 // It can be null for static initializers
1489 if (base_ctor == null)
1492 var call = new CallEmitter ();
1493 call.InstanceExpression = new CompilerGeneratedThis (type, loc);
1494 call.EmitPredefined (ec, base_ctor, argument_list);
1497 public override void EmitStatement (EmitContext ec)
1503 public class ConstructorBaseInitializer : ConstructorInitializer {
1504 public ConstructorBaseInitializer (Arguments argument_list, Location l) :
1505 base (argument_list, l)
1510 class GeneratedBaseInitializer: ConstructorBaseInitializer {
1511 public GeneratedBaseInitializer (Location loc):
1517 public class ConstructorThisInitializer : ConstructorInitializer {
1518 public ConstructorThisInitializer (Arguments argument_list, Location l) :
1519 base (argument_list, l)
1524 public class Constructor : MethodCore, IMethodData, IMethodDefinition
1526 public ConstructorBuilder ConstructorBuilder;
1527 public ConstructorInitializer Initializer;
1528 SecurityType declarative_security;
1529 bool has_compliant_args;
1530 SourceMethodBuilder debug_builder;
1533 // Modifiers allowed for a constructor.
1535 public const Modifiers AllowedModifiers =
1537 Modifiers.PROTECTED |
1538 Modifiers.INTERNAL |
1544 static readonly string[] attribute_targets = new string [] { "method" };
1546 public static readonly string ConstructorName = ".ctor";
1547 public static readonly string TypeConstructorName = ".cctor";
1549 public Constructor (TypeDefinition parent, string name, Modifiers mod, Attributes attrs, ParametersCompiled args, Location loc)
1550 : base (parent, null, mod, AllowedModifiers, new MemberName (name, loc), attrs, args)
1554 public bool HasCompliantArgs {
1556 return has_compliant_args;
1560 public override AttributeTargets AttributeTargets {
1562 return AttributeTargets.Constructor;
1566 bool IMethodData.IsAccessor {
1573 MethodBase IMethodDefinition.Metadata {
1575 return ConstructorBuilder;
1580 // Returns true if this is a default constructor
1582 public bool IsDefault ()
1584 if ((ModFlags & Modifiers.STATIC) != 0)
1585 return parameters.IsEmpty;
1587 return parameters.IsEmpty &&
1588 (Initializer is ConstructorBaseInitializer) &&
1589 (Initializer.Arguments == null);
1592 public override void Accept (StructuralVisitor visitor)
1594 visitor.Visit (this);
1597 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
1599 if (a.IsValidSecurityAttribute ()) {
1600 a.ExtractSecurityPermissionSet (ctor, ref declarative_security);
1604 if (a.Type == pa.MethodImpl) {
1605 is_external_implementation = a.IsInternalCall ();
1608 ConstructorBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
1611 protected override bool CheckBase ()
1613 if ((ModFlags & Modifiers.STATIC) != 0) {
1614 if (!parameters.IsEmpty) {
1615 Report.Error (132, Location, "`{0}': The static constructor must be parameterless",
1616 GetSignatureForError ());
1620 if ((caching_flags & Flags.MethodOverloadsExist) != 0)
1621 Parent.MemberCache.CheckExistingMembersOverloads (this, parameters);
1623 // the rest can be ignored
1627 // Check whether arguments were correct.
1628 if (!DefineParameters (parameters))
1631 if ((caching_flags & Flags.MethodOverloadsExist) != 0)
1632 Parent.MemberCache.CheckExistingMembersOverloads (this, parameters);
1634 if (Parent.PartialContainer.Kind == MemberKind.Struct && parameters.IsEmpty) {
1635 Report.Error (568, Location,
1636 "Structs cannot contain explicit parameterless constructors");
1640 CheckProtectedModifier ();
1646 // Creates the ConstructorBuilder
1648 public override bool Define ()
1650 if (ConstructorBuilder != null)
1653 if (!CheckAbstractAndExtern (block != null))
1656 // Check if arguments were correct.
1660 var ca = ModifiersExtensions.MethodAttr (ModFlags) | MethodAttributes.RTSpecialName | MethodAttributes.SpecialName;
1662 ConstructorBuilder = Parent.TypeBuilder.DefineConstructor (
1663 ca, CallingConventions,
1664 parameters.GetMetaInfo ());
1666 spec = new MethodSpec (MemberKind.Constructor, Parent.Definition, this, Compiler.BuiltinTypes.Void, parameters, ModFlags);
1668 Parent.MemberCache.AddMember (spec);
1670 if (block != null) {
1671 // It's here only to report an error
1672 if (block.IsIterator) {
1673 member_type = Compiler.BuiltinTypes.Void;
1674 Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
1677 if (Compiler.Settings.WriteMetadataOnly)
1687 public override void Emit ()
1689 if (Parent.PartialContainer.IsComImport) {
1690 if (!IsDefault ()) {
1691 Report.Error (669, Location, "`{0}': A class with the ComImport attribute cannot have a user-defined constructor",
1692 Parent.GetSignatureForError ());
1695 // Set as internal implementation and reset block data
1696 // to ensure no IL is generated
1697 ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall);
1701 if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
1702 Module.PredefinedAttributes.DebuggerHidden.EmitAttribute (ConstructorBuilder);
1704 if (OptAttributes != null)
1705 OptAttributes.Emit ();
1708 parameters.ApplyAttributes (this, ConstructorBuilder);
1711 BlockContext bc = new BlockContext (this, block, Compiler.BuiltinTypes.Void);
1712 bc.Set (ResolveContext.Options.ConstructorScope);
1714 if (block != null) {
1716 // If we use a "this (...)" constructor initializer, then
1717 // do not emit field initializers, they are initialized in the other constructor
1719 if (!(Initializer is ConstructorThisInitializer))
1720 Parent.PartialContainer.ResolveFieldInitializers (bc);
1723 if (Initializer == null) {
1724 if (Parent.PartialContainer.Kind == MemberKind.Struct) {
1726 // If this is a non-static `struct' constructor and doesn't have any
1727 // initializer, it must initialize all of the struct's fields.
1729 block.AddThisVariable (bc);
1730 } else if (Parent.PartialContainer.Kind == MemberKind.Class) {
1731 Initializer = new GeneratedBaseInitializer (Location);
1735 if (Initializer != null) {
1737 // mdb format does not support reqions. Try to workaround this by emitting the
1738 // sequence point at initializer. Any breakpoint at constructor header should
1739 // be adjusted to this sequence point as it's the next one which follows.
1741 block.AddScopeStatement (new StatementExpression (Initializer));
1745 if (block.Resolve (null, bc, this)) {
1746 debug_builder = Parent.CreateMethodSymbolEntry ();
1747 EmitContext ec = new EmitContext (this, ConstructorBuilder.GetILGenerator (), bc.ReturnType, debug_builder);
1748 ec.With (EmitContext.Options.ConstructorScope, true);
1754 if (declarative_security != null) {
1755 foreach (var de in declarative_security) {
1757 ConstructorBuilder.__AddDeclarativeSecurity (de);
1759 ConstructorBuilder.AddDeclarativeSecurity (de.Key, de.Value);
1767 protected override MemberSpec FindBaseMember (out MemberSpec bestCandidate, ref bool overrides)
1769 // Is never override
1770 bestCandidate = null;
1774 public override string GetCallerMemberName ()
1776 return IsStatic ? TypeConstructorName : ConstructorName;
1779 public override string GetSignatureForDocumentation ()
1781 return Parent.GetSignatureForDocumentation () + ".#ctor" + parameters.GetSignatureForDocumentation ();
1784 public override string GetSignatureForError()
1786 return base.GetSignatureForError () + parameters.GetSignatureForError ();
1789 public override string[] ValidAttributeTargets {
1791 return attribute_targets;
1795 protected override bool VerifyClsCompliance ()
1797 if (!base.VerifyClsCompliance () || !IsExposedFromAssembly ()) {
1801 if (!parameters.IsEmpty && Parent.Definition.IsAttribute) {
1802 foreach (TypeSpec param in parameters.Types) {
1803 if (param.IsArray) {
1809 has_compliant_args = true;
1813 public override void WriteDebugSymbol (MonoSymbolFile file)
1815 if (debug_builder == null)
1818 var token = ConstructorBuilder.GetToken ();
1819 int t = token.Token;
1821 if (ModuleBuilder.IsPseudoToken (t))
1822 t = Module.Builder.ResolvePseudoToken (t);
1825 debug_builder.DefineMethod (file, t);
1828 #region IMethodData Members
1830 public MemberName MethodName {
1836 public TypeSpec ReturnType {
1842 EmitContext IMethodData.CreateEmitContext (ILGenerator ig, SourceMethodBuilder sourceMethod)
1844 throw new NotImplementedException ();
1851 /// Interface for MethodData class. Holds links to parent members to avoid member duplication.
1853 public interface IMethodData : IMemberContext
1855 CallingConventions CallingConventions { get; }
1856 Location Location { get; }
1857 MemberName MethodName { get; }
1858 TypeSpec ReturnType { get; }
1859 ParametersCompiled ParameterInfo { get; }
1860 MethodSpec Spec { get; }
1861 bool IsAccessor { get; }
1863 Attributes OptAttributes { get; }
1864 ToplevelBlock Block { get; set; }
1866 EmitContext CreateEmitContext (ILGenerator ig, SourceMethodBuilder sourceMethod);
1870 // Encapsulates most of the Method's state
1872 public class MethodData
1874 public readonly IMethodData method;
1877 // Are we implementing an interface ?
1879 public MethodSpec implementing;
1884 protected InterfaceMemberBase member;
1885 protected Modifiers modifiers;
1886 protected MethodAttributes flags;
1887 protected TypeSpec declaring_type;
1888 protected MethodSpec parent_method;
1889 SourceMethodBuilder debug_builder;
1892 MethodBuilder builder;
1893 public MethodBuilder MethodBuilder {
1899 public TypeSpec DeclaringType {
1901 return declaring_type;
1905 public string MetadataName {
1911 public MethodData (InterfaceMemberBase member,
1912 Modifiers modifiers, MethodAttributes flags, IMethodData method)
1914 this.member = member;
1915 this.modifiers = modifiers;
1918 this.method = method;
1921 public MethodData (InterfaceMemberBase member,
1922 Modifiers modifiers, MethodAttributes flags,
1924 MethodSpec parent_method)
1925 : this (member, modifiers, flags, method)
1927 this.parent_method = parent_method;
1930 public bool Define (TypeDefinition container, string method_full_name)
1932 PendingImplementation pending = container.PendingImplementations;
1933 MethodSpec ambig_iface_method;
1934 bool optional = false;
1936 if (pending != null) {
1937 implementing = pending.IsInterfaceMethod (method.MethodName, member.InterfaceType, this, out ambig_iface_method, ref optional);
1939 if (member.InterfaceType != null) {
1940 if (implementing == null) {
1941 if (member is PropertyBase) {
1942 container.Compiler.Report.Error (550, method.Location,
1943 "`{0}' is an accessor not found in interface member `{1}{2}'",
1944 method.GetSignatureForError (), member.InterfaceType.GetSignatureForError (),
1945 member.GetSignatureForError ().Substring (member.GetSignatureForError ().LastIndexOf ('.')));
1948 container.Compiler.Report.Error (539, method.Location,
1949 "`{0}.{1}' in explicit interface declaration is not a member of interface",
1950 member.InterfaceType.GetSignatureForError (), member.ShortName);
1954 if (implementing.IsAccessor && !method.IsAccessor) {
1955 container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
1956 container.Compiler.Report.Error (683, method.Location,
1957 "`{0}' explicit method implementation cannot implement `{1}' because it is an accessor",
1958 member.GetSignatureForError (), implementing.GetSignatureForError ());
1962 if (implementing != null) {
1963 if (!method.IsAccessor) {
1964 if (implementing.IsAccessor) {
1965 container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
1966 container.Compiler.Report.Error (470, method.Location,
1967 "Method `{0}' cannot implement interface accessor `{1}'",
1968 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing));
1970 } else if (implementing.DeclaringType.IsInterface) {
1971 if (!implementing.IsAccessor) {
1972 container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
1973 container.Compiler.Report.Error (686, method.Location,
1974 "Accessor `{0}' cannot implement interface member `{1}' for type `{2}'. Use an explicit interface implementation",
1975 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing), container.GetSignatureForError ());
1977 PropertyBase.PropertyMethod pm = method as PropertyBase.PropertyMethod;
1978 if (pm != null && pm.HasCustomAccessModifier && (pm.ModFlags & Modifiers.PUBLIC) == 0) {
1979 container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
1980 container.Compiler.Report.Error (277, method.Location,
1981 "Accessor `{0}' must be declared public to implement interface member `{1}'",
1982 method.GetSignatureForError (), implementing.GetSignatureForError ());
1989 ambig_iface_method = null;
1993 // For implicit implementations, make sure we are public, for
1994 // explicit implementations, make sure we are private.
1996 if (implementing != null){
1997 if (member.IsExplicitImpl) {
1998 if (method.ParameterInfo.HasParams && !implementing.Parameters.HasParams) {
1999 container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
2000 container.Compiler.Report.Error (466, method.Location,
2001 "`{0}': the explicit interface implementation cannot introduce the params modifier",
2002 method.GetSignatureForError ());
2005 if (ambig_iface_method != null) {
2006 container.Compiler.Report.SymbolRelatedToPreviousError (ambig_iface_method);
2007 container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
2008 container.Compiler.Report.Warning (473, 2, method.Location,
2009 "Explicit interface implementation `{0}' matches more than one interface member. Consider using a non-explicit implementation instead",
2010 method.GetSignatureForError ());
2014 // Setting implementin to null inside this block will trigger a more
2015 // verbose error reporting for missing interface implementations
2017 if (implementing.DeclaringType.IsInterface) {
2019 // If this is an interface method implementation,
2020 // check for public accessibility
2022 if ((flags & MethodAttributes.MemberAccessMask) != MethodAttributes.Public) {
2023 implementing = null;
2024 } else if (optional && (container.Interfaces == null || !container.Definition.Interfaces.Contains (implementing.DeclaringType))) {
2026 // We are not implementing interface when base class already implemented it
2028 implementing = null;
2030 } else if ((flags & MethodAttributes.MemberAccessMask) == MethodAttributes.Private) {
2031 // We may never be private.
2032 implementing = null;
2034 } else if ((modifiers & Modifiers.OVERRIDE) == 0) {
2036 // We may be protected if we're overriding something.
2038 implementing = null;
2043 // Static is not allowed
2045 if ((modifiers & Modifiers.STATIC) != 0){
2046 implementing = null;
2051 // If implementing is still valid, set flags
2053 if (implementing != null){
2055 // When implementing interface methods, set NewSlot
2056 // unless, we are overwriting a method.
2058 if ((modifiers & Modifiers.OVERRIDE) == 0 && implementing.DeclaringType.IsInterface) {
2059 flags |= MethodAttributes.NewSlot;
2062 flags |= MethodAttributes.Virtual | MethodAttributes.HideBySig;
2064 // Set Final unless we're virtual, abstract or already overriding a method.
2065 if ((modifiers & (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE)) == 0)
2066 flags |= MethodAttributes.Final;
2069 // clear the pending implementation flag (requires explicit methods to be defined first)
2071 pending.ImplementMethod (method.MethodName,
2072 member.InterfaceType, this, member.IsExplicitImpl, out ambig_iface_method, ref optional);
2075 // Update indexer accessor name to match implementing abstract accessor
2077 if (!implementing.DeclaringType.IsInterface && !member.IsExplicitImpl && implementing.IsAccessor)
2078 method_full_name = implementing.MemberDefinition.Name;
2081 full_name = method_full_name;
2082 declaring_type = container.Definition;
2087 public void DefineOverride (TypeDefinition container)
2089 if (implementing == null)
2092 if (!member.IsExplicitImpl)
2095 container.TypeBuilder.DefineMethodOverride (builder, (MethodInfo) implementing.GetMetaInfo ());
2099 // Creates partial MethodBuilder for the method when has generic parameters used
2100 // as arguments or return type
2102 public MethodBuilder DefineMethodBuilder (TypeDefinition container)
2104 if (builder != null)
2105 throw new InternalErrorException ();
2107 builder = container.TypeBuilder.DefineMethod (full_name, flags, method.CallingConventions);
2112 // Creates full MethodBuilder for the method
2114 public MethodBuilder DefineMethodBuilder (TypeDefinition container, ParametersCompiled param)
2116 DefineMethodBuilder (container);
2117 builder.SetReturnType (method.ReturnType.GetMetaInfo ());
2118 builder.SetParameters (param.GetMetaInfo ());
2125 public void Emit (TypeDefinition parent)
2127 DefineOverride (parent);
2129 var mc = (IMemberContext) method;
2131 method.ParameterInfo.ApplyAttributes (mc, MethodBuilder);
2133 ToplevelBlock block = method.Block;
2134 if (block != null) {
2135 BlockContext bc = new BlockContext (mc, block, method.ReturnType);
2136 if (block.Resolve (null, bc, method)) {
2137 debug_builder = member.Parent.CreateMethodSymbolEntry ();
2138 EmitContext ec = method.CreateEmitContext (MethodBuilder.GetILGenerator (), debug_builder);
2145 public void WriteDebugSymbol (MonoSymbolFile file)
2147 if (debug_builder == null)
2150 var token = builder.GetToken ();
2151 int t = token.Token;
2153 if (ModuleBuilder.IsPseudoToken (t))
2154 t = member.Module.Builder.ResolvePseudoToken (t);
2157 debug_builder.DefineMethod (file, t);
2161 public class Destructor : MethodOrOperator
2163 const Modifiers AllowedModifiers =
2167 static readonly string[] attribute_targets = new string [] { "method" };
2169 public static readonly string MetadataName = "Finalize";
2171 public Destructor (TypeDefinition parent, Modifiers mod, ParametersCompiled parameters, Attributes attrs, Location l)
2172 : base (parent, null, mod, AllowedModifiers, new MemberName (MetadataName, l), attrs, parameters)
2174 ModFlags &= ~Modifiers.PRIVATE;
2175 ModFlags |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2178 public override void Accept (StructuralVisitor visitor)
2180 visitor.Visit (this);
2183 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
2185 if (a.Type == pa.Conditional) {
2186 Error_ConditionalAttributeIsNotValid ();
2190 base.ApplyAttributeBuilder (a, ctor, cdata, pa);
2193 protected override bool CheckBase ()
2195 // Don't check base, destructors have special syntax
2199 public override bool Define ()
2203 if (Compiler.Settings.WriteMetadataOnly)
2209 public override void Emit()
2211 var base_type = Parent.PartialContainer.BaseType;
2212 if (base_type != null && Block != null) {
2213 var base_dtor = MemberCache.FindMember (base_type,
2214 new MemberFilter (MetadataName, 0, MemberKind.Destructor, null, null), BindingRestriction.InstanceOnly) as MethodSpec;
2216 if (base_dtor == null)
2217 throw new NotImplementedException ();
2219 MethodGroupExpr method_expr = MethodGroupExpr.CreatePredefined (base_dtor, base_type, Location);
2220 method_expr.InstanceExpression = new BaseThis (base_type, Location);
2222 var try_block = new ExplicitBlock (block, block.StartLocation, block.EndLocation) {
2223 IsCompilerGenerated = true
2225 var finaly_block = new ExplicitBlock (block, Location, Location) {
2226 IsCompilerGenerated = true
2230 // 0-size arguments to avoid CS0250 error
2231 // TODO: Should use AddScopeStatement or something else which emits correct
2234 finaly_block.AddStatement (new StatementExpression (new Invocation (method_expr, new Arguments (0)), Location.Null));
2236 var tf = new TryFinally (try_block, finaly_block, Location);
2237 block.WrapIntoDestructor (tf, try_block);
2243 public override string GetSignatureForError ()
2245 return Parent.GetSignatureForError () + ".~" + Parent.MemberName.Name + "()";
2248 protected override bool ResolveMemberType ()
2250 member_type = Compiler.BuiltinTypes.Void;
2254 public override string[] ValidAttributeTargets {
2256 return attribute_targets;
2261 // Ooouh Martin, templates are missing here.
2262 // When it will be possible move here a lot of child code and template method type.
2263 public abstract class AbstractPropertyEventMethod : MemberCore, IMethodData, IMethodDefinition {
2264 protected MethodData method_data;
2265 protected ToplevelBlock block;
2266 protected SecurityType declarative_security;
2268 protected readonly string prefix;
2270 ReturnParameter return_attributes;
2272 public AbstractPropertyEventMethod (InterfaceMemberBase member, string prefix, Attributes attrs, Location loc)
2273 : base (member.Parent, SetupName (prefix, member, loc), attrs)
2275 this.prefix = prefix;
2278 static MemberName SetupName (string prefix, InterfaceMemberBase member, Location loc)
2280 return new MemberName (member.MemberName.Left, prefix + member.ShortName, member.MemberName.ExplicitInterface, loc);
2283 public void UpdateName (InterfaceMemberBase member)
2285 SetMemberName (SetupName (prefix, member, Location));
2288 #region IMethodData Members
2290 public ToplevelBlock Block {
2300 public CallingConventions CallingConventions {
2302 return CallingConventions.Standard;
2306 public EmitContext CreateEmitContext (ILGenerator ig, SourceMethodBuilder sourceMethod)
2308 return new EmitContext (this, ig, ReturnType, sourceMethod);
2311 public bool IsAccessor {
2317 public MemberName MethodName {
2323 public TypeSpec[] ParameterTypes {
2325 return ParameterInfo.Types;
2329 MethodBase IMethodDefinition.Metadata {
2331 return method_data.MethodBuilder;
2335 public abstract ParametersCompiled ParameterInfo { get ; }
2336 public abstract TypeSpec ReturnType { get; }
2340 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
2342 if (a.Type == pa.CLSCompliant || a.Type == pa.Obsolete || a.Type == pa.Conditional) {
2343 Report.Error (1667, a.Location,
2344 "Attribute `{0}' is not valid on property or event accessors. It is valid on `{1}' declarations only",
2345 a.Type.GetSignatureForError (), a.GetValidTargets ());
2349 if (a.IsValidSecurityAttribute ()) {
2350 a.ExtractSecurityPermissionSet (ctor, ref declarative_security);
2354 if (a.Target == AttributeTargets.Method) {
2355 method_data.MethodBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
2359 if (a.Target == AttributeTargets.ReturnValue) {
2360 if (return_attributes == null)
2361 return_attributes = new ReturnParameter (this, method_data.MethodBuilder, Location);
2363 return_attributes.ApplyAttributeBuilder (a, ctor, cdata, pa);
2367 ApplyToExtraTarget (a, ctor, cdata, pa);
2370 protected virtual void ApplyToExtraTarget (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
2372 throw new NotSupportedException ("You forgot to define special attribute target handling");
2375 // It is not supported for the accessors
2376 public sealed override bool Define()
2378 throw new NotSupportedException ();
2381 public virtual void Emit (TypeDefinition parent)
2383 method_data.Emit (parent);
2385 if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
2386 Module.PredefinedAttributes.CompilerGenerated.EmitAttribute (method_data.MethodBuilder);
2387 if (((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0))
2388 Module.PredefinedAttributes.DebuggerHidden.EmitAttribute (method_data.MethodBuilder);
2390 if (ReturnType.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
2391 return_attributes = new ReturnParameter (this, method_data.MethodBuilder, Location);
2392 Module.PredefinedAttributes.Dynamic.EmitAttribute (return_attributes.Builder);
2393 } else if (ReturnType.HasDynamicElement) {
2394 return_attributes = new ReturnParameter (this, method_data.MethodBuilder, Location);
2395 Module.PredefinedAttributes.Dynamic.EmitAttribute (return_attributes.Builder, ReturnType, Location);
2398 if (OptAttributes != null)
2399 OptAttributes.Emit ();
2401 if (declarative_security != null) {
2402 foreach (var de in declarative_security) {
2404 method_data.MethodBuilder.__AddDeclarativeSecurity (de);
2406 method_data.MethodBuilder.AddDeclarativeSecurity (de.Key, de.Value);
2414 public override bool EnableOverloadChecks (MemberCore overload)
2416 if (overload is MethodCore) {
2417 caching_flags |= Flags.MethodOverloadsExist;
2421 // This can only happen with indexers and it will
2422 // be catched as indexer difference
2423 if (overload is AbstractPropertyEventMethod)
2429 public override string GetCallerMemberName ()
2431 return base.GetCallerMemberName ().Substring (prefix.Length);
2434 public override string GetSignatureForDocumentation ()
2436 // should not be called
2437 throw new NotSupportedException ();
2440 public override bool IsClsComplianceRequired()
2445 public override void WriteDebugSymbol (MonoSymbolFile file)
2447 if (method_data != null)
2448 method_data.WriteDebugSymbol (file);
2451 public MethodSpec Spec { get; protected set; }
2454 // Represents header string for documentation comment.
2456 public override string DocCommentHeader {
2457 get { throw new InvalidOperationException ("Unexpected attempt to get doc comment from " + this.GetType () + "."); }
2461 public class Operator : MethodOrOperator {
2463 const Modifiers AllowedModifiers =
2469 public enum OpType : byte {
2479 // Unary and Binary operators
2502 // Implicit and Explicit
2506 // Just because of enum
2510 public readonly OpType OperatorType;
2512 static readonly string [] [] names;
2516 names = new string[(int)OpType.TOP][];
2517 names [(int) OpType.LogicalNot] = new string [] { "!", "op_LogicalNot" };
2518 names [(int) OpType.OnesComplement] = new string [] { "~", "op_OnesComplement" };
2519 names [(int) OpType.Increment] = new string [] { "++", "op_Increment" };
2520 names [(int) OpType.Decrement] = new string [] { "--", "op_Decrement" };
2521 names [(int) OpType.True] = new string [] { "true", "op_True" };
2522 names [(int) OpType.False] = new string [] { "false", "op_False" };
2523 names [(int) OpType.Addition] = new string [] { "+", "op_Addition" };
2524 names [(int) OpType.Subtraction] = new string [] { "-", "op_Subtraction" };
2525 names [(int) OpType.UnaryPlus] = new string [] { "+", "op_UnaryPlus" };
2526 names [(int) OpType.UnaryNegation] = new string [] { "-", "op_UnaryNegation" };
2527 names [(int) OpType.Multiply] = new string [] { "*", "op_Multiply" };
2528 names [(int) OpType.Division] = new string [] { "/", "op_Division" };
2529 names [(int) OpType.Modulus] = new string [] { "%", "op_Modulus" };
2530 names [(int) OpType.BitwiseAnd] = new string [] { "&", "op_BitwiseAnd" };
2531 names [(int) OpType.BitwiseOr] = new string [] { "|", "op_BitwiseOr" };
2532 names [(int) OpType.ExclusiveOr] = new string [] { "^", "op_ExclusiveOr" };
2533 names [(int) OpType.LeftShift] = new string [] { "<<", "op_LeftShift" };
2534 names [(int) OpType.RightShift] = new string [] { ">>", "op_RightShift" };
2535 names [(int) OpType.Equality] = new string [] { "==", "op_Equality" };
2536 names [(int) OpType.Inequality] = new string [] { "!=", "op_Inequality" };
2537 names [(int) OpType.GreaterThan] = new string [] { ">", "op_GreaterThan" };
2538 names [(int) OpType.LessThan] = new string [] { "<", "op_LessThan" };
2539 names [(int) OpType.GreaterThanOrEqual] = new string [] { ">=", "op_GreaterThanOrEqual" };
2540 names [(int) OpType.LessThanOrEqual] = new string [] { "<=", "op_LessThanOrEqual" };
2541 names [(int) OpType.Implicit] = new string [] { "implicit", "op_Implicit" };
2542 names [(int) OpType.Explicit] = new string [] { "explicit", "op_Explicit" };
2545 public Operator (TypeDefinition parent, OpType type, FullNamedExpression ret_type, Modifiers mod_flags, ParametersCompiled parameters,
2546 ToplevelBlock block, Attributes attrs, Location loc)
2547 : base (parent, ret_type, mod_flags, AllowedModifiers, new MemberName (GetMetadataName (type), loc), attrs, parameters)
2549 OperatorType = type;
2553 public override void Accept (StructuralVisitor visitor)
2555 visitor.Visit (this);
2558 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
2560 if (a.Type == pa.Conditional) {
2561 Error_ConditionalAttributeIsNotValid ();
2565 base.ApplyAttributeBuilder (a, ctor, cdata, pa);
2568 public override bool Define ()
2570 const Modifiers RequiredModifiers = Modifiers.PUBLIC | Modifiers.STATIC;
2571 if ((ModFlags & RequiredModifiers) != RequiredModifiers){
2572 Report.Error (558, Location, "User-defined operator `{0}' must be declared static and public", GetSignatureForError ());
2575 if (!base.Define ())
2578 if (block != null) {
2579 if (block.IsIterator) {
2581 // Current method is turned into automatically generated
2582 // wrapper which creates an instance of iterator
2584 Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
2585 ModFlags |= Modifiers.DEBUGGER_HIDDEN;
2588 if (Compiler.Settings.WriteMetadataOnly)
2592 // imlicit and explicit operator of same types are not allowed
2593 if (OperatorType == OpType.Explicit)
2594 Parent.MemberCache.CheckExistingMembersOverloads (this, GetMetadataName (OpType.Implicit), parameters);
2595 else if (OperatorType == OpType.Implicit)
2596 Parent.MemberCache.CheckExistingMembersOverloads (this, GetMetadataName (OpType.Explicit), parameters);
2598 TypeSpec declaring_type = Parent.CurrentType;
2599 TypeSpec return_type = MemberType;
2600 TypeSpec first_arg_type = ParameterTypes [0];
2602 TypeSpec first_arg_type_unwrap = first_arg_type;
2603 if (first_arg_type.IsNullableType)
2604 first_arg_type_unwrap = Nullable.NullableInfo.GetUnderlyingType (first_arg_type);
2606 TypeSpec return_type_unwrap = return_type;
2607 if (return_type.IsNullableType)
2608 return_type_unwrap = Nullable.NullableInfo.GetUnderlyingType (return_type);
2611 // Rules for conversion operators
2613 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
2614 if (first_arg_type_unwrap == return_type_unwrap && first_arg_type_unwrap == declaring_type) {
2615 Report.Error (555, Location,
2616 "User-defined operator cannot take an object of the enclosing type and convert to an object of the enclosing type");
2621 if (declaring_type == return_type || declaring_type == return_type_unwrap) {
2622 conv_type = first_arg_type;
2623 } else if (declaring_type == first_arg_type || declaring_type == first_arg_type_unwrap) {
2624 conv_type = return_type;
2626 Report.Error (556, Location,
2627 "User-defined conversion must convert to or from the enclosing type");
2631 if (conv_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
2632 Report.Error (1964, Location,
2633 "User-defined conversion `{0}' cannot convert to or from the dynamic type",
2634 GetSignatureForError ());
2639 if (conv_type.IsInterface) {
2640 Report.Error (552, Location, "User-defined conversion `{0}' cannot convert to or from an interface type",
2641 GetSignatureForError ());
2645 if (conv_type.IsClass) {
2646 if (TypeSpec.IsBaseClass (declaring_type, conv_type, true)) {
2647 Report.Error (553, Location, "User-defined conversion `{0}' cannot convert to or from a base class",
2648 GetSignatureForError ());
2652 if (TypeSpec.IsBaseClass (conv_type, declaring_type, false)) {
2653 Report.Error (554, Location, "User-defined conversion `{0}' cannot convert to or from a derived class",
2654 GetSignatureForError ());
2658 } else if (OperatorType == OpType.LeftShift || OperatorType == OpType.RightShift) {
2659 if (first_arg_type != declaring_type || parameters.Types[1].BuiltinType != BuiltinTypeSpec.Type.Int) {
2660 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");
2663 } else if (parameters.Count == 1) {
2664 // Checks for Unary operators
2666 if (OperatorType == OpType.Increment || OperatorType == OpType.Decrement) {
2667 if (return_type != declaring_type && !TypeSpec.IsBaseClass (return_type, declaring_type, false)) {
2668 Report.Error (448, Location,
2669 "The return type for ++ or -- operator must be the containing type or derived from the containing type");
2672 if (first_arg_type != declaring_type) {
2674 559, Location, "The parameter type for ++ or -- operator must be the containing type");
2679 if (first_arg_type_unwrap != declaring_type) {
2680 Report.Error (562, Location,
2681 "The parameter type of a unary operator must be the containing type");
2685 if (OperatorType == OpType.True || OperatorType == OpType.False) {
2686 if (return_type.BuiltinType != BuiltinTypeSpec.Type.Bool) {
2689 "The return type of operator True or False " +
2695 } else if (first_arg_type_unwrap != declaring_type) {
2696 // Checks for Binary operators
2698 var second_arg_type = ParameterTypes[1];
2699 if (second_arg_type.IsNullableType)
2700 second_arg_type = Nullable.NullableInfo.GetUnderlyingType (second_arg_type);
2702 if (second_arg_type != declaring_type) {
2703 Report.Error (563, Location,
2704 "One of the parameters of a binary operator must be the containing type");
2712 protected override bool ResolveMemberType ()
2714 if (!base.ResolveMemberType ())
2717 flags |= MethodAttributes.SpecialName | MethodAttributes.HideBySig;
2721 protected override MemberSpec FindBaseMember (out MemberSpec bestCandidate, ref bool overrides)
2723 // Operator cannot be override
2724 bestCandidate = null;
2728 public static string GetName (OpType ot)
2730 return names [(int) ot] [0];
2733 public static string GetName (string metadata_name)
2735 for (int i = 0; i < names.Length; ++i) {
2736 if (names [i] [1] == metadata_name)
2737 return names [i] [0];
2742 public static string GetMetadataName (OpType ot)
2744 return names [(int) ot] [1];
2747 public static string GetMetadataName (string name)
2749 for (int i = 0; i < names.Length; ++i) {
2750 if (names [i] [0] == name)
2751 return names [i] [1];
2756 public static OpType? GetType (string metadata_name)
2758 for (int i = 0; i < names.Length; ++i) {
2759 if (names[i][1] == metadata_name)
2766 public OpType GetMatchingOperator ()
2768 switch (OperatorType) {
2769 case OpType.Equality:
2770 return OpType.Inequality;
2771 case OpType.Inequality:
2772 return OpType.Equality;
2774 return OpType.False;
2777 case OpType.GreaterThan:
2778 return OpType.LessThan;
2779 case OpType.LessThan:
2780 return OpType.GreaterThan;
2781 case OpType.GreaterThanOrEqual:
2782 return OpType.LessThanOrEqual;
2783 case OpType.LessThanOrEqual:
2784 return OpType.GreaterThanOrEqual;
2790 public override string GetSignatureForDocumentation ()
2792 string s = base.GetSignatureForDocumentation ();
2793 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
2794 s = s + "~" + ReturnType.GetSignatureForDocumentation ();
2800 public override string GetSignatureForError ()
2802 StringBuilder sb = new StringBuilder ();
2803 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
2804 sb.AppendFormat ("{0}.{1} operator {2}",
2805 Parent.GetSignatureForError (), GetName (OperatorType),
2806 member_type == null ? type_expr.GetSignatureForError () : member_type.GetSignatureForError ());
2809 sb.AppendFormat ("{0}.operator {1}", Parent.GetSignatureForError (), GetName (OperatorType));
2812 sb.Append (parameters.GetSignatureForError ());
2813 return sb.ToString ();