Author: marek
Date: 2007-02-25 16:55:26 -0500 (Sun, 25 Feb 2007)
New Revision: 73414
Modified:
trunk/mcs/mcs/ChangeLog
trunk/mcs/mcs/attribute.cs
trunk/mcs/mcs/class.cs
trunk/mcs/mcs/codegen.cs
Log:
2007-02-25 Marek Safar <[EMAIL PROTECTED]>
* attribute.cs: Replaced DefinePInvoke in favor of S.R.E implementation
(Fixes #80455)
* class.cs (InterfaceMemberBase): Share common `extern' modifier checks
here.
Check property and event extern attributes.
* codegen.cs (ModuleClass): HasDefaultCharSet when module defined global
charset.
Modified: trunk/mcs/mcs/ChangeLog
===================================================================
--- trunk/mcs/mcs/ChangeLog 2007-02-25 19:41:15 UTC (rev 73413)
+++ trunk/mcs/mcs/ChangeLog 2007-02-25 21:55:26 UTC (rev 73414)
@@ -1,3 +1,15 @@
+2007-02-25 Marek Safar <[EMAIL PROTECTED]>
+
+ * attribute.cs: Replaced DefinePInvoke in favor of S.R.E implementation
+ (Fixes #80455)
+
+ * class.cs (InterfaceMemberBase): Share common `extern' modifier checks
+ here.
+ Check property and event extern attributes.
+
+ * codegen.cs (ModuleClass): HasDefaultCharSet when module defined global
+ charset.
+
2007-02-24 Marek Safar <[EMAIL PROTECTED]>
A fix for bug #80407
Modified: trunk/mcs/mcs/attribute.cs
===================================================================
--- trunk/mcs/mcs/attribute.cs 2007-02-25 19:41:15 UTC (rev 73413)
+++ trunk/mcs/mcs/attribute.cs 2007-02-25 21:55:26 UTC (rev 73414)
@@ -85,7 +85,7 @@
public readonly string Identifier;
readonly ArrayList PosArguments;
- readonly ArrayList NamedArguments;
+ ArrayList NamedArguments;
public readonly Location Location;
@@ -308,6 +308,29 @@
t == TypeManager.type_type;
}
+ [Conditional ("GMCS_SOURCE")]
+ void ApplyModuleCharSet ()
+ {
+ if (Type != TypeManager.dllimport_type)
+ return;
+
+ if (!CodeGen.Module.HasDefaultCharSet)
+ return;
+
+ const string CharSetEnumMember = "CharSet";
+ if (NamedArguments == null) {
+ NamedArguments = new ArrayList (1);
+ } else {
+ foreach (DictionaryEntry de in NamedArguments) {
+ if ((string)de.Key == CharSetEnumMember)
+ return;
+ }
+ }
+
+ NamedArguments.Add (new DictionaryEntry
(CharSetEnumMember,
+ new Argument (Constant.CreateConstant (typeof
(CharSet), CodeGen.Module.DefaultCharSet, Location))));
+ }
+
public CustomAttributeBuilder Resolve ()
{
if (resolve_error)
@@ -354,8 +377,9 @@
return null;
}
- CustomAttributeBuilder cb;
+ ApplyModuleCharSet ();
+ CustomAttributeBuilder cb;
try {
if (NamedArguments == null) {
cb = new CustomAttributeBuilder (ctor,
pos_values);
@@ -1110,11 +1134,19 @@
return (CharSet)System.Enum.Parse (typeof (CharSet),
pos_values [0].ToString ());
}
- public MethodImplOptions GetMethodImplOptions ()
- {
- if (pos_values [0].GetType () != typeof
(MethodImplOptions))
- return (MethodImplOptions)System.Enum.ToObject
(typeof (MethodImplOptions), pos_values [0]);
- return (MethodImplOptions)pos_values [0];
+ public bool IsInternalMethodImplAttribute {
+ get {
+ if (Type != TypeManager.methodimpl_attr_type)
+ return false;
+
+ MethodImplOptions options;
+ if (pos_values[0].GetType () != typeof
(MethodImplOptions))
+ options =
(MethodImplOptions)System.Enum.ToObject (typeof (MethodImplOptions),
pos_values[0]);
+ else
+ options =
(MethodImplOptions)pos_values[0];
+
+ return (options &
MethodImplOptions.InternalCall) != 0;
+ }
}
public LayoutKind GetLayoutKindValue ()
@@ -1218,122 +1250,7 @@
}
}
}
-
- public MethodBuilder DefinePInvokeMethod (TypeBuilder builder,
string name,
- MethodAttributes
flags, IMethodData method, Type [] param_types)
- {
- if (pos_values == null)
- // TODO: It is not neccessary to call whole
Resolve (ApplyAttribute does it now) we need only ctor args.
- // But because a lot of attribute class code
must be rewritten will be better to wait...
- Resolve ();
- if (resolve_error)
- return null;
-
- string dll_name = (string)pos_values [0];
-
- // Default settings
- CallingConvention cc = CallingConvention.Winapi;
- CharSet charset = CodeGen.Module.DefaultCharSet;
- bool preserve_sig = true;
- string entry_point = name;
- bool best_fit_mapping = false;
- bool throw_on_unmappable = false;
- bool exact_spelling = false;
- bool set_last_error = false;
-
- bool best_fit_mapping_set = false;
- bool throw_on_unmappable_set = false;
- bool exact_spelling_set = false;
- bool set_last_error_set = false;
-
- MethodInfo set_best_fit = null;
- MethodInfo set_throw_on = null;
- MethodInfo set_exact_spelling = null;
- MethodInfo set_set_last_error = null;
-
- if (field_info_arr != null) {
-
- for (int i = 0; i < field_info_arr.Length; i++)
{
- switch (field_info_arr [i].Name) {
- case "BestFitMapping":
- best_fit_mapping = (bool)
field_values_arr [i];
- best_fit_mapping_set = true;
- break;
- case "CallingConvention":
- cc = (CallingConvention)
field_values_arr [i];
- break;
- case "CharSet":
- charset = (CharSet)
field_values_arr [i];
- break;
- case "EntryPoint":
- entry_point = (string)
field_values_arr [i];
- break;
- case "ExactSpelling":
- exact_spelling = (bool)
field_values_arr [i];
- exact_spelling_set = true;
- break;
- case "PreserveSig":
- preserve_sig = (bool)
field_values_arr [i];
- break;
- case "SetLastError":
- set_last_error = (bool)
field_values_arr [i];
- set_last_error_set = true;
- break;
- case "ThrowOnUnmappableChar":
- throw_on_unmappable = (bool)
field_values_arr [i];
- throw_on_unmappable_set = true;
- break;
- default:
- throw new
InternalErrorException (field_info_arr [i].ToString ());
- }
- }
- }
-
- if (throw_on_unmappable_set || best_fit_mapping_set ||
exact_spelling_set || set_last_error_set) {
- set_best_fit = typeof (MethodBuilder).
- GetMethod ("set_BestFitMapping",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
- set_throw_on = typeof (MethodBuilder).
- GetMethod ("set_ThrowOnUnmappableChar",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
- set_exact_spelling = typeof (MethodBuilder).
- GetMethod ("set_ExactSpelling",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
- set_set_last_error = typeof (MethodBuilder).
- GetMethod ("set_SetLastError",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
-
- if ((set_best_fit == null) || (set_throw_on ==
null) ||
- (set_exact_spelling == null) ||
(set_set_last_error == null)) {
- Report.Error (-1, Location,
- "The
ThrowOnUnmappableChar, BestFitMapping, SetLastError, " +
- "and ExactSpelling
attributes can only be emitted when running on the mono runtime.");
- return null;
- }
- }
-
- try {
- MethodBuilder mb = builder.DefinePInvokeMethod (
- name, dll_name, entry_point, flags |
MethodAttributes.HideBySig | MethodAttributes.PinvokeImpl,
- method.ParameterInfo.CallingConvention,
method.ReturnType, param_types, cc, charset);
-
- if (preserve_sig)
- mb.SetImplementationFlags
(MethodImplAttributes.PreserveSig);
-
- if (throw_on_unmappable_set)
- set_throw_on.Invoke (mb, 0, null, new
object [] { throw_on_unmappable }, null);
- if (best_fit_mapping_set)
- set_best_fit.Invoke (mb, 0, null, new
object [] { best_fit_mapping }, null);
- if (exact_spelling_set)
- set_exact_spelling.Invoke (mb, 0,
null, new object [] { exact_spelling }, null);
- if (set_last_error_set)
- set_set_last_error.Invoke (mb, 0,
null, new object [] { set_last_error }, null);
-
- return mb;
- }
- catch (ArgumentException e) {
- Error_AttributeEmitError (e.Message);
- return null;
- }
- }
-
private Expression GetValue ()
{
if (PosArguments == null || PosArguments.Count < 1)
Modified: trunk/mcs/mcs/class.cs
===================================================================
--- trunk/mcs/mcs/class.cs 2007-02-25 19:41:15 UTC (rev 73413)
+++ trunk/mcs/mcs/class.cs 2007-02-25 21:55:26 UTC (rev 73414)
@@ -2425,9 +2425,8 @@
//
// Report.Warning (-20, "Exception while creating
class: " + TypeBuilder.Name);
// Console.WriteLine (e.Message);
- } catch {
- Console.WriteLine ("In type: " + Name);
- throw;
+ } catch (Exception e) {
+ throw new InternalErrorException (this, e);
}
if (Enums != null)
@@ -3566,6 +3565,8 @@
//
public bool IsExplicitImpl;
+ protected bool is_external_implementation;
+
//
// The interface type we are explicitly implementing
//
@@ -3924,6 +3925,19 @@
return true;
}
+ public override void Emit()
+ {
+ // for extern static method must be specified either
DllImport attribute or MethodImplAttribute.
+ // We are more strict than Microsoft and report CS0626
as error
+ if ((ModFlags & Modifiers.EXTERN) != 0 &&
!is_external_implementation) {
+ Report.Error (626, Location,
+ "`{0}' is marked as an external but has
no DllImport attribute. Consider adding a DllImport attribute to specify the
external implementation",
+ GetSignatureForError ());
+ }
+
+ base.Emit ();
+ }
+
protected void Error_CannotChangeAccessModifiers (MemberInfo
base_method, MethodAttributes ma, string suffix)
{
Report.SymbolRelatedToPreviousError (base_method);
@@ -4020,9 +4034,9 @@
return;
}
- if (a.Type == TypeManager.methodimpl_attr_type &&
- (a.GetMethodImplOptions () &
MethodImplOptions.InternalCall) != 0) {
+ if (a.IsInternalMethodImplAttribute) {
MethodBuilder.SetImplementationFlags
(MethodImplAttributes.InternalCall | MethodImplAttributes.Runtime);
+ is_external_implementation = true;
}
if (a.Type == TypeManager.dllimport_type) {
@@ -4030,8 +4044,7 @@
if ((ModFlags & extern_static) !=
extern_static) {
Report.Error (601, a.Location, "The
DllImport attribute must be specified on a method marked `static' and
`extern'");
}
-
- return;
+ is_external_implementation = true;
}
if (a.Type.IsSubclassOf
(TypeManager.security_attr_type) && a.CheckSecurityActionValidity (false)) {
@@ -4173,7 +4186,7 @@
public CallingConventions CallingConventions {
get {
CallingConventions cc =
Parameters.CallingConvention;
- if (Parameters.HasArglist)
+ if (Parameters.HasArglist && block != null)
block.HasVarargs = true;
if (!IsInterface)
@@ -4828,9 +4841,9 @@
return;
}
- if (a.Type == TypeManager.methodimpl_attr_type &&
- (a.GetMethodImplOptions () &
MethodImplOptions.InternalCall) != 0) {
+ if (a.IsInternalMethodImplAttribute) {
ConstructorBuilder.SetImplementationFlags
(MethodImplAttributes.InternalCall | MethodImplAttributes.Runtime);
+ is_external_implementation = true;
}
ConstructorBuilder.SetCustomAttribute (cb);
@@ -5431,30 +5444,6 @@
/// </summary>
void DefineMethodBuilder (TypeContainer container, string
method_name, Type[] ParameterTypes)
{
- if ((modifiers & Modifiers.EXTERN) != 0) {
- if (method.OptAttributes != null) {
- Attribute dllimport_attribute =
method.OptAttributes.Search (TypeManager.dllimport_type);
- if (dllimport_attribute != null) {
- flags |=
MethodAttributes.PinvokeImpl;
- builder =
dllimport_attribute.DefinePInvokeMethod (
- container.TypeBuilder,
method_name, flags,
- method, ParameterTypes);
-
- return;
- }
- }
-
- // for extern static method must be specified
either DllImport attribute or MethodImplAttribute.
- // We are more strict than Microsoft and report
CS0626 like error
- if (method.OptAttributes == null ||
- !method.OptAttributes.Contains
(TypeManager.methodimpl_attr_type)) {
- Report.Error (626, method.Location,
- "`{0}' is marked as an external
but has no DllImport attribute. Consider adding a DllImport attribute to
specify the external implementation",
- member.GetSignatureForError ());
- return;
- }
- }
-
if (builder == null) {
builder = container.TypeBuilder.DefineMethod (
method_name, flags,
method.CallingConventions,
@@ -5474,8 +5463,10 @@
//
public void Emit (DeclSpace parent)
{
+ ToplevelBlock block = method.Block;
+
EmitContext ec;
- if ((flags & MethodAttributes.PinvokeImpl) == 0)
+ if (block != null)
ec = method.CreateEmitContext (parent,
builder.GetILGenerator ());
else
ec = method.CreateEmitContext (parent, null);
@@ -5484,8 +5475,6 @@
if (GenericMethod != null)
GenericMethod.EmitAttributes ();
-
- ToplevelBlock block = method.Block;
SourceMethod source = SourceMethod.Create (parent,
MethodBuilder, method.Block);
@@ -6569,6 +6558,16 @@
get { return iterator; }
}
+ public override void ApplyAttributeBuilder (Attribute
a, CustomAttributeBuilder cb)
+ {
+ if (a.IsInternalMethodImplAttribute) {
+
method_data.MethodBuilder.SetImplementationFlags
(MethodImplAttributes.InternalCall | MethodImplAttributes.Runtime);
+ method.is_external_implementation =
true;
+ }
+
+ base.ApplyAttributeBuilder (a, cb);
+ }
+
public override AttributeTargets AttributeTargets {
get {
return AttributeTargets.Method;
@@ -7383,6 +7382,16 @@
get { return null; }
}
+ public override void ApplyAttributeBuilder (Attribute
a, CustomAttributeBuilder cb)
+ {
+ if (a.IsInternalMethodImplAttribute) {
+
method_data.MethodBuilder.SetImplementationFlags
(MethodImplAttributes.InternalCall | MethodImplAttributes.Runtime);
+ method.is_external_implementation =
true;
+ }
+
+ base.ApplyAttributeBuilder (a, cb);
+ }
+
protected override void ApplyToExtraTarget(Attribute a,
CustomAttributeBuilder cb)
{
if (a.Target == AttributeTargets.Parameter) {
Modified: trunk/mcs/mcs/codegen.cs
===================================================================
--- trunk/mcs/mcs/codegen.cs 2007-02-25 19:41:15 UTC (rev 73413)
+++ trunk/mcs/mcs/codegen.cs 2007-02-25 21:55:26 UTC (rev 73414)
@@ -1415,6 +1415,7 @@
// TODO: make it private and move all builder based methods here
public ModuleBuilder Builder;
bool m_module_is_unsafe;
+ bool has_default_charset;
public CharSet DefaultCharSet = CharSet.Ansi;
public TypeAttributes DefaultCharSetType =
TypeAttributes.AnsiClass;
@@ -1468,6 +1469,12 @@
Builder.SetCustomAttribute (customBuilder);
}
+ public bool HasDefaultCharSet {
+ get {
+ return has_default_charset;
+ }
+ }
+
/// <summary>
/// It is called very early therefore can resolve only
predefined attributes
/// </summary>
@@ -1482,6 +1489,7 @@
Attribute a = ResolveAttribute
(TypeManager.default_charset_type);
if (a != null) {
+ has_default_charset = true;
DefaultCharSet = a.GetCharSetValue ();
switch (DefaultCharSet) {
case CharSet.Ansi:
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches