using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
namespace Mono.CSharp {
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.ExplicitInterfaceName != 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)
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]);
}
//
if ((parent != null) && parent.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.Left != null)
- list.Add (i);
- }
- foreach (Indexer i in Indexers){
- if (i.MemberName.Left == 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, "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.
}
- 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;
+ }
+
+ 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;
}
+ }
+
+ public class Method : MethodCore, IIteratorContainer, IMethodData {
+ public MethodBuilder MethodBuilder;
+ public MethodData MethodData;
+ ReturnParameter return_attributes;
+
+ /// <summary>
+ /// Modifiers allowed in a class declaration
+ /// </summary>
+ const int AllowedModifiers =
+ Modifiers.NEW |
+ Modifiers.PUBLIC |
+ Modifiers.PROTECTED |
+ Modifiers.INTERNAL |
+ Modifiers.PRIVATE |
+ Modifiers.STATIC |
+ Modifiers.VIRTUAL |
+ Modifiers.SEALED |
+ Modifiers.OVERRIDE |
+ Modifiers.ABSTRACT |
+ Modifiers.UNSAFE |
+ Modifiers.METHOD_YIELDS |
+ Modifiers.EXTERN;
+
+ const int AllowedInterfaceModifiers =
+ Modifiers.NEW | Modifiers.UNSAFE;
//
- // The method's attributes are passed in because we need to extract
- // the "return:" attribute from there to apply on the return type
+ // return_type can be "null" for VOID values.
//
- static public void LabelParameters (EmitContext ec,
- MethodBase builder,
- Parameters parameters,
- Attributes method_attrs,
- Location loc)
- {
- //
- // Define each type attribute (in/out/ref) and
- // the argument names.
- //
- Parameter [] p = parameters.FixedParameters;
- int i = 0;
-
- MethodBuilder mb = null;
- ConstructorBuilder cb = null;
-
- if (builder is MethodBuilder)
- mb = (MethodBuilder) builder;
- else
- cb = (ConstructorBuilder) builder;
-
- 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");
- }
- }
- }
- }
-
- 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;
-
- /// <summary>
- /// Modifiers allowed in a class declaration
- /// </summary>
- const int AllowedModifiers =
- Modifiers.NEW |
- Modifiers.PUBLIC |
- Modifiers.PROTECTED |
- Modifiers.INTERNAL |
- Modifiers.PRIVATE |
- Modifiers.STATIC |
- Modifiers.VIRTUAL |
- Modifiers.SEALED |
- Modifiers.OVERRIDE |
- Modifiers.ABSTRACT |
- Modifiers.UNSAFE |
- Modifiers.METHOD_YIELDS |
- Modifiers.EXTERN;
-
- const int AllowedInterfaceModifiers =
- Modifiers.NEW;
-
- //
- // return_type can be "null" for VOID values.
- //
- public Method (DeclSpace ds, Expression return_type, int mod, bool is_iface,
- MemberName name, Parameters parameters, Attributes attrs,
- Location l)
- : base (ds, return_type, mod,
- is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
- is_iface, name, attrs, parameters, l)
+ public Method (DeclSpace ds, Expression return_type, int mod, bool is_iface,
+ MemberName name, Parameters parameters, Attributes attrs,
+ Location l)
+ : base (ds, return_type, mod,
+ is_iface ? AllowedInterfaceModifiers : AllowedModifiers,
+ is_iface, name, attrs, parameters, l)
{
}
- //
- // 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);
+ }
+
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)
+ return;
+
+ MethodBuilder.SetCustomAttribute (cb);
+ }
+
//
// Checks our base implementation if any
//
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,
+ true, 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 (!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);
+ }
+
+ #endregion
}
public abstract class ConstructorInitializer {
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;
}
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;
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; }
+
+ Attributes OptAttributes { get; }
+ Block Block { get; }
+
+ EmitContext CreateEmitContext (TypeContainer tc, ILGenerator ig);
}
//
// 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;
//
}
}
- 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, bool is_method,
+ 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,
+ public MethodData (MemberBase member, InternalParameters parameters,
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)
+ IMethodData method, MethodBuilder builder,
+ GenericMethod generic)
+ : this (member, parameters, modifiers, flags, is_method, method)
{
this.builder = builder;
this.GenericMethod = generic;
string obsolete = null;
bool obsolete_error = false;
- public virtual bool ApplyAttributes (Attributes opt_attrs, bool is_method)
+ public virtual bool ApplyAttributes (Attributes opt_attrs, bool is_method, DeclSpace ds)
{
- if ((opt_attrs == null) || (opt_attrs.AttributeSections == null))
+ if ((opt_attrs == null) || (opt_attrs.Attrs == 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;
+ foreach (Attribute a in opt_attrs.Attrs) {
+ Type attr_type = a.ResolveType (ec, true);
+ if (attr_type == TypeManager.conditional_attribute_type) {
+ if (!ApplyConditionalAttribute (a))
+ return false;
+ } else if (attr_type == TypeManager.obsolete_attribute_type) {
+ if (!ApplyObsoleteAttribute (a))
+ return false;
+ } else if (attr_type == TypeManager.dllimport_type) {
+ if (!is_method) {
+ Attribute.Error_AttributeNotValidForElement (a, method.Location);
+ return false;
}
+ if (!ApplyDllImportAttribute (a))
+ return false;
}
}
{
const int extern_static = Modifiers.EXTERN | Modifiers.STATIC;
if ((modifiers & extern_static) != extern_static) {
- Report.Error (601, Location,
+ Report.Error (601, method.Location,
"The DllImport attribute must be specified on a method " +
"marked `static' and `extern'.");
return false;
protected virtual bool ApplyObsoleteAttribute (Attribute a)
{
if (obsolete != null) {
- Report.Error (579, Location, "Duplicate `Obsolete' attribute");
+ Report.Error (579, method.Location, "Duplicate `Obsolete' attribute");
return false;
}
{
// The Conditional attribute is only valid on methods.
if (!is_method) {
- Attribute.Error_AttributeNotValidForElement (a, Location);
+ Attribute.Error_AttributeNotValidForElement (a, method.Location);
return false;
}
if (condition == null)
return false;
- if (ReturnType != TypeManager.void_type) {
- Report.Error (578, Location,
+ if (method.ReturnType != TypeManager.void_type) {
+ Report.Error (578, method.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,
+ Report.Error (243, method.Location,
"Conditional not valid on `" + member.Name + "' " +
"because it is an override method");
return false;
}
if (member.IsExplicitImpl) {
- Report.Error (577, Location,
+ Report.Error (577, method.Location,
"Conditional not valid on `" + member.Name + "' " +
"because it is an explicit interface implementation");
return false;
}
if (IsImplementing) {
- Report.Error (623, Location,
+ Report.Error (623, method.Location,
"Conditional not valid on `" + member.Name + "' " +
"because it is an interface method");
return false;
return flags;
}
- public virtual bool Define (TypeContainer container)
+ public bool Define (TypeContainer container)
{
MethodInfo implementing = null;
- string method_name, name, prefix;
+ string prefix;
- if (OptAttributes != null)
- if (!ApplyAttributes (OptAttributes, is_method))
+ if (method.OptAttributes != null)
+ if (!ApplyAttributes (method.OptAttributes, is_method, container))
return false;
if (member.IsExplicitImpl)
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);
+ 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,
+ Report.Error (601, method.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);
+ method.ReturnType, ParameterTypes);
} else if (builder == null)
builder = container.TypeBuilder.DefineMethod (
- method_name, flags, CallingConventions,
- ReturnType, ParameterTypes);
+ method_name, flags, method.CallingConventions,
+ method.ReturnType, ParameterTypes);
else
builder.SetGenericMethodSignature (
- flags, CallingConventions,
- ReturnType, ParameterTypes);
+ flags, method.CallingConventions,
+ method.ReturnType, 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 " +
//
// 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);
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;
if (IsInterface) {
ModFlags = Modifiers.PUBLIC |
Modifiers.ABSTRACT |
- Modifiers.VIRTUAL;
+ Modifiers.VIRTUAL | (ModFlags & Modifiers.UNSAFE);
flags = MethodAttributes.Public |
MethodAttributes.Abstract |
//
// Check for explicit interface implementation
//
- if (MemberName.Left != null) {
- ExplicitInterfaceName = MemberName.Left.GetTypeExpression (Location);
+ if (MemberName.TypeName != null) {
+ ExplicitInterfaceName = MemberName.TypeName.GetTypeExpression (Location);
ShortName = MemberName.Name;
} else
ShortName = Name;
return true;
}
+
+ 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;
+ }
+
+
}
//
[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;
}
- public void SetAssigned ()
+ protected override bool DoDefine (DeclSpace ds, TypeContainer container)
{
- status |= Status.ASSIGNED;
+ 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;
+ }
+
+ return true;
}
- }
- //
- // The Field class is used to represents class/struct fields during parsing.
- //
- public class Field : FieldBase {
- // <summary>
- // Modifiers allowed in a class declaration
- // </summary>
- const int AllowedModifiers =
- Modifiers.NEW |
- Modifiers.PUBLIC |
+ 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;
+ }
+ }
+
+ //
+ // The Field class is used to represents class/struct fields during parsing.
+ //
+ public class Field : FieldBase {
+ // <summary>
+ // Modifiers allowed in a class declaration
+ // </summary>
+ const int AllowedModifiers =
+ Modifiers.NEW |
+ Modifiers.PUBLIC |
Modifiers.PROTECTED |
Modifiers.INTERNAL |
Modifiers.PRIVATE |
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 `" +
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;
}
}
// their common bits.
//
abstract public class PropertyBase : MethodCore {
- public Accessor Get, Set;
+
+ 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, false, this);
+
+ if (!method_data.Define (container))
+ return null;
+
+ return method_data.MethodBuilder;
+ }
+
+ 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)
+ {
+ }
+
+ public override void ApplyAttributeBuilder(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, false, this);
+
+ if (!method_data.Define (container))
+ return null;
+
+ return method_data.MethodBuilder;
+ }
+
+ 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: Attributable, IMethodData {
+ protected readonly MethodCore method;
+ protected MethodData method_data;
+ Block block;
+
+ ReturnParameter return_attributes;
+
+ public PropertyMethod (MethodCore method, Accessor accessor):
+ base (accessor.Attributes)
+ {
+ this.method = method;
+ this.block = accessor.Block;
+ }
+
+ 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;
+ }
+ }
+
+ #region IMethodData Members
+
+ public Block Block {
+ get {
+ return block;
+ }
+ }
+
+ public CallingConventions CallingConventions {
+ get {
+ return CallingConventions.Standard;
+ }
+ }
+
+ public abstract MethodBuilder Define (TypeContainer container);
+
+ public void Emit (TypeContainer container)
+ {
+ method_data.Emit (container, this);
+ block = null;
+
+ }
+
+ public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
+ {
+ if (a.Target == "return") {
+ if (return_attributes == null)
+ return_attributes = new ReturnParameter (method_data.MethodBuilder, Location);
+
+ return_attributes.ApplyAttributeBuilder (a, cb);
+ return;
+ }
+
+ method_data.MethodBuilder.SetCustomAttribute (cb);
+ }
+
+ public virtual Type[] ParameterTypes {
+ get {
+ return TypeManager.NoTypes;
+ }
+ }
+
+ public abstract Type ReturnType { get; }
+
+ public Location Location {
+ get {
+ return method.Location;
+ }
+ }
+
+ public abstract string MethodName { get; }
+
+ public EmitContext CreateEmitContext (TypeContainer tc, ILGenerator ig)
+ {
+ return new EmitContext (tc, method.ds, method.Location, ig, ReturnType, method.ModFlags, false);
+ }
+
+ #endregion
+ }
+
+ public PropertyMethod Get, Set;
public PropertyBuilder PropertyBuilder;
public MethodBuilder GetBuilder, SetBuilder;
- public MethodData GetData, SetData;
protected EmitContext ec;
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)
{
- Get = get_block;
- Set = set_block;
+ }
+
+ 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)
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
//
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)
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 [] { "method", "field", "event" };
+
+ 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_remove_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_combine_delegate_delegate;
+ }
+ }
+
+ }
+
+ public abstract class DelegateMethod: Attributable, IMethodData
+ {
+ protected readonly Event method;
+ protected MethodData method_data;
+ Block block;
+ ReturnParameter return_attributes;
+ ImplicitParameter param_attr;
+
+ static string[] attribute_targets = new string [] { "method", "param", "return" };
+
+ public DelegateMethod (Event method):
+ base (null)
+ {
+ this.method = method;
+ }
+
+ public DelegateMethod (Event method, Accessor accessor):
+ base (accessor.Attributes)
+ {
+ this.method = method;
+ this.block = accessor.Block;
+ }
+
+ public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
+ {
+ if (a.Target == "return") {
+ if (return_attributes == null)
+ return_attributes = new ReturnParameter (method_data.MethodBuilder, Location);
+
+ return_attributes.ApplyAttributeBuilder (a, cb);
+ return;
+ }
+
+ if (a.Target == "param") {
+ if (param_attr == null)
+ param_attr = new ImplicitParameter (method_data.MethodBuilder);
+
+ param_attr.ApplyAttributeBuilder (a, cb);
+ return;
+ }
+
+ method_data.MethodBuilder.SetCustomAttribute (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, false, this);
+
+ if (!method_data.Define (container))
+ return null;
+
+ MethodBuilder mb = method_data.MethodBuilder;
+ mb.DefineParameter (1, ParameterAttributes.None, "value");
+ return mb;
+ }
+
+ #region IMethodData Members
+
+ public Block Block {
+ get {
+ return block;
+ }
+ }
+
+ public CallingConventions CallingConventions {
+ get {
+ return CallingConventions.Standard;
+ }
+ }
+
+ public void Emit (TypeContainer tc)
+ {
+ if (block != null) {
+ method_data.Emit (tc, this);
+ block = null;
+ 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 Type[] ParameterTypes {
+ get {
+ return new Type[] { method.MemberType };
+ }
+ }
+
+ public Type ReturnType {
+ get {
+ return TypeManager.void_type;
+ }
+ }
+
+ public Location Location {
+ get {
+ return method.Location;
+ }
+ }
+
+ public EmitContext CreateEmitContext (TypeContainer tc, ILGenerator ig)
+ {
+ return new EmitContext (tc, method.ds, Location, ig, ReturnType, method.ModFlags, false);
+ }
+
+ public abstract string MethodName { get; }
+
+ #endregion
+
+ 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 (!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 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)
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 [] { "param" };
+
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
param_types [0], param_types [1]);
}
+ protected override string[] ValidAttributeTargets {
+ get {
+ return attribute_targets;
+ }
+ }
+
public void SetYields ()
{
ModFlags |= Modifiers.METHOD_YIELDS;