// (C) 2001, 2002 Ximian, Inc (http://www.ximian.com)
//
//
+// 2002-10-11 Miguel de Icaza <miguel@ximian.com>
+//
+// * class.cs: Following the comment from 2002-09-26 to AddMethod, I
+// have fixed a remaining problem: not every AddXXXX was adding a
+// fully qualified name.
+//
+// Now everyone registers a fully qualified name in the DeclSpace as
+// being defined instead of the partial name.
+//
+// Downsides: we are slower than we need to be due to the excess
+// copies and the names being registered this way.
+//
+// The reason for this is that we currently depend (on the corlib
+// bootstrap for instance) that types are fully qualified, because
+// we dump all the types in the namespace, and we should really have
+// types inserted into the proper namespace, so we can only store the
+// basenames in the defined_names array.
+//
+//
#define CACHE
using System;
using System.Collections;
public AdditionResult AddConstant (Const constant)
{
AdditionResult res;
- string name = constant.Name;
+ string basename = constant.Name;
- if ((res = IsValid (name)) != AdditionResult.Success)
+ if ((res = IsValid (basename)) != AdditionResult.Success)
return res;
if (constants == null)
constants = new ArrayList ();
constants.Add (constant);
- DefineName (name, constant);
+ DefineName (Name + "." + basename, constant);
return AdditionResult.Success;
}
public AdditionResult AddEnum (Mono.CSharp.Enum e)
{
AdditionResult res;
- string name = e.Name;
- if ((res = IsValid (name)) != AdditionResult.Success)
+ if ((res = IsValid (e.Basename)) != AdditionResult.Success)
return res;
if (enums == null)
enums = new ArrayList ();
enums.Add (e);
- DefineName (name, e);
+ DefineName (e.Name, e);
return AdditionResult.Success;
}
public AdditionResult AddClass (Class c)
{
AdditionResult res;
- string name = c.Name;
-
- if ((res = IsValid (name)) != AdditionResult.Success)
+ if ((res = IsValid (c.Basename)) != AdditionResult.Success)
return res;
- DefineName (name, c);
+ DefineName (c.Name, c);
types.Add (c);
return AdditionResult.Success;
public AdditionResult AddStruct (Struct s)
{
AdditionResult res;
- string name = s.Name;
- if ((res = IsValid (name)) != AdditionResult.Success)
+ if ((res = IsValid (s.Basename)) != AdditionResult.Success)
return res;
- DefineName (name, s);
+ DefineName (s.Name, s);
types.Add (s);
return AdditionResult.Success;
public AdditionResult AddDelegate (Delegate d)
{
AdditionResult res;
- string name = d.Name;
- if ((res = IsValid (name)) != AdditionResult.Success)
+ if ((res = IsValid (d.Basename)) != AdditionResult.Success)
return res;
if (delegates == null)
delegates = new ArrayList ();
- DefineName (name, d);
+ DefineName (d.Name, d);
delegates.Add (d);
return AdditionResult.Success;
public AdditionResult AddMethod (Method method)
{
- string name = method.Name;
- Object value = defined_names [name];
-
+ string basename = method.Name;
+ string fullname = Name + "." + basename;
+
+ Object value = defined_names [fullname];
+
if (value != null && (!(value is Method)))
return AdditionResult.NameExists;
- if (name == Basename)
+ if (basename == Basename)
return AdditionResult.EnclosingClash;
if (methods == null)
else
methods.Add (method);
- if (value != null)
- DefineName (name, method);
+ if (value == null)
+ DefineName (fullname, method);
return AdditionResult.Success;
}
public AdditionResult AddInterface (Interface iface)
{
AdditionResult res;
- string name = iface.Name;
- if ((res = IsValid (name)) != AdditionResult.Success)
+ if ((res = IsValid (iface.Basename)) != AdditionResult.Success)
return res;
if (interfaces == null)
interfaces = new ArrayList ();
interfaces.Add (iface);
- DefineName (name, iface);
+ DefineName (iface.Name, iface);
return AdditionResult.Success;
}
public AdditionResult AddField (Field field)
{
AdditionResult res;
- string name = field.Name;
+ string basename = field.Name;
- if ((res = IsValid (name)) != AdditionResult.Success)
+ if ((res = IsValid (basename)) != AdditionResult.Success)
return res;
if (fields == null)
if ((field.ModFlags & Modifiers.STATIC) == 0)
have_nonstatic_fields = true;
- DefineName (name, field);
+ DefineName (Name + "." + basename, field);
return AdditionResult.Success;
}
public AdditionResult AddProperty (Property prop)
{
AdditionResult res;
- string name = prop.Name;
+ string basename = prop.Name;
- if ((res = IsValid (name)) != AdditionResult.Success)
+ if ((res = IsValid (basename)) != AdditionResult.Success)
return res;
if (properties == null)
properties.Insert (0, prop);
else
properties.Add (prop);
- DefineName (name, prop);
+ DefineName (Name + "." + basename, prop);
return AdditionResult.Success;
}
public AdditionResult AddEvent (Event e)
{
AdditionResult res;
- string name = e.Name;
+ string basename = e.Name;
- if ((res = IsValid (name)) != AdditionResult.Success)
+ if ((res = IsValid (basename)) != AdditionResult.Success)
return res;
if (events == null)
events = new ArrayList ();
events.Add (e);
- DefineName (name, e);
+ DefineName (Name + "." + basename, e);
return AdditionResult.Success;
}
// if (parent_builder is ModuleBuilder) {
if (IsTopLevel){
ModuleBuilder builder = CodeGen.ModuleBuilder;
+ TypeBuilder = builder.DefineType (
+ Name, type_attributes, parent, ifaces);
- //
- // Structs with no fields need to have a ".size 1"
- // appended
- //
-
- if (!is_class && !have_nonstatic_fields)
- TypeBuilder = builder.DefineType (Name,
- type_attributes,
- parent,
- PackingSize.Unspecified, 1);
- else
- //
- // classes or structs with fields
- //
- TypeBuilder = builder.DefineType (Name,
- type_attributes,
- parent,
- ifaces);
} else {
TypeBuilder builder = Parent.TypeBuilder;
+ TypeBuilder = builder.DefineNestedType (
+ Basename, type_attributes, parent, ifaces);
+ }
- //
- // Structs with no fields need to have a ".size 1"
- // appended
- //
- if (!is_class && !have_nonstatic_fields)
- TypeBuilder = builder.DefineNestedType (Basename,
- type_attributes,
- parent,
- PackingSize.Unspecified);
- else {
- //
- // classes or structs with fields
- //
- TypeBuilder = builder.DefineNestedType (Basename,
- type_attributes,
- parent,
- ifaces);
- }
+ //
+ // Structs with no fields need to have at least one byte.
+ // The right thing would be to set the PackingSize in a DefineType
+ // but there are no functions that allow interfaces *and* the size to
+ // be specified.
+ //
+
+ if (!is_class && !have_nonstatic_fields){
+ TypeBuilder.DefineField ("$PRIVATE$", TypeManager.byte_type,
+ FieldAttributes.Private);
}
// add interfaces that were not added at type creation (weird API issue)
IndexerName = class_indexer_name;
}
- static void Report1530 (Location loc)
+ static void Error_KeywordNotAllowed (Location loc)
{
Report.Error (1530, loc, "Keyword new not allowed for namespace elements");
}
if ((iface.ModFlags & Modifiers.NEW) == 0)
iface.DefineMembers (this);
else
- Report1530 (iface.Location);
+ Error_KeywordNotAllowed (iface.Location);
}
if (RootContext.WarningLevel > 1){
} else
IndexerName = "Item";
- if (operators != null)
+ if (operators != null){
DefineMembers (operators, null);
+ CheckPairedOperators ();
+ }
+
if (enums != null)
DefineMembers (enums, defined_names);
foreach (Event e in events) {
if ((e.ModFlags & modflags) == 0)
continue;
+ if ((e.ModFlags & static_mask) != static_flags)
+ continue;
MemberInfo eb = e.EventBuilder;
if (eb != null && filter (eb, criteria) == true)
if (Delegates != null)
foreach (Delegate d in Delegates)
- d.CloseDelegate ();
+ d.CloseType ();
}
public string MakeName (string n)
{
return FindMembers (mt, bf | BindingFlags.DeclaredOnly, null, null);
}
+
+ //
+ // Operator pair checking
+ //
+
+ class OperatorEntry {
+ public int flags;
+ public Type ret_type;
+ public Type type1, type2;
+ public Operator op;
+ public Operator.OpType ot;
+
+ public OperatorEntry (int f, Operator o)
+ {
+ flags = f;
+
+ ret_type = o.OperatorMethod.GetReturnType ();
+ Type [] pt = o.OperatorMethod.ParameterTypes;
+ type1 = pt [0];
+ type2 = pt [1];
+ op = o;
+ ot = o.OperatorType;
+ }
+
+ public override int GetHashCode ()
+ {
+ return ret_type.GetHashCode ();
+ }
+
+ public override bool Equals (object o)
+ {
+ OperatorEntry other = (OperatorEntry) o;
+
+ if (other.ret_type != ret_type)
+ return false;
+ if (other.type1 != type1)
+ return false;
+ if (other.type2 != type2)
+ return false;
+ return true;
+ }
+ }
+
+ //
+ // Checks that some operators come in pairs:
+ // == and !=
+ // > and <
+ // >= and <=
+ //
+ // They are matched based on the return type and the argument types
+ //
+ void CheckPairedOperators ()
+ {
+ Hashtable pairs = new Hashtable (null, null);
+
+ // Register all the operators we care about.
+ foreach (Operator op in operators){
+ int reg = 0;
+
+ switch (op.OperatorType){
+ case Operator.OpType.Equality:
+ reg = 1; break;
+ case Operator.OpType.Inequality:
+ reg = 2; break;
+
+ case Operator.OpType.GreaterThan:
+ reg = 1; break;
+ case Operator.OpType.LessThan:
+ reg = 2; break;
+
+ case Operator.OpType.GreaterThanOrEqual:
+ reg = 1; break;
+ case Operator.OpType.LessThanOrEqual:
+ reg = 2; break;
+ }
+ if (reg == 0)
+ continue;
+
+ OperatorEntry oe = new OperatorEntry (reg, op);
+
+ object o = pairs [oe];
+ if (o == null)
+ pairs [oe] = oe;
+ else {
+ oe = (OperatorEntry) o;
+ oe.flags |= reg;
+ }
+ }
+
+ //
+ // Look for the mistakes.
+ //
+ foreach (DictionaryEntry de in pairs){
+ OperatorEntry oe = (OperatorEntry) de.Key;
+
+ if (oe.flags == 3)
+ continue;
+
+ string s = "";
+ switch (oe.ot){
+ case Operator.OpType.Equality:
+ s = "!=";
+ break;
+ case Operator.OpType.Inequality:
+ s = "==";
+ break;
+ case Operator.OpType.GreaterThan:
+ s = "<";
+ break;
+ case Operator.OpType.LessThan:
+ s = ">";
+ break;
+ case Operator.OpType.GreaterThanOrEqual:
+ s = "<=";
+ break;
+ case Operator.OpType.LessThanOrEqual:
+ s = ">=";
+ break;
+ }
+ Report.Error (216, oe.op.Location,
+ "The operator `" + oe.op + "' requires a matching operator `" + s + "' to also be defined");
+ }
+ }
+
+
}
public class Class : TypeContainer {
// function. Provides a nice cache. (used between semantic analysis
// and actual code generation
//
- public Type GetReturnType (TypeContainer parent)
+ public Type GetReturnType ()
{
return MemberType;
}
public void Emit (EmitContext ec)
{
- if (parent_constructor != null)
- ec.ig.Emit (OpCodes.Ldarg_0);
- if (argument_list != null)
- Invocation.EmitArguments (ec, null, argument_list);
- if (parent_constructor != null)
- ec.ig.Emit (OpCodes.Call, parent_constructor);
+ if (parent_constructor != null){
+ if (ec.IsStatic)
+ Invocation.EmitCall (ec, true, true, null, parent_constructor, argument_list, loc);
+ else
+ Invocation.EmitCall (ec, true, false, ec.This, parent_constructor, argument_list, loc);
+ }
}
}
// <summary>
// Modifiers allowed for a constructor.
// </summary>
- const int AllowedModifiers =
+ public const int AllowedModifiers =
Modifiers.PUBLIC |
Modifiers.PROTECTED |
Modifiers.INTERNAL |
Modifiers.STATIC |
Modifiers.UNSAFE |
+ Modifiers.EXTERN |
Modifiers.PRIVATE;
//
"constructors");
return false;
}
- ca |= MethodAttributes.Public | MethodAttributes.HideBySig;
+ ca |= MethodAttributes.HideBySig;
+
+ if ((ModFlags & Modifiers.PUBLIC) != 0)
+ ca |= MethodAttributes.Public;
+ else if ((ModFlags & Modifiers.PROTECTED) != 0){
+ if ((ModFlags & Modifiers.INTERNAL) != 0)
+ ca |= MethodAttributes.FamORAssem;
+ else
+ ca |= MethodAttributes.Family;
+ } else if ((ModFlags & Modifiers.INTERNAL) != 0)
+ ca |= MethodAttributes.Assembly;
+ else if (IsDefault ())
+ ca |= MethodAttributes.Public;
+ else
+ ca |= MethodAttributes.Private;
}
ConstructorBuilder = parent.TypeBuilder.DefineConstructor (
else
name = member.ShortName;
method_name = prefix + name;
-
+
if (parent.Pending != null){
if (member is Indexer)
implementing = parent.Pending.IsInterfaceIndexer (
//
if (implementing.DeclaringType.IsInterface)
flags |= MethodAttributes.NewSlot;
-
+
flags |=
MethodAttributes.Virtual |
MethodAttributes.HideBySig;
if (ec.ContainerType.BaseType != null) {
Expression member_lookup = Expression.MemberLookup (
- ec, ec.ContainerType.BaseType, "Finalize",
+ ec, ec.ContainerType.BaseType, ec.ContainerType.BaseType, "Finalize",
MemberTypes.Method, Expression.AllBindingFlags, Location);
if (member_lookup != null){
}
}
}
-
+
+ FieldAttributes fa = Modifiers.FieldAttr (ModFlags);
+
+ if (parent is Struct &&
+ ((fa & FieldAttributes.Static) == 0) &&
+ t == parent.TypeBuilder &&
+ !TypeManager.IsBuiltinType (t)){
+ Report.Error (523, Location, "Struct member `" + parent.Name + "." + Name +
+ "' causes a cycle in the structure layout");
+ return false;
+ }
FieldBuilder = parent.TypeBuilder.DefineField (
Name, t, Modifiers.FieldAttr (ModFlags));
if (!DoDefineParameters (parent))
return false;
- MethodSignature ms = new MethodSignature (Name, null, ParameterTypes);
+ if (IsExplicitImpl)
+ return true;
+
+ string report_name;
+ MethodSignature ms, base_ms;
+ if (this is Indexer) {
+ string name, base_name;
+
+ report_name = "this";
+ name = TypeManager.IndexerPropertyName (parent.TypeBuilder);
+ ms = new MethodSignature (name, null, ParameterTypes);
+ base_name = TypeManager.IndexerPropertyName (parent.TypeBuilder.BaseType);
+ base_ms = new MethodSignature (base_name, null, ParameterTypes);
+ } else {
+ report_name = Name;
+ ms = base_ms = new MethodSignature (Name, null, ParameterTypes);
+ }
+
MemberList props_this;
props_this = TypeContainer.FindMembers (
if (props_this.Count > 0) {
Report.Error (111, Location, "Class `" + parent.Name + "' " +
- "already defines a member called `" + Name + "' " +
+ "already defines a member called `" + report_name + "' " +
"with the same parameter types");
return false;
}
MemberList props_static = TypeContainer.FindMembers (
parent.TypeBuilder.BaseType, MemberTypes.Property,
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static,
- MethodSignature.inheritable_property_signature_filter, ms);
+ MethodSignature.inheritable_property_signature_filter, base_ms);
MemberList props_instance = TypeContainer.FindMembers (
parent.TypeBuilder.BaseType, MemberTypes.Property,
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance,
MethodSignature.inheritable_property_signature_filter,
- ms);
+ base_ms);
//
// Find if we have anything
inherited_set : inherited_get;
if (reference != null) {
- string name = reference.DeclaringType.Name + "." + Name;
+ string name = reference.DeclaringType.Name + "." + report_name;
if (!CheckMethodAgainstBase (parent, flags, reference, name))
return false;
if (Add == null && Remove == null) {
FieldBuilder = parent.TypeBuilder.DefineField (
- Name, MemberType, FieldAttributes.FamANDAssem);
+ Name, MemberType,
+ FieldAttributes.FamANDAssem | ((ModFlags & Modifiers.STATIC) != 0 ? FieldAttributes.Static : 0));
TypeManager.RegisterPrivateFieldOfEvent (
(EventInfo) EventBuilder, FieldBuilder);
TypeManager.RegisterFieldBase (FieldBuilder, this);
Type [] param_types = OperatorMethod.ParameterTypes;
Type declaring_type = OperatorMethodBuilder.DeclaringType;
- Type return_type = OperatorMethod.GetReturnType (parent);
+ Type return_type = OperatorMethod.GetReturnType ();
Type first_arg_type = param_types [0];
// Rules for conversion operators
OperatorMethod.Block = Block;
OperatorMethod.Emit (parent);
}
+
+ public static string GetName (OpType ot)
+ {
+ switch (ot){
+ case OpType.LogicalNot:
+ return "!";
+ case OpType.OnesComplement:
+ return "~";
+ case OpType.Increment:
+ return "++";
+ case OpType.Decrement:
+ return "--";
+ case OpType.True:
+ return "true";
+ case OpType.False:
+ return "false";
+ case OpType.Addition:
+ return "+";
+ case OpType.Subtraction:
+ return "-";
+ case OpType.UnaryPlus:
+ return "+";
+ case OpType.UnaryNegation:
+ return "-";
+ case OpType.Multiply:
+ return "*";
+ case OpType.Division:
+ return "/";
+ case OpType.Modulus:
+ return "%";
+ case OpType.BitwiseAnd:
+ return "&";
+ case OpType.BitwiseOr:
+ return "|";
+ case OpType.ExclusiveOr:
+ return "^";
+ case OpType.LeftShift:
+ return "<<";
+ case OpType.RightShift:
+ return ">>";
+ case OpType.Equality:
+ return "==";
+ case OpType.Inequality:
+ return "!=";
+ case OpType.GreaterThan:
+ return ">";
+ case OpType.LessThan:
+ return "<";
+ case OpType.GreaterThanOrEqual:
+ return ">=";
+ case OpType.LessThanOrEqual:
+ return "<=";
+ case OpType.Implicit:
+ return "implicit";
+ case OpType.Explicit:
+ return "explicit";
+ default: return "";
+ }
+ }
+
+ public override string ToString ()
+ {
+ Type return_type = OperatorMethod.GetReturnType();
+ Type [] param_types = OperatorMethod.ParameterTypes;
+
+ if (SecondArgType == null)
+ return String.Format (
+ "{0} operator {1}({2})",
+ TypeManager.CSharpName (return_type),
+ GetName (OperatorType),
+ param_types [0]);
+ else
+ return String.Format (
+ "{0} operator {1}({2}, {3})",
+ TypeManager.CSharpName (return_type),
+ GetName (OperatorType),
+ param_types [0], param_types [1]);
+ }
}
//