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;
23 using XmlElement = System.Object;
29 using MetaType = IKVM.Reflection.Type;
30 using SecurityType = System.Collections.Generic.List<IKVM.Reflection.Emit.CustomAttributeBuilder>;
31 using IKVM.Reflection;
32 using IKVM.Reflection.Emit;
34 using MetaType = System.Type;
35 using SecurityType = System.Collections.Generic.Dictionary<System.Security.Permissions.SecurityAction, System.Security.PermissionSet>;
36 using System.Reflection;
37 using System.Reflection.Emit;
40 using Mono.CompilerServices.SymbolWriter;
42 namespace Mono.CSharp {
44 public abstract class MethodCore : InterfaceMemberBase, IParametersMember
46 protected ParametersCompiled parameters;
47 protected ToplevelBlock block;
48 protected MethodSpec spec;
50 public MethodCore (TypeDefinition parent, FullNamedExpression type, Modifiers mod, Modifiers allowed_mod,
51 MemberName name, Attributes attrs, ParametersCompiled parameters)
52 : base (parent, type, mod, allowed_mod, name, attrs)
54 this.parameters = parameters;
57 public override Variance ExpectedMemberTypeVariance {
59 return Variance.Covariant;
64 // Returns the System.Type array for the parameters of this method
66 public TypeSpec [] ParameterTypes {
68 return parameters.Types;
72 public ParametersCompiled ParameterInfo {
78 AParametersCollection IParametersMember.Parameters {
79 get { return parameters; }
82 public ToplevelBlock Block {
92 public CallingConventions CallingConventions {
94 CallingConventions cc = parameters.CallingConvention;
96 if ((ModFlags & Modifiers.STATIC) == 0)
97 cc |= CallingConventions.HasThis;
99 // FIXME: How is `ExplicitThis' used in C#?
105 protected override bool CheckOverrideAgainstBase (MemberSpec base_member)
107 bool res = base.CheckOverrideAgainstBase (base_member);
110 // Check that the permissions are not being changed
112 if (!CheckAccessModifiers (this, base_member)) {
113 Error_CannotChangeAccessModifiers (this, base_member);
120 protected override bool CheckBase ()
122 // Check whether arguments were correct.
123 if (!DefineParameters (parameters))
126 return base.CheckBase ();
130 // Represents header string for documentation comment.
132 public override string DocCommentHeader
137 public override void Emit ()
139 if ((ModFlags & Modifiers.COMPILER_GENERATED) == 0) {
140 parameters.CheckConstraints (this);
146 public override bool EnableOverloadChecks (MemberCore overload)
148 if (overload is MethodCore) {
149 caching_flags |= Flags.MethodOverloadsExist;
153 if (overload is AbstractPropertyEventMethod)
156 return base.EnableOverloadChecks (overload);
159 public override string GetSignatureForDocumentation ()
161 string s = base.GetSignatureForDocumentation ();
162 if (MemberName.Arity > 0)
163 s += "``" + MemberName.Arity.ToString ();
165 return s + parameters.GetSignatureForDocumentation ();
168 public MethodSpec Spec {
172 protected override bool VerifyClsCompliance ()
174 if (!base.VerifyClsCompliance ())
177 if (parameters.HasArglist) {
178 Report.Warning (3000, 1, Location, "Methods with variable arguments are not CLS-compliant");
181 if (member_type != null && !member_type.IsCLSCompliant ()) {
182 Report.Warning (3002, 1, Location, "Return type of `{0}' is not CLS-compliant",
183 GetSignatureForError ());
186 parameters.VerifyClsCompliance (this);
191 public interface IGenericMethodDefinition : IMemberDefinition
193 TypeParameterSpec[] TypeParameters { get; }
194 int TypeParametersCount { get; }
196 // MethodInfo MakeGenericMethod (TypeSpec[] targs);
199 public sealed class MethodSpec : MemberSpec, IParametersMember
201 MethodBase metaInfo, inflatedMetaInfo;
202 AParametersCollection parameters;
206 TypeParameterSpec[] constraints;
208 public MethodSpec (MemberKind kind, TypeSpec declaringType, IMemberDefinition details, TypeSpec returnType,
209 MethodBase info, AParametersCollection parameters, Modifiers modifiers)
210 : base (kind, declaringType, details, modifiers)
212 this.metaInfo = info;
213 this.parameters = parameters;
214 this.returnType = returnType;
219 public override int Arity {
221 return IsGeneric ? GenericDefinition.TypeParametersCount : 0;
225 public TypeParameterSpec[] Constraints {
227 if (constraints == null && IsGeneric)
228 constraints = GenericDefinition.TypeParameters;
234 public bool IsConstructor {
236 return Kind == MemberKind.Constructor;
240 public IGenericMethodDefinition GenericDefinition {
242 return (IGenericMethodDefinition) definition;
246 public bool IsExtensionMethod {
248 return IsStatic && parameters.HasExtensionMethodType;
252 public bool IsSealed {
254 return (Modifiers & Modifiers.SEALED) != 0;
258 // When is virtual or abstract
259 public bool IsVirtual {
261 return (Modifiers & (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE)) != 0;
265 public bool IsReservedMethod {
267 return Kind == MemberKind.Operator || IsAccessor;
271 TypeSpec IInterfaceMemberSpec.MemberType {
277 public AParametersCollection Parameters {
283 public TypeSpec ReturnType {
289 public TypeSpec[] TypeArguments {
297 public MethodSpec GetGenericMethodDefinition ()
299 if (!IsGeneric && !DeclaringType.IsGeneric)
302 return MemberCache.GetMember (declaringType, this);
305 public MethodBase GetMetaInfo ()
308 // inflatedMetaInfo is extra field needed for cases where we
309 // inflate method but another nested type can later inflate
310 // again (the cache would be build with inflated metaInfo) and
311 // TypeBuilder can work with method definitions only
313 if (inflatedMetaInfo == null) {
314 if ((state & StateFlags.PendingMetaInflate) != 0) {
315 var dt_meta = DeclaringType.GetMetaInfo ();
317 if (DeclaringType.IsTypeBuilder) {
319 inflatedMetaInfo = TypeBuilder.GetConstructor (dt_meta, (ConstructorInfo) metaInfo);
321 inflatedMetaInfo = TypeBuilder.GetMethod (dt_meta, (MethodInfo) metaInfo);
324 // it should not be reached
325 throw new NotImplementedException ();
327 inflatedMetaInfo = MethodInfo.GetMethodFromHandle (metaInfo.MethodHandle, dt_meta.TypeHandle);
331 state &= ~StateFlags.PendingMetaInflate;
333 inflatedMetaInfo = metaInfo;
337 if ((state & StateFlags.PendingMakeMethod) != 0) {
338 var sre_targs = new MetaType[targs.Length];
339 for (int i = 0; i < sre_targs.Length; ++i)
340 sre_targs[i] = targs[i].GetMetaInfo ();
342 inflatedMetaInfo = ((MethodInfo) inflatedMetaInfo).MakeGenericMethod (sre_targs);
343 state &= ~StateFlags.PendingMakeMethod;
346 return inflatedMetaInfo;
349 public override string GetSignatureForDocumentation ()
353 case MemberKind.Constructor:
356 case MemberKind.Method:
358 name = Name + "``" + Arity.ToString ();
368 name = DeclaringType.GetSignatureForDocumentation () + "." + name + parameters.GetSignatureForDocumentation ();
369 if (Kind == MemberKind.Operator) {
370 var op = Operator.GetType (Name).Value;
371 if (op == Operator.OpType.Explicit || op == Operator.OpType.Implicit) {
372 name += "~" + ReturnType.GetSignatureForDocumentation ();
379 public override string GetSignatureForError ()
383 name = DeclaringType.GetSignatureForError () + "." + DeclaringType.Name;
384 } else if (Kind == MemberKind.Operator) {
385 var op = Operator.GetType (Name).Value;
386 if (op == Operator.OpType.Implicit || op == Operator.OpType.Explicit) {
387 name = DeclaringType.GetSignatureForError () + "." + Operator.GetName (op) + " operator " + returnType.GetSignatureForError ();
389 name = DeclaringType.GetSignatureForError () + ".operator " + Operator.GetName (op);
391 } else if (IsAccessor) {
392 int split = Name.IndexOf ('_');
393 name = Name.Substring (split + 1);
394 var postfix = Name.Substring (0, split);
396 var pc = parameters.Count;
397 if (pc > 0 && postfix == "get") {
398 name = "this" + parameters.GetSignatureForError ("[", "]", pc);
399 } else if (pc > 1 && postfix == "set") {
400 name = "this" + parameters.GetSignatureForError ("[", "]", pc - 1);
404 return DeclaringType.GetSignatureForError () + "." + name + "." + postfix;
406 name = base.GetSignatureForError ();
408 name += "<" + TypeManager.CSharpName (targs) + ">";
410 name += "<" + TypeManager.CSharpName (GenericDefinition.TypeParameters) + ">";
413 return name + parameters.GetSignatureForError ();
416 public override MemberSpec InflateMember (TypeParameterInflator inflator)
418 var ms = (MethodSpec) base.InflateMember (inflator);
419 ms.inflatedMetaInfo = null;
420 ms.returnType = inflator.Inflate (returnType);
421 ms.parameters = parameters.Inflate (inflator);
423 ms.constraints = TypeParameterSpec.InflateConstraints (inflator, Constraints);
428 public MethodSpec MakeGenericMethod (IMemberContext context, params TypeSpec[] targs)
431 throw new ArgumentNullException ();
433 // if (generic_intances != null && generic_intances.TryGetValue (targs, out ginstance))
436 //if (generic_intances == null)
437 // generic_intances = new Dictionary<TypeSpec[], Method> (TypeSpecArrayComparer.Default);
439 var inflator = new TypeParameterInflator (context, DeclaringType, GenericDefinition.TypeParameters, targs);
441 var inflated = (MethodSpec) MemberwiseClone ();
442 inflated.declaringType = inflator.TypeInstance;
443 inflated.returnType = inflator.Inflate (returnType);
444 inflated.parameters = parameters.Inflate (inflator);
445 inflated.targs = targs;
446 inflated.constraints = TypeParameterSpec.InflateConstraints (inflator, constraints ?? GenericDefinition.TypeParameters);
447 inflated.state |= StateFlags.PendingMakeMethod;
449 // if (inflated.parent == null)
450 // inflated.parent = parent;
452 //generic_intances.Add (targs, inflated);
456 public MethodSpec Mutate (TypeParameterMutator mutator)
458 var targs = TypeArguments;
460 targs = mutator.Mutate (targs);
462 var decl = DeclaringType;
463 if (DeclaringType.IsGenericOrParentIsGeneric) {
464 decl = mutator.Mutate (decl);
467 if (targs == TypeArguments && decl == DeclaringType)
470 var ms = (MethodSpec) MemberwiseClone ();
471 if (decl != DeclaringType) {
472 ms.inflatedMetaInfo = null;
473 ms.declaringType = decl;
474 ms.state |= StateFlags.PendingMetaInflate;
479 ms.state |= StateFlags.PendingMakeMethod;
485 public override List<TypeSpec> ResolveMissingDependencies ()
487 var missing = returnType.ResolveMissingDependencies ();
488 foreach (var pt in parameters.Types) {
489 var m = pt.GetMissingDependencies ();
494 missing = new List<TypeSpec> ();
496 missing.AddRange (m);
502 public void SetMetaInfo (MethodInfo info)
504 if (this.metaInfo != null)
505 throw new InternalErrorException ("MetaInfo reset");
507 this.metaInfo = info;
511 public abstract class MethodOrOperator : MethodCore, IMethodData
513 public MethodBuilder MethodBuilder;
514 ReturnParameter return_attributes;
515 SecurityType declarative_security;
516 protected MethodData MethodData;
518 static readonly string[] attribute_targets = new string [] { "method", "return" };
520 protected MethodOrOperator (TypeDefinition parent, FullNamedExpression type, Modifiers mod, Modifiers allowed_mod, MemberName name,
521 Attributes attrs, ParametersCompiled parameters)
522 : base (parent, type, mod, allowed_mod, name, attrs, parameters)
526 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
528 if (a.Target == AttributeTargets.ReturnValue) {
529 if (return_attributes == null)
530 return_attributes = new ReturnParameter (this, MethodBuilder, Location);
532 return_attributes.ApplyAttributeBuilder (a, ctor, cdata, pa);
536 if (a.Type == pa.MethodImpl) {
537 is_external_implementation = a.IsInternalCall ();
540 if (a.Type == pa.DllImport) {
541 const Modifiers extern_static = Modifiers.EXTERN | Modifiers.STATIC;
542 if ((ModFlags & extern_static) != extern_static) {
543 Report.Error (601, a.Location, "The DllImport attribute must be specified on a method marked `static' and `extern'");
545 is_external_implementation = true;
548 if (a.IsValidSecurityAttribute ()) {
549 a.ExtractSecurityPermissionSet (ctor, ref declarative_security);
553 if (MethodBuilder != null)
554 MethodBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
557 public override AttributeTargets AttributeTargets {
559 return AttributeTargets.Method;
563 protected override bool CheckForDuplications ()
565 return Parent.MemberCache.CheckExistingMembersOverloads (this, parameters);
568 public virtual EmitContext CreateEmitContext (ILGenerator ig)
570 return new EmitContext (this, ig, MemberType);
573 public override bool Define ()
582 if (this is Operator)
583 kind = MemberKind.Operator;
584 else if (this is Destructor)
585 kind = MemberKind.Destructor;
587 kind = MemberKind.Method;
589 if (IsPartialDefinition) {
590 caching_flags &= ~Flags.Excluded_Undetected;
591 caching_flags |= Flags.Excluded;
593 // Add to member cache only when a partial method implementation has not been found yet
594 if ((caching_flags & Flags.PartialDefinitionExists) == 0) {
595 // MethodBase mb = new PartialMethodDefinitionInfo (this);
597 spec = new MethodSpec (kind, Parent.Definition, this, ReturnType, null, parameters, ModFlags);
598 if (MemberName.Arity > 0) {
599 spec.IsGeneric = true;
601 // TODO: Have to move DefineMethod after Define (ideally to Emit)
602 throw new NotImplementedException ("Generic partial methods");
605 Parent.MemberCache.AddMember (spec);
611 MethodData = new MethodData (
612 this, ModFlags, flags, this, MethodBuilder, base_method);
614 if (!MethodData.Define (Parent.PartialContainer, GetFullName (MemberName)))
617 MethodBuilder = MethodData.MethodBuilder;
619 spec = new MethodSpec (kind, Parent.Definition, this, ReturnType, MethodBuilder, parameters, ModFlags);
620 if (MemberName.Arity > 0)
621 spec.IsGeneric = true;
623 Parent.MemberCache.AddMember (this, MethodBuilder.Name, spec);
628 protected override void DoMemberTypeIndependentChecks ()
630 base.DoMemberTypeIndependentChecks ();
632 CheckAbstractAndExtern (block != null);
634 if ((ModFlags & Modifiers.PARTIAL) != 0) {
635 for (int i = 0; i < parameters.Count; ++i) {
636 IParameterData p = parameters.FixedParameters [i];
637 if (p.ModFlags == Parameter.Modifier.OUT) {
638 Report.Error (752, Location, "`{0}': A partial method parameters cannot use `out' modifier",
639 GetSignatureForError ());
642 if (p.HasDefaultValue && IsPartialImplementation)
643 ((Parameter) p).Warning_UselessOptionalParameter (Report);
648 protected override void DoMemberTypeDependentChecks ()
650 base.DoMemberTypeDependentChecks ();
652 if (MemberType.IsStatic) {
653 Error_StaticReturnType ();
657 public override void Emit ()
659 if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
660 Module.PredefinedAttributes.CompilerGenerated.EmitAttribute (MethodBuilder);
661 if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
662 Module.PredefinedAttributes.DebuggerHidden.EmitAttribute (MethodBuilder);
664 if (ReturnType.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
665 return_attributes = new ReturnParameter (this, MethodBuilder, Location);
666 Module.PredefinedAttributes.Dynamic.EmitAttribute (return_attributes.Builder);
667 } else if (ReturnType.HasDynamicElement) {
668 return_attributes = new ReturnParameter (this, MethodBuilder, Location);
669 Module.PredefinedAttributes.Dynamic.EmitAttribute (return_attributes.Builder, ReturnType, Location);
672 if (OptAttributes != null)
673 OptAttributes.Emit ();
675 if (declarative_security != null) {
676 foreach (var de in declarative_security) {
678 MethodBuilder.__AddDeclarativeSecurity (de);
680 MethodBuilder.AddDeclarativeSecurity (de.Key, de.Value);
685 if (type_expr != null)
686 ConstraintChecker.Check (this, member_type, type_expr.Location);
690 if (MethodData != null)
691 MethodData.Emit (Parent);
697 protected void Error_ConditionalAttributeIsNotValid ()
699 Report.Error (577, Location,
700 "Conditional not valid on `{0}' because it is a constructor, destructor, operator or explicit interface implementation",
701 GetSignatureForError ());
704 public bool IsPartialDefinition {
706 return (ModFlags & Modifiers.PARTIAL) != 0 && Block == null;
710 public bool IsPartialImplementation {
712 return (ModFlags & Modifiers.PARTIAL) != 0 && Block != null;
716 public override string[] ValidAttributeTargets {
718 return attribute_targets;
722 #region IMethodData Members
724 bool IMethodData.IsAccessor {
730 public TypeSpec ReturnType {
736 public MemberName MethodName {
743 /// Returns true if method has conditional attribute and the conditions is not defined (method is excluded).
745 public override string[] ConditionalConditions ()
747 if ((caching_flags & (Flags.Excluded_Undetected | Flags.Excluded)) == 0)
750 if ((ModFlags & Modifiers.PARTIAL) != 0 && (caching_flags & Flags.Excluded) != 0)
751 return new string [0];
753 caching_flags &= ~Flags.Excluded_Undetected;
756 if (base_method == null) {
757 if (OptAttributes == null)
760 Attribute[] attrs = OptAttributes.SearchMulti (Module.PredefinedAttributes.Conditional);
764 conditions = new string[attrs.Length];
765 for (int i = 0; i < conditions.Length; ++i)
766 conditions[i] = attrs[i].GetConditionalAttributeValue ();
768 conditions = base_method.MemberDefinition.ConditionalConditions();
771 if (conditions != null)
772 caching_flags |= Flags.Excluded;
777 public virtual void EmitExtraSymbolInfo (SourceMethod source)
784 public class SourceMethod : IMethodDef
787 SourceMethodBuilder builder;
789 SourceMethod (NamespaceContainer parent, MethodBase method, ICompileUnit file)
791 this.method = method;
792 builder = SymbolWriter.OpenMethod (file, parent == null ? -1 : parent.SymbolFileID, this);
796 get { return method.Name; }
802 var mb = method as MethodBuilder;
804 token = mb.GetToken ();
806 token = ((ConstructorBuilder) method).GetToken ();
808 if (token.IsPseudoToken)
809 return ((ModuleBuilder) method.Module).ResolvePseudoToken (token.Token);
815 public void CloseMethod ()
817 SymbolWriter.CloseMethod ();
820 public void SetRealMethodName (string name)
823 builder.SetRealMethodName (name);
826 public static SourceMethod Create (TypeDefinition parent, MethodBase method, Block block)
828 if (!SymbolWriter.HasSymbolWriter)
833 Location start_loc = block.StartLocation;
834 if (start_loc.IsNull)
837 // TODO: What to do with anonymous types
838 TypeContainer ns = parent.PartialContainer.Parent;
839 while (ns != null && !(ns is NamespaceContainer) && ns.PartialContainer != null)
840 ns = ns.PartialContainer.Parent;
842 var ns_cont = ns as NamespaceContainer;
846 return new SourceMethod (ns_cont, method, ns_cont.CompileUnitEntry);
850 public class Method : MethodOrOperator, IGenericMethodDefinition
852 Method partialMethodImplementation;
854 public Method (TypeDefinition parent, FullNamedExpression return_type, Modifiers mod, MemberName name, ParametersCompiled parameters, Attributes attrs)
855 : base (parent, return_type, mod,
856 parent.PartialContainer.Kind == MemberKind.Interface ? AllowedModifiersInterface :
857 parent.PartialContainer.Kind == MemberKind.Struct ? AllowedModifiersStruct | Modifiers.ASYNC :
858 AllowedModifiersClass | Modifiers.ASYNC,
859 name, attrs, parameters)
863 protected Method (TypeDefinition parent, FullNamedExpression return_type, Modifiers mod, Modifiers amod,
864 MemberName name, ParametersCompiled parameters, Attributes attrs)
865 : base (parent, return_type, mod, amod, name, attrs, parameters)
871 public override TypeParameters CurrentTypeParameters {
873 return MemberName.TypeParameters;
877 public TypeParameterSpec[] TypeParameters {
879 return CurrentTypeParameters.Types;
883 public int TypeParametersCount {
885 return CurrentTypeParameters == null ? 0 : CurrentTypeParameters.Count;
891 public override void Accept (StructuralVisitor visitor)
893 visitor.Visit (this);
896 public static Method Create (TypeDefinition parent, FullNamedExpression returnType, Modifiers mod,
897 MemberName name, ParametersCompiled parameters, Attributes attrs, bool hasConstraints)
899 var m = new Method (parent, returnType, mod, name, parameters, attrs);
901 if (hasConstraints && ((mod & Modifiers.OVERRIDE) != 0 || m.IsExplicitImpl)) {
902 m.Report.Error (460, m.Location,
903 "`{0}': Cannot specify constraints for overrides and explicit interface implementation methods",
904 m.GetSignatureForError ());
907 if ((mod & Modifiers.PARTIAL) != 0) {
908 const Modifiers invalid_partial_mod = Modifiers.AccessibilityMask | Modifiers.ABSTRACT | Modifiers.EXTERN |
909 Modifiers.NEW | Modifiers.OVERRIDE | Modifiers.SEALED | Modifiers.VIRTUAL;
911 if ((mod & invalid_partial_mod) != 0) {
912 m.Report.Error (750, m.Location,
913 "A partial method cannot define access modifier or any of abstract, extern, new, override, sealed, or virtual modifiers");
914 mod &= ~invalid_partial_mod;
917 if ((parent.ModFlags & Modifiers.PARTIAL) == 0) {
918 m.Report.Error (751, m.Location,
919 "A partial method must be declared within a partial class or partial struct");
923 if ((mod & Modifiers.STATIC) == 0 && parameters.HasExtensionMethodType) {
924 m.Report.Error (1105, m.Location, "`{0}': Extension methods must be declared static",
925 m.GetSignatureForError ());
932 public override string GetSignatureForError()
934 return base.GetSignatureForError () + parameters.GetSignatureForError ();
937 void Error_DuplicateEntryPoint (Method b)
939 Report.Error (17, b.Location,
940 "Program `{0}' has more than one entry point defined: `{1}'",
941 b.Module.Builder.ScopeName, b.GetSignatureForError ());
946 if (ReturnType.Kind != MemberKind.Void && ReturnType.BuiltinType != BuiltinTypeSpec.Type.Int)
949 if (parameters.IsEmpty)
952 if (parameters.Count > 1)
955 var ac = parameters.Types [0] as ArrayContainer;
956 return ac != null && ac.Rank == 1 && ac.Element.BuiltinType == BuiltinTypeSpec.Type.String &&
957 (parameters[0].ModFlags & ~Parameter.Modifier.PARAMS) == Parameter.Modifier.NONE;
960 public override FullNamedExpression LookupNamespaceOrType (string name, int arity, LookupMode mode, Location loc)
963 var tp = CurrentTypeParameters;
965 TypeParameter t = tp.Find (name);
967 return new TypeParameterExpr (t, loc);
971 return base.LookupNamespaceOrType (name, arity, mode, loc);
974 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
976 if (a.Type == pa.Conditional) {
977 if (IsExplicitImpl) {
978 Error_ConditionalAttributeIsNotValid ();
982 if ((ModFlags & Modifiers.OVERRIDE) != 0) {
983 Report.Error (243, Location, "Conditional not valid on `{0}' because it is an override method", GetSignatureForError ());
987 if (ReturnType.Kind != MemberKind.Void) {
988 Report.Error (578, Location, "Conditional not valid on `{0}' because its return type is not void", GetSignatureForError ());
993 Report.Error (582, Location, "Conditional not valid on interface members");
997 if (MethodData.implementing != null) {
998 Report.SymbolRelatedToPreviousError (MethodData.implementing.DeclaringType);
999 Report.Error (629, Location, "Conditional member `{0}' cannot implement interface member `{1}'",
1000 GetSignatureForError (), TypeManager.CSharpSignature (MethodData.implementing));
1004 for (int i = 0; i < parameters.Count; ++i) {
1005 if (parameters.FixedParameters [i].ModFlags == Parameter.Modifier.OUT) {
1006 Report.Error (685, Location, "Conditional method `{0}' cannot have an out parameter", GetSignatureForError ());
1012 if (a.Type == pa.Extension) {
1013 a.Error_MisusedExtensionAttribute ();
1017 base.ApplyAttributeBuilder (a, ctor, cdata, pa);
1020 void CreateTypeParameters ()
1022 var tparams = MemberName.TypeParameters;
1023 string[] snames = new string[MemberName.Arity];
1024 var parent_tparams = Parent.TypeParametersAll;
1026 for (int i = 0; i < snames.Length; i++) {
1027 string type_argument_name = tparams[i].MemberName.Name;
1029 if (block == null) {
1030 int idx = parameters.GetParameterIndexByName (type_argument_name);
1034 b = new ToplevelBlock (Compiler, Location);
1036 b.Error_AlreadyDeclaredTypeParameter (type_argument_name, parameters[i].Location);
1039 INamedBlockVariable variable = null;
1040 block.GetLocalName (type_argument_name, block, ref variable);
1041 if (variable != null)
1042 variable.Block.Error_AlreadyDeclaredTypeParameter (type_argument_name, variable.Location);
1045 if (parent_tparams != null) {
1046 var tp = parent_tparams.Find (type_argument_name);
1048 tparams[i].WarningParentNameConflict (tp);
1052 snames[i] = type_argument_name;
1055 GenericTypeParameterBuilder[] gen_params = MethodBuilder.DefineGenericParameters (snames);
1056 tparams.Define (gen_params, null, 0, Parent);
1059 protected virtual void DefineTypeParameters ()
1061 var tparams = CurrentTypeParameters;
1063 TypeParameterSpec[] base_tparams = null;
1064 TypeParameterSpec[] base_decl_tparams = TypeParameterSpec.EmptyTypes;
1065 TypeSpec[] base_targs = TypeSpec.EmptyTypes;
1066 if (((ModFlags & Modifiers.OVERRIDE) != 0 || IsExplicitImpl)) {
1067 if (base_method != null) {
1068 base_tparams = base_method.GenericDefinition.TypeParameters;
1070 if (base_method.DeclaringType.IsGeneric) {
1071 base_decl_tparams = base_method.DeclaringType.MemberDefinition.TypeParameters;
1073 var base_type_parent = CurrentType;
1074 while (base_type_parent.BaseType != base_method.DeclaringType) {
1075 base_type_parent = base_type_parent.BaseType;
1078 base_targs = base_type_parent.BaseType.TypeArguments;
1081 if (base_method.IsGeneric) {
1082 ObsoleteAttribute oa;
1083 foreach (var base_tp in base_tparams) {
1084 oa = base_tp.BaseType.GetAttributeObsolete ();
1086 AttributeTester.Report_ObsoleteMessage (oa, base_tp.BaseType.GetSignatureForError (), Location, Report);
1089 if (base_tp.InterfacesDefined != null) {
1090 foreach (var iface in base_tp.InterfacesDefined) {
1091 oa = iface.GetAttributeObsolete ();
1093 AttributeTester.Report_ObsoleteMessage (oa, iface.GetSignatureForError (), Location, Report);
1099 if (base_decl_tparams.Length != 0) {
1100 base_decl_tparams = base_decl_tparams.Concat (base_tparams).ToArray ();
1101 base_targs = base_targs.Concat (tparams.Types).ToArray ();
1103 base_decl_tparams = base_tparams;
1104 base_targs = tparams.Types;
1107 } else if (MethodData.implementing != null) {
1108 base_tparams = MethodData.implementing.GenericDefinition.TypeParameters;
1109 if (MethodData.implementing.DeclaringType.IsGeneric) {
1110 base_decl_tparams = MethodData.implementing.DeclaringType.MemberDefinition.TypeParameters;
1111 foreach (var iface in Parent.CurrentType.Interfaces) {
1112 if (iface == MethodData.implementing.DeclaringType) {
1113 base_targs = iface.TypeArguments;
1121 for (int i = 0; i < tparams.Count; ++i) {
1122 var tp = tparams[i];
1124 if (!tp.ResolveConstraints (this))
1128 // Copy base constraints for override/explicit methods
1130 if (base_tparams != null) {
1131 var base_tparam = base_tparams[i];
1132 var local_tparam = tp.Type;
1133 local_tparam.SpecialConstraint = base_tparam.SpecialConstraint;
1135 var inflator = new TypeParameterInflator (this, CurrentType, base_decl_tparams, base_targs);
1136 base_tparam.InflateConstraints (inflator, local_tparam);
1139 // Check all type argument constraints for possible collision or unification
1140 // introduced by inflating inherited constraints in this context
1142 // Conflict example:
1144 // class A<T> { virtual void Foo<U> () where U : class, T {} }
1145 // class B : A<int> { override void Foo<U> {} }
1147 var local_tparam_targs = local_tparam.TypeArguments;
1148 if (local_tparam_targs != null) {
1149 for (int ii = 0; ii < local_tparam_targs.Length; ++ii) {
1150 var ta = local_tparam_targs [ii];
1151 if (!ta.IsClass && !ta.IsStruct)
1154 TypeSpec[] unique_tparams = null;
1155 for (int iii = ii + 1; iii < local_tparam_targs.Length; ++iii) {
1157 // Remove any identical or unified constraint types
1159 var tparam_checked = local_tparam_targs[iii];
1160 if (TypeSpecComparer.IsEqual (ta, tparam_checked) || TypeSpec.IsBaseClass (ta, tparam_checked, false)) {
1161 unique_tparams = new TypeSpec[local_tparam_targs.Length - 1];
1162 Array.Copy (local_tparam_targs, 0, unique_tparams, 0, iii);
1163 Array.Copy (local_tparam_targs, iii + 1, unique_tparams, iii, local_tparam_targs.Length - iii - 1);
1164 } else if (!TypeSpec.IsBaseClass (tparam_checked, ta, false)) {
1165 Constraints.Error_ConflictingConstraints (this, local_tparam, ta, tparam_checked, Location);
1169 if (unique_tparams != null) {
1170 local_tparam_targs = unique_tparams;
1171 local_tparam.TypeArguments = local_tparam_targs;
1175 Constraints.CheckConflictingInheritedConstraint (local_tparam, ta, this, Location);
1183 if (base_tparams == null && MethodData != null && MethodData.implementing != null) {
1184 CheckImplementingMethodConstraints (Parent, spec, MethodData.implementing);
1188 public static bool CheckImplementingMethodConstraints (TypeContainer container, MethodSpec method, MethodSpec baseMethod)
1190 var tparams = method.Constraints;
1191 var base_tparams = baseMethod.Constraints;
1192 for (int i = 0; i < tparams.Length; ++i) {
1193 if (!tparams[i].HasSameConstraintsImplementation (base_tparams[i])) {
1194 container.Compiler.Report.SymbolRelatedToPreviousError (method);
1195 container.Compiler.Report.SymbolRelatedToPreviousError (baseMethod);
1197 // Using container location because the interface can be implemented
1199 container.Compiler.Report.Error (425, container.Location,
1200 "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",
1201 tparams[i].GetSignatureForError (), method.GetSignatureForError (),
1202 base_tparams[i].GetSignatureForError (), baseMethod.GetSignatureForError ());
1214 public override bool Define ()
1216 if (!base.Define ())
1219 if (member_type.Kind == MemberKind.Void && parameters.IsEmpty && MemberName.Arity == 0 && MemberName.Name == Destructor.MetadataName) {
1220 Report.Warning (465, 1, Location,
1221 "Introducing `Finalize' method can interfere with destructor invocation. Did you intend to declare a destructor?");
1224 if (partialMethodImplementation != null && IsPartialDefinition)
1225 MethodBuilder = partialMethodImplementation.MethodBuilder;
1227 if (Compiler.Settings.StdLib && ReturnType.IsSpecialRuntimeType) {
1228 Error1599 (Location, ReturnType, Report);
1232 if (CurrentTypeParameters == null) {
1233 if (base_method != null && !IsExplicitImpl) {
1234 if (parameters.Count == 1 && ParameterTypes[0].BuiltinType == BuiltinTypeSpec.Type.Object && MemberName.Name == "Equals")
1235 Parent.PartialContainer.Mark_HasEquals ();
1236 else if (parameters.IsEmpty && MemberName.Name == "GetHashCode")
1237 Parent.PartialContainer.Mark_HasGetHashCode ();
1241 DefineTypeParameters ();
1244 if (block != null) {
1245 if (block.IsIterator) {
1247 // Current method is turned into automatically generated
1248 // wrapper which creates an instance of iterator
1250 Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
1251 ModFlags |= Modifiers.DEBUGGER_HIDDEN;
1254 if ((ModFlags & Modifiers.ASYNC) != 0) {
1255 if (ReturnType.Kind != MemberKind.Void &&
1256 ReturnType != Module.PredefinedTypes.Task.TypeSpec &&
1257 !ReturnType.IsGenericTask) {
1258 Report.Error (1983, Location, "The return type of an async method must be void, Task, or Task<T>");
1261 AsyncInitializer.Create (this, block, parameters, Parent.PartialContainer, ReturnType, Location);
1265 if ((ModFlags & Modifiers.STATIC) == 0)
1268 if (parameters.HasExtensionMethodType) {
1269 if (Parent.PartialContainer.IsStatic && !Parent.IsGenericOrParentIsGeneric) {
1270 if (!Parent.IsTopLevel)
1271 Report.Error (1109, Location, "`{0}': Extension methods cannot be defined in a nested class",
1272 GetSignatureForError ());
1274 PredefinedAttribute pa = Module.PredefinedAttributes.Extension;
1275 if (!pa.IsDefined) {
1276 Report.Error (1110, Location,
1277 "`{0}': Extension methods require `System.Runtime.CompilerServices.ExtensionAttribute' type to be available. Are you missing an assembly reference?",
1278 GetSignatureForError ());
1281 ModFlags |= Modifiers.METHOD_EXTENSION;
1282 Parent.PartialContainer.ModFlags |= Modifiers.METHOD_EXTENSION;
1283 Spec.DeclaringType.SetExtensionMethodContainer ();
1284 Parent.Module.HasExtensionMethod = true;
1286 Report.Error (1106, Location, "`{0}': Extension methods must be defined in a non-generic static class",
1287 GetSignatureForError ());
1292 // This is used to track the Entry Point,
1294 var settings = Compiler.Settings;
1295 if (settings.NeedsEntryPoint && MemberName.Name == "Main" && (settings.MainClass == null || settings.MainClass == Parent.TypeBuilder.FullName)) {
1296 if (IsEntryPoint ()) {
1297 if (Parent.DeclaringAssembly.EntryPoint == null) {
1298 if (Parent.IsGenericOrParentIsGeneric || MemberName.IsGeneric) {
1299 Report.Warning (402, 4, Location, "`{0}': an entry point cannot be generic or in a generic type",
1300 GetSignatureForError ());
1301 } else if ((ModFlags & Modifiers.ASYNC) != 0) {
1302 Report.Error (4009, Location, "`{0}': an entry point cannot be async method",
1303 GetSignatureForError ());
1306 Parent.DeclaringAssembly.EntryPoint = this;
1309 Error_DuplicateEntryPoint (Parent.DeclaringAssembly.EntryPoint);
1310 Error_DuplicateEntryPoint (this);
1313 Report.Warning (28, 4, Location, "`{0}' has the wrong signature to be an entry point",
1314 GetSignatureForError ());
1324 public override void Emit ()
1327 if (IsPartialDefinition) {
1329 // Use partial method implementation builder for partial method declaration attributes
1331 if (partialMethodImplementation != null) {
1332 MethodBuilder = partialMethodImplementation.MethodBuilder;
1338 if ((ModFlags & Modifiers.PARTIAL) != 0 && (caching_flags & Flags.PartialDefinitionExists) == 0) {
1339 Report.Error (759, Location, "A partial method `{0}' implementation is missing a partial method declaration",
1340 GetSignatureForError ());
1343 if (CurrentTypeParameters != null) {
1344 for (int i = 0; i < CurrentTypeParameters.Count; ++i) {
1345 var tp = CurrentTypeParameters [i];
1346 tp.CheckGenericConstraints (false);
1353 if ((ModFlags & Modifiers.METHOD_EXTENSION) != 0)
1354 Module.PredefinedAttributes.Extension.EmitAttribute (MethodBuilder);
1356 Console.WriteLine ("Internal compiler error at {0}: exception caught while emitting {1}",
1357 Location, MethodBuilder);
1362 public override bool EnableOverloadChecks (MemberCore overload)
1364 if (overload is Indexer)
1367 return base.EnableOverloadChecks (overload);
1370 public static void Error1599 (Location loc, TypeSpec t, Report Report)
1372 Report.Error (1599, loc, "Method or delegate cannot return type `{0}'", TypeManager.CSharpName (t));
1375 protected override bool ResolveMemberType ()
1377 if (CurrentTypeParameters != null) {
1378 MethodBuilder = Parent.TypeBuilder.DefineMethod (GetFullName (MemberName), flags);
1379 CreateTypeParameters ();
1382 return base.ResolveMemberType ();
1385 public void SetPartialDefinition (Method methodDefinition)
1387 caching_flags |= Flags.PartialDefinitionExists;
1388 methodDefinition.partialMethodImplementation = this;
1390 // Ensure we are always using method declaration parameters
1391 for (int i = 0; i < methodDefinition.parameters.Count; ++i ) {
1392 parameters [i].Name = methodDefinition.parameters [i].Name;
1393 parameters [i].DefaultValue = methodDefinition.parameters [i].DefaultValue;
1396 if (methodDefinition.attributes == null)
1399 if (attributes == null) {
1400 attributes = methodDefinition.attributes;
1402 attributes.Attrs.AddRange (methodDefinition.attributes.Attrs);
1407 public abstract class ConstructorInitializer : ExpressionStatement
1409 Arguments argument_list;
1410 MethodSpec base_ctor;
1412 public ConstructorInitializer (Arguments argument_list, Location loc)
1414 this.argument_list = argument_list;
1418 public Arguments Arguments {
1420 return argument_list;
1424 public override bool ContainsEmitWithAwait ()
1426 throw new NotSupportedException ();
1429 public override Expression CreateExpressionTree (ResolveContext ec)
1431 throw new NotSupportedException ("ET");
1434 protected override Expression DoResolve (ResolveContext ec)
1436 eclass = ExprClass.Value;
1439 var caller_builder = (Constructor) ec.MemberContext;
1442 // Spec mandates that constructor initializer will not have `this' access
1444 using (ec.Set (ResolveContext.Options.BaseInitializer)) {
1445 if (argument_list != null) {
1447 argument_list.Resolve (ec, out dynamic);
1450 ec.Report.Error (1975, loc,
1451 "The constructor call cannot be dynamically dispatched within constructor initializer");
1457 type = ec.CurrentType;
1458 if (this is ConstructorBaseInitializer) {
1459 if (ec.CurrentType.BaseType == null)
1462 type = ec.CurrentType.BaseType;
1463 if (ec.CurrentType.IsStruct) {
1464 ec.Report.Error (522, loc,
1465 "`{0}': Struct constructors cannot call base constructors", caller_builder.GetSignatureForError ());
1470 // It is legal to have "this" initializers that take no arguments
1471 // in structs, they are just no-ops.
1473 // struct D { public D (int a) : this () {}
1475 if (ec.CurrentType.IsStruct && argument_list == null)
1479 base_ctor = ConstructorLookup (ec, type, ref argument_list, loc);
1482 // TODO MemberCache: Does it work for inflated types ?
1483 if (base_ctor == caller_builder.Spec){
1484 ec.Report.Error (516, loc, "Constructor `{0}' cannot call itself",
1485 caller_builder.GetSignatureForError ());
1491 public override void Emit (EmitContext ec)
1493 // It can be null for static initializers
1494 if (base_ctor == null)
1499 var call = new CallEmitter ();
1500 call.InstanceExpression = new CompilerGeneratedThis (type, loc);
1501 call.EmitPredefined (ec, base_ctor, argument_list);
1504 public override void EmitStatement (EmitContext ec)
1510 public class ConstructorBaseInitializer : ConstructorInitializer {
1511 public ConstructorBaseInitializer (Arguments argument_list, Location l) :
1512 base (argument_list, l)
1517 class GeneratedBaseInitializer: ConstructorBaseInitializer {
1518 public GeneratedBaseInitializer (Location loc):
1524 public class ConstructorThisInitializer : ConstructorInitializer {
1525 public ConstructorThisInitializer (Arguments argument_list, Location l) :
1526 base (argument_list, l)
1531 public class Constructor : MethodCore, IMethodData {
1532 public ConstructorBuilder ConstructorBuilder;
1533 public ConstructorInitializer Initializer;
1534 SecurityType declarative_security;
1535 bool has_compliant_args;
1538 // Modifiers allowed for a constructor.
1540 public const Modifiers AllowedModifiers =
1542 Modifiers.PROTECTED |
1543 Modifiers.INTERNAL |
1549 static readonly string[] attribute_targets = new string [] { "method" };
1551 public static readonly string ConstructorName = ".ctor";
1552 public static readonly string TypeConstructorName = ".cctor";
1554 public Constructor (TypeDefinition parent, string name, Modifiers mod, Attributes attrs, ParametersCompiled args, Location loc)
1555 : base (parent, null, mod, AllowedModifiers, new MemberName (name, loc), attrs, args)
1559 public bool HasCompliantArgs {
1561 return has_compliant_args;
1565 public override AttributeTargets AttributeTargets {
1567 return AttributeTargets.Constructor;
1571 bool IMethodData.IsAccessor {
1578 // Returns true if this is a default constructor
1580 public bool IsDefault ()
1582 if ((ModFlags & Modifiers.STATIC) != 0)
1583 return parameters.IsEmpty;
1585 return parameters.IsEmpty &&
1586 (Initializer is ConstructorBaseInitializer) &&
1587 (Initializer.Arguments == null);
1590 public override void Accept (StructuralVisitor visitor)
1592 visitor.Visit (this);
1595 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
1597 if (a.IsValidSecurityAttribute ()) {
1598 a.ExtractSecurityPermissionSet (ctor, ref declarative_security);
1602 if (a.Type == pa.MethodImpl) {
1603 is_external_implementation = a.IsInternalCall ();
1606 ConstructorBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
1609 protected override bool CheckBase ()
1611 if ((ModFlags & Modifiers.STATIC) != 0) {
1612 if (!parameters.IsEmpty) {
1613 Report.Error (132, Location, "`{0}': The static constructor must be parameterless",
1614 GetSignatureForError ());
1618 if ((caching_flags & Flags.MethodOverloadsExist) != 0)
1619 Parent.MemberCache.CheckExistingMembersOverloads (this, parameters);
1621 // the rest can be ignored
1625 // Check whether arguments were correct.
1626 if (!DefineParameters (parameters))
1629 if ((caching_flags & Flags.MethodOverloadsExist) != 0)
1630 Parent.MemberCache.CheckExistingMembersOverloads (this, parameters);
1632 if (Parent.PartialContainer.Kind == MemberKind.Struct && parameters.IsEmpty) {
1633 Report.Error (568, Location,
1634 "Structs cannot contain explicit parameterless constructors");
1638 CheckProtectedModifier ();
1644 // Creates the ConstructorBuilder
1646 public override bool Define ()
1648 if (ConstructorBuilder != null)
1651 if (!CheckAbstractAndExtern (block != null))
1654 // Check if arguments were correct.
1658 var ca = ModifiersExtensions.MethodAttr (ModFlags) | MethodAttributes.RTSpecialName | MethodAttributes.SpecialName;
1660 ConstructorBuilder = Parent.TypeBuilder.DefineConstructor (
1661 ca, CallingConventions,
1662 parameters.GetMetaInfo ());
1664 spec = new MethodSpec (MemberKind.Constructor, Parent.Definition, this, Compiler.BuiltinTypes.Void, ConstructorBuilder, parameters, ModFlags);
1666 Parent.MemberCache.AddMember (spec);
1668 // It's here only to report an error
1669 if (block != null && block.IsIterator) {
1670 member_type = Compiler.BuiltinTypes.Void;
1671 Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
1680 public override void Emit ()
1682 if (Parent.PartialContainer.IsComImport) {
1683 if (!IsDefault ()) {
1684 Report.Error (669, Location, "`{0}': A class with the ComImport attribute cannot have a user-defined constructor",
1685 Parent.GetSignatureForError ());
1688 // Set as internal implementation and reset block data
1689 // to ensure no IL is generated
1690 ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall);
1694 if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
1695 Module.PredefinedAttributes.DebuggerHidden.EmitAttribute (ConstructorBuilder);
1697 if (OptAttributes != null)
1698 OptAttributes.Emit ();
1701 parameters.ApplyAttributes (this, ConstructorBuilder);
1704 // If we use a "this (...)" constructor initializer, then
1705 // do not emit field initializers, they are initialized in the other constructor
1707 bool emit_field_initializers = ((ModFlags & Modifiers.STATIC) != 0) ||
1708 !(Initializer is ConstructorThisInitializer);
1710 BlockContext bc = new BlockContext (this, block, Compiler.BuiltinTypes.Void);
1711 bc.Set (ResolveContext.Options.ConstructorScope);
1713 if (emit_field_initializers)
1714 Parent.PartialContainer.ResolveFieldInitializers (bc);
1716 if (block != null) {
1717 // If this is a non-static `struct' constructor and doesn't have any
1718 // initializer, it must initialize all of the struct's fields.
1719 if ((Parent.PartialContainer.Kind == MemberKind.Struct) &&
1720 ((ModFlags & Modifiers.STATIC) == 0) && (Initializer == null))
1721 block.AddThisVariable (bc);
1723 if ((ModFlags & Modifiers.STATIC) == 0){
1724 if (Parent.PartialContainer.Kind == MemberKind.Class && Initializer == null)
1725 Initializer = new GeneratedBaseInitializer (Location);
1727 if (Initializer != null) {
1728 block.AddScopeStatement (new StatementExpression (Initializer));
1733 SourceMethod source = SourceMethod.Create (Parent, ConstructorBuilder, block);
1735 if (block != null) {
1736 if (block.Resolve (null, bc, this)) {
1737 EmitContext ec = new EmitContext (this, ConstructorBuilder.GetILGenerator (), bc.ReturnType);
1738 ec.With (EmitContext.Options.ConstructorScope, true);
1745 source.CloseMethod ();
1747 if (declarative_security != null) {
1748 foreach (var de in declarative_security) {
1750 ConstructorBuilder.__AddDeclarativeSecurity (de);
1752 ConstructorBuilder.AddDeclarativeSecurity (de.Key, de.Value);
1760 protected override MemberSpec FindBaseMember (out MemberSpec bestCandidate, ref bool overrides)
1762 // Is never override
1763 bestCandidate = null;
1767 public override string GetSignatureForDocumentation ()
1769 return Parent.GetSignatureForDocumentation () + ".#ctor" + parameters.GetSignatureForDocumentation ();
1772 public override string GetSignatureForError()
1774 return base.GetSignatureForError () + parameters.GetSignatureForError ();
1777 public override string[] ValidAttributeTargets {
1779 return attribute_targets;
1783 protected override bool VerifyClsCompliance ()
1785 if (!base.VerifyClsCompliance () || !IsExposedFromAssembly ()) {
1789 if (!parameters.IsEmpty && Parent.Definition.IsAttribute) {
1790 foreach (TypeSpec param in parameters.Types) {
1791 if (param.IsArray) {
1797 has_compliant_args = true;
1801 #region IMethodData Members
1803 public MemberName MethodName {
1809 public TypeSpec ReturnType {
1815 public EmitContext CreateEmitContext (ILGenerator ig)
1817 throw new NotImplementedException ();
1820 public bool IsExcluded()
1825 void IMethodData.EmitExtraSymbolInfo (SourceMethod source)
1832 /// Interface for MethodData class. Holds links to parent members to avoid member duplication.
1834 public interface IMethodData : IMemberContext
1836 CallingConventions CallingConventions { get; }
1837 Location Location { get; }
1838 MemberName MethodName { get; }
1839 TypeSpec ReturnType { get; }
1840 ParametersCompiled ParameterInfo { get; }
1841 MethodSpec Spec { get; }
1842 bool IsAccessor { get; }
1844 Attributes OptAttributes { get; }
1845 ToplevelBlock Block { get; set; }
1847 EmitContext CreateEmitContext (ILGenerator ig);
1848 void EmitExtraSymbolInfo (SourceMethod source);
1852 // Encapsulates most of the Method's state
1854 public class MethodData
1857 static FieldInfo methodbuilder_attrs_field;
1860 public readonly IMethodData method;
1863 // Are we implementing an interface ?
1865 public MethodSpec implementing;
1870 protected InterfaceMemberBase member;
1871 protected Modifiers modifiers;
1872 protected MethodAttributes flags;
1873 protected TypeSpec declaring_type;
1874 protected MethodSpec parent_method;
1876 MethodBuilder builder;
1877 public MethodBuilder MethodBuilder {
1883 public TypeSpec DeclaringType {
1885 return declaring_type;
1889 public MethodData (InterfaceMemberBase member,
1890 Modifiers modifiers, MethodAttributes flags, IMethodData method)
1892 this.member = member;
1893 this.modifiers = modifiers;
1896 this.method = method;
1899 public MethodData (InterfaceMemberBase member,
1900 Modifiers modifiers, MethodAttributes flags,
1901 IMethodData method, MethodBuilder builder,
1902 MethodSpec parent_method)
1903 : this (member, modifiers, flags, method)
1905 this.builder = builder;
1906 this.parent_method = parent_method;
1909 public bool Define (TypeDefinition container, string method_full_name)
1911 PendingImplementation pending = container.PendingImplementations;
1912 MethodSpec ambig_iface_method;
1913 bool optional = false;
1915 if (pending != null) {
1916 implementing = pending.IsInterfaceMethod (method.MethodName, member.InterfaceType, this, out ambig_iface_method, ref optional);
1918 if (member.InterfaceType != null) {
1919 if (implementing == null) {
1920 if (member is PropertyBase) {
1921 container.Compiler.Report.Error (550, method.Location,
1922 "`{0}' is an accessor not found in interface member `{1}{2}'",
1923 method.GetSignatureForError (), TypeManager.CSharpName (member.InterfaceType),
1924 member.GetSignatureForError ().Substring (member.GetSignatureForError ().LastIndexOf ('.')));
1927 container.Compiler.Report.Error (539, method.Location,
1928 "`{0}.{1}' in explicit interface declaration is not a member of interface",
1929 TypeManager.CSharpName (member.InterfaceType), member.ShortName);
1933 if (implementing.IsAccessor && !method.IsAccessor) {
1934 container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
1935 container.Compiler.Report.Error (683, method.Location,
1936 "`{0}' explicit method implementation cannot implement `{1}' because it is an accessor",
1937 member.GetSignatureForError (), TypeManager.CSharpSignature (implementing));
1941 if (implementing != null) {
1942 if (!method.IsAccessor) {
1943 if (implementing.IsAccessor) {
1944 container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
1945 container.Compiler.Report.Error (470, method.Location,
1946 "Method `{0}' cannot implement interface accessor `{1}'",
1947 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing));
1949 } else if (implementing.DeclaringType.IsInterface) {
1950 if (!implementing.IsAccessor) {
1951 container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
1952 container.Compiler.Report.Error (686, method.Location,
1953 "Accessor `{0}' cannot implement interface member `{1}' for type `{2}'. Use an explicit interface implementation",
1954 method.GetSignatureForError (), TypeManager.CSharpSignature (implementing), container.GetSignatureForError ());
1956 PropertyBase.PropertyMethod pm = method as PropertyBase.PropertyMethod;
1957 if (pm != null && pm.HasCustomAccessModifier && (pm.ModFlags & Modifiers.PUBLIC) == 0) {
1958 container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
1959 container.Compiler.Report.Error (277, method.Location,
1960 "Accessor `{0}' must be declared public to implement interface member `{1}'",
1961 method.GetSignatureForError (), implementing.GetSignatureForError ());
1968 ambig_iface_method = null;
1972 // For implicit implementations, make sure we are public, for
1973 // explicit implementations, make sure we are private.
1975 if (implementing != null){
1976 if (member.IsExplicitImpl) {
1977 if (method.ParameterInfo.HasParams && !implementing.Parameters.HasParams) {
1978 container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
1979 container.Compiler.Report.Error (466, method.Location,
1980 "`{0}': the explicit interface implementation cannot introduce the params modifier",
1981 method.GetSignatureForError ());
1984 if (ambig_iface_method != null) {
1985 container.Compiler.Report.SymbolRelatedToPreviousError (ambig_iface_method);
1986 container.Compiler.Report.SymbolRelatedToPreviousError (implementing);
1987 container.Compiler.Report.Warning (473, 2, method.Location,
1988 "Explicit interface implementation `{0}' matches more than one interface member. Consider using a non-explicit implementation instead",
1989 method.GetSignatureForError ());
1993 // Setting implementin to null inside this block will trigger a more
1994 // verbose error reporting for missing interface implementations
1996 if (implementing.DeclaringType.IsInterface) {
1998 // If this is an interface method implementation,
1999 // check for public accessibility
2001 if ((flags & MethodAttributes.MemberAccessMask) != MethodAttributes.Public) {
2002 implementing = null;
2003 } else if (optional && (container.Interfaces == null || Array.IndexOf (container.Interfaces, implementing.DeclaringType) < 0)) {
2005 // We are not implementing interface when base class already implemented it
2007 implementing = null;
2009 } else if ((flags & MethodAttributes.MemberAccessMask) == MethodAttributes.Private) {
2010 // We may never be private.
2011 implementing = null;
2013 } else if ((modifiers & Modifiers.OVERRIDE) == 0) {
2015 // We may be protected if we're overriding something.
2017 implementing = null;
2022 // Static is not allowed
2024 if ((modifiers & Modifiers.STATIC) != 0){
2025 implementing = null;
2030 // If implementing is still valid, set flags
2032 if (implementing != null){
2034 // When implementing interface methods, set NewSlot
2035 // unless, we are overwriting a method.
2037 if ((modifiers & Modifiers.OVERRIDE) == 0 && implementing.DeclaringType.IsInterface) {
2038 flags |= MethodAttributes.NewSlot;
2041 flags |= MethodAttributes.Virtual | MethodAttributes.HideBySig;
2043 // Set Final unless we're virtual, abstract or already overriding a method.
2044 if ((modifiers & (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE)) == 0)
2045 flags |= MethodAttributes.Final;
2048 // clear the pending implementation flag (requires explicit methods to be defined first)
2050 pending.ImplementMethod (method.MethodName,
2051 member.InterfaceType, this, member.IsExplicitImpl, out ambig_iface_method, ref optional);
2054 // Update indexer accessor name to match implementing abstract accessor
2056 if (!implementing.DeclaringType.IsInterface && !member.IsExplicitImpl && implementing.IsAccessor)
2057 method_full_name = implementing.MemberDefinition.Name;
2060 DefineMethodBuilder (container, method_full_name, method.ParameterInfo);
2062 if (builder == null)
2065 // if (container.CurrentType != null)
2066 // declaring_type = container.CurrentType;
2068 declaring_type = container.Definition;
2070 if (implementing != null && member.IsExplicitImpl) {
2071 container.TypeBuilder.DefineMethodOverride (builder, (MethodInfo) implementing.GetMetaInfo ());
2079 /// Create the MethodBuilder for the method
2081 void DefineMethodBuilder (TypeDefinition container, string method_name, ParametersCompiled param)
2083 var return_type = method.ReturnType.GetMetaInfo ();
2084 var p_types = param.GetMetaInfo ();
2086 if (builder == null) {
2087 builder = container.TypeBuilder.DefineMethod (
2088 method_name, flags, method.CallingConventions,
2089 return_type, p_types);
2094 // Generic method has been already defined to resolve method parameters
2095 // correctly when they use type parameters
2097 builder.SetParameters (p_types);
2098 builder.SetReturnType (return_type);
2099 if (builder.Attributes != flags) {
2101 builder.__SetAttributes (flags);
2104 if (methodbuilder_attrs_field == null)
2105 methodbuilder_attrs_field = typeof (MethodBuilder).GetField ("attrs", BindingFlags.NonPublic | BindingFlags.Instance);
2106 methodbuilder_attrs_field.SetValue (builder, flags);
2108 container.Compiler.Report.RuntimeMissingSupport (method.Location, "Generic method MethodAttributes");
2117 public void Emit (TypeDefinition parent)
2119 var mc = (IMemberContext) method;
2121 method.ParameterInfo.ApplyAttributes (mc, MethodBuilder);
2123 SourceMethod source = SourceMethod.Create (parent, MethodBuilder, method.Block);
2125 ToplevelBlock block = method.Block;
2126 if (block != null) {
2127 BlockContext bc = new BlockContext (mc, block, method.ReturnType);
2128 if (block.Resolve (null, bc, method)) {
2129 EmitContext ec = method.CreateEmitContext (MethodBuilder.GetILGenerator ());
2135 if (source != null) {
2136 method.EmitExtraSymbolInfo (source);
2137 source.CloseMethod ();
2142 public class Destructor : MethodOrOperator
2144 const Modifiers AllowedModifiers =
2148 static readonly string[] attribute_targets = new string [] { "method" };
2150 public static readonly string MetadataName = "Finalize";
2152 public Destructor (TypeDefinition parent, Modifiers mod, ParametersCompiled parameters, Attributes attrs, Location l)
2153 : base (parent, null, mod, AllowedModifiers, new MemberName (MetadataName, l), attrs, parameters)
2155 ModFlags &= ~Modifiers.PRIVATE;
2156 ModFlags |= Modifiers.PROTECTED | Modifiers.OVERRIDE;
2159 public override void Accept (StructuralVisitor visitor)
2161 visitor.Visit (this);
2164 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
2166 if (a.Type == pa.Conditional) {
2167 Error_ConditionalAttributeIsNotValid ();
2171 base.ApplyAttributeBuilder (a, ctor, cdata, pa);
2174 protected override bool CheckBase ()
2176 // Don't check base, destructors have special syntax
2180 public override void Emit()
2182 var base_type = Parent.PartialContainer.BaseType;
2183 if (base_type != null && Block != null) {
2184 var base_dtor = MemberCache.FindMember (base_type,
2185 new MemberFilter (MetadataName, 0, MemberKind.Destructor, null, null), BindingRestriction.InstanceOnly) as MethodSpec;
2187 if (base_dtor == null)
2188 throw new NotImplementedException ();
2190 MethodGroupExpr method_expr = MethodGroupExpr.CreatePredefined (base_dtor, base_type, Location);
2191 method_expr.InstanceExpression = new BaseThis (base_type, Location);
2193 var try_block = new ExplicitBlock (block, block.StartLocation, block.EndLocation);
2194 var finaly_block = new ExplicitBlock (block, Location, Location);
2197 // 0-size arguments to avoid CS0250 error
2198 // TODO: Should use AddScopeStatement or something else which emits correct
2201 finaly_block.AddStatement (new StatementExpression (new Invocation (method_expr, new Arguments (0))));
2203 var tf = new TryFinally (try_block, finaly_block, Location);
2204 block.WrapIntoDestructor (tf, try_block);
2210 public override string GetSignatureForError ()
2212 return Parent.GetSignatureForError () + ".~" + Parent.MemberName.Name + "()";
2215 protected override bool ResolveMemberType ()
2217 member_type = Compiler.BuiltinTypes.Void;
2221 public override string[] ValidAttributeTargets {
2223 return attribute_targets;
2228 // Ooouh Martin, templates are missing here.
2229 // When it will be possible move here a lot of child code and template method type.
2230 public abstract class AbstractPropertyEventMethod : MemberCore, IMethodData {
2231 protected MethodData method_data;
2232 protected ToplevelBlock block;
2233 protected SecurityType declarative_security;
2235 protected readonly string prefix;
2237 ReturnParameter return_attributes;
2239 public AbstractPropertyEventMethod (InterfaceMemberBase member, string prefix, Attributes attrs, Location loc)
2240 : base (member.Parent, SetupName (prefix, member, loc), attrs)
2242 this.prefix = prefix;
2245 static MemberName SetupName (string prefix, InterfaceMemberBase member, Location loc)
2247 return new MemberName (member.MemberName.Left, prefix + member.ShortName, member.MemberName.ExplicitInterface, loc);
2250 public void UpdateName (InterfaceMemberBase member)
2252 SetMemberName (SetupName (prefix, member, Location));
2255 #region IMethodData Members
2257 public ToplevelBlock Block {
2267 public CallingConventions CallingConventions {
2269 return CallingConventions.Standard;
2273 public EmitContext CreateEmitContext (ILGenerator ig)
2275 return new EmitContext (this, ig, ReturnType);
2278 public bool IsAccessor {
2284 public bool IsExcluded ()
2289 public MemberName MethodName {
2295 public TypeSpec[] ParameterTypes {
2297 return ParameterInfo.Types;
2301 public abstract ParametersCompiled ParameterInfo { get ; }
2302 public abstract TypeSpec ReturnType { get; }
2306 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
2308 if (a.Type == pa.CLSCompliant || a.Type == pa.Obsolete || a.Type == pa.Conditional) {
2309 Report.Error (1667, a.Location,
2310 "Attribute `{0}' is not valid on property or event accessors. It is valid on `{1}' declarations only",
2311 TypeManager.CSharpName (a.Type), a.GetValidTargets ());
2315 if (a.IsValidSecurityAttribute ()) {
2316 a.ExtractSecurityPermissionSet (ctor, ref declarative_security);
2320 if (a.Target == AttributeTargets.Method) {
2321 method_data.MethodBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
2325 if (a.Target == AttributeTargets.ReturnValue) {
2326 if (return_attributes == null)
2327 return_attributes = new ReturnParameter (this, method_data.MethodBuilder, Location);
2329 return_attributes.ApplyAttributeBuilder (a, ctor, cdata, pa);
2333 ApplyToExtraTarget (a, ctor, cdata, pa);
2336 protected virtual void ApplyToExtraTarget (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
2338 throw new NotSupportedException ("You forgot to define special attribute target handling");
2341 // It is not supported for the accessors
2342 public sealed override bool Define()
2344 throw new NotSupportedException ();
2347 public virtual void Emit (TypeDefinition parent)
2349 method_data.Emit (parent);
2351 if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
2352 Module.PredefinedAttributes.CompilerGenerated.EmitAttribute (method_data.MethodBuilder);
2353 if (((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0))
2354 Module.PredefinedAttributes.DebuggerHidden.EmitAttribute (method_data.MethodBuilder);
2356 if (ReturnType.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
2357 return_attributes = new ReturnParameter (this, method_data.MethodBuilder, Location);
2358 Module.PredefinedAttributes.Dynamic.EmitAttribute (return_attributes.Builder);
2359 } else if (ReturnType.HasDynamicElement) {
2360 return_attributes = new ReturnParameter (this, method_data.MethodBuilder, Location);
2361 Module.PredefinedAttributes.Dynamic.EmitAttribute (return_attributes.Builder, ReturnType, Location);
2364 if (OptAttributes != null)
2365 OptAttributes.Emit ();
2367 if (declarative_security != null) {
2368 foreach (var de in declarative_security) {
2370 method_data.MethodBuilder.__AddDeclarativeSecurity (de);
2372 method_data.MethodBuilder.AddDeclarativeSecurity (de.Key, de.Value);
2380 public override bool EnableOverloadChecks (MemberCore overload)
2382 if (overload is MethodCore) {
2383 caching_flags |= Flags.MethodOverloadsExist;
2387 // This can only happen with indexers and it will
2388 // be catched as indexer difference
2389 if (overload is AbstractPropertyEventMethod)
2395 public override string GetSignatureForDocumentation ()
2397 // should not be called
2398 throw new NotSupportedException ();
2401 public override bool IsClsComplianceRequired()
2406 public MethodSpec Spec { get; protected set; }
2409 // Represents header string for documentation comment.
2411 public override string DocCommentHeader {
2412 get { throw new InvalidOperationException ("Unexpected attempt to get doc comment from " + this.GetType () + "."); }
2415 void IMethodData.EmitExtraSymbolInfo (SourceMethod source)
2419 public class Operator : MethodOrOperator {
2421 const Modifiers AllowedModifiers =
2427 public enum OpType : byte {
2437 // Unary and Binary operators
2460 // Implicit and Explicit
2464 // Just because of enum
2468 public readonly OpType OperatorType;
2470 static readonly string [] [] names;
2474 names = new string[(int)OpType.TOP][];
2475 names [(int) OpType.LogicalNot] = new string [] { "!", "op_LogicalNot" };
2476 names [(int) OpType.OnesComplement] = new string [] { "~", "op_OnesComplement" };
2477 names [(int) OpType.Increment] = new string [] { "++", "op_Increment" };
2478 names [(int) OpType.Decrement] = new string [] { "--", "op_Decrement" };
2479 names [(int) OpType.True] = new string [] { "true", "op_True" };
2480 names [(int) OpType.False] = new string [] { "false", "op_False" };
2481 names [(int) OpType.Addition] = new string [] { "+", "op_Addition" };
2482 names [(int) OpType.Subtraction] = new string [] { "-", "op_Subtraction" };
2483 names [(int) OpType.UnaryPlus] = new string [] { "+", "op_UnaryPlus" };
2484 names [(int) OpType.UnaryNegation] = new string [] { "-", "op_UnaryNegation" };
2485 names [(int) OpType.Multiply] = new string [] { "*", "op_Multiply" };
2486 names [(int) OpType.Division] = new string [] { "/", "op_Division" };
2487 names [(int) OpType.Modulus] = new string [] { "%", "op_Modulus" };
2488 names [(int) OpType.BitwiseAnd] = new string [] { "&", "op_BitwiseAnd" };
2489 names [(int) OpType.BitwiseOr] = new string [] { "|", "op_BitwiseOr" };
2490 names [(int) OpType.ExclusiveOr] = new string [] { "^", "op_ExclusiveOr" };
2491 names [(int) OpType.LeftShift] = new string [] { "<<", "op_LeftShift" };
2492 names [(int) OpType.RightShift] = new string [] { ">>", "op_RightShift" };
2493 names [(int) OpType.Equality] = new string [] { "==", "op_Equality" };
2494 names [(int) OpType.Inequality] = new string [] { "!=", "op_Inequality" };
2495 names [(int) OpType.GreaterThan] = new string [] { ">", "op_GreaterThan" };
2496 names [(int) OpType.LessThan] = new string [] { "<", "op_LessThan" };
2497 names [(int) OpType.GreaterThanOrEqual] = new string [] { ">=", "op_GreaterThanOrEqual" };
2498 names [(int) OpType.LessThanOrEqual] = new string [] { "<=", "op_LessThanOrEqual" };
2499 names [(int) OpType.Implicit] = new string [] { "implicit", "op_Implicit" };
2500 names [(int) OpType.Explicit] = new string [] { "explicit", "op_Explicit" };
2503 public Operator (TypeDefinition parent, OpType type, FullNamedExpression ret_type, Modifiers mod_flags, ParametersCompiled parameters,
2504 ToplevelBlock block, Attributes attrs, Location loc)
2505 : base (parent, ret_type, mod_flags, AllowedModifiers, new MemberName (GetMetadataName (type), loc), attrs, parameters)
2507 OperatorType = type;
2511 public override void Accept (StructuralVisitor visitor)
2513 visitor.Visit (this);
2516 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
2518 if (a.Type == pa.Conditional) {
2519 Error_ConditionalAttributeIsNotValid ();
2523 base.ApplyAttributeBuilder (a, ctor, cdata, pa);
2526 public override bool Define ()
2528 const Modifiers RequiredModifiers = Modifiers.PUBLIC | Modifiers.STATIC;
2529 if ((ModFlags & RequiredModifiers) != RequiredModifiers){
2530 Report.Error (558, Location, "User-defined operator `{0}' must be declared static and public", GetSignatureForError ());
2533 if (!base.Define ())
2536 if (block != null && block.IsIterator) {
2538 // Current method is turned into automatically generated
2539 // wrapper which creates an instance of iterator
2541 Iterator.CreateIterator (this, Parent.PartialContainer, ModFlags);
2542 ModFlags |= Modifiers.DEBUGGER_HIDDEN;
2545 // imlicit and explicit operator of same types are not allowed
2546 if (OperatorType == OpType.Explicit)
2547 Parent.MemberCache.CheckExistingMembersOverloads (this, GetMetadataName (OpType.Implicit), parameters);
2548 else if (OperatorType == OpType.Implicit)
2549 Parent.MemberCache.CheckExistingMembersOverloads (this, GetMetadataName (OpType.Explicit), parameters);
2551 TypeSpec declaring_type = Parent.CurrentType;
2552 TypeSpec return_type = MemberType;
2553 TypeSpec first_arg_type = ParameterTypes [0];
2555 TypeSpec first_arg_type_unwrap = first_arg_type;
2556 if (first_arg_type.IsNullableType)
2557 first_arg_type_unwrap = Nullable.NullableInfo.GetUnderlyingType (first_arg_type);
2559 TypeSpec return_type_unwrap = return_type;
2560 if (return_type.IsNullableType)
2561 return_type_unwrap = Nullable.NullableInfo.GetUnderlyingType (return_type);
2564 // Rules for conversion operators
2566 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
2567 if (first_arg_type_unwrap == return_type_unwrap && first_arg_type_unwrap == declaring_type) {
2568 Report.Error (555, Location,
2569 "User-defined operator cannot take an object of the enclosing type and convert to an object of the enclosing type");
2574 if (declaring_type == return_type || declaring_type == return_type_unwrap) {
2575 conv_type = first_arg_type;
2576 } else if (declaring_type == first_arg_type || declaring_type == first_arg_type_unwrap) {
2577 conv_type = return_type;
2579 Report.Error (556, Location,
2580 "User-defined conversion must convert to or from the enclosing type");
2584 if (conv_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
2585 Report.Error (1964, Location,
2586 "User-defined conversion `{0}' cannot convert to or from the dynamic type",
2587 GetSignatureForError ());
2592 if (conv_type.IsInterface) {
2593 Report.Error (552, Location, "User-defined conversion `{0}' cannot convert to or from an interface type",
2594 GetSignatureForError ());
2598 if (conv_type.IsClass) {
2599 if (TypeSpec.IsBaseClass (declaring_type, conv_type, true)) {
2600 Report.Error (553, Location, "User-defined conversion `{0}' cannot convert to or from a base class",
2601 GetSignatureForError ());
2605 if (TypeSpec.IsBaseClass (conv_type, declaring_type, false)) {
2606 Report.Error (554, Location, "User-defined conversion `{0}' cannot convert to or from a derived class",
2607 GetSignatureForError ());
2611 } else if (OperatorType == OpType.LeftShift || OperatorType == OpType.RightShift) {
2612 if (first_arg_type != declaring_type || parameters.Types[1].BuiltinType != BuiltinTypeSpec.Type.Int) {
2613 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");
2616 } else if (parameters.Count == 1) {
2617 // Checks for Unary operators
2619 if (OperatorType == OpType.Increment || OperatorType == OpType.Decrement) {
2620 if (return_type != declaring_type && !TypeSpec.IsBaseClass (return_type, declaring_type, false)) {
2621 Report.Error (448, Location,
2622 "The return type for ++ or -- operator must be the containing type or derived from the containing type");
2625 if (first_arg_type != declaring_type) {
2627 559, Location, "The parameter type for ++ or -- operator must be the containing type");
2632 if (first_arg_type_unwrap != declaring_type) {
2633 Report.Error (562, Location,
2634 "The parameter type of a unary operator must be the containing type");
2638 if (OperatorType == OpType.True || OperatorType == OpType.False) {
2639 if (return_type.BuiltinType != BuiltinTypeSpec.Type.Bool) {
2642 "The return type of operator True or False " +
2648 } else if (first_arg_type_unwrap != declaring_type) {
2649 // Checks for Binary operators
2651 var second_arg_type = ParameterTypes[1];
2652 if (second_arg_type.IsNullableType)
2653 second_arg_type = Nullable.NullableInfo.GetUnderlyingType (second_arg_type);
2655 if (second_arg_type != declaring_type) {
2656 Report.Error (563, Location,
2657 "One of the parameters of a binary operator must be the containing type");
2665 protected override bool ResolveMemberType ()
2667 if (!base.ResolveMemberType ())
2670 flags |= MethodAttributes.SpecialName | MethodAttributes.HideBySig;
2674 protected override MemberSpec FindBaseMember (out MemberSpec bestCandidate, ref bool overrides)
2676 // Operator cannot be override
2677 bestCandidate = null;
2681 public static string GetName (OpType ot)
2683 return names [(int) ot] [0];
2686 public static string GetName (string metadata_name)
2688 for (int i = 0; i < names.Length; ++i) {
2689 if (names [i] [1] == metadata_name)
2690 return names [i] [0];
2695 public static string GetMetadataName (OpType ot)
2697 return names [(int) ot] [1];
2700 public static string GetMetadataName (string name)
2702 for (int i = 0; i < names.Length; ++i) {
2703 if (names [i] [0] == name)
2704 return names [i] [1];
2709 public static OpType? GetType (string metadata_name)
2711 for (int i = 0; i < names.Length; ++i) {
2712 if (names[i][1] == metadata_name)
2719 public OpType GetMatchingOperator ()
2721 switch (OperatorType) {
2722 case OpType.Equality:
2723 return OpType.Inequality;
2724 case OpType.Inequality:
2725 return OpType.Equality;
2727 return OpType.False;
2730 case OpType.GreaterThan:
2731 return OpType.LessThan;
2732 case OpType.LessThan:
2733 return OpType.GreaterThan;
2734 case OpType.GreaterThanOrEqual:
2735 return OpType.LessThanOrEqual;
2736 case OpType.LessThanOrEqual:
2737 return OpType.GreaterThanOrEqual;
2743 public override string GetSignatureForDocumentation ()
2745 string s = base.GetSignatureForDocumentation ();
2746 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
2747 s = s + "~" + ReturnType.GetSignatureForDocumentation ();
2753 public override string GetSignatureForError ()
2755 StringBuilder sb = new StringBuilder ();
2756 if (OperatorType == OpType.Implicit || OperatorType == OpType.Explicit) {
2757 sb.AppendFormat ("{0}.{1} operator {2}",
2758 Parent.GetSignatureForError (), GetName (OperatorType),
2759 member_type == null ? type_expr.GetSignatureForError () : member_type.GetSignatureForError ());
2762 sb.AppendFormat ("{0}.operator {1}", Parent.GetSignatureForError (), GetName (OperatorType));
2765 sb.Append (parameters.GetSignatureForError ());
2766 return sb.ToString ();