//
// Authors: Miguel de Icaza (miguel@gnu.org)
// Martin Baulig (martin@gnome.org)
+// Marek Safar (marek.safar@seznam.cz)
//
// Licensed under the terms of the GNU GPL
//
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
namespace Mono.CSharp {
// from classes from the arraylist `type_bases'
//
string base_class_name;
+ TypeExpr parent_type;
ArrayList type_bases;
bool members_defined;
bool members_defined_ok;
- // Information in the case we are an attribute type
-
- public AttributeTargets Targets = AttributeTargets.All;
- public bool AllowMultiple = false;
- public bool Inherited;
-
// The interfaces we implement.
TypeExpr [] ifaces;
+ Type[] base_inteface_types;
// The parent member container and our member cache
IMemberContainer parent_container;
Type GenericType;
public TypeContainer ():
- this (null, null, "", null, new Location (-1)) {
+ this (null, null, MemberName.Null, null, new Location (-1)) {
}
- public TypeContainer (NamespaceEntry ns, TypeContainer parent, string name, Attributes attrs, Location l)
+ public TypeContainer (NamespaceEntry ns, TypeContainer parent,
+ MemberName name, Attributes attrs, Location l)
: base (ns, parent, name, attrs, l)
{
types = new ArrayList ();
if (indexers == null)
indexers = new ArrayList ();
- if (i.InterfaceType != null)
+ if (i.MemberName.Left != null)
indexers.Insert (0, i);
else
indexers.Add (i);
return AdditionResult.Success;
}
+ public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+ {
+ if (a.Type == TypeManager.default_member_type) {
+ if (Indexers != null) {
+ Report.Error (646, a.Location,
+ "Cannot specify the DefaultMember attribute on" +
+ " a type containing an indexer");
+ return;
+ }
+ }
+
+ base.ApplyAttributeBuilder (a, cb);
+ }
+
+ public override AttributeTargets AttributeTargets {
+ get {
+ throw new NotSupportedException ();
+ }
+ }
+
public void RegisterOrder (Interface iface)
{
if (interface_order == null)
/// The @parent argument is set to the parent object or null
/// if this is `System.Object'.
/// </summary>
- TypeExpr [] GetClassBases (bool is_class, bool is_iface,
- out TypeExpr parent, out bool error)
+ TypeExpr [] GetClassBases (bool is_class, bool is_iface, out bool error)
{
ArrayList bases = Bases;
int count;
error = false;
if (is_class || is_iface)
- parent = null;
+ parent_type = null;
else
- parent = TypeManager.system_valuetype_expr;
+ parent_type = TypeManager.system_valuetype_expr;
if (bases == null){
if (is_class){
if (RootContext.StdLib)
- parent = TypeManager.system_object_expr;
+ parent_type = TypeManager.system_object_expr;
else if (Name != "System.Object")
- parent = TypeManager.system_object_expr;
+ parent_type = TypeManager.system_object_expr;
} else {
//
// If we are compiling our runtime,
// parent is `System.Object'.
//
if (!RootContext.StdLib && Name == "System.ValueType")
- parent = TypeManager.system_object_expr;
+ parent_type = TypeManager.system_object_expr;
}
return null;
}
if (name.IsClass){
- parent = name;
+ parent_type = name;
start = 1;
} else {
- parent = TypeManager.system_object_expr;
+ parent_type = TypeManager.system_object_expr;
start = 0;
}
if (name.IsSealed){
return null;
}
- if (!parent.CanInheritFrom ()){
+ if (!parent_type.CanInheritFrom ()){
Report.Error (644, Location,
"`{0}' cannot inherit from special class `{1}'",
- Name, parent.Name);
+ Name, parent_type.Name);
error = true;
return null;
}
- if (!parent.AsAccessible (this, ModFlags))
+ if (!parent_type.AsAccessible (this, ModFlags))
Report.Error (60, Location,
"Inconsistent accessibility: base class `" +
name.Name + "' is less accessible than class `" +
start = 0;
}
- if (parent != null)
- base_class_name = parent.Name;
+ if (parent_type != null)
+ base_class_name = parent_type.Name;
TypeExpr [] ifaces = new TypeExpr [count-start];
}
if (resolved.IsClass) {
- if (parent != null){
+ if (parent_type != null){
Report.Error (527, "In Class `" + Name + "', type `"+
name+"' is not an interface");
error = true;
ec = new EmitContext (this, Mono.CSharp.Location.Null, null, null, ModFlags);
- ifaces = GetClassBases (is_class, is_iface, out parent, out error);
+ ifaces = GetClassBases (is_class, is_iface, out error);
if (error)
return null;
TypeAttributes type_attributes = TypeAttr;
Type ptype;
- ConstructedType constructed = parent as ConstructedType;
- if ((constructed == null) && (parent != null))
- ptype = parent.ResolveType (ec);
+ ConstructedType constructed = parent_type as ConstructedType;
+ if ((constructed == null) && (parent_type != null))
+ ptype = parent_type.ResolveType (ec);
else
ptype = null;
ModuleBuilder builder = CodeGen.Module.Builder;
TypeBuilder = builder.DefineType (
Name, type_attributes, ptype, null);
-
} else {
TypeBuilder builder = Parent.DefineType ();
if (builder == null) {
}
TypeBuilder = builder.DefineNestedType (
- Basename, type_attributes, ptype, null);
+ MemberName.Basename, type_attributes, ptype, null);
}
TypeManager.AddUserType (Name, TypeBuilder, this, ifaces);
CurrentType = new ConstructedType (
Name, TypeParameters, Location);
- foreach (TypeParameter type_param in TypeParameters)
- type_param.Define (TypeBuilder);
+ string[] param_names = new string [TypeParameters.Length];
+ for (int i = 0; i < TypeParameters.Length; i++)
+ param_names [i] = TypeParameters [i].Name;
+
+ GenericTypeParameterBuilder[] gen_params;
+
+ gen_params = TypeBuilder.DefineGenericParameters (param_names);
+
+ for (int i = 0; i < gen_params.Length; i++)
+ TypeParameters [i].Define (gen_params [i]);
}
if (constructed != null) {
if (IsGeneric) {
foreach (TypeParameter type_param in TypeParameters)
- if (!type_param.DefineType (ec, TypeBuilder))
+ if (!type_param.DefineType (ec))
error = true;
if (error)
// add interfaces that were not added at type creation
if (ifaces != null) {
- Type[] itypes = new Type [ifaces.Length];
- for (int i = 0; i < ifaces.Length; i++) {
- itypes [i] = ifaces [i].ResolveType (ec);
- if (itypes [i] == null)
- error = true;
- }
+ Type[] itypes = new Type[ifaces.Length];
+ for (int i = 0; i < ifaces.Length; ++i) {
+ Type itype = ifaces [i].ResolveType (ec);
+ TypeBuilder.AddInterfaceImplementation (itype);
+ itypes [i] = itype;
+ }
if (error)
return null;
error = true;
return null;
}
-
- for (int i = 0; i < ifaces.Length; i++)
- TypeBuilder.AddInterfaceImplementation (itypes [i]);
}
//
//
ec.ContainerType = TypeBuilder;
- if ((parent != null) && parent.IsAttribute) {
+ if ((parent_type != null) && parent_type.IsAttribute) {
RootContext.RegisterAttribute (this);
- TypeManager.RegisterAttrType (TypeBuilder, this);
} else
RootContext.RegisterOrder (this);
//
// Defines the indexers, and also verifies that the IndexerNameAttribute in the
- // class is consisten. Either it is `Item' or it is the name defined by all the
+ // class is consistent. Either it is `Item' or it is the name defined by all the
// indexers with the `IndexerName' attribute.
//
// Turns out that the IndexerNameAttribute is applied to each indexer,
- // but it is never emitted, instead a DefaultName attribute is attached
+ // but it is never emitted, instead a DefaultMember attribute is attached
// to the class.
//
void DefineIndexers ()
// a normal indexer. See bug #37714.
//
- ArrayList list = new ArrayList ();
- foreach (Indexer i in Indexers){
- if (i.MemberName.TypeName != null)
- list.Add (i);
- }
- foreach (Indexer i in Indexers){
- if (i.MemberName.TypeName == null)
- list.Add (i);
- }
-
- foreach (Indexer i in list){
+ // Invariant maintained by AddIndexer(): All explicit interface indexers precede normal indexers
+ bool seen_normal_indexers = false;
+ foreach (Indexer i in Indexers) {
string name;
i.Define (this);
name = i.IndexerName;
- if (i.InterfaceType != null)
+ if (i.InterfaceType != null) {
+ if (seen_normal_indexers)
+ throw new Exception ("Internal Error: 'Indexers' array not sorted properly.");
continue;
+ }
+
+ seen_normal_indexers = true;
- if (class_indexer_name == null){
+ if (class_indexer_name == null)
class_indexer_name = name;
- continue;
- }
-
- if (name == class_indexer_name)
- continue;
-
- Report.Error (
- 668, "Two indexers have different names, " +
- " you should use the same name for all your indexers");
+ else if (name != class_indexer_name)
+ Report.Error (668, i.Location, "Two indexers have different names, " +
+ " you should use the same name for all your indexers");
}
- if (class_indexer_name == null)
+
+ if (seen_normal_indexers && class_indexer_name == null)
class_indexer_name = "Item";
IndexerName = class_indexer_name;
}
if (events != null)
DefineMembers (events, defined_names);
- if (indexers != null) {
+ if (indexers != null)
DefineIndexers ();
- } else
- IndexerName = "Item";
if (operators != null){
DefineMembers (operators, null);
}
}
- MethodInfo[] methods = new MethodInfo [members.Count];
- members.CopyTo (methods, 0);
- return methods;
+ MethodInfo[] retMethods = new MethodInfo [members.Count];
+ members.CopyTo (retMethods, 0);
+ return retMethods;
}
/// <summary>
//
// Lookup members in parent if requested.
//
- if (((bf & BindingFlags.DeclaredOnly) == 0) && (TypeBuilder.BaseType != null)) {
+ if ((bf & BindingFlags.DeclaredOnly) == 0) {
+ if (TypeBuilder.BaseType != null) {
MemberList list = FindMembers (TypeBuilder.BaseType, mt, bf, filter, criteria);
if (list.Count > 0) {
if (members == null)
members.AddRange (list);
}
}
+
+ if (base_inteface_types != null) {
+ foreach (Type base_type in base_inteface_types) {
+ MemberList list = TypeContainer.FindMembers (base_type, mt, bf, filter, criteria);
+
+ if (list.Count > 0) {
+ if (members == null)
+ members = new ArrayList ();
+ members.AddRange (list);
+ }
+ }
+ }
+ }
Timer.StopTimer (TimerType.TcFindMembers);
return;
}
+ protected virtual void VerifyMembers (EmitContext ec)
+ {
+ //
+ // Check for internal or private fields that were never assigned
+ //
+ if (RootContext.WarningLevel >= 3) {
+ if (fields != null){
+ foreach (Field f in fields) {
+ if ((f.ModFlags & Modifiers.Accessibility) != Modifiers.PRIVATE)
+ continue;
+
+ if ((f.status & Field.Status.USED) == 0){
+ Report.Warning (
+ 169, f.Location, "Private field " +
+ MakeName (f.Name) + " is never used");
+ continue;
+ }
+
+ //
+ // Only report 649 on level 4
+ //
+ if (RootContext.WarningLevel < 4)
+ continue;
+
+ if ((f.status & Field.Status.ASSIGNED) != 0)
+ continue;
+
+ Report.Warning (
+ 649, f.Location,
+ "Field " + MakeName (f.Name) + " is never assigned " +
+ " to and will always have its default value");
+ }
+ }
+
+ if (events != null){
+ foreach (Event e in events){
+ if (e.status == 0)
+ Report.Warning (67, "The event " + MakeName (e.Name) + " is never used");
+ }
+ }
+ }
+ }
+
/// <summary>
/// Emits the code, this step is performed after all
/// the types, enumerations, constructors
/// </summary>
public void Emit ()
{
- if (instance_constructors != null)
+ if (OptAttributes != null)
+ OptAttributes.Emit (ec, this);
+
+ Emit (this);
+
+ if (instance_constructors != null) {
+ if (TypeBuilder.IsSubclassOf (TypeManager.attribute_type) && IsClsCompliaceRequired (this)) {
+ bool has_compliant_args = false;
+
+ foreach (Constructor c in instance_constructors) {
+ c.Emit (this);
+
+ if (has_compliant_args)
+ continue;
+
+ has_compliant_args = c.HasCompliantArgs;
+ }
+ if (!has_compliant_args)
+ Report.Error_T (3015, Location, GetSignatureForError ());
+ } else {
foreach (Constructor c in instance_constructors)
c.Emit (this);
+ }
+ }
if (default_static_constructor != null)
default_static_constructor.Emit (this);
if (indexers != null){
foreach (Indexer ix in indexers)
ix.Emit (this);
-
- CustomAttributeBuilder cb = EmitDefaultMemberAttr ();
- TypeBuilder.SetCustomAttribute (cb);
+ if (IndexerName != null) {
+ CustomAttributeBuilder cb = EmitDefaultMemberAttr ();
+ TypeBuilder.SetCustomAttribute (cb);
+ }
}
if (fields != null)
e.Emit (this);
}
+ if (delegates != null) {
+ foreach (Delegate d in Delegates) {
+ d.Emit (this);
+ }
+ }
+
if (Pending != null)
if (Pending.VerifyPendingMethods ())
return;
-
- Attribute.ApplyAttributes (ec, TypeBuilder, this, OptAttributes);
-
- //
- // Check for internal or private fields that were never assigned
- //
- if (RootContext.WarningLevel >= 3) {
- if (fields != null){
- foreach (Field f in fields) {
- if ((f.ModFlags & Modifiers.Accessibility) != Modifiers.PRIVATE)
- continue;
-
- if ((f.status & Field.Status.USED) == 0){
- Report.Warning (
- 169, f.Location, "Private field " +
- MakeName (f.Name) + " is never used");
- continue;
- }
-
- //
- // Only report 649 on level 4
- //
- if (RootContext.WarningLevel < 4)
- continue;
-
- if ((f.status & Field.Status.ASSIGNED) != 0)
- continue;
-
- Report.Warning (
- 649, f.Location,
- "Field " + MakeName (f.Name) + " is never assigned " +
- " to and will always have its default value");
- }
- }
- if (events != null){
- foreach (Event e in events){
- if (e.status == 0)
- Report.Warning (67, "The event " + MakeName (e.Name) + " is never used");
- }
- }
- }
+ VerifyMembers (ec);
// if (types != null)
// foreach (TypeContainer tc in types)
public override void CloseType ()
{
- if (Created)
+ if ((caching_flags & Flags.CloseTypeCreated) != 0)
return;
try {
- Created = true;
+ caching_flags |= Flags.CloseTypeCreated;
TypeBuilder.CreateType ();
} catch (TypeLoadException){
//
return true;
}
+ protected override bool VerifyClsCompliance (DeclSpace ds)
+ {
+ if (!base.VerifyClsCompliance (ds))
+ return false;
+
+ // parent_container is null for System.Object
+ if (parent_container != null && !AttributeTester.IsClsCompliant (parent_container.Type)) {
+ Report.Error_T (3009, Location, GetSignatureForError (), TypeManager.CSharpName (parent_container.Type));
+ }
+ return true;
+ }
+
+
/// <summary>
/// Performs checks for an explicit interface implementation. First it
/// checks whether the `interface_type' is a base inteface implementation.
return true;
}
+ protected override void VerifyObsoleteAttribute()
+ {
+ CheckUsageOfObsoleteAttribute (TypeBuilder.BaseType);
+
+ if (ifaces == null)
+ return;
+
+ foreach (TypeExpr expr in ifaces) {
+ CheckUsageOfObsoleteAttribute (expr.Type);
+ }
+ }
+
//
// IMemberContainer
//
}
- public class Class : TypeContainer {
+ public class ClassOrStruct : TypeContainer {
+ bool hasExplicitLayout = false;
+ public ClassOrStruct (NamespaceEntry ns, TypeContainer parent, MemberName name,
+ Attributes attrs, Location l)
+ : base (ns, parent, name, attrs, l)
+ {
+ }
+
+ protected override void VerifyMembers (EmitContext ec)
+ {
+ if (Fields != null) {
+ foreach (Field f in Fields) {
+ if ((f.ModFlags & Modifiers.STATIC) != 0)
+ continue;
+ if (hasExplicitLayout) {
+ if (f.OptAttributes == null
+ || !f.OptAttributes.Contains (TypeManager.field_offset_attribute_type, ec)) {
+ Report.Error (625, f.Location,
+ "Instance field of type marked with"
+ + " StructLayout(LayoutKind.Explicit) must have a"
+ + " FieldOffset attribute.");
+ }
+ }
+ else {
+ if (f.OptAttributes != null
+ && f.OptAttributes.Contains (TypeManager.field_offset_attribute_type, ec)) {
+ Report.Error (636, f.Location,
+ "The FieldOffset attribute can only be placed on members of "
+ + "types marked with the StructLayout(LayoutKind.Explicit)");
+ }
+ }
+ }
+ }
+ base.VerifyMembers (ec);
+ }
+
+ public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+ {
+ if (a.Type == TypeManager.struct_layout_attribute_type
+ && (LayoutKind) a.GetPositionalValue (0) == LayoutKind.Explicit)
+ hasExplicitLayout = true;
+
+ base.ApplyAttributeBuilder (a, cb);
+ }
+ }
+
+ public class Class : ClassOrStruct {
// <summary>
// Modifiers allowed in a class declaration
// </summary>
Modifiers.SEALED |
Modifiers.UNSAFE;
- public Class (NamespaceEntry ns, TypeContainer parent, string name, int mod,
- Attributes attrs, Location l)
+ // Information in the case we are an attribute type
+ AttributeUsageAttribute attribute_usage;
+
+ public Class (NamespaceEntry ns, TypeContainer parent, MemberName name,
+ int mod, Attributes attrs, Location l)
: base (ns, parent, name, attrs, l)
{
int accmods;
accmods = Modifiers.PRIVATE;
this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, l);
+ attribute_usage = new AttributeUsageAttribute (AttributeTargets.All);
+ }
+
+ public override AttributeTargets AttributeTargets {
+ get {
+ return AttributeTargets.Class;
+ }
+ }
+
+ public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
+ {
+ if (a.UsageAttribute != null)
+ attribute_usage = a.UsageAttribute;
+
+ base.ApplyAttributeBuilder (a, cb);
+ }
+
+ public AttributeUsageAttribute AttributeUsage {
+ get {
+ return attribute_usage;
+ }
}
//
}
}
- public class Struct : TypeContainer {
+ public class Struct : ClassOrStruct {
// <summary>
// Modifiers allowed in a struct declaration
// </summary>
Modifiers.UNSAFE |
Modifiers.PRIVATE;
- public Struct (NamespaceEntry ns, TypeContainer parent, string name, int mod, Attributes attrs, Location l)
+ public Struct (NamespaceEntry ns, TypeContainer parent, MemberName name,
+ int mod, Attributes attrs, Location l)
: base (ns, parent, name, attrs, l)
{
int accmods;
this.ModFlags |= Modifiers.SEALED;
}
+ public override AttributeTargets AttributeTargets {
+ get {
+ return AttributeTargets.Struct;
+ }
+ }
+
+
//
// FIXME: Allow the user to specify a different set of attributes
// in some cases (Sealed for example is mandatory for a class,
Modifiers.UNSAFE |
Modifiers.PRIVATE;
- public Interface (NamespaceEntry ns, TypeContainer parent, string name, int mod,
- Attributes attrs, Location l)
+ public Interface (NamespaceEntry ns, TypeContainer parent, MemberName name,
+ int mod, Attributes attrs, Location l)
: base (ns, parent, name, attrs, l)
{
int accmods;
this.ModFlags = Modifiers.Check (AllowedModifiers, mod, accmods, l);
}
+ public override AttributeTargets AttributeTargets {
+ get {
+ return AttributeTargets.Interface;
+ }
+ }
+
public override TypeAttributes TypeAttr {
get {
return base.TypeAttr |
public abstract class MethodCore : MemberBase {
public readonly Parameters Parameters;
+ public readonly GenericMethod GenericMethod;
protected Block block;
- protected DeclSpace ds;
+ public DeclSpace ds;
//
// Parameters, cached for semantic analysis.
// Whether this is an operator method.
public bool IsOperator;
+ static string[] attribute_targets = new string [] { "method", "return" };
+
public MethodCore (DeclSpace ds, Expression type, int mod, int allowed_mod,
bool is_interface, MemberName name, Attributes attrs,
Parameters parameters, Location loc)
Parameters = parameters;
IsInterface = is_interface;
this.ds = ds;
+ this.GenericMethod = ds as GenericMethod;
}
//
return false;
}
- Type[] oct = ogc.Types;
- Type[] ct = gc.Types;
+ if (ogc.HasClassConstraint != gc.HasClassConstraint) {
+ error_425 (ot, t, name);
+ return false;
+ }
+
+ if (ogc.HasClassConstraint &&
+ !ogc.ClassConstraint.Equals (gc.ClassConstraint)) {
+ error_425 (ot, t, name);
+ return false;
+ }
+
+ Type[] oct = ogc.InterfaceConstraints;
+ Type[] ct = gc.InterfaceConstraints;
if (oct.Length != ct.Length) {
error_425 (ot, t, name);
return true;
}
+ protected override string[] ValidAttributeTargets {
+ get {
+ return attribute_targets;
+ }
+ }
+
+ protected override bool VerifyClsCompliance (DeclSpace ds)
+ {
+ if (!base.VerifyClsCompliance (ds)) {
+ if ((ModFlags & Modifiers.ABSTRACT) != 0 && IsExposedFromAssembly (ds) && ds.IsClsCompliaceRequired (ds)) {
+ Report.Error_T (3011, Location, GetSignatureForError ());
+ }
+ return false;
+ }
+
+ if (Parameters.HasArglist) {
+ // "Methods with variable arguments are not CLS-compliant"
+ Report.Error_T (3000, Location);
+ }
+
+ AttributeTester.AreParametersCompliant (Parameters.FixedParameters, Location);
+
+ if (!AttributeTester.IsClsCompliant (MemberType)) {
+ Report.Error_T (3002, Location, GetSignatureForError ());
+ }
+
+ return true;
+ }
+
protected bool IsDuplicateImplementation (TypeContainer tc, MethodCore method)
{
if ((method == this) || (method.Name != Name))
if (param_types.Length != ParameterTypes.Length)
return false;
+ int type_params = 0;
+ if (GenericMethod != null)
+ type_params = GenericMethod.CountTypeParameters;
+
+ int m_type_params = 0;
+ if (method.GenericMethod != null)
+ m_type_params = method.GenericMethod.CountTypeParameters;
+
+ if (type_params != m_type_params)
+ return false;
+
bool equal = true;
bool may_unify;
}
if (equal) {
+ //
+ // Try to report 663: method only differs on out/ref
+ //
+ ParameterData info = ParameterInfo;
+ ParameterData other_info = method.ParameterInfo;
+ for (int i = 0; i < info.Count; i++){
+ if (info.ParameterModifier (i) != other_info.ParameterModifier (i)){
+ Report.Error (663, Location,
+ "Overload method only differs " +
+ "in parameter modifier");
+ return false;
+ }
+ }
+
Report.Error (111, Location,
"Class `{0}' already defines a member called " +
"`{1}' with the same parameter types",
return cc;
}
- //
- // The method's attributes are passed in because we need to extract
- // the "return:" attribute from there to apply on the return type
- //
- static public void LabelParameters (EmitContext ec,
- MethodBase builder,
- Parameters parameters,
- Attributes method_attrs,
- Location loc)
+ protected override void VerifyObsoleteAttribute()
{
- //
- // Define each type attribute (in/out/ref) and
- // the argument names.
- //
- Parameter [] p = parameters.FixedParameters;
- int i = 0;
-
- MethodBuilder mb = null;
- ConstructorBuilder cb = null;
+ base.VerifyObsoleteAttribute ();
- if (builder is MethodBuilder)
- mb = (MethodBuilder) builder;
- else
- cb = (ConstructorBuilder) builder;
+ if (parameter_types == null)
+ return;
- if (p != null){
- for (i = 0; i < p.Length; i++) {
- ParameterBuilder pb;
- ParameterAttributes par_attr = p [i].Attributes;
-
- if (mb == null)
- pb = cb.DefineParameter (
- i + 1, par_attr, p [i].Name);
- else
- pb = mb.DefineParameter (
- i + 1, par_attr, p [i].Name);
-
- Attributes attr = p [i].OptAttributes;
- if (attr != null){
- Attribute.ApplyAttributes (ec, pb, pb, attr);
-
- if (par_attr == ParameterAttributes.Out){
- if (attr.Contains (TypeManager.in_attribute_type))
- Report.Error (36, loc,
- "Can not use [In] attribute on out parameter");
- }
- }
- }
+ foreach (Type type in parameter_types) {
+ CheckUsageOfObsoleteAttribute (type);
}
+ }
+ }
- if (parameters.ArrayParameter != null){
- ParameterBuilder pb;
- Parameter array_param = parameters.ArrayParameter;
-
- if (mb == null)
- pb = cb.DefineParameter (
- i + 1, array_param.Attributes,
- array_param.Name);
- else
- pb = mb.DefineParameter (
- i + 1, array_param.Attributes,
- array_param.Name);
-
- CustomAttributeBuilder a = new CustomAttributeBuilder (
- TypeManager.cons_param_array_attribute, new object [0]);
-
- pb.SetCustomAttribute (a);
- }
-
- //
- // And now for the return type attribute decoration
- //
- ParameterBuilder ret_pb;
- Attributes ret_attrs = null;
-
- if (mb == null || method_attrs == null)
- return;
-
- foreach (AttributeSection asec in method_attrs.AttributeSections) {
-
- if (asec.Target != "return")
- continue;
-
- if (ret_attrs == null)
- ret_attrs = new Attributes (asec);
- else
- ret_attrs.AddAttributeSection (asec);
- }
-
- if (ret_attrs != null) {
- try {
- ret_pb = mb.DefineParameter (0, ParameterAttributes.None, "");
- Attribute.ApplyAttributes (ec, ret_pb, ret_pb, ret_attrs);
-
- } catch (ArgumentOutOfRangeException) {
- Report.Warning (
- -24, loc,
- ".NET SDK 1.0 does not permit setting custom attributes" +
- " on the return type of a method");
- }
- }
- }
- }
-
- public class Method : MethodCore, IIteratorContainer {
- public MethodBuilder MethodBuilder;
- public MethodData MethodData;
- public readonly GenericMethod GenericMethod;
+ public class Method : MethodCore, IIteratorContainer, IMethodData {
+ public MethodBuilder MethodBuilder;
+ public MethodData MethodData;
+ ReturnParameter return_attributes;
/// <summary>
/// Modifiers allowed in a class declaration
Modifiers.EXTERN;
const int AllowedInterfaceModifiers =
- Modifiers.NEW;
+ Modifiers.NEW | Modifiers.UNSAFE;
//
// return_type can be "null" for VOID values.
{
}
- //
- // return_type can be "null" for VOID values.
- //
- public Method (GenericMethod generic, Expression return_type, int mod,
- bool is_iface, MemberName name, Parameters parameters,
- Attributes attrs, Location l)
- : this ((DeclSpace) generic, return_type, mod, is_iface, name,
- parameters, attrs, l)
- {
- GenericMethod = generic;
+ public override AttributeTargets AttributeTargets {
+ get {
+ return AttributeTargets.Method | AttributeTargets.ReturnValue;
+ }
}
-
+
//
// Returns the `System.Type' for the ReturnType of this
// function. Provides a nice cache. (used between semantic analysis
return MemberType;
}
+ public override string GetSignatureForError()
+ {
+ return TypeManager.CSharpSignature (MethodBuilder);
+ }
+
+ /// <summary>
+ /// Use this method when MethodBuilder is null
+ /// </summary>
+ public override string GetSignatureForError (TypeContainer tc)
+ {
+ // TODO: move to parameters
+ System.Text.StringBuilder args = new System.Text.StringBuilder ();
+ if (parameter_info.Parameters.FixedParameters != null) {
+ for (int i = 0; i < parameter_info.Parameters.FixedParameters.Length; ++i) {
+ Parameter p = parameter_info.Parameters.FixedParameters [i];
+ args.Append (p.GetSignatureForError ());
+
+ if (i < parameter_info.Parameters.FixedParameters.Length - 1)
+ args.Append (',');
+ }
+ }
+
+ return String.Concat (base.GetSignatureForError (tc), "(", args.ToString (), ")");
+ }
+
void DuplicateEntryPoint (MethodInfo b, Location location)
{
Report.Error (
return false;
}
+ public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+ {
+ if (a.Target == "return") {
+ if (return_attributes == null)
+ return_attributes = new ReturnParameter (MethodBuilder, Location);
+
+ return_attributes.ApplyAttributeBuilder (a, cb);
+ return;
+ }
+
+ if (a.Type == TypeManager.methodimpl_attr_type && a.IsInternalCall) {
+ MethodBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall | MethodImplAttributes.Runtime);
+ }
+
+ if (a.Type == TypeManager.dllimport_type) {
+ const int extern_static = Modifiers.EXTERN | Modifiers.STATIC;
+ if ((ModFlags & extern_static) != extern_static) {
+ //"The DllImport attribute must be specified on a method marked `static' and `extern'"
+ Report.Error_T (601, a.Location);
+ }
+
+ return;
+ }
+
+ if (a.Type == TypeManager.conditional_attribute_type) {
+ if (IsOperator || IsExplicitImpl) {
+ // Conditional not valid on '{0}' because it is a destructor, operator, or explicit interface implementation
+ Report.Error_T (577, Location, GetSignatureForError ());
+ return;
+ }
+
+ if (ReturnType != TypeManager.void_type) {
+ // Conditional not valid on '{0}' because its return type is not void
+ Report.Error_T (578, Location, GetSignatureForError ());
+ return;
+ }
+
+ if ((ModFlags & Modifiers.OVERRIDE) != 0) {
+ // Conditional not valid on '{0}' because it is an override method
+ Report.Error_T (243, Location, GetSignatureForError ());
+ return;
+ }
+
+ if (IsInterface) {
+ // Conditional not valid on interface members
+ Report.Error_T (582, Location);
+ return;
+ }
+
+ if (MethodData.IsImplementing) {
+ // Conditional member '{0}' cannot implement interface member
+ Report.Error_T (629, Location, GetSignatureForError ());
+ return;
+ }
+ }
+
+ MethodBuilder.SetCustomAttribute (cb);
+ }
+
//
// Checks our base implementation if any
//
return false;
}
}
+
+ ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (parent_method);
+ if (oa != null) {
+ Report.SymbolRelatedToPreviousError (parent_method);
+ Report.Warning_T (672, Location, GetSignatureForError (container));
+ }
} else {
if (!OverridesSomething && ((ModFlags & Modifiers.NEW) != 0))
WarningNotHiding (container);
//
public override bool Define (TypeContainer container)
{
+ if (!DoDefineBase (container))
+ return false;
+
DeclSpace decl;
MethodBuilder mb = null;
if (GenericMethod != null) {
if (!CheckBase (container))
return false;
- CallingConventions cc = GetCallingConvention (container is Class);
-
- MethodData = new MethodData (ds, this, null, MemberType,
- ParameterTypes, ParameterInfo, cc,
- OptAttributes, ModFlags, flags, true,
- mb, GenericMethod);
+ MethodData = new MethodData (this, ParameterInfo, ModFlags, flags,
+ this, mb, GenericMethod);
if (!MethodData.Define (container))
return false;
//
// Emits the code
//
- public void Emit (TypeContainer container)
+ public override void Emit (TypeContainer container)
{
- MethodData.Emit (container, Block, this);
+ MethodData.Emit (container, this);
+ base.Emit (container);
Block = null;
MethodData = null;
}
{
ModFlags |= Modifiers.METHOD_YIELDS;
}
+
+ protected override bool IsIdentifierClsCompliant (DeclSpace ds)
+ {
+ return IsIdentifierAndParamClsCompliant (ds, Name, MethodBuilder, parameter_types);
+ }
+
+ #region IMethodData Members
+
+ public CallingConventions CallingConventions {
+ get {
+ CallingConventions cc = Parameters.GetCallingConvention ();
+ if (Parameters.HasArglist)
+ block.HasVarargs = true;
+
+ if (!IsInterface)
+ if ((ModFlags & Modifiers.STATIC) == 0)
+ cc |= CallingConventions.HasThis;
+
+ // FIXME: How is `ExplicitThis' used in C#?
+
+ return cc;
+ }
+ }
+
+ public Type ReturnType {
+ get {
+ return MemberType;
+ }
+ }
+
+ public string MethodName {
+ get {
+ return ShortName;
+ }
+ }
+
+ public new Location Location {
+ get {
+ return base.Location;
+ }
+ }
+
+ public EmitContext CreateEmitContext (TypeContainer tc, ILGenerator ig)
+ {
+ return new EmitContext (tc, ds, Location, ig, ReturnType, ModFlags, false);
+ }
+
+ public ObsoleteAttribute GetObsoleteAttribute ()
+ {
+ return GetObsoleteAttribute (ds);
+ }
+
+ /// <summary>
+ /// Returns true if method has conditional attribute and the conditions is not defined (method is excluded).
+ /// </summary>
+ public bool IsExcluded (EmitContext ec)
+ {
+ if ((caching_flags & Flags.Excluded_Undetected) == 0)
+ return (caching_flags & Flags.Excluded) != 0;
+
+ caching_flags &= ~Flags.Excluded_Undetected;
+
+ if (parent_method == null) {
+ if (OptAttributes == null)
+ return false;
+
+ Attribute[] attrs = OptAttributes.SearchMulti (TypeManager.conditional_attribute_type, ec);
+
+ if (attrs == null)
+ return false;
+
+ foreach (Attribute a in attrs) {
+ string condition = a.GetConditionalAttributeValue (ds);
+ if (RootContext.AllDefines.Contains (condition))
+ return false;
+ }
+
+ caching_flags |= Flags.Excluded;
+ return true;
+ }
+
+ IMethodData md = TypeManager.GetMethod (parent_method);
+ if (md == null) {
+ if (AttributeTester.IsConditionalMethodExcluded (parent_method)) {
+ caching_flags |= Flags.Excluded;
+ return true;
+ }
+ return false;
+ }
+
+ if (md.IsExcluded (ec)) {
+ caching_flags |= Flags.Excluded;
+ return true;
+ }
+ return false;
+ }
+
+ GenericMethod IMethodData.GenericMethod {
+ get {
+ return GenericMethod;
+ }
+ }
+ #endregion
}
public abstract class ConstructorInitializer {
ArrayList argument_list;
- ConstructorInfo parent_constructor;
+ protected ConstructorInfo parent_constructor;
Parameters parameters;
Location loc;
t = ec.ContainerType;
parent_constructor_group = Expression.MemberLookup (
- ec, t, null, t, ".ctor", 0,
- MemberTypes.Constructor,
+ ec, t, ".ctor", MemberTypes.Constructor,
BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
loc);
if (parent_constructor_group == null){
- Report.Error (1501, loc,
- "Can not find a constructor for this argument list");
+ parent_constructor_group = Expression.MemberLookup (
+ ec, t, ".ctor", MemberTypes.Constructor,
+ BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly,
+ loc);
+
+ if (parent_constructor_group != null)
+ Report.Error (
+ 112, loc, "`{0}.{1}' is inaccessible due to " +
+ "its protection level", t.FullName, t.Name);
+ else
+ Report.Error (
+ 1501, loc, "Can not find a constructor for " +
+ "this argument list");
return false;
}
Invocation.EmitCall (ec, true, false, ec.GetThis (loc), parent_constructor, argument_list, loc);
}
}
+
+ /// <summary>
+ /// Method search for base ctor. (We do not cache it).
+ /// </summary>
+ Constructor GetOverloadedConstructor (TypeContainer tc)
+ {
+ if (tc.InstanceConstructors == null)
+ return null;
+
+ foreach (Constructor c in tc.InstanceConstructors) {
+ if (Arguments == null) {
+ if (c.ParameterTypes.Length == 0)
+ return c;
+
+ continue;
+ }
+
+ bool ok = true;
+
+ int count = c.ParameterInfo.Count;
+ if ((count > 0) &&
+ c.ParameterInfo.ParameterModifier (count - 1) == Parameter.Modifier.PARAMS) {
+ for (int i = 0; i < count-1; i++)
+ if (c.ParameterTypes [i] != ((Argument)Arguments [i]).Type) {
+ ok = false;
+ break;
+ }
+ } else {
+ if (c.ParameterTypes.Length != Arguments.Count)
+ continue;
+
+ for (int i = 0; i < Arguments.Count; ++i)
+ if (c.ParameterTypes [i] != ((Argument)Arguments [i]).Type) {
+ ok = false;
+ break;
+ }
+ }
+
+ if (!ok)
+ continue;
+
+ return c;
+ }
+
+ throw new InternalErrorException ();
+ }
+
+ //TODO: implement caching when it will be necessary
+ public virtual void CheckObsoleteAttribute (TypeContainer tc, Location loc)
+ {
+ Constructor ctor = GetOverloadedConstructor (tc);
+ if (ctor == null)
+ return;
+
+ ObsoleteAttribute oa = ctor.GetObsoleteAttribute (tc);
+ if (oa == null)
+ return;
+
+ AttributeTester.Report_ObsoleteMessage (oa, ctor.GetSignatureForError (), loc);
+ }
}
public class ConstructorBaseInitializer : ConstructorInitializer {
base (argument_list, pars, l)
{
}
+
+ public override void CheckObsoleteAttribute(TypeContainer tc, Location loc) {
+ if (parent_constructor == null)
+ return;
+
+ TypeContainer type_ds = TypeManager.LookupTypeContainer (tc.TypeBuilder.BaseType);
+ if (type_ds == null) {
+ ObsoleteAttribute oa = AttributeTester.GetMemberObsoleteAttribute (parent_constructor);
+
+ if (oa != null)
+ AttributeTester.Report_ObsoleteMessage (oa, TypeManager.CSharpSignature (parent_constructor), loc);
+
+ return;
+ }
+
+ base.CheckObsoleteAttribute (type_ds, loc);
+ }
+
}
public class ConstructorThisInitializer : ConstructorInitializer {
Modifiers.EXTERN |
Modifiers.PRIVATE;
+ bool has_compliant_args = false;
//
// The spec claims that static is not permitted, but
// my very own code has static constructors.
Initializer = init;
}
+ public override string GetSignatureForError()
+ {
+ return TypeManager.CSharpSignature (ConstructorBuilder);
+ }
+
+ public bool HasCompliantArgs {
+ get {
+ return has_compliant_args;
+ }
+ }
+
+ public override AttributeTargets AttributeTargets {
+ get {
+ return AttributeTargets.Constructor;
+ }
+ }
+
+
//
// Returns true if this is a default constructor
//
(Initializer.Arguments == null);
}
+ public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+ {
+ ConstructorBuilder.SetCustomAttribute (cb);
+ }
+
protected override bool CheckBase (TypeContainer container)
{
base.CheckBase (container);
//
// Emits the code
//
- public void Emit (TypeContainer container)
+ public override void Emit (TypeContainer container)
{
ILGenerator ig = ConstructorBuilder.GetILGenerator ();
EmitContext ec = new EmitContext (container, Location, ig, null, ModFlags, true);
ec.IsStatic = false;
}
- MethodCore.LabelParameters (ec, ConstructorBuilder,
- Parameters, OptAttributes, Location);
+ Parameters.LabelParameters (ec, ConstructorBuilder, Location);
SymbolWriter sw = CodeGen.SymbolWriter;
bool generate_debugging = false;
container.EmitFieldInitializers (ec);
}
}
- if (Initializer != null)
+ if (Initializer != null) {
+ Initializer.CheckObsoleteAttribute (container, Location);
Initializer.Emit (ec);
+ }
if ((ModFlags & Modifiers.STATIC) != 0)
container.EmitFieldInitializers (ec);
- Attribute.ApplyAttributes (ec, ConstructorBuilder, this, OptAttributes);
+ if (OptAttributes != null)
+ OptAttributes.Emit (ec, this);
// If this is a non-static `struct' constructor and doesn't have any
// initializer, it must initialize all of the struct's fields.
if (generate_debugging)
sw.CloseMethod ();
+ base.Emit (container);
+
block = null;
}
+
+ // For constructors is needed to test only parameters
+ protected override bool IsIdentifierClsCompliant (DeclSpace ds)
+ {
+ if (parameter_types == null || parameter_types.Length == 0)
+ return true;
+
+ TypeContainer tc = ds as TypeContainer;
+
+ for (int i = 0; i < tc.InstanceConstructors.Count; i++) {
+ Constructor c = (Constructor) tc.InstanceConstructors [i];
+
+ if (c == this || c.ParameterTypes.Length == 0)
+ continue;
+
+ if (!c.IsClsCompliaceRequired (ds))
+ continue;
+
+ if (!AttributeTester.AreOverloadedMethodParamsClsCompliant (parameter_types, c.ParameterTypes)) {
+ Report.Error_T (3006, Location, GetSignatureForError ());
+ return false;
+ }
+ }
+
+ if (tc.TypeBuilder.BaseType == null)
+ return true;
+
+ DeclSpace temp_ds = TypeManager.LookupDeclSpace (tc.TypeBuilder.BaseType);
+ if (temp_ds != null)
+ return IsIdentifierClsCompliant (temp_ds);
+
+ MemberInfo[] ml = tc.TypeBuilder.BaseType.FindMembers (MemberTypes.Constructor, BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance, null, null);
+ // Skip parameter-less ctor
+ if (ml.Length < 2)
+ return true;
+
+ foreach (ConstructorInfo ci in ml) {
+ object[] cls_attribute = ci.GetCustomAttributes (TypeManager.cls_compliant_attribute_type, false);
+ if (cls_attribute.Length == 1 && (!((CLSCompliantAttribute)cls_attribute[0]).IsCompliant))
+ continue;
+
+ if (!AttributeTester.AreOverloadedMethodParamsClsCompliant (parameter_types, TypeManager.GetArgumentTypes (ci))) {
+ Report.Error_T (3006, Location, GetSignatureForError ());
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ protected override bool VerifyClsCompliance (DeclSpace ds)
+ {
+ if (!base.VerifyClsCompliance (ds) || !IsExposedFromAssembly (ds)) {
+ return false;
+ }
+
+ if (ds.TypeBuilder.IsSubclassOf (TypeManager.attribute_type)) {
+ foreach (Type param in parameter_types) {
+ if (param.IsArray) {
+ return false;
+ }
+ }
+ }
+ has_compliant_args = true;
+ return true;
+ }
+
+ }
+
+ /// <summary>
+ /// Interface for MethodData class. Holds links to parent members to avoid member duplication.
+ /// </summary>
+ public interface IMethodData
+ {
+ CallingConventions CallingConventions { get; }
+ Location Location { get; }
+ string MethodName { get; }
+ Type[] ParameterTypes { get; }
+ Type ReturnType { get; }
+ GenericMethod GenericMethod { get; }
+
+ Attributes OptAttributes { get; }
+ Block Block { get; }
+
+ EmitContext CreateEmitContext (TypeContainer tc, ILGenerator ig);
+ ObsoleteAttribute GetObsoleteAttribute ();
+ string GetSignatureForError (TypeContainer tc);
+ bool IsExcluded (EmitContext ec);
}
//
// Encapsulates most of the Method's state
//
public class MethodData {
+
+ readonly IMethodData method;
+
//
// The return type of this method
//
- public readonly Type ReturnType;
- public readonly Type[] ParameterTypes;
public readonly GenericMethod GenericMethod;
public readonly InternalParameters ParameterInfo;
- public readonly CallingConventions CallingConventions;
- public readonly Attributes OptAttributes;
- public readonly Location Location;
//
// Are we implementing an interface ?
//
// Protected data.
//
- protected DeclSpace ds;
protected MemberBase member;
protected int modifiers;
protected MethodAttributes flags;
- protected bool is_method;
- protected string accessor_name;
protected Type declaring_type;
- //
- // It can either hold a string with the condition, or an arraylist of conditions.
- object conditionals;
EmitContext ec;
MethodBuilder builder = null;
}
}
- public MethodData (DeclSpace ds, MemberBase member, string name, Type return_type,
- Type [] parameter_types, InternalParameters parameters,
- CallingConventions cc, Attributes opt_attrs,
- int modifiers, MethodAttributes flags, bool is_method)
+ public MethodData (MemberBase member, InternalParameters parameters,
+ int modifiers, MethodAttributes flags, IMethodData method)
{
- this.ds = ds;
this.member = member;
- this.accessor_name = name;
- this.ReturnType = return_type;
- this.ParameterTypes = parameter_types;
this.ParameterInfo = parameters;
- this.CallingConventions = cc;
- this.OptAttributes = opt_attrs;
this.modifiers = modifiers;
this.flags = flags;
- this.is_method = is_method;
- this.Location = member.Location;
- this.conditionals = null;
+
+ this.method = method;
}
- public MethodData (DeclSpace ds, MemberBase member, string name, Type return_type,
- Type [] parameter_types, InternalParameters parameters,
- CallingConventions cc, Attributes opt_attrs,
- int modifiers, MethodAttributes flags, bool is_method,
- MethodBuilder builder, GenericMethod generic)
- : this (ds, member, name, return_type, parameter_types, parameters,
- cc, opt_attrs, modifiers, flags, is_method)
+ public MethodData (MemberBase member, InternalParameters parameters,
+ int modifiers, MethodAttributes flags,
+ IMethodData method, MethodBuilder builder,
+ GenericMethod generic)
+ : this (member, parameters, modifiers, flags, method)
{
this.builder = builder;
this.GenericMethod = generic;
}
- //
- // Attributes.
- //
- Attribute dllimport_attribute = null;
- string obsolete = null;
- bool obsolete_error = false;
-
- public virtual bool ApplyAttributes (Attributes opt_attrs, bool is_method)
+ public bool Define (TypeContainer container)
{
- if ((opt_attrs == null) || (opt_attrs.AttributeSections == null))
- return true;
-
- foreach (AttributeSection asec in opt_attrs.AttributeSections) {
- if (asec.Attributes == null)
- continue;
-
- foreach (Attribute a in asec.Attributes) {
- if (a.Name == "Conditional") {
- if (!ApplyConditionalAttribute (a))
- return false;
- } else if (a.Name == "Obsolete") {
- if (!ApplyObsoleteAttribute (a))
- return false;
- } else if (a.Name.IndexOf ("DllImport") != -1) {
- if (!is_method) {
- a.Type = TypeManager.dllimport_type;
- Attribute.Error_AttributeNotValidForElement (a, Location);
- return false;
- }
- if (!ApplyDllImportAttribute (a))
- return false;
- }
- }
- }
-
- return true;
- }
-
- //
- // Applies the `DllImport' attribute to the method.
- //
- protected virtual bool ApplyDllImportAttribute (Attribute a)
- {
- const int extern_static = Modifiers.EXTERN | Modifiers.STATIC;
- if ((modifiers & extern_static) != extern_static) {
- Report.Error (601, Location,
- "The DllImport attribute must be specified on a method " +
- "marked `static' and `extern'.");
- return false;
- }
-
- flags |= MethodAttributes.PinvokeImpl;
- dllimport_attribute = a;
- return true;
- }
-
- //
- // Applies the `Obsolete' attribute to the method.
- //
- protected virtual bool ApplyObsoleteAttribute (Attribute a)
- {
- if (obsolete != null) {
- Report.Error (579, Location, "Duplicate `Obsolete' attribute");
- return false;
- }
-
- obsolete = a.Obsolete_GetObsoleteMessage (out obsolete_error);
- return obsolete != null;
- }
-
- //
- // Applies the `Conditional' attribute to the method.
- //
- protected virtual bool ApplyConditionalAttribute (Attribute a)
- {
- // The Conditional attribute is only valid on methods.
- if (!is_method) {
- Attribute.Error_AttributeNotValidForElement (a, Location);
- return false;
- }
-
- string condition = a.Conditional_GetConditionName ();
-
- if (condition == null)
- return false;
-
- if (ReturnType != TypeManager.void_type) {
- Report.Error (578, Location,
- "Conditional not valid on `" + member.Name + "' " +
- "because its return type is not void");
- return false;
- }
-
- if ((modifiers & Modifiers.OVERRIDE) != 0) {
- Report.Error (243, Location,
- "Conditional not valid on `" + member.Name + "' " +
- "because it is an override method");
- return false;
- }
-
- if (member.IsExplicitImpl) {
- Report.Error (577, Location,
- "Conditional not valid on `" + member.Name + "' " +
- "because it is an explicit interface implementation");
- return false;
- }
-
- if (IsImplementing) {
- Report.Error (623, Location,
- "Conditional not valid on `" + member.Name + "' " +
- "because it is an interface method");
- return false;
- }
-
- //
- // The likelyhood that the conditional will be more than 1 is very slim
- //
- if (conditionals == null)
- conditionals = condition;
- else if (conditionals is string){
- string s = (string) conditionals;
- conditionals = new ArrayList ();
- ((ArrayList)conditionals).Add (s);
- } else
- ((ArrayList)conditionals).Add (condition);
-
- return true;
- }
-
- //
- // Checks whether this method should be ignored due to its Conditional attributes.
- //
- bool ShouldIgnore (Location loc)
- {
- // When we're overriding a virtual method, we implicitly inherit the
- // Conditional attributes from our parent.
- if (member.ParentMethod != null) {
- TypeManager.MethodFlags flags = TypeManager.GetMethodFlags (
- member.ParentMethod, loc);
-
- if ((flags & TypeManager.MethodFlags.ShouldIgnore) != 0)
- return true;
- }
-
- if (conditionals != null){
- if (conditionals is string){
- if (RootContext.AllDefines [conditionals] == null)
- return true;
- } else {
- foreach (string condition in (ArrayList) conditionals)
- if (RootContext.AllDefines [condition] == null)
- return true;
- }
- }
- return false;
- }
-
- //
- // Returns the TypeManager.MethodFlags for this method.
- // This emits an error 619 / warning 618 if the method is obsolete.
- // In the former case, TypeManager.MethodFlags.IsObsoleteError is returned.
- //
- public virtual TypeManager.MethodFlags GetMethodFlags (Location loc)
- {
- TypeManager.MethodFlags flags = 0;
-
- if (obsolete != null) {
- if (obsolete_error) {
- Report.Error (619, loc, "Method `" + member.Name +
- "' is obsolete: `" + obsolete + "'");
- return TypeManager.MethodFlags.IsObsoleteError;
- } else
- Report.Warning (618, loc, "Method `" + member.Name +
- "' is obsolete: `" + obsolete + "'");
-
- flags |= TypeManager.MethodFlags.IsObsolete;
- }
-
- if (ShouldIgnore (loc))
- flags |= TypeManager.MethodFlags.ShouldIgnore;
-
- return flags;
- }
-
- public virtual bool Define (TypeContainer container)
- {
- MethodInfo implementing = null;
- string method_name, name, prefix;
-
- if (OptAttributes != null)
- if (!ApplyAttributes (OptAttributes, is_method))
- return false;
+ MethodInfo implementing = null;
+ string prefix;
if (member.IsExplicitImpl)
prefix = member.InterfaceType.FullName + ".";
else
prefix = "";
- if (accessor_name != null)
- name = accessor_name + "_" + member.ShortName;
- else
- name = member.ShortName;
- method_name = prefix + name;
+ string name = method.MethodName;
+ string method_name = prefix + name;
+ Type[] ParameterTypes = method.ParameterTypes;
if (container.Pending != null){
if (member is Indexer)
implementing = container.Pending.IsInterfaceIndexer (
- member.InterfaceType, ReturnType, ParameterTypes);
+ member.InterfaceType, method.ReturnType, ParameterTypes);
else
implementing = container.Pending.IsInterfaceMethod (
- member.InterfaceType, name, ReturnType, ParameterTypes);
+ member.InterfaceType, name, method.ReturnType, ParameterTypes);
if (member.InterfaceType != null && implementing == null){
- Report.Error (539, Location, "'{0}' in explicit interface declaration is not an interface", method_name);
+ Report.Error (539, method.Location, "'{0}' in explicit interface declaration is not an interface", method_name);
return false;
}
}
//
if (member.IsExplicitImpl){
if ((modifiers & (Modifiers.PUBLIC | Modifiers.ABSTRACT | Modifiers.VIRTUAL)) != 0){
- Modifiers.Error_InvalidModifier (Location, "public, virtual or abstract");
+ Modifiers.Error_InvalidModifier (method.Location, "public, virtual or abstract");
implementing = null;
}
} else if ((flags & MethodAttributes.MemberAccessMask) != MethodAttributes.Public){
//
if ((modifiers & Modifiers.STATIC) != 0){
implementing = null;
- Modifiers.Error_InvalidModifier (Location, "static");
+ Modifiers.Error_InvalidModifier (method.Location, "static");
}
}
IsImplementing = true;
}
- ec = new EmitContext (
- container, ds, Location, null, ReturnType, modifiers, false);
+ EmitContext ec = method.CreateEmitContext (container, null);
- //
- // Create the MethodBuilder for the method
- //
- if ((flags & MethodAttributes.PinvokeImpl) != 0) {
- if ((modifiers & Modifiers.STATIC) == 0) {
- Report.Error (601, Location,
- "The DllImport attribute must be specified on " +
- "a method marked 'static' and 'extern'.");
- return false;
- }
- builder = dllimport_attribute.DefinePInvokeMethod (
- ec, container.TypeBuilder, method_name, flags,
- ReturnType, ParameterTypes);
- } else if (builder == null)
- builder = container.TypeBuilder.DefineMethod (
- method_name, flags, CallingConventions,
- ReturnType, ParameterTypes);
- else
- builder.SetGenericMethodSignature (
- flags, CallingConventions,
- ReturnType, ParameterTypes);
+ DefineMethodBuilder (ec, container, method_name, ParameterTypes);
if (builder == null)
return false;
//
if (member is Indexer) {
container.Pending.ImplementIndexer (
- member.InterfaceType, builder, ReturnType,
+ member.InterfaceType, builder, method.ReturnType,
ParameterTypes, true);
} else
container.Pending.ImplementMethod (
- member.InterfaceType, name, ReturnType,
+ member.InterfaceType, name, method.ReturnType,
ParameterTypes, member.IsExplicitImpl);
if (member.IsExplicitImpl)
}
if (!TypeManager.RegisterMethod (builder, ParameterInfo, ParameterTypes)) {
- Report.Error (111, Location,
+ Report.Error (111, method.Location,
"Class `" + container.Name +
"' already contains a definition with the " +
"same return value and parameter types as the " +
return false;
}
- TypeManager.AddMethod (builder, this);
+ TypeManager.AddMethod (builder, method);
return true;
}
+ /// <summary>
+ /// Create the MethodBuilder for the method
+ /// </summary>
+ void DefineMethodBuilder (EmitContext ec, TypeContainer container, string method_name, Type[] ParameterTypes)
+ {
+ const int extern_static = Modifiers.EXTERN | Modifiers.STATIC;
+
+ if ((modifiers & extern_static) == extern_static) {
+
+ if (method.OptAttributes != null) {
+ Attribute dllimport_attribute = method.OptAttributes.Search (TypeManager.dllimport_type, ec);
+ if (dllimport_attribute != null) {
+ flags |= MethodAttributes.PinvokeImpl;
+ builder = dllimport_attribute.DefinePInvokeMethod (
+ ec, container.TypeBuilder, method_name, flags,
+ method.ReturnType, ParameterTypes);
+
+ return;
+ }
+ }
+
+ // for extern static method must be specified either DllImport attribute or MethodImplAttribute.
+ // We are more strict than Microsoft and report CS0626 like error
+ if (method.OptAttributes == null ||
+ !method.OptAttributes.Contains (TypeManager.methodimpl_attr_type, ec)) {
+ //"Method, operator, or accessor '{0}' is marked external and has no attributes on it. Consider adding a DllImport attribute to specify the external implementation"
+ Report.Error_T (626, method.Location, method.GetSignatureForError (container));
+ return;
+ }
+ }
+
+ if (builder == null)
+ builder = container.TypeBuilder.DefineMethod (
+ method_name, flags, method.CallingConventions,
+ method.ReturnType, ParameterTypes);
+ else
+ builder.SetGenericMethodSignature (
+ flags, method.CallingConventions,
+ method.ReturnType, ParameterTypes);
+ }
+
//
// Emits the code
//
- public virtual void Emit (TypeContainer container, Block block, object kind)
+ public void Emit (TypeContainer container, Attributable kind)
{
- ILGenerator ig;
EmitContext ec;
if ((flags & MethodAttributes.PinvokeImpl) == 0)
- ig = builder.GetILGenerator ();
+ ec = method.CreateEmitContext (container, builder.GetILGenerator ());
else
- ig = null;
+ ec = method.CreateEmitContext (container, null);
- ec = new EmitContext (container, ds, Location, ig, ReturnType, modifiers, false);
+ Location loc = method.Location;
+ Attributes OptAttributes = method.OptAttributes;
if (OptAttributes != null)
- Attribute.ApplyAttributes (ec, builder, kind, OptAttributes);
+ OptAttributes.Emit (ec, kind);
if (member is MethodCore)
- MethodCore.LabelParameters (ec, MethodBuilder,
- ((MethodCore) member).Parameters,
- OptAttributes,
- Location);
+ ((MethodCore) member).Parameters.LabelParameters (ec, MethodBuilder, loc);
SymbolWriter sw = CodeGen.SymbolWriter;
+ Block block = method.Block;
//
// abstract or extern methods have no bodies
if ((modifiers & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0){
if (block == null) {
if ((sw != null) && ((modifiers & Modifiers.EXTERN) != 0) &&
- !Location.IsNull (Location) &&
- (Location.SymbolDocument != null)) {
- sw.OpenMethod (container, MethodBuilder, Location, Location);
+ !Location.IsNull (loc) &&
+ (method.Location.SymbolDocument != null)) {
+ sw.OpenMethod (container, MethodBuilder, loc, loc);
sw.CloseMethod ();
}
//
if ((modifiers & Modifiers.ABSTRACT) != 0)
Report.Error (
- 500, Location, "Abstract method `" +
+ 500, method.Location, "Abstract method `" +
TypeManager.CSharpSignature (builder) +
"' can not have a body");
if ((modifiers & Modifiers.EXTERN) != 0)
Report.Error (
- 179, Location, "External method `" +
+ 179, method.Location, "External method `" +
TypeManager.CSharpSignature (builder) +
"' can not have a body");
//
if (block == null) {
Report.Error (
- 501, Location, "Method `" +
+ 501, method.Location, "Method `" +
TypeManager.CSharpSignature (builder) +
"' must declare a body since it is not marked " +
"abstract or extern");
//
// FIXME: This code generates buggy code
//
- if ((sw != null) && !Location.IsNull (Location) &&
+ if ((sw != null) && !Location.IsNull (loc) &&
!Location.IsNull (block.EndLocation) &&
- (Location.SymbolDocument != null)) {
- sw.OpenMethod (container, MethodBuilder, Location, block.EndLocation);
+ (loc.SymbolDocument != null)) {
+ sw.OpenMethod (container, MethodBuilder, loc, block.EndLocation);
if (member is Destructor)
EmitDestructor (ec, block);
else
- ec.EmitTopBlock (block, ParameterInfo, Location);
+ ec.EmitTopBlock (block, ParameterInfo, loc);
sw.CloseMethod ();
} else {
if (member is Destructor)
EmitDestructor (ec, block);
else
- ec.EmitTopBlock (block, ParameterInfo, Location);
+ ec.EmitTopBlock (block, ParameterInfo, loc);
}
}
ig.BeginExceptionBlock ();
ec.ReturnLabel = finish;
ec.HasReturnLabel = true;
- ec.EmitTopBlock (block, null, Location);
+ ec.EmitTopBlock (block, null, method.Location);
// ig.MarkLabel (finish);
ig.BeginFinallyBlock ();
if (ec.ContainerType.BaseType != null) {
Expression member_lookup = Expression.MemberLookup (
ec, ec.ContainerType.BaseType, null, ec.ContainerType.BaseType,
- "Finalize", 0, MemberTypes.Method, Expression.AllBindingFlags, Location);
+ "Finalize", MemberTypes.Method, Expression.AllBindingFlags, method.Location);
if (member_lookup != null){
MethodGroupExpr parent_destructor = ((MethodGroupExpr) member_lookup);
parameters, attrs, l)
{ }
+ public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
+ {
+ if (a.Type == TypeManager.conditional_attribute_type) {
+ // Conditional not valid on '{0}' because it is a destructor, operator, or explicit interface implementation
+ Report.Error_T (577, Location, GetSignatureForError ());
+ return;
+ }
+
+ base.ApplyAttributeBuilder (a, cb);
+ }
}
abstract public class MemberBase : MemberCore {
public Expression Type;
- protected MethodAttributes flags;
+ public MethodAttributes flags;
protected readonly int explicit_mod_flags;
- public readonly MemberName MemberName;
-
//
// The "short" name of this property / indexer / event. This is the
// name without the explicit interface.
//
protected MemberBase (Expression type, int mod, int allowed_mod, int def_mod,
MemberName name, Attributes attrs, Location loc)
- : base (name.GetMemberName (), attrs, loc)
+ : base (name, attrs, loc)
{
explicit_mod_flags = mod;
Type = type;
- MemberName = name;
ModFlags = Modifiers.Check (allowed_mod, mod, def_mod, loc);
}
bool error = false;
foreach (Type partype in parameters){
+ if (partype == TypeManager.void_type) {
+ Report.Error (
+ 1547, Location, "Keyword 'void' cannot " +
+ "be used in this context");
+ return false;
+ }
+
if (partype.IsPointer){
if (!UnsafeOK (ds))
error = true;
return !error;
}
- protected virtual bool DoDefine (DeclSpace decl, TypeContainer container)
+ protected virtual bool DoDefineBase (TypeContainer container)
{
if (Name == null)
Name = "this";
if (IsInterface) {
ModFlags = Modifiers.PUBLIC |
Modifiers.ABSTRACT |
- Modifiers.VIRTUAL;
+ Modifiers.VIRTUAL | (ModFlags & Modifiers.UNSAFE);
flags = MethodAttributes.Public |
MethodAttributes.Abstract |
MethodAttributes.NewSlot |
MethodAttributes.Virtual;
} else {
- if (!container.MethodModifiersValid (ModFlags, Name, Location))
- return false;
+ if (!container.MethodModifiersValid (ModFlags, Name, Location))
+ return false;
- flags = Modifiers.MethodAttr (ModFlags);
+ flags = Modifiers.MethodAttr (ModFlags);
}
+ return true;
+ }
+
+ protected virtual bool DoDefine (DeclSpace decl, TypeContainer container)
+ {
// Lookup Type, verify validity
MemberType = decl.ResolveType (Type, false, Location);
if (MemberType == null)
if (MemberType.IsPointer && !UnsafeOK (container))
return false;
-
+
//
// Check for explicit interface implementation
//
- if (MemberName.TypeName != null) {
- ExplicitInterfaceName = MemberName.TypeName.GetTypeExpression (Location);
+ if (MemberName.Left != null) {
+ ExplicitInterfaceName = MemberName.Left.GetTypeExpression (Location);
ShortName = MemberName.Name;
} else
ShortName = Name;
return true;
}
+
+ /// <summary>
+ /// Use this method when MethodBuilder is null
+ /// </summary>
+ public virtual string GetSignatureForError (TypeContainer tc)
+ {
+ return String.Concat (tc.Name, '.', Name);
+ }
+
+ protected override bool IsIdentifierClsCompliant (DeclSpace ds)
+ {
+ return IsIdentifierAndParamClsCompliant (ds, Name, null, null);
+ }
+
+ protected override bool VerifyClsCompliance(DeclSpace ds)
+ {
+ if (base.VerifyClsCompliance (ds)) {
+ return true;
+ }
+
+ if (IsInterface && HasClsCompliantAttribute && ds.IsClsCompliaceRequired (ds)) {
+ Report.Error_T (3010, Location, GetSignatureForError ());
+ }
+ return false;
+ }
+
+ protected override void VerifyObsoleteAttribute()
+ {
+ CheckUsageOfObsoleteAttribute (MemberType);
+ }
}
//
[Flags]
public enum Status : byte { ASSIGNED = 1, USED = 2 }
+ static string[] attribute_targets = new string [] { "field" };
+
//
// The constructor is only exposed to our children
//
this.init = init;
}
+ public override AttributeTargets AttributeTargets {
+ get {
+ return AttributeTargets.Field;
+ }
+ }
+
+ public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+ {
+ if (a.Type == TypeManager.marshal_as_attr_type) {
+ UnmanagedMarshal marshal = a.GetMarshal ();
+ if (marshal != null) {
+ FieldBuilder.SetMarshal (marshal);
+ return;
+ }
+ Report.Warning_T (-24, a.Location);
+ return;
+ }
+
+
+ FieldBuilder.SetCustomAttribute (cb);
+ }
+
//
// Whether this field has an initializer.
//
return init_expr;
}
+ protected override bool DoDefine (DeclSpace ds, TypeContainer container)
+ {
+ if (!base.DoDefine (ds, container))
+ return false;
+
+ if (MemberType == TypeManager.void_type) {
+ Report.Error (1547, Location,
+ "Keyword 'void' cannot be used in this context");
+ return false;
+ }
+
+ if (MemberType == TypeManager.arg_iterator_type || MemberType == TypeManager.typed_reference_type) {
+ // "Field or property cannot be of type '{0}'";
+ Report.Error_T (610, Location, TypeManager.CSharpName (MemberType));
+ return false;
+ }
+
+ return true;
+ }
+
+ public override string GetSignatureForError ()
+ {
+ return TypeManager.GetFullNameSignature (FieldBuilder);
+ }
+
+ protected override string[] ValidAttributeTargets {
+ get {
+ return attribute_targets;
+ }
+ }
+
+ protected override bool VerifyClsCompliance (DeclSpace ds)
+ {
+ if (!base.VerifyClsCompliance (ds))
+ return false;
+
+ if (FieldBuilder == null) {
+ return true;
+ }
+
+ if (!AttributeTester.IsClsCompliant (FieldBuilder.FieldType)) {
+ Report.Error_T (3003, Location, GetSignatureForError ());
+ }
+ return true;
+ }
+
+
public void SetAssigned ()
{
status |= Status.ASSIGNED;
public override bool Define (TypeContainer container)
{
- Type t = container.ResolveType (Type, false, Location);
+ MemberType = container.ResolveType (Type, false, Location);
- if (t == null)
+ if (MemberType == null)
return false;
CheckBase (container);
- if (!container.AsAccessible (t, ModFlags)) {
+ if (!container.AsAccessible (MemberType, ModFlags)) {
Report.Error (52, Location,
"Inconsistent accessibility: field type `" +
- TypeManager.CSharpName (t) + "' is less " +
+ TypeManager.CSharpName (MemberType) + "' is less " +
"accessible than field `" + Name + "'");
return false;
}
- if (t.IsPointer && !UnsafeOK (container))
+ if (MemberType.IsPointer && !UnsafeOK (container))
return false;
if (RootContext.WarningLevel > 1){
}
if ((ModFlags & Modifiers.VOLATILE) != 0){
- if (!t.IsClass){
- Type vt = t;
+ if (!MemberType.IsClass){
+ Type vt = MemberType;
if (TypeManager.IsEnumType (vt))
- vt = TypeManager.EnumToUnderlying (t);
+ vt = TypeManager.EnumToUnderlying (MemberType);
if (!((vt == TypeManager.bool_type) ||
(vt == TypeManager.sbyte_type) ||
(vt == TypeManager.byte_type) ||
- (vt == TypeManager.short_type) ||
+ (vt == TypeManager.short_type) ||
(vt == TypeManager.ushort_type) ||
- (vt == TypeManager.int32_type) ||
+ (vt == TypeManager.int32_type) ||
(vt == TypeManager.uint32_type) ||
- (vt == TypeManager.char_type) ||
- (vt == TypeManager.float_type))){
+ (vt == TypeManager.char_type) ||
+ (vt == TypeManager.float_type) ||
+ (!vt.IsValueType))){
Report.Error (
677, Location, container.MakeName (Name) +
" A volatile field can not be of type `" +
if (container is Struct &&
((fa & FieldAttributes.Static) == 0) &&
- t == container.TypeBuilder &&
- !TypeManager.IsBuiltinType (t)){
+ MemberType == container.TypeBuilder &&
+ !TypeManager.IsBuiltinType (MemberType)){
Report.Error (523, Location, "Struct member `" + container.Name + "." + Name +
"' causes a cycle in the structure layout");
return false;
try {
FieldBuilder = container.TypeBuilder.DefineField (
- Name, t, Modifiers.FieldAttr (ModFlags));
+ Name, MemberType, Modifiers.FieldAttr (ModFlags));
TypeManager.RegisterFieldBase (FieldBuilder, this);
}
return true;
}
- public void Emit (TypeContainer tc)
+ public override void Emit (TypeContainer tc)
{
- EmitContext ec = new EmitContext (tc, Location, null,
- FieldBuilder.FieldType, ModFlags);
+ if (OptAttributes != null) {
+ EmitContext ec = new EmitContext (tc, Location, null, FieldBuilder.FieldType, ModFlags);
+ OptAttributes.Emit (ec, this);
+ }
- Attribute.ApplyAttributes (ec, FieldBuilder, this, OptAttributes);
+ base.Emit (tc);
}
}
// Null if the accessor is empty, or a Block if not
//
public Block Block;
- public Attributes OptAttributes;
+ public Attributes Attributes;
public Accessor (Block b, Attributes attrs)
{
Block = b;
- OptAttributes = attrs;
+ Attributes = attrs;
}
}
- //
- // Properties and Indexers both generate PropertyBuilders, we use this to share
- // their common bits.
- //
- abstract public class PropertyBase : MethodCore {
- public Accessor Get, Set;
- public PropertyBuilder PropertyBuilder;
- public MethodBuilder GetBuilder, SetBuilder;
- public MethodData GetData, SetData;
- protected EmitContext ec;
+ // Ooouh Martin, templates are missing here.
+ // When it will be possible move here a lot of child code and template method type.
+ public abstract class AbstractPropertyEventMethod: Attributable, IMethodData
+ {
+ protected MethodData method_data;
+ protected Block block;
- public PropertyBase (DeclSpace ds, Expression type, int mod_flags,
- int allowed_mod, bool is_iface, MemberName name,
- Parameters parameters, Attributes attrs,
- Accessor get_block, Accessor set_block,
- Location loc)
- : base (ds, type, mod_flags, allowed_mod, is_iface, name,
- attrs, parameters, loc)
+ ReturnParameter return_attributes;
+
+ public AbstractPropertyEventMethod ():
+ base (null)
{
- Get = get_block;
- Set = set_block;
}
- protected override bool DoDefine (DeclSpace decl, TypeContainer container)
+ public AbstractPropertyEventMethod (Accessor accessor):
+ base (accessor.Attributes)
{
- if (!base.DoDefine (decl, container))
- return false;
+ this.block = accessor.Block;
+ }
- ec = new EmitContext (container, Location, null, MemberType, ModFlags);
+ #region IMethodData Members
- return true;
+ public Block Block {
+ get {
+ return block;
+ }
}
- //
- // Checks our base implementation if any
- //
- protected override bool CheckBase (TypeContainer container)
+ public CallingConventions CallingConventions {
+ get {
+ return CallingConventions.Standard;
+ }
+ }
+
+ public bool IsExcluded (EmitContext ec)
{
- base.CheckBase (container);
-
- // Check whether arguments were correct.
- if (!DoDefineParameters ())
- return false;
+ return false;
+ }
- if (IsExplicitImpl)
- return true;
+ GenericMethod IMethodData.GenericMethod {
+ get {
+ return null;
+ }
+ }
- //
- // Check in our class for dups
- //
- ArrayList ar = container.Properties;
- if (ar != null) {
- int arLen = ar.Count;
+ public abstract ObsoleteAttribute GetObsoleteAttribute ();
+ public abstract string GetSignatureForError (TypeContainer tc);
+ public abstract Location Location { get; }
+ public abstract string MethodName { get; }
+ public abstract Type[] ParameterTypes { get; }
+ public abstract Type ReturnType { get; }
+ public abstract EmitContext CreateEmitContext(TypeContainer tc, ILGenerator ig);
+
+ #endregion
+
+ public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
+ {
+ if (a.Type == TypeManager.cls_compliant_attribute_type || a.Type == TypeManager.obsolete_attribute_type ||
+ a.Type == TypeManager.conditional_attribute_type) {
+ //"'{0}' is not valid on property or event accessors. It is valid on '{1}' declarations only"
+ Report.Error_T (1667, a.Location, TypeManager.CSharpName (a.Type), a.GetValidTargets ());
+ return;
+ }
+
+ if (a.Target == "method") {
+ method_data.MethodBuilder.SetCustomAttribute (cb);
+ return;
+ }
+
+ if (a.Target == "return") {
+ if (return_attributes == null)
+ return_attributes = new ReturnParameter (method_data.MethodBuilder, Location);
+
+ return_attributes.ApplyAttributeBuilder (a, cb);
+ return;
+ }
+
+ ApplyToExtraTarget (a, cb);
+ }
+
+ virtual protected void ApplyToExtraTarget (Attribute a, CustomAttributeBuilder cb)
+ {
+ System.Diagnostics.Debug.Fail ("You forgot to define special attribute target handling");
+ }
+
+ public virtual void Emit (TypeContainer container)
+ {
+ method_data.Emit (container, this);
+ block = null;
+ }
+ }
+
+ //
+ // Properties and Indexers both generate PropertyBuilders, we use this to share
+ // their common bits.
+ //
+ abstract public class PropertyBase : MethodCore {
+
+ public class GetMethod: PropertyMethod
+ {
+ static string[] attribute_targets = new string [] { "method", "return" };
+
+ public GetMethod (MethodCore method, Accessor accessor):
+ base (method, accessor)
+ {
+ }
+
+ public override MethodBuilder Define(TypeContainer container)
+ {
+ method_data = new MethodData (method, method.ParameterInfo, method.ModFlags, method.flags, this);
+
+ if (!method_data.Define (container))
+ return null;
+
+ return method_data.MethodBuilder;
+ }
+
+ public override string GetSignatureForError (TypeContainer tc)
+ {
+ return String.Concat (base.GetSignatureForError (tc), ".get");
+ }
+
+ public override string MethodName
+ {
+ get {
+ return "get_" + method.ShortName;
+ }
+ }
+
+ public override Type ReturnType {
+ get {
+ return method.MemberType;
+ }
+ }
+
+ protected override string[] ValidAttributeTargets {
+ get {
+ return attribute_targets;
+ }
+ }
+ }
+
+ public class SetMethod: PropertyMethod {
+
+ static string[] attribute_targets = new string [] { "method", "param", "return" };
+ ImplicitParameter param_attr;
+
+ public SetMethod (MethodCore method, Accessor accessor):
+ base (method, accessor)
+ {
+ }
+
+ protected override void ApplyToExtraTarget(Attribute a, CustomAttributeBuilder cb)
+ {
+ if (a.Target == "param") {
+ if (param_attr == null)
+ param_attr = new ImplicitParameter (method_data.MethodBuilder);
+
+ param_attr.ApplyAttributeBuilder (a, cb);
+ return;
+ }
+
+ base.ApplyAttributeBuilder (a, cb);
+ }
+
+ protected virtual InternalParameters GetParameterInfo (TypeContainer container)
+ {
+ Parameter [] parms = new Parameter [1];
+ parms [0] = new Parameter (method.Type, "value", Parameter.Modifier.NONE, null);
+ return new InternalParameters (
+ container, new Parameters (parms, null, method.Location));
+ }
+
+ public override MethodBuilder Define(TypeContainer container)
+ {
+ method_data = new MethodData (method, GetParameterInfo (container), method.ModFlags, method.flags, this);
+
+ if (!method_data.Define (container))
+ return null;
+
+ return method_data.MethodBuilder;
+ }
+
+ public override string GetSignatureForError (TypeContainer tc)
+ {
+ return String.Concat (base.GetSignatureForError (tc), ".set");
+ }
+
+ public override string MethodName {
+ get {
+ return "set_" + method.ShortName;
+ }
+ }
+
+ public override Type[] ParameterTypes {
+ get {
+ return new Type[] { method.MemberType };
+ }
+ }
+
+ public override Type ReturnType {
+ get {
+ return TypeManager.void_type;
+ }
+ }
+
+ protected override string[] ValidAttributeTargets {
+ get {
+ return attribute_targets;
+ }
+ }
+ }
+
+ static string[] attribute_targets = new string [] { "property" };
+
+ public abstract class PropertyMethod: AbstractPropertyEventMethod {
+ protected readonly MethodCore method;
+
+ public PropertyMethod (MethodCore method, Accessor accessor):
+ base (accessor)
+ {
+ this.method = method;
+ }
+
+ public override AttributeTargets AttributeTargets {
+ get {
+ return AttributeTargets.Method | AttributeTargets.ReturnValue;
+ }
+ }
+
+ public override bool IsClsCompliaceRequired(DeclSpace ds)
+ {
+ return method.IsClsCompliaceRequired (ds);
+ }
+
+ public InternalParameters ParameterInfo
+ {
+ get {
+ return method_data.ParameterInfo;
+ }
+ }
+
+ public abstract MethodBuilder Define (TypeContainer container);
+
+ public override Type[] ParameterTypes {
+ get {
+ return TypeManager.NoTypes;
+ }
+ }
+
+ public override Location Location {
+ get {
+ return method.Location;
+ }
+ }
+
+ public override EmitContext CreateEmitContext (TypeContainer tc, ILGenerator ig)
+ {
+ return new EmitContext (tc, method.ds, method.Location, ig, ReturnType, method.ModFlags, false);
+ }
+
+ public override ObsoleteAttribute GetObsoleteAttribute ()
+ {
+ return method.GetObsoleteAttribute (method.ds);
+ }
+
+ public override string GetSignatureForError (TypeContainer tc)
+ {
+ return String.Concat (tc.Name, '.', method.Name);
+ }
+ }
+
+ public PropertyMethod Get, Set;
+ public PropertyBuilder PropertyBuilder;
+ public MethodBuilder GetBuilder, SetBuilder;
+
+ protected EmitContext ec;
+
+ public PropertyBase (DeclSpace ds, Expression type, int mod_flags,
+ int allowed_mod, bool is_iface, MemberName name,
+ Parameters parameters, Attributes attrs,
+ Location loc)
+ : base (ds, type, mod_flags, allowed_mod, is_iface, name,
+ attrs, parameters, loc)
+ {
+ }
+
+ public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+ {
+ PropertyBuilder.SetCustomAttribute (cb);
+ }
+
+ public override AttributeTargets AttributeTargets {
+ get {
+ return AttributeTargets.Property;
+ }
+ }
+
+ protected override bool DoDefine (DeclSpace decl, TypeContainer container)
+ {
+ if (!base.DoDefine (decl, container))
+ return false;
+
+ if (MemberType == TypeManager.arg_iterator_type || MemberType == TypeManager.typed_reference_type) {
+ // "Field or property cannot be of type '{0}'";
+ Report.Error_T (610, Location, TypeManager.CSharpName (MemberType));
+ return false;
+ }
+
+ ec = new EmitContext (container, Location, null, MemberType, ModFlags);
+
+ return true;
+ }
+
+ public override string GetSignatureForError()
+ {
+ return TypeManager.CSharpSignature (PropertyBuilder, false);
+ }
+
+ protected virtual string RealMethodName {
+ get {
+ return Name;
+ }
+ }
+
+ protected override bool IsIdentifierClsCompliant (DeclSpace ds)
+ {
+ if (!IsIdentifierAndParamClsCompliant (ds, RealMethodName, null, null))
+ return false;
+
+ if (Get != null && !IsIdentifierAndParamClsCompliant (ds, "get_" + RealMethodName, null, null))
+ return false;
+
+ if (Set != null && !IsIdentifierAndParamClsCompliant (ds, "set_" + RealMethodName, null, null))
+ return false;
+
+ return true;
+ }
+
+
+ //
+ // Checks our base implementation if any
+ //
+ protected override bool CheckBase (TypeContainer container)
+ {
+ base.CheckBase (container);
+
+ // Check whether arguments were correct.
+ if (!DoDefineParameters ())
+ return false;
+
+ if (IsExplicitImpl)
+ return true;
+
+ //
+ // Check in our class for dups
+ //
+ ArrayList ar = container.Properties;
+ if (ar != null) {
+ int arLen = ar.Count;
for (int i = 0; i < arLen; i++) {
Property m = (Property) ar [i];
return true;
}
- PropertyInfo parent_property = null;
+ MemberInfo parent_member = null;
//
// Explicit implementations do not have `parent' methods, however,
// an incorrect warning in corlib.
//
if (! IsExplicitImpl) {
- parent_property = (PropertyInfo) ((IMemberContainer)container).Parent.MemberCache.FindMemberToOverride (
+ parent_member = ((IMemberContainer)container).Parent.MemberCache.FindMemberToOverride (
container.TypeBuilder, Name, ParameterTypes, true);
}
- if (parent_property != null) {
+ if (parent_member is PropertyInfo) {
+ PropertyInfo parent_property = (PropertyInfo)parent_member;
+
string name = parent_property.DeclaringType.Name + "." +
parent_property.Name;
return false;
}
}
- } else {
+ } else if (parent_member == null){
if ((ModFlags & Modifiers.NEW) != 0)
WarningNotHiding (container);
return true;
}
- public void Emit (TypeContainer tc)
+ public override void Emit (TypeContainer tc)
{
//
// The PropertyBuilder can be null for explicit implementations, in that
// case, we do not actually emit the ".property", so there is nowhere to
// put the attribute
//
- if (PropertyBuilder != null)
- Attribute.ApplyAttributes (ec, PropertyBuilder, this, OptAttributes);
+ if (PropertyBuilder != null && OptAttributes != null)
+ OptAttributes.Emit (ec, this);
- if (GetData != null) {
- GetData.Emit (tc, Get.Block, Get);
- Get.Block = null;
- }
+ if (Get != null)
+ Get.Emit (tc);
+
+ if (Set != null)
+ Set.Emit (tc);
+
+ base.Emit (tc);
+ }
- if (SetData != null) {
- SetData.Emit (tc, Set.Block, Set);
- Set.Block = null;
+ protected override string[] ValidAttributeTargets {
+ get {
+ return attribute_targets;
}
}
}
: base (ds, type, mod_flags,
is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
is_iface, name, Parameters.EmptyReadOnlyParameters, attrs,
- get_block, set_block, loc)
+ loc)
{
+ if (get_block != null)
+ Get = new GetMethod (this, get_block);
+
+ if (set_block != null)
+ Set = new SetMethod (this, set_block);
}
public override bool Define (TypeContainer container)
{
+ if (!DoDefineBase (container))
+ return false;
+
if (!DoDefine (container, container))
return false;
flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
if (Get != null) {
- Type [] parameters = TypeManager.NoTypes;
- InternalParameters ip = new InternalParameters (
- container, Parameters.EmptyReadOnlyParameters);
-
- GetData = new MethodData (container, this, "get", MemberType,
- parameters, ip, CallingConventions.Standard,
- Get.OptAttributes, ModFlags, flags, false);
+ GetBuilder = Get.Define (container);
+ if (GetBuilder == null)
+ return false;
//
// Setup iterator if we are one
if ((ModFlags & Modifiers.METHOD_YIELDS) != 0){
IteratorHandler ih = new IteratorHandler (
"get", container, MemberType,
- parameters, ip, ModFlags, Location);
+ TypeManager.NoTypes, Get.ParameterInfo, ModFlags, Location);
Block new_block = ih.Setup (block);
if (new_block == null)
return false;
block = new_block;
}
-
- if (!GetData.Define (container))
- return false;
-
- GetBuilder = GetData.MethodBuilder;
}
if (Set != null) {
- Type [] parameters = new Type [1];
- parameters [0] = MemberType;
-
- Parameter [] parms = new Parameter [1];
- parms [0] = new Parameter (Type, "value", Parameter.Modifier.NONE, null);
- InternalParameters ip = new InternalParameters (
- container, new Parameters (parms, null, Location));
-
- SetData = new MethodData (container, this, "set", TypeManager.void_type,
- parameters, ip, CallingConventions.Standard,
- Set.OptAttributes, ModFlags, flags, false);
-
- if (!SetData.Define (container))
+ SetBuilder = Set.Define (container);
+ if (SetBuilder == null)
return false;
- SetBuilder = SetData.MethodBuilder;
SetBuilder.DefineParameter (1, ParameterAttributes.None, "value");
}
}
}
- public class Event : FieldBase {
+ /// <summary>
+ /// For case when event is declared like property (with add and remove accessors).
+ /// </summary>
+ public class EventProperty: Event {
+
+ static string[] attribute_targets = new string [] { "event", "property" };
+
+ public EventProperty (DeclSpace ds, Expression type, int mod_flags,
+ bool is_iface, MemberName name, Object init,
+ Attributes attrs, Accessor add, Accessor remove,
+ Location loc)
+ : base (ds, type, mod_flags, is_iface, name, init, attrs, loc)
+ {
+ Add = new AddDelegateMethod (this, add);
+ Remove = new RemoveDelegateMethod (this, remove);
+ }
+
+ protected override string[] ValidAttributeTargets {
+ get {
+ return attribute_targets;
+ }
+ }
+ }
+
+ /// <summary>
+ /// Event is declared like field.
+ /// </summary>
+ public class EventField: Event {
+
+ static string[] attribute_targets = new string [] { "event", "field", "method" };
+
+ public EventField (DeclSpace ds, Expression type, int mod_flags, bool is_iface,
+ MemberName name, Object init, Attributes attrs, Location loc)
+ : base (ds, type, mod_flags, is_iface, name, init, attrs, loc)
+ {
+ Add = new AddDelegateMethod (this);
+ Remove = new RemoveDelegateMethod (this);
+ }
+
+ public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+ {
+ if (a.Target == "field") {
+ FieldBuilder.SetCustomAttribute (cb);
+ return;
+ }
+
+ if (a.Target == "method") {
+ AddBuilder.SetCustomAttribute (cb);
+ RemoveBuilder.SetCustomAttribute (cb);
+ return;
+ }
+
+ base.ApplyAttributeBuilder (a, cb);
+ }
+
+ protected override string[] ValidAttributeTargets {
+ get {
+ return attribute_targets;
+ }
+ }
+ }
+
+ public abstract class Event : FieldBase {
+
+ protected sealed class AddDelegateMethod: DelegateMethod
+ {
+ public AddDelegateMethod (Event method):
+ base (method)
+ {
+ }
+
+ public AddDelegateMethod (Event method, Accessor accessor):
+ base (method, accessor)
+ {
+ }
+
+ public override string MethodName {
+ get {
+ return "add_" + method.ShortName;
+ }
+ }
+
+ protected override MethodInfo DelegateMethodInfo {
+ get {
+ return TypeManager.delegate_combine_delegate_delegate;
+ }
+ }
+
+ }
+
+ protected sealed class RemoveDelegateMethod: DelegateMethod
+ {
+ public RemoveDelegateMethod (Event method):
+ base (method)
+ {
+ }
+
+ public RemoveDelegateMethod (Event method, Accessor accessor):
+ base (method, accessor)
+ {
+ }
+
+ public override string MethodName {
+ get {
+ return "remove_" + method.ShortName;
+ }
+ }
+
+ protected override MethodInfo DelegateMethodInfo {
+ get {
+ return TypeManager.delegate_remove_delegate_delegate;
+ }
+ }
+
+ }
+
+ public abstract class DelegateMethod: AbstractPropertyEventMethod
+ {
+ protected readonly Event method;
+ ImplicitParameter param_attr;
+
+ static string[] attribute_targets = new string [] { "method", "param", "return" };
+
+ public DelegateMethod (Event method)
+ {
+ this.method = method;
+ }
+
+ public DelegateMethod (Event method, Accessor accessor):
+ base (accessor)
+ {
+ this.method = method;
+ }
+
+ protected override void ApplyToExtraTarget(Attribute a, CustomAttributeBuilder cb)
+ {
+ if (a.Target == "param") {
+ if (param_attr == null)
+ param_attr = new ImplicitParameter (method_data.MethodBuilder);
+
+ param_attr.ApplyAttributeBuilder (a, cb);
+ return;
+ }
+
+ base.ApplyAttributeBuilder (a, cb);
+ }
+
+ public override AttributeTargets AttributeTargets {
+ get {
+ return AttributeTargets.Method;
+ }
+ }
+
+ public override bool IsClsCompliaceRequired(DeclSpace ds)
+ {
+ return method.IsClsCompliaceRequired (ds);
+ }
+
+ public MethodBuilder Define (TypeContainer container, InternalParameters ip)
+ {
+ method_data = new MethodData (method, ip, method.ModFlags,
+ method.flags | MethodAttributes.HideBySig | MethodAttributes.SpecialName, this);
+
+ if (!method_data.Define (container))
+ return null;
+
+ MethodBuilder mb = method_data.MethodBuilder;
+ mb.DefineParameter (1, ParameterAttributes.None, "value");
+ return mb;
+ }
+
+
+ public override void Emit (TypeContainer tc)
+ {
+ if (block != null) {
+ base.Emit (tc);
+ return;
+ }
+
+ ILGenerator ig = method_data.MethodBuilder.GetILGenerator ();
+ EmitContext ec = CreateEmitContext (tc, ig);
+ FieldInfo field_info = (FieldInfo)method.FieldBuilder;
+
+ if ((method.ModFlags & Modifiers.STATIC) != 0) {
+ ig.Emit (OpCodes.Ldsfld, field_info);
+ ig.Emit (OpCodes.Ldarg_0);
+ ig.Emit (OpCodes.Call, DelegateMethodInfo);
+ ig.Emit (OpCodes.Castclass, method.MemberType);
+ ig.Emit (OpCodes.Stsfld, field_info);
+ } else {
+ ig.Emit (OpCodes.Ldarg_0);
+ ig.Emit (OpCodes.Ldarg_0);
+ ig.Emit (OpCodes.Ldfld, field_info);
+ ig.Emit (OpCodes.Ldarg_1);
+ ig.Emit (OpCodes.Call, DelegateMethodInfo);
+ ig.Emit (OpCodes.Castclass, method.MemberType);
+ ig.Emit (OpCodes.Stfld, field_info);
+ }
+ ig.Emit (OpCodes.Ret);
+ }
+
+ protected abstract MethodInfo DelegateMethodInfo { get; }
+
+ public override Type[] ParameterTypes {
+ get {
+ return new Type[] { method.MemberType };
+ }
+ }
+
+ public override Type ReturnType {
+ get {
+ return TypeManager.void_type;
+ }
+ }
+
+ public override Location Location {
+ get {
+ return method.Location;
+ }
+ }
+
+ public override EmitContext CreateEmitContext (TypeContainer tc, ILGenerator ig)
+ {
+ return new EmitContext (tc, method.ds, Location, ig, ReturnType, method.ModFlags, false);
+ }
+
+ public override string GetSignatureForError (TypeContainer tc)
+ {
+ return String.Concat (tc.Name, '.', method.Name);
+ }
+
+ public override ObsoleteAttribute GetObsoleteAttribute ()
+ {
+ return method.GetObsoleteAttribute (method.ds);
+ }
+
+ protected override string[] ValidAttributeTargets {
+ get {
+ return attribute_targets;
+ }
+ }
+ }
+
+
const int AllowedModifiers =
Modifiers.NEW |
Modifiers.PUBLIC |
const int AllowedInterfaceModifiers =
Modifiers.NEW;
- public readonly Accessor Add;
- public readonly Accessor Remove;
+ protected DelegateMethod Add, Remove;
public MyEventBuilder EventBuilder;
-
public MethodBuilder AddBuilder, RemoveBuilder;
+ public DeclSpace ds;
+
MethodData AddData, RemoveData;
- public Event (Expression type, int mod_flags, bool is_iface, MemberName name,
- Object init, Attributes attrs, Accessor add, Accessor remove,
- Location loc)
+ public Event (DeclSpace ds, Expression type, int mod_flags, bool is_iface,
+ MemberName name, Object init, Attributes attrs, Location loc)
: base (type, mod_flags,
is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
name, init, attrs, loc)
{
- Add = add;
- Remove = remove;
IsInterface = is_iface;
+ this.ds = ds;
+ }
+
+ public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+ {
+ EventBuilder.SetCustomAttribute (cb);
}
+ public override AttributeTargets AttributeTargets {
+ get {
+ return AttributeTargets.Event;
+ }
+ }
+
public override bool Define (TypeContainer container)
{
- EventAttributes e_attr = EventAttributes.RTSpecialName | EventAttributes.SpecialName;
- MethodAttributes m_attr = MethodAttributes.HideBySig | MethodAttributes.SpecialName
+ EventAttributes e_attr;
+ if (IsInterface)
+ e_attr = EventAttributes.None;
+ else
+ e_attr = EventAttributes.RTSpecialName |
+ EventAttributes.SpecialName;
;
+ if (!DoDefineBase (container))
+ return false;
+
if (!DoDefine (container, container))
return false;
return false;
}
- Type [] parameter_types = new Type [1];
- parameter_types [0] = MemberType;
-
Parameter [] parms = new Parameter [1];
parms [0] = new Parameter (Type, "value", Parameter.Modifier.NONE, null);
InternalParameters ip = new InternalParameters (
//
// Now define the accessors
//
- AddData = new MethodData (container, this, "add", TypeManager.void_type,
- parameter_types, ip, CallingConventions.Standard,
- (Add != null) ? Add.OptAttributes : null,
- ModFlags, flags | m_attr, false);
- if (!AddData.Define (container))
+ AddBuilder = Add.Define (container, ip);
+ if (AddBuilder == null)
return false;
- AddBuilder = AddData.MethodBuilder;
- AddBuilder.DefineParameter (1, ParameterAttributes.None, "value");
-
- RemoveData = new MethodData (container, this, "remove", TypeManager.void_type,
- parameter_types, ip, CallingConventions.Standard,
- (Remove != null) ? Remove.OptAttributes : null,
- ModFlags, flags | m_attr, false);
-
- if (!RemoveData.Define (container))
+ RemoveBuilder = Remove.Define (container, ip);
+ if (RemoveBuilder == null)
return false;
- RemoveBuilder = RemoveData.MethodBuilder;
- RemoveBuilder.DefineParameter (1, ParameterAttributes.None, "value");
-
if (!IsExplicitImpl){
EventBuilder = new MyEventBuilder (this,
container.TypeBuilder, Name, e_attr, MemberType);
- if (Add == null && Remove == null) {
+ if (Add.Block == null && Remove.Block == null &&
+ !IsInterface) {
FieldBuilder = container.TypeBuilder.DefineField (
Name, MemberType,
FieldAttributes.Private | ((ModFlags & Modifiers.STATIC) != 0 ? FieldAttributes.Static : 0));
return true;
}
- void EmitDefaultMethod (EmitContext ec, bool is_add)
+ public override void Emit (TypeContainer tc)
{
- ILGenerator ig = ec.ig;
- MethodInfo method = null;
-
- if (is_add)
- method = TypeManager.delegate_combine_delegate_delegate;
- else
- method = TypeManager.delegate_remove_delegate_delegate;
-
- if ((ModFlags & Modifiers.STATIC) != 0) {
- ig.Emit (OpCodes.Ldsfld, (FieldInfo) FieldBuilder);
- ig.Emit (OpCodes.Ldarg_0);
- ig.Emit (OpCodes.Call, method);
- ig.Emit (OpCodes.Castclass, MemberType);
- ig.Emit (OpCodes.Stsfld, (FieldInfo) FieldBuilder);
- } else {
- ig.Emit (OpCodes.Ldarg_0);
- ig.Emit (OpCodes.Ldarg_0);
- ig.Emit (OpCodes.Ldfld, (FieldInfo) FieldBuilder);
- ig.Emit (OpCodes.Ldarg_1);
- ig.Emit (OpCodes.Call, method);
- ig.Emit (OpCodes.Castclass, MemberType);
- ig.Emit (OpCodes.Stfld, (FieldInfo) FieldBuilder);
+ if (OptAttributes != null) {
+ EmitContext ec = new EmitContext (tc, Location, null, MemberType, ModFlags);
+ OptAttributes.Emit (ec, this);
}
- ig.Emit (OpCodes.Ret);
- }
-
- public void Emit (TypeContainer tc)
- {
- EmitContext ec;
- ec = new EmitContext (tc, Location, null, MemberType, ModFlags);
- Attribute.ApplyAttributes (ec, EventBuilder, this, OptAttributes);
-
- if (Add != null) {
- AddData.Emit (tc, Add.Block, Add);
- Add.Block = null;
- } else {
- ILGenerator ig = AddData.MethodBuilder.GetILGenerator ();
- ec = new EmitContext (tc, Location, ig, TypeManager.void_type, ModFlags);
- EmitDefaultMethod (ec, true);
+ if (!IsInterface) {
+ Add.Emit (tc);
+ Remove.Emit (tc);
}
- if (Remove != null) {
- RemoveData.Emit (tc, Remove.Block, Remove);
- Remove.Block = null;
- } else {
- ILGenerator ig = RemoveData.MethodBuilder.GetILGenerator ();
- ec = new EmitContext (tc, Location, ig, TypeManager.void_type, ModFlags);
- EmitDefaultMethod (ec, false);
- }
+ base.Emit (tc);
+ }
+
+ public override string GetSignatureForError ()
+ {
+ return TypeManager.GetFullNameSignature (EventBuilder);
}
-
}
//
public class Indexer : PropertyBase {
+ class GetIndexerMethod: GetMethod
+ {
+ public GetIndexerMethod (MethodCore method, Accessor accessor):
+ base (method, accessor)
+ {
+ }
+
+ public override Type[] ParameterTypes {
+ get {
+ return method.ParameterTypes;
+ }
+ }
+ }
+
+ class SetIndexerMethod: SetMethod
+ {
+ readonly Parameters parameters;
+
+ public SetIndexerMethod (MethodCore method, Parameters parameters, Accessor accessor):
+ base (method, accessor)
+ {
+ this.parameters = parameters;
+ }
+
+ public override Type[] ParameterTypes {
+ get {
+ int top = method.ParameterTypes.Length;
+ Type [] set_pars = new Type [top + 1];
+ method.ParameterTypes.CopyTo (set_pars, 0);
+ set_pars [top] = method.MemberType;
+ return set_pars;
+ }
+ }
+
+ protected override InternalParameters GetParameterInfo (TypeContainer container)
+ {
+ Parameter [] fixed_parms = parameters.FixedParameters;
+
+ if (fixed_parms == null){
+ throw new Exception ("We currently do not support only array arguments in an indexer at: " + method.Location);
+ // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
+ // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
+ //
+ // Here is the problem: the `value' parameter has
+ // to come *after* the array parameter in the declaration
+ // like this:
+ // X (object [] x, Type value)
+ // .param [0]
+ //
+ // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
+ // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
+
+ }
+
+ Parameter [] tmp = new Parameter [fixed_parms.Length + 1];
+
+ fixed_parms.CopyTo (tmp, 0);
+ tmp [fixed_parms.Length] = new Parameter (
+ method.Type, "value", Parameter.Modifier.NONE, null);
+
+ Parameters set_formal_params = new Parameters (tmp, null, method.Location);
+
+ return new InternalParameters (container, set_formal_params);
+ }
+
+ }
+
+
const int AllowedModifiers =
Modifiers.NEW |
Modifiers.PUBLIC |
Accessor get_block, Accessor set_block, Location loc)
: base (ds, type, mod_flags,
is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
- is_iface, name, parameters, attrs, get_block, set_block, loc)
+ is_iface, name, parameters, attrs, loc)
{
+ if (get_block != null)
+ Get = new GetIndexerMethod (this, get_block);
+
+ if (set_block != null)
+ Set = new SetIndexerMethod (this, parameters, set_block);
}
public override bool Define (TypeContainer container)
PropertyAttributes.RTSpecialName |
PropertyAttributes.SpecialName;
+ if (!DoDefineBase (container))
+ return false;
+
if (!DoDefine (container, container))
return false;
- IndexerName = Attribute.ScanForIndexerName (ec, OptAttributes);
+ if (OptAttributes != null)
+ IndexerName = OptAttributes.ScanForIndexerName (ec);
+
if (IndexerName == null)
IndexerName = "Item";
- else if (IsExplicitImpl)
+ else {
+ if (! Tokenizer.IsValidIdentifier (IndexerName)) {
+ Report.Error (633, Location, "The IndexerName specified is an invalid identifier");
+ return false;
+ }
+
+ if (IsExplicitImpl) {
Report.Error (592, Location,
- "Attribute 'IndexerName' is not valid on this declaration " +
- "type. It is valid on `property' declarations only.");
+ "Attribute 'IndexerName' is not valid on explicit implementations.");
+ return false;
+ }
+ }
ShortName = IndexerName;
if (IsExplicitImpl) {
flags |= MethodAttributes.HideBySig | MethodAttributes.SpecialName;
if (Get != null){
- InternalParameters ip = new InternalParameters (container, Parameters);
-
- GetData = new MethodData (container, this, "get", MemberType,
- ParameterTypes, ip, CallingConventions.Standard,
- Get.OptAttributes, ModFlags, flags, false);
-
- if (!GetData.Define (container))
+ GetBuilder = Get.Define (container);
+ if (GetBuilder == null)
return false;
-
- GetBuilder = GetData.MethodBuilder;
}
if (Set != null){
- int top = ParameterTypes.Length;
- Type [] set_pars = new Type [top + 1];
- ParameterTypes.CopyTo (set_pars, 0);
- set_pars [top] = MemberType;
-
- Parameter [] fixed_parms = Parameters.FixedParameters;
-
- if (fixed_parms == null){
- throw new Exception ("We currently do not support only array arguments in an indexer at: " + Location);
- // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
- // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
- //
- // Here is the problem: the `value' parameter has
- // to come *after* the array parameter in the declaration
- // like this:
- // X (object [] x, Type value)
- // .param [0]
- //
- // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
- // BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
-
- }
-
- Parameter [] tmp = new Parameter [fixed_parms.Length + 1];
-
-
- fixed_parms.CopyTo (tmp, 0);
- tmp [fixed_parms.Length] = new Parameter (
- Type, "value", Parameter.Modifier.NONE, null);
-
- Parameters set_formal_params = new Parameters (tmp, null, Location);
-
- InternalParameters ip = new InternalParameters (container, set_formal_params);
-
- SetData = new MethodData (container, this, "set", TypeManager.void_type,
- set_pars, ip, CallingConventions.Standard,
- Set.OptAttributes, ModFlags, flags, false);
-
- if (!SetData.Define (container))
+ SetBuilder = Set.Define (container);
+ if (SetBuilder == null)
return false;
-
- SetBuilder = SetData.MethodBuilder;
}
//
PropertyBuilder = container.TypeBuilder.DefineProperty (
IndexerName, prop_attr, MemberType, ParameterTypes);
- if (GetData != null)
+ if (Get != null)
PropertyBuilder.SetGetMethod (GetBuilder);
- if (SetData != null)
+ if (Set != null)
PropertyBuilder.SetSetMethod (SetBuilder);
TypeManager.RegisterIndexer (PropertyBuilder, GetBuilder, SetBuilder,
return true;
}
- public override string GetSignatureForError () {
+ public override string GetSignatureForError ()
+ {
return TypeManager.CSharpSignature (PropertyBuilder, true);
}
+
+ protected override string RealMethodName {
+ get {
+ return IndexerName;
+ }
+ }
}
public class Operator : MemberBase, IIteratorContainer {
public string MethodName;
public Method OperatorMethod;
+ static string[] attribute_targets = new string [] { "method", "return" };
+
public Operator (OpType type, Expression ret_type, int mod_flags,
Expression arg1type, string arg1name,
Expression arg2type, string arg2name,
return container.Name + ".operator " + OperatorType + " (" + FirstArgType + "," +
SecondArgType + ")";
}
+
+ public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+ {
+ OperatorMethod.ApplyAttributeBuilder (a, cb);
+ }
+
+ public override AttributeTargets AttributeTargets {
+ get {
+ return AttributeTargets.Method;
+ }
+ }
protected override bool CheckGenericOverride (MethodInfo method, string name)
{
return true;
}
- public void Emit (TypeContainer container)
+ public override void Emit (TypeContainer container)
{
//
// abstract or extern methods have no bodies
default: return "";
}
}
+
+ public override string GetSignatureForError(TypeContainer tc)
+ {
+ return Prototype (tc);
+ }
public override string ToString ()
{
param_types [0], param_types [1]);
}
+ protected override string[] ValidAttributeTargets {
+ get {
+ return attribute_targets;
+ }
+ }
+
public void SetYields ()
{
ModFlags |= Modifiers.METHOD_YIELDS;