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 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 static readonly MethodSpec Excluded = new MethodSpec (MemberKind.Method, InternalType.FakeInternalType, null, null, ParametersCompiled.EmptyReadOnlyParameters, 0);
210 public MethodSpec (MemberKind kind, TypeSpec declaringType, IMethodDefinition details, TypeSpec returnType,
211 AParametersCollection parameters, Modifiers modifiers)
212 : base (kind, declaringType, details, modifiers)
214 this.parameters = parameters;
215 this.returnType = returnType;
220 public override int Arity {
222 return IsGeneric ? GenericDefinition.TypeParametersCount : 0;
226 public TypeParameterSpec[] Constraints {
228 if (constraints == null && IsGeneric)
229 constraints = GenericDefinition.TypeParameters;
235 public bool IsConstructor {
237 return Kind == MemberKind.Constructor;
241 public new IMethodDefinition MemberDefinition {
243 return (IMethodDefinition) definition;
247 public IGenericMethodDefinition GenericDefinition {
249 return (IGenericMethodDefinition) definition;
253 public bool IsAsync {
255 return (Modifiers & Modifiers.ASYNC) != 0;
259 public bool IsExtensionMethod {
261 return IsStatic && parameters.HasExtensionMethodType;
265 public bool IsSealed {
267 return (Modifiers & Modifiers.SEALED) != 0;
271 // When is virtual or abstract
272 public bool IsVirtual {
274 return (Modifiers & (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE)) != 0;
278 public bool IsReservedMethod {
280 return Kind == MemberKind.Operator || IsAccessor;
284 TypeSpec IInterfaceMemberSpec.MemberType {
290 public AParametersCollection Parameters {
296 public TypeSpec ReturnType {
302 public TypeSpec[] TypeArguments {
310 public MethodSpec GetGenericMethodDefinition ()
312 if (!IsGeneric && !DeclaringType.IsGeneric)
315 return MemberCache.GetMember (declaringType, this);
318 public MethodBase GetMetaInfo ()
321 // inflatedMetaInfo is extra field needed for cases where we
322 // inflate method but another nested type can later inflate
323 // again (the cache would be build with inflated metaInfo) and
324 // TypeBuilder can work with method definitions only
326 if (inflatedMetaInfo == null) {
327 if ((state & StateFlags.PendingMetaInflate) != 0) {
328 var dt_meta = DeclaringType.GetMetaInfo ();
330 if (DeclaringType.IsTypeBuilder) {
332 inflatedMetaInfo = TypeBuilder.GetConstructor (dt_meta, (ConstructorInfo) MemberDefinition.Metadata);
334 inflatedMetaInfo = TypeBuilder.GetMethod (dt_meta, (MethodInfo) MemberDefinition.Metadata);
337 // it should not be reached
338 throw new NotImplementedException ();
340 inflatedMetaInfo = MethodInfo.GetMethodFromHandle (MemberDefinition.Metadata.MethodHandle, dt_meta.TypeHandle);
344 state &= ~StateFlags.PendingMetaInflate;
346 inflatedMetaInfo = MemberDefinition.Metadata;
350 if ((state & StateFlags.PendingMakeMethod) != 0) {
351 var sre_targs = new MetaType[targs.Length];
352 for (int i = 0; i < sre_targs.Length; ++i)
353 sre_targs[i] = targs[i].GetMetaInfo ();
355 inflatedMetaInfo = ((MethodInfo) inflatedMetaInfo).MakeGenericMethod (sre_targs);
356 state &= ~StateFlags.PendingMakeMethod;
359 return inflatedMetaInfo;
362 public override string GetSignatureForDocumentation ()
366 case MemberKind.Constructor:
369 case MemberKind.Method:
371 name = Name + "``" + Arity.ToString ();
381 name = DeclaringType.GetSignatureForDocumentation () + "." + name + parameters.GetSignatureForDocumentation ();
382 if (Kind == MemberKind.Operator) {
383 var op = Operator.GetType (Name).Value;
384 if (op == Operator.OpType.Explicit || op == Operator.OpType.Implicit) {
385 name += "~" + ReturnType.GetSignatureForDocumentation ();
392 public override string GetSignatureForError ()
396 name = DeclaringType.GetSignatureForError () + "." + DeclaringType.Name;
397 } else if (Kind == MemberKind.Operator) {
398 var op = Operator.GetType (Name).Value;
399 if (op == Operator.OpType.Implicit || op == Operator.OpType.Explicit) {
400 name = DeclaringType.GetSignatureForError () + "." + Operator.GetName (op) + " operator " + returnType.GetSignatureForError ();
402 name = DeclaringType.GetSignatureForError () + ".operator " + Operator.GetName (op);
404 } else if (IsAccessor) {
405 int split = Name.IndexOf ('_');
406 name = Name.Substring (split + 1);
407 var postfix = Name.Substring (0, split);
409 var pc = parameters.Count;
410 if (pc > 0 && postfix == "get") {
411 name = "this" + parameters.GetSignatureForError ("[", "]", pc);
412 } else if (pc > 1 && postfix == "set") {
413 name = "this" + parameters.GetSignatureForError ("[", "]", pc - 1);
417 return DeclaringType.GetSignatureForError () + "." + name + "." + postfix;
419 name = base.GetSignatureForError ();
421 name += "<" + TypeManager.CSharpName (targs) + ">";
423 name += "<" + TypeManager.CSharpName (GenericDefinition.TypeParameters) + ">";
426 return name + parameters.GetSignatureForError ();
429 public override MemberSpec InflateMember (TypeParameterInflator inflator)
431 var ms = (MethodSpec) base.InflateMember (inflator);
432 ms.inflatedMetaInfo = null;
433 ms.returnType = inflator.Inflate (returnType);
434 ms.parameters = parameters.Inflate (inflator);
436 ms.constraints = TypeParameterSpec.InflateConstraints (inflator, Constraints);
441 public MethodSpec MakeGenericMethod (IMemberContext context, params TypeSpec[] targs)
444 throw new ArgumentNullException ();
446 // if (generic_intances != null && generic_intances.TryGetValue (targs, out ginstance))
449 //if (generic_intances == null)
450 // generic_intances = new Dictionary<TypeSpec[], Method> (TypeSpecArrayComparer.Default);
452 var inflator = new TypeParameterInflator (context, DeclaringType, GenericDefinition.TypeParameters, targs);
454 var inflated = (MethodSpec) MemberwiseClone ();
455 inflated.declaringType = inflator.TypeInstance;
456 inflated.returnType = inflator.Inflate (returnType);
457 inflated.parameters = parameters.Inflate (inflator);
458 inflated.targs = targs;
459 inflated.constraints = TypeParameterSpec.InflateConstraints (inflator, constraints ?? GenericDefinition.TypeParameters);
460 inflated.state |= StateFlags.PendingMakeMethod;
462 // if (inflated.parent == null)
463 // inflated.parent = parent;
465 //generic_intances.Add (targs, inflated);
469 public MethodSpec Mutate (TypeParameterMutator mutator)
471 var targs = TypeArguments;
473 targs = mutator.Mutate (targs);
475 var decl = DeclaringType;
476 if (DeclaringType.IsGenericOrParentIsGeneric) {
477 decl = mutator.Mutate (decl);
480 if (targs == TypeArguments && decl == DeclaringType)
483 var ms = (MethodSpec) MemberwiseClone ();
484 if (decl != DeclaringType) {
485 ms.inflatedMetaInfo = null;
486 ms.declaringType = decl;
487 ms.state |= StateFlags.PendingMetaInflate;
492 ms.state |= StateFlags.PendingMakeMethod;
498 public override List<MissingTypeSpecReference> ResolveMissingDependencies (MemberSpec caller)
500 var missing = returnType.ResolveMissingDependencies (this);
501 foreach (var pt in parameters.Types) {
502 var m = pt.GetMissingDependencies (this);
507 missing = new List<MissingTypeSpecReference> ();
509 missing.AddRange (m);
513 foreach (var tp in GenericDefinition.TypeParameters) {
514 var m = tp.GetMissingDependencies (this);
520 missing = new List<MissingTypeSpecReference> ();
522 missing.AddRange (m);
530 public abstract class MethodOrOperator : MethodCore, IMethodData, IMethodDefinition
532 ReturnParameter return_attributes;
533 SecurityType declarative_security;
534 protected MethodData MethodData;
536 static readonly string[] attribute_targets = new string [] { "method", "return" };
538 protected MethodOrOperator (TypeDefinition parent, FullNamedExpression type, Modifiers mod, Modifiers allowed_mod, MemberName name,
539 Attributes attrs, ParametersCompiled parameters)
540 : base (parent, type, mod, allowed_mod, name, attrs, parameters)
544 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
546 if (a.Target == AttributeTargets.ReturnValue) {
547 if (return_attributes == null)
548 return_attributes = new ReturnParameter (this, MethodBuilder, Location);
550 return_attributes.ApplyAttributeBuilder (a, ctor, cdata, pa);
554 if (a.Type == pa.MethodImpl) {
555 if ((ModFlags & Modifiers.ASYNC) != 0 && (a.GetMethodImplOptions () & MethodImplOptions.Synchronized) != 0) {
556 Report.Error (4015, a.Location, "`{0}': Async methods cannot use `MethodImplOptions.Synchronized'",
557 GetSignatureForError ());
560 is_external_implementation = a.IsInternalCall ();
561 } else if (a.Type == pa.DllImport) {
562 const Modifiers extern_static = Modifiers.EXTERN | Modifiers.STATIC;
563 if ((ModFlags & extern_static) != extern_static) {
564 Report.Error (601, a.Location, "The DllImport attribute must be specified on a method marked `static' and `extern'");
567 if (MemberName.IsGeneric || Parent.IsGenericOrParentIsGeneric) {
568 Report.Error (7042, a.Location,
569 "The DllImport attribute cannot be applied to a method that is generic or contained in a generic type");
572 is_external_implementation = true;
575 if (a.IsValidSecurityAttribute ()) {
576 a.ExtractSecurityPermissionSet (ctor, ref declarative_security);
580 if (MethodBuilder != null)
581 MethodBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
584 public override AttributeTargets AttributeTargets {
586 return AttributeTargets.Method;
590 MethodBase IMethodDefinition.Metadata {
592 return MethodData.MethodBuilder;
596 // TODO: Remove and use MethodData abstraction
597 public MethodBuilder MethodBuilder {
599 return MethodData.MethodBuilder;
603 protected override bool CheckForDuplications ()
605 return Parent.MemberCache.CheckExistingMembersOverloads (this, parameters);
608 public virtual EmitContext CreateEmitContext (ILGenerator ig, SourceMethodBuilder sourceMethod)
610 return new EmitContext (this, ig, MemberType, sourceMethod);
613 public override bool Define ()
622 if (this is Operator)
623 kind = MemberKind.Operator;
624 else if (this is Destructor)
625 kind = MemberKind.Destructor;
627 kind = MemberKind.Method;
629 string explicit_name;
631 if (IsPartialDefinition) {
632 caching_flags &= ~Flags.Excluded_Undetected;
633 caching_flags |= Flags.Excluded;
635 // Add to member cache only when a partial method implementation has not been found yet
636 if ((caching_flags & Flags.PartialDefinitionExists) != 0)
642 explicit_name = null;
644 MethodData = new MethodData (this, ModFlags, flags, this, base_method);
646 if (!MethodData.Define (Parent.PartialContainer, GetFullName (MemberName)))
649 explicit_name = MethodData.MetadataName;
652 spec = new MethodSpec (kind, Parent.Definition, this, ReturnType, parameters, ModFlags);
653 if (MemberName.Arity > 0)
654 spec.IsGeneric = true;
656 Parent.MemberCache.AddMember (this, explicit_name, spec);
661 protected override void DoMemberTypeIndependentChecks ()
663 base.DoMemberTypeIndependentChecks ();
665 CheckAbstractAndExtern (block != null);
667 if ((ModFlags & Modifiers.PARTIAL) != 0) {
668 for (int i = 0; i < parameters.Count; ++i) {
669 IParameterData p = parameters.FixedParameters [i];
670 if ((p.ModFlags & Parameter.Modifier.OUT) != 0) {
671 Report.Error (752, Location, "`{0}': A partial method parameters cannot use `out' modifier",
672 GetSignatureForError ());
675 if (p.HasDefaultValue && IsPartialImplementation)
676 ((Parameter) p).Warning_UselessOptionalParameter (Report);
681 protected override void DoMemberTypeDependentChecks ()
683 base.DoMemberTypeDependentChecks ();
685 if (MemberType.IsStatic) {
686 Error_StaticReturnType ();
690 public override void Emit ()
692 if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
693 Module.PredefinedAttributes.CompilerGenerated.EmitAttribute (MethodBuilder);
694 if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
695 Module.PredefinedAttributes.DebuggerHidden.EmitAttribute (MethodBuilder);
696 if ((ModFlags & Modifiers.DEBUGGER_STEP_THROUGH) != 0)
697 Module.PredefinedAttributes.DebuggerStepThrough.EmitAttribute (MethodBuilder);
699 if (ReturnType.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
700 return_attributes = new ReturnParameter (this, MethodBuilder, Location);
701 Module.PredefinedAttributes.Dynamic.EmitAttribute (return_attributes.Builder);
702 } else if (ReturnType.HasDynamicElement) {
703 return_attributes = new ReturnParameter (this, MethodBuilder, Location);
704 Module.PredefinedAttributes.Dynamic.EmitAttribute (return_attributes.Builder, ReturnType, Location);
707 if (OptAttributes != null)
708 OptAttributes.Emit ();
710 if (declarative_security != null) {
711 foreach (var de in declarative_security) {
713 MethodBuilder.__AddDeclarativeSecurity (de);
715 MethodBuilder.AddDeclarativeSecurity (de.Key, de.Value);
720 if (type_expr != null)
721 ConstraintChecker.Check (this, member_type, type_expr.Location);
725 if (MethodData != null)
726 MethodData.Emit (Parent);
728 if (block != null && block.StateMachine is AsyncTaskStorey) {
729 var psm = Module.PredefinedAttributes.AsyncStateMachine;
730 psm.EmitAttribute (MethodBuilder, block.StateMachine);
733 if ((ModFlags & Modifiers.PARTIAL) == 0)
737 protected void Error_ConditionalAttributeIsNotValid ()
739 Report.Error (577, Location,
740 "Conditional not valid on `{0}' because it is a constructor, destructor, operator or explicit interface implementation",
741 GetSignatureForError ());
744 public bool IsPartialDefinition {
746 return (ModFlags & Modifiers.PARTIAL) != 0 && Block == null;
750 public bool IsPartialImplementation {
752 return (ModFlags & Modifiers.PARTIAL) != 0 && Block != null;
756 public override string[] ValidAttributeTargets {
758 return attribute_targets;
762 #region IMethodData Members
764 bool IMethodData.IsAccessor {
770 public TypeSpec ReturnType {
776 public MemberName MethodName {
783 /// Returns true if method has conditional attribute and the conditions is not defined (method is excluded).
785 public override string[] ConditionalConditions ()
787 if ((caching_flags & (Flags.Excluded_Undetected | Flags.Excluded)) == 0)
790 if ((ModFlags & Modifiers.PARTIAL) != 0 && (caching_flags & Flags.Excluded) != 0)
791 return new string [0];
793 caching_flags &= ~Flags.Excluded_Undetected;
796 if (base_method == null) {
797 if (OptAttributes == null)
800 Attribute[] attrs = OptAttributes.SearchMulti (Module.PredefinedAttributes.Conditional);
804 conditions = new string[attrs.Length];
805 for (int i = 0; i < conditions.Length; ++i)
806 conditions[i] = attrs[i].GetConditionalAttributeValue ();
808 conditions = base_method.MemberDefinition.ConditionalConditions();
811 if (conditions != null)
812 caching_flags |= Flags.Excluded;
819 public virtual void PrepareEmit ()
821 var mb = MethodData.DefineMethodBuilder (Parent);
823 if (CurrentTypeParameters != null) {
824 string[] gnames = new string[CurrentTypeParameters.Count];
825 for (int i = 0; i < gnames.Length; ++i) {
826 gnames[i] = CurrentTypeParameters[i].Name;
829 var gen_params = MethodBuilder.DefineGenericParameters (gnames);
831 for (int i = 0; i < CurrentTypeParameters.Count; ++i) {
832 var tp = CurrentTypeParameters[i];
834 tp.Define (gen_params[i]);
839 // Generic method has been already defined to resolve method parameters
840 // correctly when they use type parameters
842 mb.SetParameters (parameters.GetMetaInfo ());
843 mb.SetReturnType (ReturnType.GetMetaInfo ());
846 public override void WriteDebugSymbol (MonoSymbolFile file)
848 if (MethodData != null && !IsPartialDefinition)
849 MethodData.WriteDebugSymbol (file);
853 public class Method : MethodOrOperator, IGenericMethodDefinition
855 Method partialMethodImplementation;
857 public Method (TypeDefinition parent, FullNamedExpression return_type, Modifiers mod, MemberName name, ParametersCompiled parameters, Attributes attrs)
858 : base (parent, return_type, mod,
859 parent.PartialContainer.Kind == MemberKind.Interface ? AllowedModifiersInterface :
860 parent.PartialContainer.Kind == MemberKind.Struct ? AllowedModifiersStruct | Modifiers.ASYNC :
861 AllowedModifiersClass | Modifiers.ASYNC,
862 name, attrs, parameters)
866 protected Method (TypeDefinition parent, FullNamedExpression return_type, Modifiers mod, Modifiers amod,
867 MemberName name, ParametersCompiled parameters, Attributes attrs)
868 : base (parent, return_type, mod, amod, name, attrs, parameters)
874 public override TypeParameters CurrentTypeParameters {
876 return MemberName.TypeParameters;
880 public TypeParameterSpec[] TypeParameters {
882 return CurrentTypeParameters.Types;
886 public int TypeParametersCount {
888 return CurrentTypeParameters == null ? 0 : CurrentTypeParameters.Count;
894 public override void Accept (StructuralVisitor visitor)
896 visitor.Visit (this);
899 public static Method Create (TypeDefinition parent, FullNamedExpression returnType, Modifiers mod,
900 MemberName name, ParametersCompiled parameters, Attributes attrs)
902 var m = new Method (parent, returnType, mod, name, parameters, attrs);
904 if ((mod & Modifiers.PARTIAL) != 0) {
905 const Modifiers invalid_partial_mod = Modifiers.AccessibilityMask | Modifiers.ABSTRACT | Modifiers.EXTERN |
906 Modifiers.NEW | Modifiers.OVERRIDE | Modifiers.SEALED | Modifiers.VIRTUAL;
908 if ((mod & invalid_partial_mod) != 0) {
909 m.Report.Error (750, m.Location,
910 "A partial method cannot define access modifier or any of abstract, extern, new, override, sealed, or virtual modifiers");
911 mod &= ~invalid_partial_mod;
914 if ((parent.ModFlags & Modifiers.PARTIAL) == 0) {
915 m.Report.Error (751, m.Location,
916 "A partial method must be declared within a partial class or partial struct");
920 if ((mod & Modifiers.STATIC) == 0 && parameters.HasExtensionMethodType) {
921 m.Report.Error (1105, m.Location, "`{0}': Extension methods must be declared static",
922 m.GetSignatureForError ());
929 public override string GetSignatureForError()
931 return base.GetSignatureForError () + parameters.GetSignatureForError ();
934 void Error_DuplicateEntryPoint (Method b)
936 Report.Error (17, b.Location,
937 "Program `{0}' has more than one entry point defined: `{1}'",
938 b.Module.Builder.ScopeName, b.GetSignatureForError ());
943 if (ReturnType.Kind != MemberKind.Void && ReturnType.BuiltinType != BuiltinTypeSpec.Type.Int)
946 if (parameters.IsEmpty)
949 if (parameters.Count > 1)
952 var ac = parameters.Types [0] as ArrayContainer;
953 return ac != null && ac.Rank == 1 && ac.Element.BuiltinType == BuiltinTypeSpec.Type.String &&
954 (parameters[0].ModFlags & Parameter.Modifier.RefOutMask) == 0;
957 public override FullNamedExpression LookupNamespaceOrType (string name, int arity, LookupMode mode, Location loc)
960 var tp = CurrentTypeParameters;
962 TypeParameter t = tp.Find (name);
964 return new TypeParameterExpr (t, loc);
968 return base.LookupNamespaceOrType (name, arity, mode, loc);
971 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
973 if (a.Type == pa.Conditional) {
974 if (IsExplicitImpl) {
975 Error_ConditionalAttributeIsNotValid ();
979 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
980 Report.Error (243, Location, "Conditional not valid on `{0}' because it is an override method", GetSignatureForError ());
984 if (ReturnType.Kind != MemberKind.Void) {
985 Report.Error (578, Location, "Conditional not valid on `{0}' because its return type is not void", GetSignatureForError ());
990 Report.Error (582, Location, "Conditional not valid on interface members");
994 if (MethodData.implementing != null) {
995 Report.SymbolRelatedToPreviousError (MethodData.implementing.DeclaringType);
996 Report.Error (629, Location, "Conditional member `{0}' cannot implement interface member `{1}'",
997 GetSignatureForError (), TypeManager.CSharpSignature (MethodData.implementing));
1001 for (int i = 0; i < parameters.Count; ++i) {
1002 if ((parameters.FixedParameters [i].ModFlags & Parameter.Modifier.OUT) != 0) {
1003 Report.Error (685, Location, "Conditional method `{0}' cannot have an out parameter", GetSignatureForError ());
1009 if (a.Type == pa.Extension) {
1010 a.Error_MisusedExtensionAttribute ();
1014 base.ApplyAttributeBuilder (a, ctor, cdata, pa);
1017 void CreateTypeParameters ()
1019 var tparams = MemberName.TypeParameters;
1020 var parent_tparams = Parent.TypeParametersAll;
1022 for (int i = 0; i < MemberName.Arity; i++) {
1023 string type_argument_name = tparams[i].MemberName.Name;
1025 if (block == null) {
1026 int idx = parameters.GetParameterIndexByName (type_argument_name);
1030 b = new ToplevelBlock (Compiler, Location);
1032 b.Error_AlreadyDeclaredTypeParameter (type_argument_name, parameters[i].Location);
1035 INamedBlockVariable variable = null;
1036 block.GetLocalName (type_argument_name, block, ref variable);
1037 if (variable != null)
1038 variable.Block.Error_AlreadyDeclaredTypeParameter (type_argument_name, variable.Location);
1041 if (parent_tparams != null) {
1042 var tp = parent_tparams.Find (type_argument_name);
1044 tparams[i].WarningParentNameConflict (tp);
1049 tparams.Create (null, 0, Parent);
1052 protected virtual void DefineTypeParameters ()
1054 var tparams = CurrentTypeParameters;
1056 TypeParameterSpec[] base_tparams = null;
1057 TypeParameterSpec[] base_decl_tparams = TypeParameterSpec.EmptyTypes;
1058 TypeSpec[] base_targs = TypeSpec.EmptyTypes;
1059 if (((ModFlags & Modifiers.OVERRIDE) != 0 || IsExplicitImpl)) {
1060 MethodSpec base_override = base_method ?? MethodData.implementing;
1062 if (base_override != null) {
1063 base_tparams = base_override.GenericDefinition.TypeParameters;
1065 if (base_override.DeclaringType.IsGeneric) {
1066 base_decl_tparams = base_override.DeclaringType.MemberDefinition.TypeParameters;
1068 if (base_method != null) {
1069 var base_type_parent = CurrentType;
1070 while (base_type_parent.BaseType != base_override.DeclaringType) {
1071 base_type_parent = base_type_parent.BaseType;
1074 base_targs = base_type_parent.BaseType.TypeArguments;
1076 foreach (var iface in Parent.CurrentType.Interfaces) {
1077 if (iface == base_override.DeclaringType) {
1078 base_targs = iface.TypeArguments;
1085 if (base_override.IsGeneric) {
1086 ObsoleteAttribute oa;
1087 foreach (var base_tp in base_tparams) {
1088 oa = base_tp.BaseType.GetAttributeObsolete ();
1090 AttributeTester.Report_ObsoleteMessage (oa, base_tp.BaseType.GetSignatureForError (), Location, Report);
1093 if (base_tp.InterfacesDefined != null) {
1094 foreach (var iface in base_tp.InterfacesDefined) {
1095 oa = iface.GetAttributeObsolete ();
1097 AttributeTester.Report_ObsoleteMessage (oa, iface.GetSignatureForError (), Location, Report);
1103 if (base_decl_tparams.Length != 0) {
1104 base_decl_tparams = base_decl_tparams.Concat (base_tparams).ToArray ();
1105 base_targs = base_targs.Concat (tparams.Types).ToArray ();
1107 base_decl_tparams = base_tparams;
1108 base_targs = tparams.Types;
1114 for (int i = 0; i < tparams.Count; ++i) {
1115 var tp = tparams [i];
1117 if (base_tparams == null) {
1118 tp.ResolveConstraints (this);
1123 // Copy base constraints for override/explicit methods
1125 var base_tparam = base_tparams [i];
1126 var local_tparam = tp.Type;
1127 local_tparam.SpecialConstraint = base_tparam.SpecialConstraint;
1129 var inflator = new TypeParameterInflator (this, CurrentType, base_decl_tparams, base_targs);
1130 base_tparam.InflateConstraints (inflator, local_tparam);
1133 // Check all type argument constraints for possible collision or unification
1134 // introduced by inflating inherited constraints in this context
1136 // Conflict example:
1138 // class A<T> { virtual void Foo<U> () where U : class, T {} }
1139 // class B : A<int> { override void Foo<U> {} }
1141 var local_tparam_targs = local_tparam.TypeArguments;
1142 if (local_tparam_targs != null) {
1143 for (int ii = 0; ii < local_tparam_targs.Length; ++ii) {
1144 var ta = local_tparam_targs [ii];
1145 if (!ta.IsClass && !ta.IsStruct)
1148 TypeSpec[] unique_tparams = null;
1149 for (int iii = ii + 1; iii < local_tparam_targs.Length; ++iii) {
1151 // Remove any identical or unified constraint types
1153 var tparam_checked = local_tparam_targs [iii];
1154 if (TypeSpecComparer.IsEqual (ta, tparam_checked) || TypeSpec.IsBaseClass (ta, tparam_checked, false)) {
1155 unique_tparams = new TypeSpec[local_tparam_targs.Length - 1];
1156 Array.Copy (local_tparam_targs, 0, unique_tparams, 0, iii);
1157 Array.Copy (local_tparam_targs, iii + 1, unique_tparams, iii, local_tparam_targs.Length - iii - 1);
1158 } else if (!TypeSpec.IsBaseClass (tparam_checked, ta, false)) {
1159 Constraints.Error_ConflictingConstraints (this, local_tparam, ta, tparam_checked, Location);
1163 if (unique_tparams != null) {
1164 local_tparam_targs = unique_tparams;
1165 local_tparam.TypeArguments = local_tparam_targs;
1169 Constraints.CheckConflictingInheritedConstraint (local_tparam, ta, this, Location);
1174 if (base_tparams == null && MethodData != null && MethodData.implementing != null) {
1175 CheckImplementingMethodConstraints (Parent, spec, MethodData.implementing);
1179 public static bool CheckImplementingMethodConstraints (TypeContainer container, MethodSpec method, MethodSpec baseMethod)
1181 var tparams = method.Constraints;
1182 var base_tparams = baseMethod.Constraints;
1183 for (int i = 0; i < tparams.Length; ++i) {
1184 if (!tparams[i].HasSameConstraintsImplementation (base_tparams[i])) {
1185 container.Compiler.Report.SymbolRelatedToPreviousError (method);
1186 container.Compiler.Report.SymbolRelatedToPreviousError (baseMethod);
1188 // Using container location because the interface can be implemented
1190 container.Compiler.Report.Error (425, container.Location,
1191 "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",
1192 tparams[i].GetSignatureForError (), method.GetSignatureForError (),
1193 base_tparams[i].GetSignatureForError (), baseMethod.GetSignatureForError ());
1205 public override bool Define ()
1207 if (!base.Define ())
1210 if (member_type.Kind == MemberKind.Void && parameters.IsEmpty && MemberName.Arity == 0 && MemberName.Name == Destructor.MetadataName) {
1211 Report.Warning (465, 1, Location,
1212 "Introducing `Finalize' method can interfere with destructor invocation. Did you intend to declare a destructor?");
1215 if (Compiler.Settings.StdLib && ReturnType.IsSpecialRuntimeType) {
1216 Error1599 (Location, ReturnType, Report);
1220 if (CurrentTypeParameters == null) {
1221 if (base_method != null && !IsExplicitImpl) {
1222 if (parameters.Count == 1 && ParameterTypes[0].BuiltinType == BuiltinTypeSpec.Type.Object && MemberName.Name == "Equals")
1223 Parent.PartialContainer.Mark_HasEquals ();
1224 else if (parameters.IsEmpty && MemberName.Name == "GetHashCode")
1225 Parent.PartialContainer.Mark_HasGetHashCode ();
1229 DefineTypeParameters ();
1232 if (block != null) {
1233 if (block.IsIterator) {
1235 // Current method is turned into automatically generated
1236 // wrapper which creates an instance of iterator
1238 Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
1239 ModFlags |= Modifiers.DEBUGGER_HIDDEN;
1242 if ((ModFlags & Modifiers.ASYNC) != 0) {
1243 if (ReturnType.Kind != MemberKind.Void &&
1244 ReturnType != Module.PredefinedTypes.Task.TypeSpec &&
1245 !ReturnType.IsGenericTask) {
1246 Report.Error (1983, Location, "The return type of an async method must be void, Task, or Task<T>");
1249 block = (ToplevelBlock) block.ConvertToAsyncTask (this, Parent.PartialContainer, parameters, ReturnType, null, Location);
1250 ModFlags |= Modifiers.DEBUGGER_STEP_THROUGH;
1253 if (Compiler.Settings.WriteMetadataOnly)
1257 if ((ModFlags & Modifiers.STATIC) == 0)
1260 if (parameters.HasExtensionMethodType) {
1261 if (Parent.PartialContainer.IsStatic && !Parent.IsGenericOrParentIsGeneric) {
1262 if (!Parent.IsTopLevel)
1263 Report.Error (1109, Location, "`{0}': Extension methods cannot be defined in a nested class",
1264 GetSignatureForError ());
1266 PredefinedAttribute pa = Module.PredefinedAttributes.Extension;
1267 if (!pa.IsDefined) {
1268 Report.Error (1110, Location,
1269 "`{0}': Extension methods require `System.Runtime.CompilerServices.ExtensionAttribute' type to be available. Are you missing an assembly reference?",
1270 GetSignatureForError ());
1273 ModFlags |= Modifiers.METHOD_EXTENSION;
1274 Parent.PartialContainer.ModFlags |= Modifiers.METHOD_EXTENSION;
1275 Spec.DeclaringType.SetExtensionMethodContainer ();
1276 Parent.Module.HasExtensionMethod = true;
1278 Report.Error (1106, Location, "`{0}': Extension methods must be defined in a non-generic static class",
1279 GetSignatureForError ());
1284 // This is used to track the Entry Point,
1286 var settings = Compiler.Settings;
1287 if (settings.NeedsEntryPoint && MemberName.Name == "Main" && (settings.MainClass == null || settings.MainClass == Parent.TypeBuilder.FullName)) {
1288 if (IsEntryPoint ()) {
1289 if (Parent.DeclaringAssembly.EntryPoint == null) {
1290 if (Parent.IsGenericOrParentIsGeneric || MemberName.IsGeneric) {
1291 Report.Warning (402, 4, Location, "`{0}': an entry point cannot be generic or in a generic type",
1292 GetSignatureForError ());
1293 } else if ((ModFlags & Modifiers.ASYNC) != 0) {
1294 Report.Error (4009, Location, "`{0}': an entry point cannot be async method",
1295 GetSignatureForError ());
1298 Parent.DeclaringAssembly.EntryPoint = this;
1301 Error_DuplicateEntryPoint (Parent.DeclaringAssembly.EntryPoint);
1302 Error_DuplicateEntryPoint (this);
1305 Report.Warning (28, 4, Location, "`{0}' has the wrong signature to be an entry point",
1306 GetSignatureForError ());
1313 public override void PrepareEmit ()
1315 if (IsPartialDefinition) {
1317 // Use partial method implementation builder for partial method declaration attributes
1319 if (partialMethodImplementation != null) {
1320 MethodData = partialMethodImplementation.MethodData;
1326 base.PrepareEmit ();
1332 public override void Emit ()
1335 if (IsPartialDefinition) {
1336 if (partialMethodImplementation != null && CurrentTypeParameters != null) {
1337 CurrentTypeParameters.CheckPartialConstraints (partialMethodImplementation);
1339 var otp = partialMethodImplementation.CurrentTypeParameters;
1340 for (int i = 0; i < CurrentTypeParameters.Count; ++i) {
1341 var tp = CurrentTypeParameters [i];
1349 if ((ModFlags & Modifiers.PARTIAL) != 0 && (caching_flags & Flags.PartialDefinitionExists) == 0) {
1350 Report.Error (759, Location, "A partial method `{0}' implementation is missing a partial method declaration",
1351 GetSignatureForError ());
1354 if (CurrentTypeParameters != null) {
1355 for (int i = 0; i < CurrentTypeParameters.Count; ++i) {
1356 var tp = CurrentTypeParameters [i];
1358 tp.CheckGenericConstraints (false);
1363 if ((ModFlags & Modifiers.METHOD_EXTENSION) != 0)
1364 Module.PredefinedAttributes.Extension.EmitAttribute (MethodBuilder);
1367 } catch (Exception e) {
1368 throw new InternalErrorException (this, e);
1372 public override bool EnableOverloadChecks (MemberCore overload)
1374 if (overload is Indexer)
1377 return base.EnableOverloadChecks (overload);
1380 public static void Error1599 (Location loc, TypeSpec t, Report Report)
1382 Report.Error (1599, loc, "Method or delegate cannot return type `{0}'", t.GetSignatureForError ());
1385 protected override bool ResolveMemberType ()
1387 if (CurrentTypeParameters != null) {
1388 CreateTypeParameters ();
1391 return base.ResolveMemberType ();
1394 public void SetPartialDefinition (Method methodDefinition)
1396 caching_flags |= Flags.PartialDefinitionExists;
1397 methodDefinition.partialMethodImplementation = this;
1399 // Ensure we are always using method declaration parameters
1400 for (int i = 0; i < methodDefinition.parameters.Count; ++i ) {
1401 var md_p = methodDefinition.parameters [i];
1402 var p = parameters [i];
1404 p.DefaultValue = md_p.DefaultValue;
1405 if (md_p.OptAttributes != null) {
1406 if (p.OptAttributes == null) {
1407 p.OptAttributes = md_p.OptAttributes;
1409 p.OptAttributes.Attrs.AddRange (md_p.OptAttributes.Attrs);
1414 if (methodDefinition.attributes != null) {
1415 if (attributes == null) {
1416 attributes = methodDefinition.attributes;
1418 attributes.Attrs.AddRange (methodDefinition.attributes.Attrs);
1422 if (CurrentTypeParameters != null) {
1423 for (int i = 0; i < CurrentTypeParameters.Count; ++i) {
1424 var tp_other = methodDefinition.CurrentTypeParameters [i];
1425 if (tp_other.OptAttributes == null)
1428 var tp = CurrentTypeParameters [i];
1429 if (tp.OptAttributes == null) {
1430 tp.OptAttributes = tp_other.OptAttributes;
1432 tp.OptAttributes.Attrs.AddRange (tp.OptAttributes.Attrs);
1439 public abstract class ConstructorInitializer : ExpressionStatement
1441 Arguments argument_list;
1442 MethodSpec base_ctor;
1444 protected ConstructorInitializer (Arguments argument_list, Location loc)
1446 this.argument_list = argument_list;
1450 public Arguments Arguments {
1452 return argument_list;
1456 public override bool ContainsEmitWithAwait ()
1458 throw new NotSupportedException ();
1461 public override Expression CreateExpressionTree (ResolveContext ec)
1463 throw new NotSupportedException ("ET");
1466 protected override Expression DoResolve (ResolveContext ec)
1468 eclass = ExprClass.Value;
1471 var caller_builder = (Constructor) ec.MemberContext;
1474 // Spec mandates that constructor initializer will not have `this' access
1476 using (ec.Set (ResolveContext.Options.BaseInitializer)) {
1477 if (argument_list != null) {
1479 argument_list.Resolve (ec, out dynamic);
1482 ec.Report.Error (1975, loc,
1483 "The constructor call cannot be dynamically dispatched within constructor initializer");
1489 type = ec.CurrentType;
1490 if (this is ConstructorBaseInitializer) {
1491 if (ec.CurrentType.BaseType == null)
1494 type = ec.CurrentType.BaseType;
1495 if (ec.CurrentType.IsStruct) {
1496 ec.Report.Error (522, loc,
1497 "`{0}': Struct constructors cannot call base constructors", caller_builder.GetSignatureForError ());
1502 // It is legal to have "this" initializers that take no arguments
1505 // struct D { public D (int a) : this () {}
1507 if (ec.CurrentType.IsStruct && argument_list == null)
1511 base_ctor = ConstructorLookup (ec, type, ref argument_list, loc);
1514 if (base_ctor != null && base_ctor.MemberDefinition == caller_builder.Spec.MemberDefinition) {
1515 ec.Report.Error (516, loc, "Constructor `{0}' cannot call itself",
1516 caller_builder.GetSignatureForError ());
1522 public override void Emit (EmitContext ec)
1525 // It can be null for struct initializers or System.Object
1527 if (base_ctor == null) {
1528 if (type == ec.BuiltinTypes.Object)
1531 ec.Emit (OpCodes.Ldarg_0);
1532 ec.Emit (OpCodes.Initobj, type);
1536 var call = new CallEmitter ();
1537 call.InstanceExpression = new CompilerGeneratedThis (type, loc);
1538 call.EmitPredefined (ec, base_ctor, argument_list, false);
1541 public override void EmitStatement (EmitContext ec)
1546 public override void FlowAnalysis (FlowAnalysisContext fc)
1548 if (argument_list != null)
1549 argument_list.FlowAnalysis (fc);
1553 public class ConstructorBaseInitializer : ConstructorInitializer {
1554 public ConstructorBaseInitializer (Arguments argument_list, Location l) :
1555 base (argument_list, l)
1560 class GeneratedBaseInitializer: ConstructorBaseInitializer {
1561 public GeneratedBaseInitializer (Location loc, Arguments arguments)
1562 : base (arguments, loc)
1567 public class ConstructorThisInitializer : ConstructorInitializer {
1568 public ConstructorThisInitializer (Arguments argument_list, Location l) :
1569 base (argument_list, l)
1574 public class Constructor : MethodCore, IMethodData, IMethodDefinition
1576 public ConstructorBuilder ConstructorBuilder;
1577 public ConstructorInitializer Initializer;
1578 SecurityType declarative_security;
1579 bool has_compliant_args;
1580 SourceMethodBuilder debug_builder;
1583 // Modifiers allowed for a constructor.
1585 public const Modifiers AllowedModifiers =
1587 Modifiers.PROTECTED |
1588 Modifiers.INTERNAL |
1594 static readonly string[] attribute_targets = new string [] { "method" };
1596 public static readonly string ConstructorName = ".ctor";
1597 public static readonly string TypeConstructorName = ".cctor";
1599 public Constructor (TypeDefinition parent, string name, Modifiers mod, Attributes attrs, ParametersCompiled args, Location loc)
1600 : base (parent, null, mod, AllowedModifiers, new MemberName (name, loc), attrs, args)
1604 public bool HasCompliantArgs {
1606 return has_compliant_args;
1610 public override AttributeTargets AttributeTargets {
1612 return AttributeTargets.Constructor;
1616 bool IMethodData.IsAccessor {
1622 public bool IsPrimaryConstructor { get; set; }
1624 MethodBase IMethodDefinition.Metadata {
1626 return ConstructorBuilder;
1631 // Returns true if this is a default constructor
1633 public bool IsDefault ()
1635 if ((ModFlags & Modifiers.STATIC) != 0)
1636 return parameters.IsEmpty;
1638 return parameters.IsEmpty &&
1639 (Initializer is ConstructorBaseInitializer) &&
1640 (Initializer.Arguments == null);
1643 public override void Accept (StructuralVisitor visitor)
1645 visitor.Visit (this);
1648 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
1650 if (a.IsValidSecurityAttribute ()) {
1651 a.ExtractSecurityPermissionSet (ctor, ref declarative_security);
1655 if (a.Type == pa.MethodImpl) {
1656 is_external_implementation = a.IsInternalCall ();
1659 ConstructorBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
1662 protected override bool CheckBase ()
1664 if ((ModFlags & Modifiers.STATIC) != 0) {
1665 if (!parameters.IsEmpty) {
1666 Report.Error (132, Location, "`{0}': The static constructor must be parameterless",
1667 GetSignatureForError ());
1671 if ((caching_flags & Flags.MethodOverloadsExist) != 0)
1672 Parent.MemberCache.CheckExistingMembersOverloads (this, parameters);
1674 // the rest can be ignored
1678 // Check whether arguments were correct.
1679 if (!DefineParameters (parameters))
1682 if ((caching_flags & Flags.MethodOverloadsExist) != 0)
1683 Parent.MemberCache.CheckExistingMembersOverloads (this, parameters);
1685 if (Parent.PartialContainer.Kind == MemberKind.Struct && parameters.IsEmpty) {
1686 Report.Error (568, Location,
1687 "Structs cannot contain explicit parameterless constructors");
1691 CheckProtectedModifier ();
1697 // Creates the ConstructorBuilder
1699 public override bool Define ()
1701 if (ConstructorBuilder != null)
1704 if (!CheckAbstractAndExtern (block != null))
1707 // Check if arguments were correct.
1711 if (Parent.PrimaryConstructorParameters != null && !IsPrimaryConstructor && !IsStatic) {
1712 if (Parent.Kind == MemberKind.Struct && Initializer is ConstructorThisInitializer && Initializer.Arguments == null) {
1713 Report.Error (8043, Location, "`{0}': Structs with primary constructor cannot specify default constructor initializer",
1714 GetSignatureForError ());
1715 } else if (Initializer == null || Initializer is ConstructorBaseInitializer) {
1716 Report.Error (8037, Location, "`{0}': Instance constructor of type with primary constructor must specify `this' constructor initializer",
1717 GetSignatureForError ());
1721 var ca = ModifiersExtensions.MethodAttr (ModFlags) | MethodAttributes.RTSpecialName | MethodAttributes.SpecialName;
1723 ConstructorBuilder = Parent.TypeBuilder.DefineConstructor (
1724 ca, CallingConventions,
1725 parameters.GetMetaInfo ());
1727 spec = new MethodSpec (MemberKind.Constructor, Parent.Definition, this, Compiler.BuiltinTypes.Void, parameters, ModFlags);
1729 Parent.MemberCache.AddMember (spec);
1731 if (block != null) {
1732 // It's here only to report an error
1733 if (block.IsIterator) {
1734 member_type = Compiler.BuiltinTypes.Void;
1735 Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
1738 if (Compiler.Settings.WriteMetadataOnly)
1748 public override void Emit ()
1750 if (Parent.PartialContainer.IsComImport) {
1751 if (!IsDefault ()) {
1752 Report.Error (669, Location, "`{0}': A class with the ComImport attribute cannot have a user-defined constructor",
1753 Parent.GetSignatureForError ());
1756 // Set as internal implementation and reset block data
1757 // to ensure no IL is generated
1758 ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall);
1762 if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
1763 Module.PredefinedAttributes.DebuggerHidden.EmitAttribute (ConstructorBuilder);
1765 if (OptAttributes != null)
1766 OptAttributes.Emit ();
1769 parameters.ApplyAttributes (this, ConstructorBuilder);
1772 BlockContext bc = new BlockContext (this, block, Compiler.BuiltinTypes.Void);
1773 bc.Set (ResolveContext.Options.ConstructorScope);
1775 if (block != null) {
1776 if (!IsStatic && Initializer == null && Parent.PartialContainer.Kind == MemberKind.Struct) {
1778 // If this is a non-static `struct' constructor and doesn't have any
1779 // initializer, it must initialize all of the struct's fields.
1781 block.AddThisVariable (bc);
1785 // If we use a "this (...)" constructor initializer, then
1786 // do not emit field initializers, they are initialized in the other constructor
1788 if (!(Initializer is ConstructorThisInitializer))
1789 Parent.PartialContainer.ResolveFieldInitializers (bc);
1792 if (Initializer == null && Parent.PartialContainer.Kind == MemberKind.Class) {
1793 Initializer = new GeneratedBaseInitializer (Location, null);
1796 if (Initializer != null) {
1798 // mdb format does not support reqions. Try to workaround this by emitting the
1799 // sequence point at initializer. Any breakpoint at constructor header should
1800 // be adjusted to this sequence point as it's the next one which follows.
1802 block.AddScopeStatement (new StatementExpression (Initializer));
1806 if (block.Resolve (bc, this)) {
1807 debug_builder = Parent.CreateMethodSymbolEntry ();
1808 EmitContext ec = new EmitContext (this, ConstructorBuilder.GetILGenerator (), bc.ReturnType, debug_builder);
1809 ec.With (EmitContext.Options.ConstructorScope, true);
1815 if (declarative_security != null) {
1816 foreach (var de in declarative_security) {
1818 ConstructorBuilder.__AddDeclarativeSecurity (de);
1820 ConstructorBuilder.AddDeclarativeSecurity (de.Key, de.Value);
1828 protected override MemberSpec FindBaseMember (out MemberSpec bestCandidate, ref bool overrides)
1830 // Is never override
1831 bestCandidate = null;
1835 public override string GetCallerMemberName ()
1837 return IsStatic ? TypeConstructorName : ConstructorName;
1840 public override string GetSignatureForDocumentation ()
1842 return Parent.GetSignatureForDocumentation () + ".#ctor" + parameters.GetSignatureForDocumentation ();
1845 public override string GetSignatureForError()
1847 return base.GetSignatureForError () + parameters.GetSignatureForError ();
1850 public override string[] ValidAttributeTargets {
1852 return attribute_targets;
1856 protected override bool VerifyClsCompliance ()
1858 if (!base.VerifyClsCompliance () || !IsExposedFromAssembly ()) {
1862 if (!parameters.IsEmpty && Parent.Definition.IsAttribute) {
1863 foreach (TypeSpec param in parameters.Types) {
1864 if (param.IsArray) {
1870 has_compliant_args = true;
1874 public override void WriteDebugSymbol (MonoSymbolFile file)
1876 if (debug_builder == null)
1879 var token = ConstructorBuilder.GetToken ();
1880 int t = token.Token;
1882 if (ModuleBuilder.IsPseudoToken (t))
1883 t = Module.Builder.ResolvePseudoToken (t);
1886 debug_builder.DefineMethod (file, t);
1889 #region IMethodData Members
1891 public MemberName MethodName {
1897 public TypeSpec ReturnType {
1903 EmitContext IMethodData.CreateEmitContext (ILGenerator ig, SourceMethodBuilder sourceMethod)
1905 throw new NotImplementedException ();
1912 /// Interface for MethodData class. Holds links to parent members to avoid member duplication.
1914 public interface IMethodData : IMemberContext
1916 CallingConventions CallingConventions { get; }
1917 Location Location { get; }
1918 MemberName MethodName { get; }
1919 TypeSpec ReturnType { get; }
1920 ParametersCompiled ParameterInfo { get; }
1921 MethodSpec Spec { get; }
1922 bool IsAccessor { get; }
1924 Attributes OptAttributes { get; }
1925 ToplevelBlock Block { get; set; }
1927 EmitContext CreateEmitContext (ILGenerator ig, SourceMethodBuilder sourceMethod);
1931 // Encapsulates most of the Method's state
1933 public class MethodData
1935 public readonly IMethodData method;
1938 // Are we implementing an interface ?
1940 public MethodSpec implementing;
1945 protected InterfaceMemberBase member;
1946 protected Modifiers modifiers;
1947 protected MethodAttributes flags;
1948 protected TypeSpec declaring_type;
1949 protected MethodSpec parent_method;
1950 SourceMethodBuilder debug_builder;
1953 MethodBuilder builder;
1954 public MethodBuilder MethodBuilder {
1960 public TypeSpec DeclaringType {
1962 return declaring_type;
1966 public string MetadataName {
1972 public MethodData (InterfaceMemberBase member,
1973 Modifiers modifiers, MethodAttributes flags, IMethodData method)
1975 this.member = member;
1976 this.modifiers = modifiers;
1979 this.method = method;
1982 public MethodData (InterfaceMemberBase member,
1983 Modifiers modifiers, MethodAttributes flags,
1985 MethodSpec parent_method)
1986 : this (member, modifiers, flags, method)
1988 this.parent_method = parent_method;
1991 public bool Define (TypeDefinition container, string method_full_name)
1993 PendingImplementation pending = container.PendingImplementations;
1994 MethodSpec ambig_iface_method;
1995 bool optional = false;
1997 if (pending != null) {
1998 implementing = pending.IsInterfaceMethod (method.MethodName, member.InterfaceType, this, out ambig_iface_method, ref optional);
2000 if (member.InterfaceType != null) {
2001 if (implementing == null) {
2002 if (member is PropertyBase) {
2003 container.Compiler.Report.Error (550, method.Location,
2004 "`{0}' is an accessor not found in interface member `{1}{2}'",
2005 method.GetSignatureForError (), member.InterfaceType.GetSignatureForError (),
2006 member.GetSignatureForError ().Substring (member.GetSignatureForError ().LastIndexOf ('.')));
2009 container.Compiler.Report.Error (539, method.Location,
2010 "`{0}.{1}' in explicit interface declaration is not a member of interface",
2011 member.InterfaceType.GetSignatureForError (), member.ShortName);
2015 if (implementing.IsAccessor && !method.IsAccessor) {
2016 container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
2017 container.Compiler.Report.Error (683, method.Location,
2018 "`{0}' explicit method implementation cannot implement `{1}' because it is an accessor",
2019 member.GetSignatureForError (), implementing.GetSignatureForError ());
2023 if (implementing != null && !optional) {
2024 if (!method.IsAccessor) {
2025 if (implementing.IsAccessor) {
2026 container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
2027 container.Compiler.Report.Error (470, method.Location,
2028 "Method `{0}' cannot implement interface accessor `{1}'",
2029 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing));
2031 } else if (implementing.DeclaringType.IsInterface) {
2032 if (!implementing.IsAccessor) {
2033 container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
2034 container.Compiler.Report.Error (686, method.Location,
2035 "Accessor `{0}' cannot implement interface member `{1}' for type `{2}'. Use an explicit interface implementation",
2036 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing), container.GetSignatureForError ());
2038 PropertyBase.PropertyMethod pm = method as PropertyBase.PropertyMethod;
2039 if (pm != null && pm.HasCustomAccessModifier && (pm.ModFlags & Modifiers.PUBLIC) == 0) {
2040 container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
2041 container.Compiler.Report.Error (277, method.Location,
2042 "Accessor `{0}' must be declared public to implement interface member `{1}'",
2043 method.GetSignatureForError (), implementing.GetSignatureForError ());
2050 ambig_iface_method = null;
2054 // For implicit implementations, make sure we are public, for
2055 // explicit implementations, make sure we are private.
2057 if (implementing != null){
2058 if (member.IsExplicitImpl) {
2059 if (method.ParameterInfo.HasParams && !implementing.Parameters.HasParams) {
2060 container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
2061 container.Compiler.Report.Error (466, method.Location,
2062 "`{0}': the explicit interface implementation cannot introduce the params modifier",
2063 method.GetSignatureForError ());
2066 if (ambig_iface_method != null) {
2067 container.Compiler.Report.SymbolRelatedToPreviousError (ambig_iface_method);
2068 container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
2069 container.Compiler.Report.Warning (473, 2, method.Location,
2070 "Explicit interface implementation `{0}' matches more than one interface member. Consider using a non-explicit implementation instead",
2071 method.GetSignatureForError ());
2075 // Setting implementin to null inside this block will trigger a more
2076 // verbose error reporting for missing interface implementations
2078 if (implementing.DeclaringType.IsInterface) {
2080 // If this is an interface method implementation,
2081 // check for public accessibility
2083 if ((flags & MethodAttributes.MemberAccessMask) != MethodAttributes.Public) {
2084 implementing = null;
2085 } else if (optional && (container.Interfaces == null || !container.Definition.Interfaces.Contains (implementing.DeclaringType))) {
2087 // We are not implementing interface when base class already implemented it
2089 implementing = null;
2091 } else if ((flags & MethodAttributes.MemberAccessMask) == MethodAttributes.Private) {
2092 // We may never be private.
2093 implementing = null;
2095 } else if ((modifiers & Modifiers.OVERRIDE) == 0) {
2097 // We may be protected if we're overriding something.
2099 implementing = null;
2104 // Static is not allowed
2106 if ((modifiers & Modifiers.STATIC) != 0){
2107 implementing = null;
2112 // If implementing is still valid, set flags
2114 if (implementing != null){
2116 // When implementing interface methods, set NewSlot
2117 // unless, we are overwriting a method.
2119 if ((modifiers & Modifiers.OVERRIDE) == 0 && implementing.DeclaringType.IsInterface) {
2120 flags |= MethodAttributes.NewSlot;
2123 flags |= MethodAttributes.Virtual | MethodAttributes.HideBySig;
2125 // Set Final unless we're virtual, abstract or already overriding a method.
2126 if ((modifiers & (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE)) == 0)
2127 flags |= MethodAttributes.Final;
2130 // clear the pending implementation flag (requires explicit methods to be defined first)
2132 pending.ImplementMethod (method.MethodName,
2133 member.InterfaceType, this, member.IsExplicitImpl, out ambig_iface_method, ref optional);
2136 // Update indexer accessor name to match implementing abstract accessor
2138 if (!implementing.DeclaringType.IsInterface && !member.IsExplicitImpl && implementing.IsAccessor)
2139 method_full_name = implementing.MemberDefinition.Name;
2142 full_name = method_full_name;
2143 declaring_type = container.Definition;
2148 void DefineOverride (TypeDefinition container)
2150 if (implementing == null)
2153 if (!member.IsExplicitImpl)
2156 container.TypeBuilder.DefineMethodOverride (builder, (MethodInfo) implementing.GetMetaInfo ());
2160 // Creates partial MethodBuilder for the method when has generic parameters used
2161 // as arguments or return type
2163 public MethodBuilder DefineMethodBuilder (TypeDefinition container)
2165 if (builder != null)
2166 throw new InternalErrorException ();
2168 builder = container.TypeBuilder.DefineMethod (full_name, flags, method.CallingConventions);
2173 // Creates full MethodBuilder for the method
2175 public MethodBuilder DefineMethodBuilder (TypeDefinition container, ParametersCompiled param)
2177 DefineMethodBuilder (container);
2178 builder.SetReturnType (method.ReturnType.GetMetaInfo ());
2179 builder.SetParameters (param.GetMetaInfo ());
2186 public void Emit (TypeDefinition parent)
2188 DefineOverride (parent);
2190 method.ParameterInfo.ApplyAttributes (method, MethodBuilder);
2192 ToplevelBlock block = method.Block;
2193 if (block != null) {
2194 BlockContext bc = new BlockContext (method, block, method.ReturnType);
2195 if (block.Resolve (bc, method)) {
2196 debug_builder = member.Parent.CreateMethodSymbolEntry ();
2197 EmitContext ec = method.CreateEmitContext (MethodBuilder.GetILGenerator (), debug_builder);
2204 public void WriteDebugSymbol (MonoSymbolFile file)
2206 if (debug_builder == null)
2209 var token = builder.GetToken ();
2210 int t = token.Token;
2212 if (ModuleBuilder.IsPseudoToken (t))
2213 t = member.Module.Builder.ResolvePseudoToken (t);
2216 debug_builder.DefineMethod (file, t);
2220 public class Destructor : MethodOrOperator
2222 const Modifiers AllowedModifiers =
2226 static readonly string[] attribute_targets = new string [] { "method" };
2228 public static readonly string MetadataName = "Finalize";
2230 public Destructor (TypeDefinition parent, Modifiers mod, ParametersCompiled parameters, Attributes attrs, Location l)
2231 : base (parent, null, mod, AllowedModifiers, new MemberName (MetadataName, l), attrs, parameters)
2233 ModFlags &= ~Modifiers.PRIVATE;
2234 ModFlags |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2237 public override void Accept (StructuralVisitor visitor)
2239 visitor.Visit (this);
2242 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
2244 if (a.Type == pa.Conditional) {
2245 Error_ConditionalAttributeIsNotValid ();
2249 base.ApplyAttributeBuilder (a, ctor, cdata, pa);
2252 protected override bool CheckBase ()
2254 if ((caching_flags & Flags.MethodOverloadsExist) != 0)
2255 CheckForDuplications ();
2257 // Don't check base, destructors have special syntax
2261 public override bool Define ()
2265 if (Compiler.Settings.WriteMetadataOnly)
2271 public override void Emit()
2273 var base_type = Parent.PartialContainer.BaseType;
2274 if (base_type != null && Block != null) {
2275 var base_dtor = MemberCache.FindMember (base_type,
2276 new MemberFilter (MetadataName, 0, MemberKind.Destructor, null, null), BindingRestriction.InstanceOnly) as MethodSpec;
2278 if (base_dtor == null)
2279 throw new NotImplementedException ();
2281 MethodGroupExpr method_expr = MethodGroupExpr.CreatePredefined (base_dtor, base_type, Location);
2282 method_expr.InstanceExpression = new BaseThis (base_type, Location);
2284 var try_block = new ExplicitBlock (block, block.StartLocation, block.EndLocation) {
2285 IsCompilerGenerated = true
2287 var finaly_block = new ExplicitBlock (block, Location, Location) {
2288 IsCompilerGenerated = true
2292 // 0-size arguments to avoid CS0250 error
2293 // TODO: Should use AddScopeStatement or something else which emits correct
2296 finaly_block.AddStatement (new StatementExpression (new Invocation (method_expr, new Arguments (0)), Location.Null));
2298 var tf = new TryFinally (try_block, finaly_block, Location);
2299 block.WrapIntoDestructor (tf, try_block);
2305 public override string GetSignatureForError ()
2307 return Parent.GetSignatureForError () + ".~" + Parent.MemberName.Name + "()";
2310 protected override bool ResolveMemberType ()
2312 member_type = Compiler.BuiltinTypes.Void;
2316 public override string[] ValidAttributeTargets {
2318 return attribute_targets;
2323 // Ooouh Martin, templates are missing here.
2324 // When it will be possible move here a lot of child code and template method type.
2325 public abstract class AbstractPropertyEventMethod : MemberCore, IMethodData, IMethodDefinition {
2326 protected MethodData method_data;
2327 protected ToplevelBlock block;
2328 protected SecurityType declarative_security;
2330 protected readonly string prefix;
2332 ReturnParameter return_attributes;
2334 protected AbstractPropertyEventMethod (InterfaceMemberBase member, string prefix, Attributes attrs, Location loc)
2335 : base (member.Parent, SetupName (prefix, member, loc), attrs)
2337 this.prefix = prefix;
2340 static MemberName SetupName (string prefix, InterfaceMemberBase member, Location loc)
2342 return new MemberName (member.MemberName.Left, prefix + member.ShortName, member.MemberName.ExplicitInterface, loc);
2345 public void UpdateName (InterfaceMemberBase member)
2347 SetMemberName (SetupName (prefix, member, Location));
2350 #region IMethodData Members
2352 public ToplevelBlock Block {
2362 public CallingConventions CallingConventions {
2364 return CallingConventions.Standard;
2368 public EmitContext CreateEmitContext (ILGenerator ig, SourceMethodBuilder sourceMethod)
2370 return new EmitContext (this, ig, ReturnType, sourceMethod);
2373 public bool IsAccessor {
2379 public MemberName MethodName {
2385 public TypeSpec[] ParameterTypes {
2387 return ParameterInfo.Types;
2391 MethodBase IMethodDefinition.Metadata {
2393 return method_data.MethodBuilder;
2397 public abstract ParametersCompiled ParameterInfo { get ; }
2398 public abstract TypeSpec ReturnType { get; }
2402 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
2404 if (a.Type == pa.CLSCompliant || a.Type == pa.Obsolete || a.Type == pa.Conditional) {
2405 Report.Error (1667, a.Location,
2406 "Attribute `{0}' is not valid on property or event accessors. It is valid on `{1}' declarations only",
2407 a.Type.GetSignatureForError (), a.GetValidTargets ());
2411 if (a.IsValidSecurityAttribute ()) {
2412 a.ExtractSecurityPermissionSet (ctor, ref declarative_security);
2416 if (a.Target == AttributeTargets.Method) {
2417 method_data.MethodBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
2421 if (a.Target == AttributeTargets.ReturnValue) {
2422 if (return_attributes == null)
2423 return_attributes = new ReturnParameter (this, method_data.MethodBuilder, Location);
2425 return_attributes.ApplyAttributeBuilder (a, ctor, cdata, pa);
2429 ApplyToExtraTarget (a, ctor, cdata, pa);
2432 protected virtual void ApplyToExtraTarget (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
2434 throw new NotSupportedException ("You forgot to define special attribute target handling");
2437 // It is not supported for the accessors
2438 public sealed override bool Define()
2440 throw new NotSupportedException ();
2443 public virtual void Emit (TypeDefinition parent)
2445 method_data.Emit (parent);
2447 if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
2448 Module.PredefinedAttributes.CompilerGenerated.EmitAttribute (method_data.MethodBuilder);
2449 if (((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0))
2450 Module.PredefinedAttributes.DebuggerHidden.EmitAttribute (method_data.MethodBuilder);
2452 if (ReturnType.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
2453 return_attributes = new ReturnParameter (this, method_data.MethodBuilder, Location);
2454 Module.PredefinedAttributes.Dynamic.EmitAttribute (return_attributes.Builder);
2455 } else if (ReturnType.HasDynamicElement) {
2456 return_attributes = new ReturnParameter (this, method_data.MethodBuilder, Location);
2457 Module.PredefinedAttributes.Dynamic.EmitAttribute (return_attributes.Builder, ReturnType, Location);
2460 if (OptAttributes != null)
2461 OptAttributes.Emit ();
2463 if (declarative_security != null) {
2464 foreach (var de in declarative_security) {
2466 method_data.MethodBuilder.__AddDeclarativeSecurity (de);
2468 method_data.MethodBuilder.AddDeclarativeSecurity (de.Key, de.Value);
2476 public override bool EnableOverloadChecks (MemberCore overload)
2478 if (overload is MethodCore) {
2479 caching_flags |= Flags.MethodOverloadsExist;
2483 // This can only happen with indexers and it will
2484 // be catched as indexer difference
2485 if (overload is AbstractPropertyEventMethod)
2491 public override string GetCallerMemberName ()
2493 return base.GetCallerMemberName ().Substring (prefix.Length);
2496 public override string GetSignatureForDocumentation ()
2498 // should not be called
2499 throw new NotSupportedException ();
2502 public override bool IsClsComplianceRequired()
2507 public void PrepareEmit ()
2509 method_data.DefineMethodBuilder (Parent.PartialContainer, ParameterInfo);
2512 public override void WriteDebugSymbol (MonoSymbolFile file)
2514 if (method_data != null)
2515 method_data.WriteDebugSymbol (file);
2518 public MethodSpec Spec { get; protected set; }
2521 // Represents header string for documentation comment.
2523 public override string DocCommentHeader {
2524 get { throw new InvalidOperationException ("Unexpected attempt to get doc comment from " + this.GetType () + "."); }
2528 public class Operator : MethodOrOperator {
2530 const Modifiers AllowedModifiers =
2536 public enum OpType : byte {
2546 // Unary and Binary operators
2569 // Implicit and Explicit
2573 // Just because of enum
2577 public readonly OpType OperatorType;
2579 static readonly string [] [] names;
2583 names = new string[(int)OpType.TOP][];
2584 names [(int) OpType.LogicalNot] = new string [] { "!", "op_LogicalNot" };
2585 names [(int) OpType.OnesComplement] = new string [] { "~", "op_OnesComplement" };
2586 names [(int) OpType.Increment] = new string [] { "++", "op_Increment" };
2587 names [(int) OpType.Decrement] = new string [] { "--", "op_Decrement" };
2588 names [(int) OpType.True] = new string [] { "true", "op_True" };
2589 names [(int) OpType.False] = new string [] { "false", "op_False" };
2590 names [(int) OpType.Addition] = new string [] { "+", "op_Addition" };
2591 names [(int) OpType.Subtraction] = new string [] { "-", "op_Subtraction" };
2592 names [(int) OpType.UnaryPlus] = new string [] { "+", "op_UnaryPlus" };
2593 names [(int) OpType.UnaryNegation] = new string [] { "-", "op_UnaryNegation" };
2594 names [(int) OpType.Multiply] = new string [] { "*", "op_Multiply" };
2595 names [(int) OpType.Division] = new string [] { "/", "op_Division" };
2596 names [(int) OpType.Modulus] = new string [] { "%", "op_Modulus" };
2597 names [(int) OpType.BitwiseAnd] = new string [] { "&", "op_BitwiseAnd" };
2598 names [(int) OpType.BitwiseOr] = new string [] { "|", "op_BitwiseOr" };
2599 names [(int) OpType.ExclusiveOr] = new string [] { "^", "op_ExclusiveOr" };
2600 names [(int) OpType.LeftShift] = new string [] { "<<", "op_LeftShift" };
2601 names [(int) OpType.RightShift] = new string [] { ">>", "op_RightShift" };
2602 names [(int) OpType.Equality] = new string [] { "==", "op_Equality" };
2603 names [(int) OpType.Inequality] = new string [] { "!=", "op_Inequality" };
2604 names [(int) OpType.GreaterThan] = new string [] { ">", "op_GreaterThan" };
2605 names [(int) OpType.LessThan] = new string [] { "<", "op_LessThan" };
2606 names [(int) OpType.GreaterThanOrEqual] = new string [] { ">=", "op_GreaterThanOrEqual" };
2607 names [(int) OpType.LessThanOrEqual] = new string [] { "<=", "op_LessThanOrEqual" };
2608 names [(int) OpType.Implicit] = new string [] { "implicit", "op_Implicit" };
2609 names [(int) OpType.Explicit] = new string [] { "explicit", "op_Explicit" };
2612 public Operator (TypeDefinition parent, OpType type, FullNamedExpression ret_type, Modifiers mod_flags, ParametersCompiled parameters,
2613 ToplevelBlock block, Attributes attrs, Location loc)
2614 : base (parent, ret_type, mod_flags, AllowedModifiers, new MemberName (GetMetadataName (type), loc), attrs, parameters)
2616 OperatorType = type;
2620 public override void Accept (StructuralVisitor visitor)
2622 visitor.Visit (this);
2625 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
2627 if (a.Type == pa.Conditional) {
2628 Error_ConditionalAttributeIsNotValid ();
2632 base.ApplyAttributeBuilder (a, ctor, cdata, pa);
2635 public override bool Define ()
2637 const Modifiers RequiredModifiers = Modifiers.PUBLIC | Modifiers.STATIC;
2638 if ((ModFlags & RequiredModifiers) != RequiredModifiers){
2639 Report.Error (558, Location, "User-defined operator `{0}' must be declared static and public", GetSignatureForError ());
2642 if (!base.Define ())
2645 if (block != null) {
2646 if (block.IsIterator) {
2648 // Current method is turned into automatically generated
2649 // wrapper which creates an instance of iterator
2651 Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
2652 ModFlags |= Modifiers.DEBUGGER_HIDDEN;
2655 if (Compiler.Settings.WriteMetadataOnly)
2659 // imlicit and explicit operator of same types are not allowed
2660 if (OperatorType == OpType.Explicit)
2661 Parent.MemberCache.CheckExistingMembersOverloads (this, GetMetadataName (OpType.Implicit), parameters);
2662 else if (OperatorType == OpType.Implicit)
2663 Parent.MemberCache.CheckExistingMembersOverloads (this, GetMetadataName (OpType.Explicit), parameters);
2665 TypeSpec declaring_type = Parent.CurrentType;
2666 TypeSpec return_type = MemberType;
2667 TypeSpec first_arg_type = ParameterTypes [0];
2669 TypeSpec first_arg_type_unwrap = first_arg_type;
2670 if (first_arg_type.IsNullableType)
2671 first_arg_type_unwrap = Nullable.NullableInfo.GetUnderlyingType (first_arg_type);
2673 TypeSpec return_type_unwrap = return_type;
2674 if (return_type.IsNullableType)
2675 return_type_unwrap = Nullable.NullableInfo.GetUnderlyingType (return_type);
2678 // Rules for conversion operators
2680 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
2681 if (first_arg_type_unwrap == return_type_unwrap && first_arg_type_unwrap == declaring_type) {
2682 Report.Error (555, Location,
2683 "User-defined operator cannot take an object of the enclosing type and convert to an object of the enclosing type");
2688 if (declaring_type == return_type || declaring_type == return_type_unwrap) {
2689 conv_type = first_arg_type;
2690 } else if (declaring_type == first_arg_type || declaring_type == first_arg_type_unwrap) {
2691 conv_type = return_type;
2693 Report.Error (556, Location,
2694 "User-defined conversion must convert to or from the enclosing type");
2698 if (conv_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
2699 Report.Error (1964, Location,
2700 "User-defined conversion `{0}' cannot convert to or from the dynamic type",
2701 GetSignatureForError ());
2706 if (conv_type.IsInterface) {
2707 Report.Error (552, Location, "User-defined conversion `{0}' cannot convert to or from an interface type",
2708 GetSignatureForError ());
2712 if (conv_type.IsClass) {
2713 if (TypeSpec.IsBaseClass (declaring_type, conv_type, true)) {
2714 Report.Error (553, Location, "User-defined conversion `{0}' cannot convert to or from a base class",
2715 GetSignatureForError ());
2719 if (TypeSpec.IsBaseClass (conv_type, declaring_type, false)) {
2720 Report.Error (554, Location, "User-defined conversion `{0}' cannot convert to or from a derived class",
2721 GetSignatureForError ());
2725 } else if (OperatorType == OpType.LeftShift || OperatorType == OpType.RightShift) {
2726 if (first_arg_type != declaring_type || parameters.Types[1].BuiltinType != BuiltinTypeSpec.Type.Int) {
2727 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");
2730 } else if (parameters.Count == 1) {
2731 // Checks for Unary operators
2733 if (OperatorType == OpType.Increment || OperatorType == OpType.Decrement) {
2734 if (return_type != declaring_type && !TypeSpec.IsBaseClass (return_type, declaring_type, false)) {
2735 Report.Error (448, Location,
2736 "The return type for ++ or -- operator must be the containing type or derived from the containing type");
2739 if (first_arg_type != declaring_type) {
2741 559, Location, "The parameter type for ++ or -- operator must be the containing type");
2746 if (first_arg_type_unwrap != declaring_type) {
2747 Report.Error (562, Location,
2748 "The parameter type of a unary operator must be the containing type");
2752 if (OperatorType == OpType.True || OperatorType == OpType.False) {
2753 if (return_type.BuiltinType != BuiltinTypeSpec.Type.Bool) {
2756 "The return type of operator True or False " +
2762 } else if (first_arg_type_unwrap != declaring_type) {
2763 // Checks for Binary operators
2765 var second_arg_type = ParameterTypes[1];
2766 if (second_arg_type.IsNullableType)
2767 second_arg_type = Nullable.NullableInfo.GetUnderlyingType (second_arg_type);
2769 if (second_arg_type != declaring_type) {
2770 Report.Error (563, Location,
2771 "One of the parameters of a binary operator must be the containing type");
2779 protected override bool ResolveMemberType ()
2781 if (!base.ResolveMemberType ())
2784 flags |= MethodAttributes.SpecialName | MethodAttributes.HideBySig;
2788 protected override MemberSpec FindBaseMember (out MemberSpec bestCandidate, ref bool overrides)
2790 // Operator cannot be override
2791 bestCandidate = null;
2795 public static string GetName (OpType ot)
2797 return names [(int) ot] [0];
2800 public static string GetName (string metadata_name)
2802 for (int i = 0; i < names.Length; ++i) {
2803 if (names [i] [1] == metadata_name)
2804 return names [i] [0];
2809 public static string GetMetadataName (OpType ot)
2811 return names [(int) ot] [1];
2814 public static string GetMetadataName (string name)
2816 for (int i = 0; i < names.Length; ++i) {
2817 if (names [i] [0] == name)
2818 return names [i] [1];
2823 public static OpType? GetType (string metadata_name)
2825 for (int i = 0; i < names.Length; ++i) {
2826 if (names[i][1] == metadata_name)
2833 public OpType GetMatchingOperator ()
2835 switch (OperatorType) {
2836 case OpType.Equality:
2837 return OpType.Inequality;
2838 case OpType.Inequality:
2839 return OpType.Equality;
2841 return OpType.False;
2844 case OpType.GreaterThan:
2845 return OpType.LessThan;
2846 case OpType.LessThan:
2847 return OpType.GreaterThan;
2848 case OpType.GreaterThanOrEqual:
2849 return OpType.LessThanOrEqual;
2850 case OpType.LessThanOrEqual:
2851 return OpType.GreaterThanOrEqual;
2857 public override string GetSignatureForDocumentation ()
2859 string s = base.GetSignatureForDocumentation ();
2860 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
2861 s = s + "~" + ReturnType.GetSignatureForDocumentation ();
2867 public override string GetSignatureForError ()
2869 StringBuilder sb = new StringBuilder ();
2870 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
2871 sb.AppendFormat ("{0}.{1} operator {2}",
2872 Parent.GetSignatureForError (), GetName (OperatorType),
2873 member_type == null ? type_expr.GetSignatureForError () : member_type.GetSignatureForError ());
2876 sb.AppendFormat ("{0}.operator {1}", Parent.GetSignatureForError (), GetName (OperatorType));
2879 sb.Append (parameters.GetSignatureForError ());
2880 return sb.ToString ();