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 protected 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 override void PrepareEmit ()
171 parameters.ResolveDefaultValues (this);
174 public MethodSpec Spec {
178 protected override bool VerifyClsCompliance ()
180 if (!base.VerifyClsCompliance ())
183 if (parameters.HasArglist) {
184 Report.Warning (3000, 1, Location, "Methods with variable arguments are not CLS-compliant");
187 if (member_type != null && !member_type.IsCLSCompliant ()) {
188 Report.Warning (3002, 1, Location, "Return type of `{0}' is not CLS-compliant",
189 GetSignatureForError ());
192 parameters.VerifyClsCompliance (this);
197 public interface IGenericMethodDefinition : IMethodDefinition
199 TypeParameterSpec[] TypeParameters { get; }
200 int TypeParametersCount { get; }
202 // MethodInfo MakeGenericMethod (TypeSpec[] targs);
205 public sealed class MethodSpec : MemberSpec, IParametersMember
207 MethodBase inflatedMetaInfo;
208 AParametersCollection parameters;
212 TypeParameterSpec[] constraints;
214 public static readonly MethodSpec Excluded = new MethodSpec (MemberKind.Method, InternalType.FakeInternalType, null, null, ParametersCompiled.EmptyReadOnlyParameters, 0);
216 public MethodSpec (MemberKind kind, TypeSpec declaringType, IMethodDefinition details, TypeSpec returnType,
217 AParametersCollection parameters, Modifiers modifiers)
218 : base (kind, declaringType, details, modifiers)
220 this.parameters = parameters;
221 this.returnType = returnType;
226 public override int Arity {
228 return IsGeneric ? GenericDefinition.TypeParametersCount : 0;
232 public TypeParameterSpec[] Constraints {
234 if (constraints == null && IsGeneric)
235 constraints = GenericDefinition.TypeParameters;
241 public bool IsConstructor {
243 return Kind == MemberKind.Constructor;
247 public new IMethodDefinition MemberDefinition {
249 return (IMethodDefinition) definition;
253 public IGenericMethodDefinition GenericDefinition {
255 return (IGenericMethodDefinition) definition;
259 public bool IsAsync {
261 return (Modifiers & Modifiers.ASYNC) != 0;
265 public bool IsExtensionMethod {
267 return IsStatic && parameters.HasExtensionMethodType;
271 public bool IsSealed {
273 return (Modifiers & Modifiers.SEALED) != 0;
277 // When is virtual or abstract
278 public bool IsVirtual {
280 return (Modifiers & (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE)) != 0;
284 public bool IsReservedMethod {
286 return Kind == MemberKind.Operator || IsAccessor;
290 TypeSpec IInterfaceMemberSpec.MemberType {
296 public AParametersCollection Parameters {
302 public TypeSpec ReturnType {
308 public TypeSpec[] TypeArguments {
316 public MethodSpec GetGenericMethodDefinition ()
318 if (!IsGeneric && !DeclaringType.IsGeneric)
321 return MemberCache.GetMember (declaringType, this);
324 public MethodBase GetMetaInfo ()
327 // inflatedMetaInfo is extra field needed for cases where we
328 // inflate method but another nested type can later inflate
329 // again (the cache would be build with inflated metaInfo) and
330 // TypeBuilder can work with method definitions only
332 if (inflatedMetaInfo == null) {
333 if ((state & StateFlags.PendingMetaInflate) != 0) {
334 var dt_meta = DeclaringType.GetMetaInfo ();
336 if (DeclaringType.IsTypeBuilder) {
338 inflatedMetaInfo = TypeBuilder.GetConstructor (dt_meta, (ConstructorInfo) MemberDefinition.Metadata);
340 inflatedMetaInfo = TypeBuilder.GetMethod (dt_meta, (MethodInfo) MemberDefinition.Metadata);
343 // it should not be reached
344 throw new NotImplementedException ();
346 inflatedMetaInfo = MethodInfo.GetMethodFromHandle (MemberDefinition.Metadata.MethodHandle, dt_meta.TypeHandle);
350 state &= ~StateFlags.PendingMetaInflate;
352 inflatedMetaInfo = MemberDefinition.Metadata;
356 if ((state & StateFlags.PendingMakeMethod) != 0) {
357 var sre_targs = new MetaType[targs.Length];
358 for (int i = 0; i < sre_targs.Length; ++i)
359 sre_targs[i] = targs[i].GetMetaInfo ();
361 inflatedMetaInfo = ((MethodInfo) inflatedMetaInfo).MakeGenericMethod (sre_targs);
362 state &= ~StateFlags.PendingMakeMethod;
365 return inflatedMetaInfo;
368 public override string GetSignatureForDocumentation ()
372 case MemberKind.Constructor:
375 case MemberKind.Method:
377 name = Name + "``" + Arity.ToString ();
387 name = DeclaringType.GetSignatureForDocumentation () + "." + name + parameters.GetSignatureForDocumentation ();
388 if (Kind == MemberKind.Operator) {
389 var op = Operator.GetType (Name).Value;
390 if (op == Operator.OpType.Explicit || op == Operator.OpType.Implicit) {
391 name += "~" + ReturnType.GetSignatureForDocumentation ();
398 public override string GetSignatureForError ()
402 name = DeclaringType.GetSignatureForError () + "." + DeclaringType.Name;
403 } else if (Kind == MemberKind.Operator) {
404 var op = Operator.GetType (Name).Value;
405 if (op == Operator.OpType.Implicit || op == Operator.OpType.Explicit) {
406 name = DeclaringType.GetSignatureForError () + "." + Operator.GetName (op) + " operator " + returnType.GetSignatureForError ();
408 name = DeclaringType.GetSignatureForError () + ".operator " + Operator.GetName (op);
410 } else if (IsAccessor) {
411 int split = Name.IndexOf ('_');
412 name = Name.Substring (split + 1);
413 var postfix = Name.Substring (0, split);
415 var pc = parameters.Count;
416 if (pc > 0 && postfix == "get") {
417 name = "this" + parameters.GetSignatureForError ("[", "]", pc);
418 } else if (pc > 1 && postfix == "set") {
419 name = "this" + parameters.GetSignatureForError ("[", "]", pc - 1);
423 return DeclaringType.GetSignatureForError () + "." + name + "." + postfix;
425 name = base.GetSignatureForError ();
427 name += "<" + TypeManager.CSharpName (targs) + ">";
429 name += "<" + TypeManager.CSharpName (GenericDefinition.TypeParameters) + ">";
432 return name + parameters.GetSignatureForError ();
435 public override MemberSpec InflateMember (TypeParameterInflator inflator)
437 var ms = (MethodSpec) base.InflateMember (inflator);
438 ms.inflatedMetaInfo = null;
439 ms.returnType = inflator.Inflate (returnType);
440 ms.parameters = parameters.Inflate (inflator);
442 ms.constraints = TypeParameterSpec.InflateConstraints (inflator, Constraints);
447 public MethodSpec MakeGenericMethod (IMemberContext context, params TypeSpec[] targs)
450 throw new ArgumentNullException ();
452 // if (generic_intances != null && generic_intances.TryGetValue (targs, out ginstance))
455 //if (generic_intances == null)
456 // generic_intances = new Dictionary<TypeSpec[], Method> (TypeSpecArrayComparer.Default);
458 var inflator = new TypeParameterInflator (context, DeclaringType, GenericDefinition.TypeParameters, targs);
460 var inflated = (MethodSpec) MemberwiseClone ();
461 inflated.declaringType = inflator.TypeInstance;
462 inflated.returnType = inflator.Inflate (returnType);
463 inflated.parameters = parameters.Inflate (inflator);
464 inflated.targs = targs;
465 inflated.constraints = TypeParameterSpec.InflateConstraints (inflator, constraints ?? GenericDefinition.TypeParameters);
466 inflated.state |= StateFlags.PendingMakeMethod;
468 // if (inflated.parent == null)
469 // inflated.parent = parent;
471 //generic_intances.Add (targs, inflated);
475 public MethodSpec Mutate (TypeParameterMutator mutator)
477 var targs = TypeArguments;
479 targs = mutator.Mutate (targs);
481 var decl = DeclaringType;
482 if (DeclaringType.IsGenericOrParentIsGeneric) {
483 decl = mutator.Mutate (decl);
486 if (targs == TypeArguments && decl == DeclaringType)
489 var ms = (MethodSpec) MemberwiseClone ();
490 if (decl != DeclaringType) {
491 ms.inflatedMetaInfo = null;
492 ms.declaringType = decl;
493 ms.state |= StateFlags.PendingMetaInflate;
498 ms.state |= StateFlags.PendingMakeMethod;
504 public override List<MissingTypeSpecReference> ResolveMissingDependencies (MemberSpec caller)
506 var missing = returnType.ResolveMissingDependencies (this);
507 foreach (var pt in parameters.Types) {
508 var m = pt.GetMissingDependencies (this);
513 missing = new List<MissingTypeSpecReference> ();
515 missing.AddRange (m);
519 foreach (var tp in GenericDefinition.TypeParameters) {
520 var m = tp.GetMissingDependencies (this);
526 missing = new List<MissingTypeSpecReference> ();
528 missing.AddRange (m);
536 public abstract class MethodOrOperator : MethodCore, IMethodData, IMethodDefinition
538 ReturnParameter return_attributes;
539 SecurityType declarative_security;
540 protected MethodData MethodData;
542 static readonly string[] attribute_targets = new string [] { "method", "return" };
544 protected MethodOrOperator (TypeDefinition parent, FullNamedExpression type, Modifiers mod, Modifiers allowed_mod, MemberName name,
545 Attributes attrs, ParametersCompiled parameters)
546 : base (parent, type, mod, allowed_mod, name, attrs, parameters)
550 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
552 if (a.Target == AttributeTargets.ReturnValue) {
553 if (return_attributes == null)
554 return_attributes = new ReturnParameter (this, MethodBuilder, Location);
556 return_attributes.ApplyAttributeBuilder (a, ctor, cdata, pa);
560 if (a.Type == pa.MethodImpl) {
561 if ((ModFlags & Modifiers.ASYNC) != 0 && (a.GetMethodImplOptions () & MethodImplOptions.Synchronized) != 0) {
562 Report.Error (4015, a.Location, "`{0}': Async methods cannot use `MethodImplOptions.Synchronized'",
563 GetSignatureForError ());
566 is_external_implementation = a.IsInternalCall ();
567 } else if (a.Type == pa.DllImport) {
568 const Modifiers extern_static = Modifiers.EXTERN | Modifiers.STATIC;
569 if ((ModFlags & extern_static) != extern_static) {
570 Report.Error (601, a.Location, "The DllImport attribute must be specified on a method marked `static' and `extern'");
573 if (MemberName.IsGeneric || Parent.IsGenericOrParentIsGeneric) {
574 Report.Error (7042, a.Location,
575 "The DllImport attribute cannot be applied to a method that is generic or contained in a generic type");
578 is_external_implementation = true;
581 if (a.IsValidSecurityAttribute ()) {
582 a.ExtractSecurityPermissionSet (ctor, ref declarative_security);
586 if (MethodBuilder != null)
587 MethodBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
590 public override AttributeTargets AttributeTargets {
592 return AttributeTargets.Method;
596 MethodBase IMethodDefinition.Metadata {
598 return MethodData.MethodBuilder;
602 // TODO: Remove and use MethodData abstraction
603 public MethodBuilder MethodBuilder {
605 return MethodData.MethodBuilder;
609 protected override bool CheckForDuplications ()
611 return Parent.MemberCache.CheckExistingMembersOverloads (this, parameters);
614 public virtual EmitContext CreateEmitContext (ILGenerator ig, SourceMethodBuilder sourceMethod)
616 return new EmitContext (this, ig, MemberType, sourceMethod);
619 public override bool Define ()
628 if (this is Operator)
629 kind = MemberKind.Operator;
630 else if (this is Destructor)
631 kind = MemberKind.Destructor;
633 kind = MemberKind.Method;
635 string explicit_name;
637 if (IsPartialDefinition) {
638 caching_flags &= ~Flags.Excluded_Undetected;
639 caching_flags |= Flags.Excluded;
641 // Add to member cache only when a partial method implementation has not been found yet
642 if ((caching_flags & Flags.PartialDefinitionExists) != 0)
648 explicit_name = null;
650 MethodData = new MethodData (this, ModFlags, flags, this, base_method);
652 if (!MethodData.Define (Parent.PartialContainer, GetFullName (MemberName)))
655 explicit_name = MethodData.MetadataName;
658 spec = new MethodSpec (kind, Parent.Definition, this, ReturnType, parameters, ModFlags);
659 if (MemberName.Arity > 0)
660 spec.IsGeneric = true;
662 Parent.MemberCache.AddMember (this, explicit_name, spec);
667 protected override void DoMemberTypeIndependentChecks ()
669 base.DoMemberTypeIndependentChecks ();
671 CheckAbstractAndExtern (block != null);
673 if ((ModFlags & Modifiers.PARTIAL) != 0) {
674 for (int i = 0; i < parameters.Count; ++i) {
675 IParameterData p = parameters.FixedParameters [i];
676 if ((p.ModFlags & Parameter.Modifier.OUT) != 0) {
677 Report.Error (752, Location, "`{0}': A partial method parameters cannot use `out' modifier",
678 GetSignatureForError ());
681 if (p.HasDefaultValue && IsPartialImplementation)
682 ((Parameter) p).Warning_UselessOptionalParameter (Report);
687 protected override void DoMemberTypeDependentChecks ()
689 base.DoMemberTypeDependentChecks ();
691 if (MemberType.IsStatic) {
692 Error_StaticReturnType ();
696 public override void Emit ()
698 if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
699 Module.PredefinedAttributes.CompilerGenerated.EmitAttribute (MethodBuilder);
700 if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
701 Module.PredefinedAttributes.DebuggerHidden.EmitAttribute (MethodBuilder);
702 if ((ModFlags & Modifiers.DEBUGGER_STEP_THROUGH) != 0)
703 Module.PredefinedAttributes.DebuggerStepThrough.EmitAttribute (MethodBuilder);
705 if (ReturnType.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
706 return_attributes = new ReturnParameter (this, MethodBuilder, Location);
707 Module.PredefinedAttributes.Dynamic.EmitAttribute (return_attributes.Builder);
708 } else if (ReturnType.HasDynamicElement) {
709 return_attributes = new ReturnParameter (this, MethodBuilder, Location);
710 Module.PredefinedAttributes.Dynamic.EmitAttribute (return_attributes.Builder, ReturnType, Location);
713 if (OptAttributes != null)
714 OptAttributes.Emit ();
716 if (declarative_security != null) {
717 foreach (var de in declarative_security) {
719 MethodBuilder.__AddDeclarativeSecurity (de);
721 MethodBuilder.AddDeclarativeSecurity (de.Key, de.Value);
726 if (type_expr != null)
727 ConstraintChecker.Check (this, member_type, type_expr.Location);
731 if (MethodData != null)
732 MethodData.Emit (Parent);
734 if (block != null && block.StateMachine is AsyncTaskStorey) {
735 var psm = Module.PredefinedAttributes.AsyncStateMachine;
736 psm.EmitAttribute (MethodBuilder, block.StateMachine);
739 if ((ModFlags & Modifiers.PARTIAL) == 0)
743 protected void Error_ConditionalAttributeIsNotValid ()
745 Report.Error (577, Location,
746 "Conditional not valid on `{0}' because it is a constructor, destructor, operator or explicit interface implementation",
747 GetSignatureForError ());
750 public bool IsPartialDefinition {
752 return (ModFlags & Modifiers.PARTIAL) != 0 && Block == null;
756 public bool IsPartialImplementation {
758 return (ModFlags & Modifiers.PARTIAL) != 0 && Block != null;
762 public override string[] ValidAttributeTargets {
764 return attribute_targets;
768 #region IMethodData Members
770 bool IMethodData.IsAccessor {
776 public TypeSpec ReturnType {
782 public MemberName MethodName {
789 /// Returns true if method has conditional attribute and the conditions is not defined (method is excluded).
791 public override string[] ConditionalConditions ()
793 if ((caching_flags & (Flags.Excluded_Undetected | Flags.Excluded)) == 0)
796 if ((ModFlags & Modifiers.PARTIAL) != 0 && (caching_flags & Flags.Excluded) != 0)
797 return new string [0];
799 caching_flags &= ~Flags.Excluded_Undetected;
802 if (base_method == null) {
803 if (OptAttributes == null)
806 Attribute[] attrs = OptAttributes.SearchMulti (Module.PredefinedAttributes.Conditional);
810 conditions = new string[attrs.Length];
811 for (int i = 0; i < conditions.Length; ++i)
812 conditions[i] = attrs[i].GetConditionalAttributeValue ();
814 conditions = base_method.MemberDefinition.ConditionalConditions();
817 if (conditions != null)
818 caching_flags |= Flags.Excluded;
825 public override void PrepareEmit ()
829 var mb = MethodData.DefineMethodBuilder (Parent);
831 if (CurrentTypeParameters != null) {
832 string[] gnames = new string[CurrentTypeParameters.Count];
833 for (int i = 0; i < gnames.Length; ++i) {
834 gnames[i] = CurrentTypeParameters[i].Name;
837 var gen_params = MethodBuilder.DefineGenericParameters (gnames);
839 for (int i = 0; i < CurrentTypeParameters.Count; ++i) {
840 var tp = CurrentTypeParameters[i];
842 tp.Define (gen_params[i]);
847 // Generic method has been already defined to resolve method parameters
848 // correctly when they use type parameters
850 mb.SetParameters (parameters.GetMetaInfo ());
851 mb.SetReturnType (ReturnType.GetMetaInfo ());
854 public override void WriteDebugSymbol (MonoSymbolFile file)
856 if (MethodData != null && !IsPartialDefinition)
857 MethodData.WriteDebugSymbol (file);
861 public class Method : MethodOrOperator, IGenericMethodDefinition
863 Method partialMethodImplementation;
865 public Method (TypeDefinition parent, FullNamedExpression return_type, Modifiers mod, MemberName name, ParametersCompiled parameters, Attributes attrs)
866 : base (parent, return_type, mod,
867 parent.PartialContainer.Kind == MemberKind.Interface ? AllowedModifiersInterface :
868 parent.PartialContainer.Kind == MemberKind.Struct ? AllowedModifiersStruct | Modifiers.ASYNC :
869 AllowedModifiersClass | Modifiers.ASYNC,
870 name, attrs, parameters)
874 protected Method (TypeDefinition parent, FullNamedExpression return_type, Modifiers mod, Modifiers amod,
875 MemberName name, ParametersCompiled parameters, Attributes attrs)
876 : base (parent, return_type, mod, amod, name, attrs, parameters)
882 public override TypeParameters CurrentTypeParameters {
884 return MemberName.TypeParameters;
888 public TypeParameterSpec[] TypeParameters {
890 return CurrentTypeParameters.Types;
894 public int TypeParametersCount {
896 return CurrentTypeParameters == null ? 0 : CurrentTypeParameters.Count;
902 public override void Accept (StructuralVisitor visitor)
904 visitor.Visit (this);
907 public static Method Create (TypeDefinition parent, FullNamedExpression returnType, Modifiers mod,
908 MemberName name, ParametersCompiled parameters, Attributes attrs)
910 var m = new Method (parent, returnType, mod, name, parameters, attrs);
912 if ((mod & Modifiers.PARTIAL) != 0) {
913 const Modifiers invalid_partial_mod = Modifiers.AccessibilityMask | Modifiers.ABSTRACT | Modifiers.EXTERN |
914 Modifiers.NEW | Modifiers.OVERRIDE | Modifiers.SEALED | Modifiers.VIRTUAL;
916 if ((mod & invalid_partial_mod) != 0) {
917 m.Report.Error (750, m.Location,
918 "A partial method cannot define access modifier or any of abstract, extern, new, override, sealed, or virtual modifiers");
919 mod &= ~invalid_partial_mod;
922 if ((parent.ModFlags & Modifiers.PARTIAL) == 0) {
923 m.Report.Error (751, m.Location,
924 "A partial method must be declared within a partial class or partial struct");
928 if ((mod & Modifiers.STATIC) == 0 && parameters.HasExtensionMethodType) {
929 m.Report.Error (1105, m.Location, "`{0}': Extension methods must be declared static",
930 m.GetSignatureForError ());
937 public override string GetSignatureForError()
939 return base.GetSignatureForError () + parameters.GetSignatureForError ();
942 void Error_DuplicateEntryPoint (Method b)
944 Report.Error (17, b.Location,
945 "Program `{0}' has more than one entry point defined: `{1}'",
946 b.Module.Builder.ScopeName, b.GetSignatureForError ());
951 if (ReturnType.Kind != MemberKind.Void && ReturnType.BuiltinType != BuiltinTypeSpec.Type.Int)
954 if (parameters.IsEmpty)
957 if (parameters.Count > 1)
960 var ac = parameters.Types [0] as ArrayContainer;
961 return ac != null && ac.Rank == 1 && ac.Element.BuiltinType == BuiltinTypeSpec.Type.String &&
962 (parameters[0].ModFlags & Parameter.Modifier.RefOutMask) == 0;
965 public override FullNamedExpression LookupNamespaceOrType (string name, int arity, LookupMode mode, Location loc)
968 var tp = CurrentTypeParameters;
970 TypeParameter t = tp.Find (name);
972 return new TypeParameterExpr (t, loc);
976 return base.LookupNamespaceOrType (name, arity, mode, loc);
979 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
981 if (a.Type == pa.Conditional) {
982 if (IsExplicitImpl) {
983 Error_ConditionalAttributeIsNotValid ();
987 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
988 Report.Error (243, Location, "Conditional not valid on `{0}' because it is an override method", GetSignatureForError ());
992 if (ReturnType.Kind != MemberKind.Void) {
993 Report.Error (578, Location, "Conditional not valid on `{0}' because its return type is not void", GetSignatureForError ());
998 Report.Error (582, Location, "Conditional not valid on interface members");
1002 if (MethodData.implementing != null) {
1003 Report.SymbolRelatedToPreviousError (MethodData.implementing.DeclaringType);
1004 Report.Error (629, Location, "Conditional member `{0}' cannot implement interface member `{1}'",
1005 GetSignatureForError (), TypeManager.CSharpSignature (MethodData.implementing));
1009 for (int i = 0; i < parameters.Count; ++i) {
1010 if ((parameters.FixedParameters [i].ModFlags & Parameter.Modifier.OUT) != 0) {
1011 Report.Error (685, Location, "Conditional method `{0}' cannot have an out parameter", GetSignatureForError ());
1017 if (a.Type == pa.Extension) {
1018 a.Error_MisusedExtensionAttribute ();
1022 base.ApplyAttributeBuilder (a, ctor, cdata, pa);
1025 void CreateTypeParameters ()
1027 var tparams = MemberName.TypeParameters;
1028 var parent_tparams = Parent.TypeParametersAll;
1030 for (int i = 0; i < MemberName.Arity; i++) {
1031 string type_argument_name = tparams[i].MemberName.Name;
1033 if (block == null) {
1034 int idx = parameters.GetParameterIndexByName (type_argument_name);
1038 b = new ToplevelBlock (Compiler, Location);
1040 b.Error_AlreadyDeclaredTypeParameter (type_argument_name, parameters[i].Location);
1043 INamedBlockVariable variable = null;
1044 block.GetLocalName (type_argument_name, block, ref variable);
1045 if (variable != null)
1046 variable.Block.Error_AlreadyDeclaredTypeParameter (type_argument_name, variable.Location);
1049 if (parent_tparams != null) {
1050 var tp = parent_tparams.Find (type_argument_name);
1052 tparams[i].WarningParentNameConflict (tp);
1057 tparams.Create (null, 0, Parent);
1060 protected virtual void DefineTypeParameters ()
1062 var tparams = CurrentTypeParameters;
1064 TypeParameterSpec[] base_tparams = null;
1065 TypeParameterSpec[] base_decl_tparams = TypeParameterSpec.EmptyTypes;
1066 TypeSpec[] base_targs = TypeSpec.EmptyTypes;
1067 if (((ModFlags & Modifiers.OVERRIDE) != 0 || IsExplicitImpl)) {
1068 MethodSpec base_override = base_method ?? MethodData.implementing;
1070 if (base_override != null) {
1071 base_tparams = base_override.GenericDefinition.TypeParameters;
1073 if (base_override.DeclaringType.IsGeneric) {
1074 base_decl_tparams = base_override.DeclaringType.MemberDefinition.TypeParameters;
1076 if (base_method != null) {
1077 var base_type_parent = CurrentType;
1078 while (base_type_parent.BaseType != base_override.DeclaringType) {
1079 base_type_parent = base_type_parent.BaseType;
1082 base_targs = base_type_parent.BaseType.TypeArguments;
1084 foreach (var iface in Parent.CurrentType.Interfaces) {
1085 if (iface == base_override.DeclaringType) {
1086 base_targs = iface.TypeArguments;
1093 if (base_override.IsGeneric) {
1094 foreach (var base_tp in base_tparams) {
1095 base_tp.BaseType.CheckObsoleteness (this, Location);
1097 if (base_tp.InterfacesDefined != null) {
1098 foreach (var iface in base_tp.InterfacesDefined) {
1099 iface.CheckObsoleteness (this, Location);
1104 if (base_decl_tparams.Length != 0) {
1105 base_decl_tparams = base_decl_tparams.Concat (base_tparams).ToArray ();
1106 base_targs = base_targs.Concat (tparams.Types).ToArray ();
1108 base_decl_tparams = base_tparams;
1109 base_targs = tparams.Types;
1115 for (int i = 0; i < tparams.Count; ++i) {
1116 var tp = tparams [i];
1118 if (base_tparams == null) {
1119 tp.ResolveConstraints (this);
1124 // Copy base constraints for override/explicit methods
1126 var base_tparam = base_tparams [i];
1127 var local_tparam = tp.Type;
1128 local_tparam.SpecialConstraint = base_tparam.SpecialConstraint;
1130 var inflator = new TypeParameterInflator (this, CurrentType, base_decl_tparams, base_targs);
1131 base_tparam.InflateConstraints (inflator, local_tparam);
1134 // Check all type argument constraints for possible collision or unification
1135 // introduced by inflating inherited constraints in this context
1137 // Conflict example:
1139 // class A<T> { virtual void Foo<U> () where U : class, T {} }
1140 // class B : A<int> { override void Foo<U> {} }
1142 var local_tparam_targs = local_tparam.TypeArguments;
1143 if (local_tparam_targs != null) {
1144 for (int ii = 0; ii < local_tparam_targs.Length; ++ii) {
1145 var ta = local_tparam_targs [ii];
1146 if (!ta.IsClass && !ta.IsStruct)
1149 TypeSpec[] unique_tparams = null;
1150 for (int iii = ii + 1; iii < local_tparam_targs.Length; ++iii) {
1152 // Remove any identical or unified constraint types
1154 var tparam_checked = local_tparam_targs [iii];
1155 if (TypeSpecComparer.IsEqual (ta, tparam_checked) || TypeSpec.IsBaseClass (ta, tparam_checked, false)) {
1156 unique_tparams = new TypeSpec[local_tparam_targs.Length - 1];
1157 Array.Copy (local_tparam_targs, 0, unique_tparams, 0, iii);
1158 Array.Copy (local_tparam_targs, iii + 1, unique_tparams, iii, local_tparam_targs.Length - iii - 1);
1159 } else if (!TypeSpec.IsBaseClass (tparam_checked, ta, false)) {
1160 Constraints.Error_ConflictingConstraints (this, local_tparam, ta, tparam_checked, Location);
1164 if (unique_tparams != null) {
1165 local_tparam_targs = unique_tparams;
1166 local_tparam.TypeArguments = local_tparam_targs;
1170 Constraints.CheckConflictingInheritedConstraint (local_tparam, ta, this, Location);
1175 if (base_tparams == null && MethodData != null && MethodData.implementing != null) {
1176 CheckImplementingMethodConstraints (Parent, spec, MethodData.implementing);
1180 public static bool CheckImplementingMethodConstraints (TypeContainer container, MethodSpec method, MethodSpec baseMethod)
1182 var tparams = method.Constraints;
1183 var base_tparams = baseMethod.Constraints;
1184 for (int i = 0; i < tparams.Length; ++i) {
1185 if (!tparams[i].HasSameConstraintsImplementation (base_tparams[i])) {
1186 container.Compiler.Report.SymbolRelatedToPreviousError (method);
1187 container.Compiler.Report.SymbolRelatedToPreviousError (baseMethod);
1189 // Using container location because the interface can be implemented
1191 var tp = (tparams [i].MemberDefinition as MemberCore) ?? container;
1192 container.Compiler.Report.Error (425, tp.Location,
1193 "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",
1194 tparams[i].GetSignatureForError (), method.GetSignatureForError (),
1195 base_tparams[i].GetSignatureForError (), baseMethod.GetSignatureForError ());
1207 public override bool Define ()
1209 if (!base.Define ())
1212 if (member_type.Kind == MemberKind.Void && parameters.IsEmpty && MemberName.Arity == 0 && MemberName.Name == Destructor.MetadataName) {
1213 Report.Warning (465, 1, Location,
1214 "Introducing `Finalize' method can interfere with destructor invocation. Did you intend to declare a destructor?");
1217 if (Compiler.Settings.StdLib && ReturnType.IsSpecialRuntimeType) {
1218 Error1599 (Location, ReturnType, Report);
1222 if (CurrentTypeParameters == null) {
1223 if (base_method != null && !IsExplicitImpl) {
1224 if (parameters.Count == 1 && ParameterTypes[0].BuiltinType == BuiltinTypeSpec.Type.Object && MemberName.Name == "Equals")
1225 Parent.PartialContainer.Mark_HasEquals ();
1226 else if (parameters.IsEmpty && MemberName.Name == "GetHashCode")
1227 Parent.PartialContainer.Mark_HasGetHashCode ();
1231 DefineTypeParameters ();
1234 if (block != null) {
1235 if (block.IsIterator) {
1237 // Current method is turned into automatically generated
1238 // wrapper which creates an instance of iterator
1240 Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
1241 ModFlags |= Modifiers.DEBUGGER_HIDDEN;
1244 if ((ModFlags & Modifiers.ASYNC) != 0) {
1245 if (ReturnType.Kind != MemberKind.Void &&
1246 ReturnType != Module.PredefinedTypes.Task.TypeSpec &&
1247 !ReturnType.IsGenericTask) {
1248 Report.Error (1983, Location, "The return type of an async method must be void, Task, or Task<T>");
1251 block = (ToplevelBlock) block.ConvertToAsyncTask (this, Parent.PartialContainer, parameters, ReturnType, null, Location);
1252 ModFlags |= Modifiers.DEBUGGER_STEP_THROUGH;
1255 if (Compiler.Settings.WriteMetadataOnly)
1259 if ((ModFlags & Modifiers.STATIC) == 0)
1262 if (parameters.HasExtensionMethodType) {
1263 if (Parent.PartialContainer.IsStatic && !Parent.IsGenericOrParentIsGeneric) {
1264 if (!Parent.IsTopLevel)
1265 Report.Error (1109, Location, "`{0}': Extension methods cannot be defined in a nested class",
1266 GetSignatureForError ());
1268 PredefinedAttribute pa = Module.PredefinedAttributes.Extension;
1269 if (!pa.IsDefined) {
1270 Report.Error (1110, Location,
1271 "`{0}': Extension methods require `System.Runtime.CompilerServices.ExtensionAttribute' type to be available. Are you missing an assembly reference?",
1272 GetSignatureForError ());
1275 ModFlags |= Modifiers.METHOD_EXTENSION;
1276 Parent.PartialContainer.ModFlags |= Modifiers.METHOD_EXTENSION;
1277 Spec.DeclaringType.SetExtensionMethodContainer ();
1278 Parent.Module.HasExtensionMethod = true;
1280 Report.Error (1106, Location, "`{0}': Extension methods must be defined in a non-generic static class",
1281 GetSignatureForError ());
1286 // This is used to track the Entry Point,
1288 var settings = Compiler.Settings;
1289 if (settings.NeedsEntryPoint && MemberName.Name == "Main" && !IsPartialDefinition && (settings.MainClass == null || settings.MainClass == Parent.TypeBuilder.FullName)) {
1290 if (IsEntryPoint ()) {
1291 if (Parent.DeclaringAssembly.EntryPoint == null) {
1292 if (Parent.IsGenericOrParentIsGeneric || MemberName.IsGeneric) {
1293 Report.Warning (402, 4, Location, "`{0}': an entry point cannot be generic or in a generic type",
1294 GetSignatureForError ());
1295 } else if ((ModFlags & Modifiers.ASYNC) != 0) {
1296 Report.Error (4009, Location, "`{0}': an entry point cannot be async method",
1297 GetSignatureForError ());
1300 Parent.DeclaringAssembly.EntryPoint = this;
1303 Error_DuplicateEntryPoint (Parent.DeclaringAssembly.EntryPoint);
1304 Error_DuplicateEntryPoint (this);
1307 Report.Warning (28, 4, Location, "`{0}' has the wrong signature to be an entry point",
1308 GetSignatureForError ());
1315 public override void PrepareEmit ()
1317 if (IsPartialDefinition) {
1319 // Use partial method implementation builder for partial method declaration attributes
1321 if (partialMethodImplementation != null) {
1322 MethodData = partialMethodImplementation.MethodData;
1328 base.PrepareEmit ();
1334 public override void Emit ()
1337 if (IsPartialDefinition) {
1338 if (partialMethodImplementation != null && CurrentTypeParameters != null) {
1339 CurrentTypeParameters.CheckPartialConstraints (partialMethodImplementation);
1341 var otp = partialMethodImplementation.CurrentTypeParameters;
1342 for (int i = 0; i < CurrentTypeParameters.Count; ++i) {
1343 var tp = CurrentTypeParameters [i];
1351 if ((ModFlags & Modifiers.PARTIAL) != 0 && (caching_flags & Flags.PartialDefinitionExists) == 0) {
1352 Report.Error (759, Location, "A partial method `{0}' implementation is missing a partial method declaration",
1353 GetSignatureForError ());
1356 if (CurrentTypeParameters != null) {
1357 for (int i = 0; i < CurrentTypeParameters.Count; ++i) {
1358 var tp = CurrentTypeParameters [i];
1360 tp.CheckGenericConstraints (false);
1365 if ((ModFlags & Modifiers.METHOD_EXTENSION) != 0)
1366 Module.PredefinedAttributes.Extension.EmitAttribute (MethodBuilder);
1369 } catch (Exception e) {
1370 throw new InternalErrorException (this, e);
1374 public override bool EnableOverloadChecks (MemberCore overload)
1376 if (overload is Indexer)
1379 return base.EnableOverloadChecks (overload);
1382 public static void Error1599 (Location loc, TypeSpec t, Report Report)
1384 Report.Error (1599, loc, "Method or delegate cannot return type `{0}'", t.GetSignatureForError ());
1387 protected override bool ResolveMemberType ()
1389 if (CurrentTypeParameters != null) {
1390 CreateTypeParameters ();
1393 return base.ResolveMemberType ();
1396 public void SetPartialDefinition (Method methodDefinition)
1398 caching_flags |= Flags.PartialDefinitionExists;
1399 methodDefinition.partialMethodImplementation = this;
1401 // Ensure we are always using method declaration parameters
1402 for (int i = 0; i < methodDefinition.parameters.Count; ++i ) {
1403 var md_p = methodDefinition.parameters [i];
1404 var p = parameters [i];
1406 p.DefaultValue = md_p.DefaultValue;
1407 if (md_p.OptAttributes != null) {
1408 Attributes.AttachFromPartial (p, md_p);
1412 if (methodDefinition.attributes != null) {
1413 if (attributes == null) {
1414 attributes = methodDefinition.attributes;
1416 attributes.Attrs.AddRange (methodDefinition.attributes.Attrs);
1420 if (CurrentTypeParameters != null) {
1421 for (int i = 0; i < CurrentTypeParameters.Count; ++i) {
1422 var tp_other = methodDefinition.CurrentTypeParameters [i];
1423 if (tp_other.OptAttributes == null)
1426 var tp = CurrentTypeParameters [i];
1427 if (tp.OptAttributes == null) {
1428 tp.OptAttributes = tp_other.OptAttributes;
1430 tp.OptAttributes.Attrs.AddRange (tp.OptAttributes.Attrs);
1437 public abstract class ConstructorInitializer : ExpressionStatement
1439 Arguments argument_list;
1440 MethodSpec base_ctor;
1442 protected ConstructorInitializer (Arguments argument_list, Location loc)
1444 this.argument_list = argument_list;
1448 public Arguments Arguments {
1450 return argument_list;
1454 public override bool ContainsEmitWithAwait ()
1456 throw new NotSupportedException ();
1459 public override Expression CreateExpressionTree (ResolveContext ec)
1461 throw new NotSupportedException ("ET");
1464 protected override Expression DoResolve (ResolveContext ec)
1466 eclass = ExprClass.Value;
1469 var caller_builder = (Constructor) ec.MemberContext;
1472 // Spec mandates that constructor initializer will not have `this' access
1474 using (ec.Set (ResolveContext.Options.BaseInitializer)) {
1475 if (argument_list != null) {
1477 argument_list.Resolve (ec, out dynamic);
1480 ec.Report.Error (1975, loc,
1481 "The constructor call cannot be dynamically dispatched within constructor initializer");
1487 type = ec.CurrentType;
1488 if (this is ConstructorBaseInitializer) {
1489 if (ec.CurrentType.BaseType == null)
1492 type = ec.CurrentType.BaseType;
1493 if (ec.CurrentType.IsStruct) {
1494 ec.Report.Error (522, loc,
1495 "`{0}': Struct constructors cannot call base constructors", caller_builder.GetSignatureForError ());
1500 base_ctor = ConstructorLookup (ec, type, ref argument_list, loc);
1503 if (base_ctor != null && base_ctor.MemberDefinition == caller_builder.Spec.MemberDefinition) {
1504 ec.Report.Error (516, loc, "Constructor `{0}' cannot call itself",
1505 caller_builder.GetSignatureForError ());
1511 public override void Emit (EmitContext ec)
1514 // It can be null for struct initializers or System.Object
1516 if (base_ctor == null) {
1517 if (type == ec.BuiltinTypes.Object)
1520 ec.Emit (OpCodes.Ldarg_0);
1521 ec.Emit (OpCodes.Initobj, type);
1525 var call = new CallEmitter ();
1526 call.InstanceExpression = new CompilerGeneratedThis (type, loc);
1527 call.EmitPredefined (ec, base_ctor, argument_list, false);
1530 public override void EmitStatement (EmitContext ec)
1535 public override void FlowAnalysis (FlowAnalysisContext fc)
1537 if (argument_list != null)
1538 argument_list.FlowAnalysis (fc);
1542 public class ConstructorBaseInitializer : ConstructorInitializer {
1543 public ConstructorBaseInitializer (Arguments argument_list, Location l) :
1544 base (argument_list, l)
1549 class GeneratedBaseInitializer: ConstructorBaseInitializer {
1550 public GeneratedBaseInitializer (Location loc, Arguments arguments)
1551 : base (arguments, loc)
1556 public class ConstructorThisInitializer : ConstructorInitializer {
1557 public ConstructorThisInitializer (Arguments argument_list, Location l) :
1558 base (argument_list, l)
1563 public class Constructor : MethodCore, IMethodData, IMethodDefinition
1565 public ConstructorBuilder ConstructorBuilder;
1566 public ConstructorInitializer Initializer;
1567 SecurityType declarative_security;
1568 bool has_compliant_args;
1569 SourceMethodBuilder debug_builder;
1572 // Modifiers allowed for a constructor.
1574 public const Modifiers AllowedModifiers =
1576 Modifiers.PROTECTED |
1577 Modifiers.INTERNAL |
1583 static readonly string[] attribute_targets = new string [] { "method" };
1585 public static readonly string ConstructorName = ".ctor";
1586 public static readonly string TypeConstructorName = ".cctor";
1588 public Constructor (TypeDefinition parent, string name, Modifiers mod, Attributes attrs, ParametersCompiled args, Location loc)
1589 : base (parent, null, mod, AllowedModifiers, new MemberName (name, loc), attrs, args)
1593 public bool HasCompliantArgs {
1595 return has_compliant_args;
1599 public override AttributeTargets AttributeTargets {
1601 return AttributeTargets.Constructor;
1605 bool IMethodData.IsAccessor {
1611 public bool IsPrimaryConstructor { get; set; }
1613 MethodBase IMethodDefinition.Metadata {
1615 return ConstructorBuilder;
1620 // Returns true if this is a default constructor
1622 public bool IsDefault ()
1624 if ((ModFlags & Modifiers.STATIC) != 0)
1625 return parameters.IsEmpty;
1627 return parameters.IsEmpty &&
1628 (Initializer is ConstructorBaseInitializer) &&
1629 (Initializer.Arguments == null);
1632 public override void Accept (StructuralVisitor visitor)
1634 visitor.Visit (this);
1637 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
1639 if (a.IsValidSecurityAttribute ()) {
1640 a.ExtractSecurityPermissionSet (ctor, ref declarative_security);
1644 if (a.Type == pa.MethodImpl) {
1645 is_external_implementation = a.IsInternalCall ();
1648 ConstructorBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
1651 protected override bool CheckBase ()
1653 if ((ModFlags & Modifiers.STATIC) != 0) {
1654 if ((caching_flags & Flags.MethodOverloadsExist) != 0)
1655 Parent.MemberCache.CheckExistingMembersOverloads (this, parameters);
1657 // the rest can be ignored
1661 // Check whether arguments were correct.
1662 if (!DefineParameters (parameters))
1665 if ((caching_flags & Flags.MethodOverloadsExist) != 0)
1666 Parent.MemberCache.CheckExistingMembersOverloads (this, parameters);
1668 CheckProtectedModifier ();
1674 // Creates the ConstructorBuilder
1676 public override bool Define ()
1678 if (ConstructorBuilder != null)
1681 if (!CheckAbstractAndExtern (block != null))
1684 // Check if arguments were correct.
1688 if (Parent.PrimaryConstructorParameters != null && !IsPrimaryConstructor && !IsStatic) {
1689 if (Parent.Kind == MemberKind.Struct && Initializer is ConstructorThisInitializer && Initializer.Arguments == null) {
1690 Report.Error (8043, Location, "`{0}': Structs with primary constructor cannot specify default constructor initializer",
1691 GetSignatureForError ());
1692 } else if (Initializer == null || Initializer is ConstructorBaseInitializer) {
1693 Report.Error (8037, Location, "`{0}': Instance constructor of type with primary constructor must specify `this' constructor initializer",
1694 GetSignatureForError ());
1698 if ((ModFlags & Modifiers.EXTERN) != 0 && Initializer != null) {
1699 Report.Error (8091, Location, "`{0}': Contructors cannot be extern and have a constructor initializer",
1700 GetSignatureForError ());
1703 var ca = ModifiersExtensions.MethodAttr (ModFlags) | MethodAttributes.RTSpecialName | MethodAttributes.SpecialName;
1705 ConstructorBuilder = Parent.TypeBuilder.DefineConstructor (
1706 ca, CallingConventions,
1707 parameters.GetMetaInfo ());
1709 spec = new MethodSpec (MemberKind.Constructor, Parent.Definition, this, Compiler.BuiltinTypes.Void, parameters, ModFlags);
1711 Parent.MemberCache.AddMember (spec);
1713 if (block != null) {
1714 // It's here only to report an error
1715 if (block.IsIterator) {
1716 member_type = Compiler.BuiltinTypes.Void;
1717 Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
1720 if (Compiler.Settings.WriteMetadataOnly)
1730 public override void Emit ()
1732 if (Parent.PartialContainer.IsComImport) {
1733 if (!IsDefault ()) {
1734 Report.Error (669, Location, "`{0}': A class with the ComImport attribute cannot have a user-defined constructor",
1735 Parent.GetSignatureForError ());
1738 // Set as internal implementation and reset block data
1739 // to ensure no IL is generated
1740 ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall);
1744 if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
1745 Module.PredefinedAttributes.DebuggerHidden.EmitAttribute (ConstructorBuilder);
1747 if (OptAttributes != null)
1748 OptAttributes.Emit ();
1751 parameters.ApplyAttributes (this, ConstructorBuilder);
1754 BlockContext bc = new BlockContext (this, block, Compiler.BuiltinTypes.Void);
1755 bc.Set (ResolveContext.Options.ConstructorScope);
1757 if (block != null) {
1758 if (!IsStatic && Initializer == null && Parent.PartialContainer.Kind == MemberKind.Struct) {
1760 // If this is a non-static `struct' constructor and doesn't have any
1761 // initializer, it must initialize all of the struct's fields.
1763 block.AddThisVariable (bc);
1767 // If we use a "this (...)" constructor initializer, then
1768 // do not emit field initializers, they are initialized in the other constructor
1770 if (!(Initializer is ConstructorThisInitializer))
1771 Parent.PartialContainer.ResolveFieldInitializers (bc);
1774 if (Initializer == null && Parent.PartialContainer.Kind == MemberKind.Class) {
1775 Initializer = new GeneratedBaseInitializer (Location, null);
1778 if (Initializer != null) {
1780 // mdb format does not support reqions. Try to workaround this by emitting the
1781 // sequence point at initializer. Any breakpoint at constructor header should
1782 // be adjusted to this sequence point as it's the next one which follows.
1784 block.AddScopeStatement (new StatementExpression (Initializer));
1788 if (block.Resolve (bc, this)) {
1789 debug_builder = Parent.CreateMethodSymbolEntry ();
1790 EmitContext ec = new EmitContext (this, ConstructorBuilder.GetILGenerator (), bc.ReturnType, debug_builder);
1791 ec.With (EmitContext.Options.ConstructorScope, true);
1797 if (declarative_security != null) {
1798 foreach (var de in declarative_security) {
1800 ConstructorBuilder.__AddDeclarativeSecurity (de);
1802 ConstructorBuilder.AddDeclarativeSecurity (de.Key, de.Value);
1810 protected override MemberSpec FindBaseMember (out MemberSpec bestCandidate, ref bool overrides)
1812 // Is never override
1813 bestCandidate = null;
1817 public override string GetCallerMemberName ()
1819 return IsStatic ? TypeConstructorName : ConstructorName;
1822 public override string GetSignatureForDocumentation ()
1824 return Parent.GetSignatureForDocumentation () + ".#ctor" + parameters.GetSignatureForDocumentation ();
1827 public override string GetSignatureForError()
1829 return base.GetSignatureForError () + parameters.GetSignatureForError ();
1832 public override string[] ValidAttributeTargets {
1834 return attribute_targets;
1838 protected override bool VerifyClsCompliance ()
1840 if (!base.VerifyClsCompliance () || !IsExposedFromAssembly ()) {
1844 if (!parameters.IsEmpty && Parent.Definition.IsAttribute) {
1845 foreach (TypeSpec param in parameters.Types) {
1846 if (param.IsArray) {
1852 has_compliant_args = true;
1856 public override void WriteDebugSymbol (MonoSymbolFile file)
1858 if (debug_builder == null)
1861 var token = ConstructorBuilder.GetToken ();
1862 int t = token.Token;
1864 if (ModuleBuilder.IsPseudoToken (t))
1865 t = Module.Builder.ResolvePseudoToken (t);
1868 debug_builder.DefineMethod (file, t);
1871 #region IMethodData Members
1873 public MemberName MethodName {
1879 public TypeSpec ReturnType {
1885 EmitContext IMethodData.CreateEmitContext (ILGenerator ig, SourceMethodBuilder sourceMethod)
1887 throw new NotImplementedException ();
1894 /// Interface for MethodData class. Holds links to parent members to avoid member duplication.
1896 public interface IMethodData : IMemberContext
1898 CallingConventions CallingConventions { get; }
1899 Location Location { get; }
1900 MemberName MethodName { get; }
1901 TypeSpec ReturnType { get; }
1902 ParametersCompiled ParameterInfo { get; }
1903 MethodSpec Spec { get; }
1904 bool IsAccessor { get; }
1906 Attributes OptAttributes { get; }
1907 ToplevelBlock Block { get; set; }
1909 EmitContext CreateEmitContext (ILGenerator ig, SourceMethodBuilder sourceMethod);
1913 // Encapsulates most of the Method's state
1915 public class MethodData
1917 public readonly IMethodData method;
1920 // Are we implementing an interface ?
1922 public MethodSpec implementing;
1927 protected InterfaceMemberBase member;
1928 protected Modifiers modifiers;
1929 protected MethodAttributes flags;
1930 protected TypeSpec declaring_type;
1931 protected MethodSpec parent_method;
1932 SourceMethodBuilder debug_builder;
1935 MethodBuilder builder;
1936 public MethodBuilder MethodBuilder {
1942 public TypeSpec DeclaringType {
1944 return declaring_type;
1948 public string MetadataName {
1954 public MethodData (InterfaceMemberBase member,
1955 Modifiers modifiers, MethodAttributes flags, IMethodData method)
1957 this.member = member;
1958 this.modifiers = modifiers;
1961 this.method = method;
1964 public MethodData (InterfaceMemberBase member,
1965 Modifiers modifiers, MethodAttributes flags,
1967 MethodSpec parent_method)
1968 : this (member, modifiers, flags, method)
1970 this.parent_method = parent_method;
1973 public bool Define (TypeDefinition container, string method_full_name)
1975 PendingImplementation pending = container.PendingImplementations;
1976 MethodSpec ambig_iface_method;
1977 bool optional = false;
1979 if (pending != null) {
1980 implementing = pending.IsInterfaceMethod (method.MethodName, member.InterfaceType, this, out ambig_iface_method, ref optional);
1982 if (member.InterfaceType != null) {
1983 if (implementing == null) {
1984 if (member is PropertyBase) {
1985 container.Compiler.Report.Error (550, method.Location,
1986 "`{0}' is an accessor not found in interface member `{1}{2}'",
1987 method.GetSignatureForError (), member.InterfaceType.GetSignatureForError (),
1988 member.GetSignatureForError ().Substring (member.GetSignatureForError ().LastIndexOf ('.')));
1991 container.Compiler.Report.Error (539, method.Location,
1992 "`{0}.{1}' in explicit interface declaration is not a member of interface",
1993 member.InterfaceType.GetSignatureForError (), member.ShortName);
1997 if (implementing.IsAccessor && !method.IsAccessor) {
1998 container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
1999 container.Compiler.Report.Error (683, method.Location,
2000 "`{0}' explicit method implementation cannot implement `{1}' because it is an accessor",
2001 member.GetSignatureForError (), implementing.GetSignatureForError ());
2005 if (implementing != null && !optional) {
2006 if (!method.IsAccessor) {
2007 if (implementing.IsAccessor) {
2008 container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
2009 container.Compiler.Report.Error (470, method.Location,
2010 "Method `{0}' cannot implement interface accessor `{1}'",
2011 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing));
2013 } else if (implementing.DeclaringType.IsInterface) {
2014 if (!implementing.IsAccessor) {
2015 container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
2016 container.Compiler.Report.Error (686, method.Location,
2017 "Accessor `{0}' cannot implement interface member `{1}' for type `{2}'. Use an explicit interface implementation",
2018 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing), container.GetSignatureForError ());
2020 PropertyBase.PropertyMethod pm = method as PropertyBase.PropertyMethod;
2021 if (pm != null && pm.HasCustomAccessModifier && (pm.ModFlags & Modifiers.PUBLIC) == 0) {
2022 container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
2023 container.Compiler.Report.Error (277, method.Location,
2024 "Accessor `{0}' must be declared public to implement interface member `{1}'",
2025 method.GetSignatureForError (), implementing.GetSignatureForError ());
2032 ambig_iface_method = null;
2036 // For implicit implementations, make sure we are public, for
2037 // explicit implementations, make sure we are private.
2039 if (implementing != null){
2040 if (member.IsExplicitImpl) {
2041 if (method.ParameterInfo.HasParams && !implementing.Parameters.HasParams) {
2042 container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
2043 container.Compiler.Report.Error (466, method.Location,
2044 "`{0}': the explicit interface implementation cannot introduce the params modifier",
2045 method.GetSignatureForError ());
2048 if (ambig_iface_method != null) {
2049 container.Compiler.Report.SymbolRelatedToPreviousError (ambig_iface_method);
2050 container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
2051 container.Compiler.Report.Warning (473, 2, method.Location,
2052 "Explicit interface implementation `{0}' matches more than one interface member. Consider using a non-explicit implementation instead",
2053 method.GetSignatureForError ());
2057 // Setting implementin to null inside this block will trigger a more
2058 // verbose error reporting for missing interface implementations
2060 if (implementing.DeclaringType.IsInterface) {
2062 // If this is an interface method implementation,
2063 // check for public accessibility
2065 if ((flags & MethodAttributes.MemberAccessMask) != MethodAttributes.Public) {
2066 implementing = null;
2067 } else if (optional && (container.Interfaces == null || !container.Definition.Interfaces.Contains (implementing.DeclaringType))) {
2069 // We are not implementing interface when base class already implemented it
2071 implementing = null;
2073 } else if ((flags & MethodAttributes.MemberAccessMask) == MethodAttributes.Private) {
2074 // We may never be private.
2075 implementing = null;
2077 } else if ((modifiers & Modifiers.OVERRIDE) == 0) {
2079 // We may be protected if we're overriding something.
2081 implementing = null;
2086 // Static is not allowed
2088 if ((modifiers & Modifiers.STATIC) != 0){
2089 implementing = null;
2094 // If implementing is still valid, set flags
2096 if (implementing != null){
2098 // When implementing interface methods, set NewSlot
2099 // unless, we are overwriting a method.
2101 if ((modifiers & Modifiers.OVERRIDE) == 0 && implementing.DeclaringType.IsInterface) {
2102 flags |= MethodAttributes.NewSlot;
2105 flags |= MethodAttributes.Virtual | MethodAttributes.HideBySig;
2107 // Set Final unless we're virtual, abstract or already overriding a method.
2108 if ((modifiers & (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE)) == 0)
2109 flags |= MethodAttributes.Final;
2112 // clear the pending implementation flag (requires explicit methods to be defined first)
2114 pending.ImplementMethod (method.MethodName,
2115 member.InterfaceType, this, member.IsExplicitImpl, out ambig_iface_method, ref optional);
2118 // Update indexer accessor name to match implementing abstract accessor
2120 if (!implementing.DeclaringType.IsInterface && !member.IsExplicitImpl && implementing.IsAccessor)
2121 method_full_name = implementing.MemberDefinition.Name;
2124 full_name = method_full_name;
2125 declaring_type = container.Definition;
2130 void DefineOverride (TypeDefinition container)
2132 if (implementing == null)
2135 if (!member.IsExplicitImpl)
2138 container.TypeBuilder.DefineMethodOverride (builder, (MethodInfo) implementing.GetMetaInfo ());
2142 // Creates partial MethodBuilder for the method when has generic parameters used
2143 // as arguments or return type
2145 public MethodBuilder DefineMethodBuilder (TypeDefinition container)
2147 if (builder != null)
2148 throw new InternalErrorException ();
2150 builder = container.TypeBuilder.DefineMethod (full_name, flags, method.CallingConventions);
2155 // Creates full MethodBuilder for the method
2157 public MethodBuilder DefineMethodBuilder (TypeDefinition container, ParametersCompiled param)
2159 DefineMethodBuilder (container);
2160 builder.SetReturnType (method.ReturnType.GetMetaInfo ());
2161 builder.SetParameters (param.GetMetaInfo ());
2168 public void Emit (TypeDefinition parent)
2170 DefineOverride (parent);
2172 method.ParameterInfo.ApplyAttributes (method, MethodBuilder);
2174 ToplevelBlock block = method.Block;
2175 if (block != null) {
2176 BlockContext bc = new BlockContext (method, block, method.ReturnType);
2177 if (block.Resolve (bc, method)) {
2178 debug_builder = member.Parent.CreateMethodSymbolEntry ();
2179 EmitContext ec = method.CreateEmitContext (MethodBuilder.GetILGenerator (), debug_builder);
2186 public void WriteDebugSymbol (MonoSymbolFile file)
2188 if (debug_builder == null)
2191 var token = builder.GetToken ();
2192 int t = token.Token;
2194 if (ModuleBuilder.IsPseudoToken (t))
2195 t = member.Module.Builder.ResolvePseudoToken (t);
2198 debug_builder.DefineMethod (file, t);
2202 public class Destructor : MethodOrOperator
2204 const Modifiers AllowedModifiers =
2208 static readonly string[] attribute_targets = new string [] { "method" };
2210 public static readonly string MetadataName = "Finalize";
2212 public Destructor (TypeDefinition parent, Modifiers mod, ParametersCompiled parameters, Attributes attrs, Location l)
2213 : base (parent, null, mod, AllowedModifiers, new MemberName (MetadataName, l), attrs, parameters)
2215 ModFlags &= ~Modifiers.PRIVATE;
2216 ModFlags |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2219 public override void Accept (StructuralVisitor visitor)
2221 visitor.Visit (this);
2224 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
2226 if (a.Type == pa.Conditional) {
2227 Error_ConditionalAttributeIsNotValid ();
2231 base.ApplyAttributeBuilder (a, ctor, cdata, pa);
2234 protected override bool CheckBase ()
2236 if ((caching_flags & Flags.MethodOverloadsExist) != 0)
2237 CheckForDuplications ();
2239 // Don't check base, destructors have special syntax
2243 public override bool Define ()
2247 if (Compiler.Settings.WriteMetadataOnly)
2253 public override void Emit()
2255 var base_type = Parent.PartialContainer.BaseType;
2256 if (base_type != null && Block != null) {
2257 var base_dtor = MemberCache.FindMember (base_type,
2258 new MemberFilter (MetadataName, 0, MemberKind.Destructor, null, null), BindingRestriction.InstanceOnly) as MethodSpec;
2260 if (base_dtor == null)
2261 throw new NotImplementedException ();
2263 MethodGroupExpr method_expr = MethodGroupExpr.CreatePredefined (base_dtor, base_type, Location);
2264 method_expr.InstanceExpression = new BaseThis (base_type, Location);
2266 var try_block = new ExplicitBlock (block, block.StartLocation, block.EndLocation) {
2267 IsCompilerGenerated = true
2269 var finaly_block = new ExplicitBlock (block, Location, Location) {
2270 IsCompilerGenerated = true
2274 // 0-size arguments to avoid CS0250 error
2275 // TODO: Should use AddScopeStatement or something else which emits correct
2278 finaly_block.AddStatement (new StatementExpression (new Invocation (method_expr, new Arguments (0)), Location.Null));
2280 var tf = new TryFinally (try_block, finaly_block, Location);
2281 block.WrapIntoDestructor (tf, try_block);
2287 public override string GetSignatureForError ()
2289 return Parent.GetSignatureForError () + ".~" + Parent.MemberName.Name + "()";
2292 protected override bool ResolveMemberType ()
2294 member_type = Compiler.BuiltinTypes.Void;
2298 public override string[] ValidAttributeTargets {
2300 return attribute_targets;
2305 // Ooouh Martin, templates are missing here.
2306 // When it will be possible move here a lot of child code and template method type.
2307 public abstract class AbstractPropertyEventMethod : MemberCore, IMethodData, IMethodDefinition {
2308 protected MethodData method_data;
2309 protected ToplevelBlock block;
2310 protected SecurityType declarative_security;
2312 protected readonly string prefix;
2314 ReturnParameter return_attributes;
2316 protected AbstractPropertyEventMethod (InterfaceMemberBase member, string prefix, Attributes attrs, Location loc)
2317 : base (member.Parent, SetupName (prefix, member, loc), attrs)
2319 this.prefix = prefix;
2322 static MemberName SetupName (string prefix, InterfaceMemberBase member, Location loc)
2324 return new MemberName (member.MemberName.Left, prefix + member.ShortName, member.MemberName.ExplicitInterface, loc);
2327 public void UpdateName (InterfaceMemberBase member)
2329 SetMemberName (SetupName (prefix, member, Location));
2332 #region IMethodData Members
2334 public ToplevelBlock Block {
2344 public CallingConventions CallingConventions {
2346 return CallingConventions.Standard;
2350 public EmitContext CreateEmitContext (ILGenerator ig, SourceMethodBuilder sourceMethod)
2352 return new EmitContext (this, ig, ReturnType, sourceMethod);
2355 public bool IsAccessor {
2361 public MemberName MethodName {
2367 public TypeSpec[] ParameterTypes {
2369 return ParameterInfo.Types;
2373 MethodBase IMethodDefinition.Metadata {
2375 return method_data.MethodBuilder;
2379 public abstract ParametersCompiled ParameterInfo { get ; }
2380 public abstract TypeSpec ReturnType { get; }
2384 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
2386 if (a.Type == pa.CLSCompliant || a.Type == pa.Obsolete || a.Type == pa.Conditional) {
2387 Report.Error (1667, a.Location,
2388 "Attribute `{0}' is not valid on property or event accessors. It is valid on `{1}' declarations only",
2389 a.Type.GetSignatureForError (), a.GetValidTargets ());
2393 if (a.IsValidSecurityAttribute ()) {
2394 a.ExtractSecurityPermissionSet (ctor, ref declarative_security);
2398 if (a.Target == AttributeTargets.Method) {
2399 method_data.MethodBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
2403 if (a.Target == AttributeTargets.ReturnValue) {
2404 if (return_attributes == null)
2405 return_attributes = new ReturnParameter (this, method_data.MethodBuilder, Location);
2407 return_attributes.ApplyAttributeBuilder (a, ctor, cdata, pa);
2411 ApplyToExtraTarget (a, ctor, cdata, pa);
2414 protected virtual void ApplyToExtraTarget (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
2416 throw new NotSupportedException ("You forgot to define special attribute target handling");
2419 // It is not supported for the accessors
2420 public sealed override bool Define()
2422 throw new NotSupportedException ();
2425 public virtual void Emit (TypeDefinition parent)
2427 method_data.Emit (parent);
2429 if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
2430 Module.PredefinedAttributes.CompilerGenerated.EmitAttribute (method_data.MethodBuilder);
2431 if (((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0))
2432 Module.PredefinedAttributes.DebuggerHidden.EmitAttribute (method_data.MethodBuilder);
2434 if (ReturnType.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
2435 return_attributes = new ReturnParameter (this, method_data.MethodBuilder, Location);
2436 Module.PredefinedAttributes.Dynamic.EmitAttribute (return_attributes.Builder);
2437 } else if (ReturnType.HasDynamicElement) {
2438 return_attributes = new ReturnParameter (this, method_data.MethodBuilder, Location);
2439 Module.PredefinedAttributes.Dynamic.EmitAttribute (return_attributes.Builder, ReturnType, Location);
2442 if (OptAttributes != null)
2443 OptAttributes.Emit ();
2445 if (declarative_security != null) {
2446 foreach (var de in declarative_security) {
2448 method_data.MethodBuilder.__AddDeclarativeSecurity (de);
2450 method_data.MethodBuilder.AddDeclarativeSecurity (de.Key, de.Value);
2458 public override bool EnableOverloadChecks (MemberCore overload)
2460 if (overload is MethodCore) {
2461 caching_flags |= Flags.MethodOverloadsExist;
2465 // This can only happen with indexers and it will
2466 // be catched as indexer difference
2467 if (overload is AbstractPropertyEventMethod)
2473 public override string GetCallerMemberName ()
2475 return base.GetCallerMemberName ().Substring (prefix.Length);
2478 public override string GetSignatureForDocumentation ()
2480 // should not be called
2481 throw new NotSupportedException ();
2484 public override bool IsClsComplianceRequired()
2489 public void PrepareEmit ()
2491 method_data.DefineMethodBuilder (Parent.PartialContainer, ParameterInfo);
2494 public override void WriteDebugSymbol (MonoSymbolFile file)
2496 if (method_data != null)
2497 method_data.WriteDebugSymbol (file);
2500 public MethodSpec Spec { get; protected set; }
2503 // Represents header string for documentation comment.
2505 public override string DocCommentHeader {
2506 get { throw new InvalidOperationException ("Unexpected attempt to get doc comment from " + this.GetType () + "."); }
2510 public class Operator : MethodOrOperator {
2512 const Modifiers AllowedModifiers =
2518 public enum OpType : byte {
2528 // Unary and Binary operators
2551 // Implicit and Explicit
2558 // Just because of enum
2562 public readonly OpType OperatorType;
2564 static readonly string [] [] names;
2568 names = new string[(int)OpType.TOP][];
2569 names [(int) OpType.LogicalNot] = new string [] { "!", "op_LogicalNot" };
2570 names [(int) OpType.OnesComplement] = new string [] { "~", "op_OnesComplement" };
2571 names [(int) OpType.Increment] = new string [] { "++", "op_Increment" };
2572 names [(int) OpType.Decrement] = new string [] { "--", "op_Decrement" };
2573 names [(int) OpType.True] = new string [] { "true", "op_True" };
2574 names [(int) OpType.False] = new string [] { "false", "op_False" };
2575 names [(int) OpType.Addition] = new string [] { "+", "op_Addition" };
2576 names [(int) OpType.Subtraction] = new string [] { "-", "op_Subtraction" };
2577 names [(int) OpType.UnaryPlus] = new string [] { "+", "op_UnaryPlus" };
2578 names [(int) OpType.UnaryNegation] = new string [] { "-", "op_UnaryNegation" };
2579 names [(int) OpType.Multiply] = new string [] { "*", "op_Multiply" };
2580 names [(int) OpType.Division] = new string [] { "/", "op_Division" };
2581 names [(int) OpType.Modulus] = new string [] { "%", "op_Modulus" };
2582 names [(int) OpType.BitwiseAnd] = new string [] { "&", "op_BitwiseAnd" };
2583 names [(int) OpType.BitwiseOr] = new string [] { "|", "op_BitwiseOr" };
2584 names [(int) OpType.ExclusiveOr] = new string [] { "^", "op_ExclusiveOr" };
2585 names [(int) OpType.LeftShift] = new string [] { "<<", "op_LeftShift" };
2586 names [(int) OpType.RightShift] = new string [] { ">>", "op_RightShift" };
2587 names [(int) OpType.Equality] = new string [] { "==", "op_Equality" };
2588 names [(int) OpType.Inequality] = new string [] { "!=", "op_Inequality" };
2589 names [(int) OpType.GreaterThan] = new string [] { ">", "op_GreaterThan" };
2590 names [(int) OpType.LessThan] = new string [] { "<", "op_LessThan" };
2591 names [(int) OpType.GreaterThanOrEqual] = new string [] { ">=", "op_GreaterThanOrEqual" };
2592 names [(int) OpType.LessThanOrEqual] = new string [] { "<=", "op_LessThanOrEqual" };
2593 names [(int) OpType.Implicit] = new string [] { "implicit", "op_Implicit" };
2594 names [(int) OpType.Explicit] = new string [] { "explicit", "op_Explicit" };
2595 names [(int) OpType.Is] = new string[] { "is", "op_Is" };
2598 public Operator (TypeDefinition parent, OpType type, FullNamedExpression ret_type, Modifiers mod_flags, ParametersCompiled parameters,
2599 ToplevelBlock block, Attributes attrs, Location loc)
2600 : base (parent, ret_type, mod_flags, AllowedModifiers, new MemberName (GetMetadataName (type), loc), attrs, parameters)
2602 OperatorType = type;
2606 public override void Accept (StructuralVisitor visitor)
2608 visitor.Visit (this);
2611 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
2613 if (a.Type == pa.Conditional) {
2614 Error_ConditionalAttributeIsNotValid ();
2618 base.ApplyAttributeBuilder (a, ctor, cdata, pa);
2621 public override bool Define ()
2623 const Modifiers RequiredModifiers = Modifiers.PUBLIC | Modifiers.STATIC;
2624 if ((ModFlags & RequiredModifiers) != RequiredModifiers){
2625 Report.Error (558, Location, "User-defined operator `{0}' must be declared static and public", GetSignatureForError ());
2628 if (!base.Define ())
2631 if (block != null) {
2632 if (block.IsIterator) {
2634 // Current method is turned into automatically generated
2635 // wrapper which creates an instance of iterator
2637 Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
2638 ModFlags |= Modifiers.DEBUGGER_HIDDEN;
2641 if (Compiler.Settings.WriteMetadataOnly)
2645 // imlicit and explicit operator of same types are not allowed
2646 if (OperatorType == OpType.Explicit)
2647 Parent.MemberCache.CheckExistingMembersOverloads (this, GetMetadataName (OpType.Implicit), parameters);
2648 else if (OperatorType == OpType.Implicit)
2649 Parent.MemberCache.CheckExistingMembersOverloads (this, GetMetadataName (OpType.Explicit), parameters);
2651 TypeSpec declaring_type = Parent.PartialContainer.CurrentType;
2652 TypeSpec return_type = MemberType;
2653 TypeSpec first_arg_type = ParameterTypes [0];
2655 TypeSpec first_arg_type_unwrap = first_arg_type;
2656 if (first_arg_type.IsNullableType)
2657 first_arg_type_unwrap = Nullable.NullableInfo.GetUnderlyingType (first_arg_type);
2659 TypeSpec return_type_unwrap = return_type;
2660 if (return_type.IsNullableType)
2661 return_type_unwrap = Nullable.NullableInfo.GetUnderlyingType (return_type);
2664 // Rules for conversion operators
2666 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
2667 if (first_arg_type_unwrap == return_type_unwrap && first_arg_type_unwrap == declaring_type) {
2668 Report.Error (555, Location,
2669 "User-defined operator cannot take an object of the enclosing type and convert to an object of the enclosing type");
2674 if (declaring_type == return_type || declaring_type == return_type_unwrap) {
2675 conv_type = first_arg_type;
2676 } else if (declaring_type == first_arg_type || declaring_type == first_arg_type_unwrap) {
2677 conv_type = return_type;
2679 Report.Error (556, Location,
2680 "User-defined conversion must convert to or from the enclosing type");
2684 if (conv_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
2685 Report.Error (1964, Location,
2686 "User-defined conversion `{0}' cannot convert to or from the dynamic type",
2687 GetSignatureForError ());
2692 if (conv_type.IsInterface) {
2693 Report.Error (552, Location, "User-defined conversion `{0}' cannot convert to or from an interface type",
2694 GetSignatureForError ());
2698 if (conv_type.IsClass) {
2699 if (TypeSpec.IsBaseClass (declaring_type, conv_type, true)) {
2700 Report.Error (553, Location, "User-defined conversion `{0}' cannot convert to or from a base class",
2701 GetSignatureForError ());
2705 if (TypeSpec.IsBaseClass (conv_type, declaring_type, false)) {
2706 Report.Error (554, Location, "User-defined conversion `{0}' cannot convert to or from a derived class",
2707 GetSignatureForError ());
2711 } else if (OperatorType == OpType.LeftShift || OperatorType == OpType.RightShift) {
2712 if (first_arg_type != declaring_type || parameters.Types[1].BuiltinType != BuiltinTypeSpec.Type.Int) {
2713 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");
2716 } else if (parameters.Count == 1) {
2717 // Checks for Unary operators
2719 if (OperatorType == OpType.Increment || OperatorType == OpType.Decrement) {
2720 if (return_type != declaring_type && !TypeSpec.IsBaseClass (return_type, declaring_type, false)) {
2721 Report.Error (448, Location,
2722 "The return type for ++ or -- operator must be the containing type or derived from the containing type");
2725 if (first_arg_type != declaring_type) {
2727 559, Location, "The parameter type for ++ or -- operator must be the containing type");
2732 if (first_arg_type_unwrap != declaring_type) {
2733 Report.Error (562, Location,
2734 "The parameter type of a unary operator must be the containing type");
2738 if (OperatorType == OpType.True || OperatorType == OpType.False) {
2739 if (return_type.BuiltinType != BuiltinTypeSpec.Type.Bool) {
2742 "The return type of operator True or False " +
2748 } else if (first_arg_type_unwrap != declaring_type) {
2749 // Checks for Binary operators
2751 var second_arg_type = ParameterTypes[1];
2752 if (second_arg_type.IsNullableType)
2753 second_arg_type = Nullable.NullableInfo.GetUnderlyingType (second_arg_type);
2755 if (second_arg_type != declaring_type) {
2756 Report.Error (563, Location,
2757 "One of the parameters of a binary operator must be the containing type");
2765 protected override bool ResolveMemberType ()
2767 if (!base.ResolveMemberType ())
2770 flags |= MethodAttributes.SpecialName | MethodAttributes.HideBySig;
2774 protected override MemberSpec FindBaseMember (out MemberSpec bestCandidate, ref bool overrides)
2776 // Operator cannot be override
2777 bestCandidate = null;
2781 public static string GetName (OpType ot)
2783 return names [(int) ot] [0];
2786 public static string GetName (string metadata_name)
2788 for (int i = 0; i < names.Length; ++i) {
2789 if (names [i] [1] == metadata_name)
2790 return names [i] [0];
2795 public static string GetMetadataName (OpType ot)
2797 return names [(int) ot] [1];
2800 public static string GetMetadataName (string name)
2802 for (int i = 0; i < names.Length; ++i) {
2803 if (names [i] [0] == name)
2804 return names [i] [1];
2809 public static OpType? GetType (string metadata_name)
2811 for (int i = 0; i < names.Length; ++i) {
2812 if (names[i][1] == metadata_name)
2819 public OpType GetMatchingOperator ()
2821 switch (OperatorType) {
2822 case OpType.Equality:
2823 return OpType.Inequality;
2824 case OpType.Inequality:
2825 return OpType.Equality;
2827 return OpType.False;
2830 case OpType.GreaterThan:
2831 return OpType.LessThan;
2832 case OpType.LessThan:
2833 return OpType.GreaterThan;
2834 case OpType.GreaterThanOrEqual:
2835 return OpType.LessThanOrEqual;
2836 case OpType.LessThanOrEqual:
2837 return OpType.GreaterThanOrEqual;
2843 public override string GetSignatureForDocumentation ()
2845 string s = base.GetSignatureForDocumentation ();
2846 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
2847 s = s + "~" + ReturnType.GetSignatureForDocumentation ();
2853 public override string GetSignatureForError ()
2855 StringBuilder sb = new StringBuilder ();
2856 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
2857 sb.AppendFormat ("{0}.{1} operator {2}",
2858 Parent.GetSignatureForError (), GetName (OperatorType),
2859 member_type == null ? type_expr.GetSignatureForError () : member_type.GetSignatureForError ());
2862 sb.AppendFormat ("{0}.operator {1}", Parent.GetSignatureForError (), GetName (OperatorType));
2865 sb.Append (parameters.GetSignatureForError ());
2866 return sb.ToString ();