using System;
using System.Diagnostics;
-using System.Collections;
-using System.Collections.Specialized;
+using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
object [] prop_values_arr;
object [] pos_values;
- static PtrHashtable usage_attr_cache;
+ static Dictionary<Type, AttributeUsageAttribute> usage_attr_cache;
// Cache for parameter-less attributes
- static PtrHashtable att_cache;
+ static Dictionary<Type, CustomAttributeBuilder> att_cache;
public Attribute (string target, ATypeNameExpression expr, Arguments[] args, Location loc, bool nameEscaped)
{
public static void Reset ()
{
- usage_attr_cache = new PtrHashtable ();
- att_cache = new PtrHashtable ();
+ usage_attr_cache = new Dictionary<Type, AttributeUsageAttribute> (ReferenceEquality<Type>.Default);
+ att_cache = new Dictionary<Type, CustomAttributeBuilder> (ReferenceEquality<Type>.Default);
}
//
static void Error_InvalidNamedArgument (ResolveContext rc, NamedArgument name)
{
- rc.Report.Error (617, name.Name.Location, "`{0}' is not a valid named attribute argument. Named attribute arguments " +
+ rc.Report.Error (617, name.Location, "`{0}' is not a valid named attribute argument. Named attribute arguments " +
"must be fields which are not readonly, static, const or read-write properties which are " +
"public and not static",
- name.Name.Value);
+ name.Name);
}
static void Error_InvalidNamedArgumentType (ResolveContext rc, NamedArgument name)
{
- rc.Report.Error (655, name.Name.Location,
+ rc.Report.Error (655, name.Location,
"`{0}' is not a valid named attribute argument because it is not a valid attribute parameter type",
- name.Name.Value);
+ name.Name);
}
public static void Error_AttributeArgumentNotValid (ResolveContext rc, Location loc)
get { return expression.Name; }
}
- void ApplyModuleCharSet ()
+ void ApplyModuleCharSet (ResolveContext rc)
{
if (Type != PredefinedAttributes.Get.DllImport)
return;
NamedArguments = new Arguments (1);
} else {
foreach (NamedArgument a in NamedArguments) {
- if (a.Name.Value == CharSetEnumMember)
+ if (a.Name == CharSetEnumMember)
return;
}
}
- NamedArguments.Add (new NamedArgument (new LocatedToken (loc, CharSetEnumMember),
- Constant.CreateConstant (typeof (CharSet), RootContext.ToplevelTypes.DefaultCharSet, Location)));
+ NamedArguments.Add (new NamedArgument (CharSetEnumMember, loc,
+ Constant.CreateConstant (rc, typeof (CharSet), RootContext.ToplevelTypes.DefaultCharSet, Location)));
}
- Report Report {
+ public Report Report {
get { return context.Compiler.Report; }
}
AttributeTester.Report_ObsoleteMessage (obsolete_attr, TypeManager.CSharpName (Type), Location, Report);
}
+ CustomAttributeBuilder cb;
if (PosArguments == null && NamedArguments == null) {
- object o = att_cache [Type];
- if (o != null) {
+ if (att_cache.TryGetValue (Type, out cb)) {
resolve_error = false;
- return (CustomAttributeBuilder)o;
+ return cb;
}
}
ResolveContext rc = new ResolveContext (context, ResolveContext.Options.ConstantScope);
- ConstructorInfo ctor = ResolveConstructor (rc);
+ var ctor = ResolveConstructor (rc);
if (ctor == null) {
if (Type is TypeBuilder &&
TypeManager.LookupDeclSpace (Type).MemberCache == null)
return null;
}
- ApplyModuleCharSet ();
+ ApplyModuleCharSet (rc);
- CustomAttributeBuilder cb;
try {
+ var ctor_meta = (ConstructorInfo) ctor.MetaInfo;
// SRE does not allow private ctor but we want to report all source code errors
- if (ctor.IsPrivate)
+ if (ctor.MetaInfo.IsPrivate)
return null;
if (NamedArguments == null) {
- cb = new CustomAttributeBuilder (ctor, pos_values);
+ cb = new CustomAttributeBuilder (ctor_meta, pos_values);
if (pos_values.Length == 0)
att_cache.Add (Type, cb);
return null;
}
- cb = new CustomAttributeBuilder (ctor, pos_values,
+ cb = new CustomAttributeBuilder (ctor_meta, pos_values,
prop_info_arr, prop_values_arr,
field_info_arr, field_values_arr);
}
}
- protected virtual ConstructorInfo ResolveConstructor (ResolveContext ec)
+ protected virtual MethodSpec ResolveConstructor (ResolveContext ec)
{
if (PosArguments != null) {
bool dynamic;
if (mg == null)
return null;
- ConstructorInfo constructor = (ConstructorInfo)mg;
+ var constructor = (MethodSpec) mg;
if (PosArguments == null) {
pos_values = EmptyObject;
return constructor;
}
- AParametersCollection pd = TypeManager.GetParameterData (constructor);
-
if (!PosArguments.GetAttributableValue (ec, out pos_values))
return null;
}
if (Type == pa.MethodImpl && pos_values.Length == 1 &&
- pd.Types [0] == TypeManager.short_type &&
+ constructor.Parameters.Types [0] == TypeManager.short_type &&
!System.Enum.IsDefined (typeof (MethodImplOptions), pos_values [0].ToString ())) {
Error_AttributeEmitError ("Incorrect argument value.");
return null;
{
int named_arg_count = NamedArguments.Count;
- ArrayList field_infos = new ArrayList (named_arg_count);
- ArrayList prop_infos = new ArrayList (named_arg_count);
- ArrayList field_values = new ArrayList (named_arg_count);
- ArrayList prop_values = new ArrayList (named_arg_count);
+ var field_infos = new List<FieldInfo> (named_arg_count);
+ var prop_infos = new List<PropertyInfo> (named_arg_count);
+ var field_values = new List<object> (named_arg_count);
+ var prop_values = new List<object> (named_arg_count);
- ArrayList seen_names = new ArrayList (named_arg_count);
+ var seen_names = new List<string> (named_arg_count);
foreach (NamedArgument a in NamedArguments) {
- string name = a.Name.Value;
+ string name = a.Name;
if (seen_names.Contains (name)) {
- ec.Report.Error (643, a.Name.Location, "Duplicate named attribute `{0}' argument", name);
+ ec.Report.Error (643, a.Location, "Duplicate named attribute `{0}' argument", name);
continue;
}
prop_infos.Add (pi);
} else {
- FieldInfo fi = ((FieldExpr) member).FieldInfo;
+ var fi = ((FieldExpr) member).Spec;
- if (fi.IsInitOnly || fi.IsStatic) {
+ if (fi.IsReadOnly || fi.IsStatic) {
Error_InvalidNamedArgument (ec, a);
return false;
}
if (!IsValidArgumentType (member.Type)) {
- ec.Report.SymbolRelatedToPreviousError (fi);
+ ec.Report.SymbolRelatedToPreviousError (fi.MetaInfo);
Error_InvalidNamedArgumentType (ec, a);
return false;
}
if (!a.Expr.GetAttributableValue (ec, member.Type, out value))
return false;
- FieldBase fb = TypeManager.GetField (fi);
+ FieldBase fb = TypeManager.GetField (fi.MetaInfo);
if (fb != null)
obsolete_attr = fb.GetObsoleteAttribute ();
else
- obsolete_attr = AttributeTester.GetMemberObsoleteAttribute (fi);
+ obsolete_attr = AttributeTester.GetMemberObsoleteAttribute (fi.MetaInfo);
- field_values.Add (value);
- field_infos.Add (fi);
+ field_values.Add (value);
+ field_infos.Add (fi.MetaInfo);
}
if (obsolete_attr != null && !context.IsObsolete)
/// </summary>
static AttributeUsageAttribute GetAttributeUsage (Type type)
{
- AttributeUsageAttribute ua = usage_attr_cache [type] as AttributeUsageAttribute;
- if (ua != null)
+ AttributeUsageAttribute ua;
+ if (usage_attr_cache.TryGetValue (type, out ua))
return ua;
Class attr_class = TypeManager.LookupClass (type);
}
// TODO: we can skip the first item
- if (((IList) valid_targets).Contains (ExplicitTarget)) {
+ if (Array.Exists (valid_targets, i => i == ExplicitTarget)) {
switch (ExplicitTarget) {
case "return": Target = AttributeTargets.ReturnValue; return true;
case "param": Target = AttributeTargets.Parameter; return true;
/// Creates instance of SecurityAttribute class and add result of CreatePermission method to permission table.
/// </summary>
/// <returns></returns>
- public void ExtractSecurityPermissionSet (ListDictionary permissions)
+ public void ExtractSecurityPermissionSet (Dictionary<SecurityAction, PermissionSet> permissions)
{
Type orig_assembly_type = null;
}
}
- PermissionSet ps = (PermissionSet)permissions [action];
- if (ps == null) {
+ PermissionSet ps;
+ if (!permissions.TryGetValue (action, out ps)) {
if (sa is PermissionSetAttribute)
ps = new PermissionSet (sa.Unrestricted ? PermissionState.Unrestricted : PermissionState.None);
else
public override int GetHashCode ()
{
- return base.GetHashCode ();
+ return type.GetHashCode () ^ Target.GetHashCode ();
}
/// <summary>
/// Emit attribute for Attributable symbol
/// </summary>
- public void Emit (ListDictionary allEmitted)
+ public void Emit (Dictionary<Attribute, List<Attribute>> allEmitted)
{
CustomAttributeBuilder cb = Resolve ();
if (cb == null)
}
if (!usage_attr.AllowMultiple && allEmitted != null) {
- if (allEmitted.Contains (this)) {
- ArrayList a = allEmitted [this] as ArrayList;
+ if (allEmitted.ContainsKey (this)) {
+ var a = allEmitted [this];
if (a == null) {
- a = new ArrayList (2);
+ a = new List<Attribute> (2);
allEmitted [this] = a;
}
a.Add (this);
throw new NotSupportedException ("ET");
}
- public override Expression DoResolve (ResolveContext ec)
+ protected override Expression DoResolve (ResolveContext ec)
{
throw new NotImplementedException ();
}
}
}
- protected override ConstructorInfo ResolveConstructor (ResolveContext ec)
+ protected override MethodSpec ResolveConstructor (ResolveContext ec)
{
try {
Enter ();
}
public class Attributes {
- public readonly ArrayList Attrs;
+ public readonly List<Attribute> Attrs;
public Attributes (Attribute a)
{
- Attrs = new ArrayList ();
+ Attrs = new List<Attribute> ();
Attrs.Add (a);
}
- public Attributes (ArrayList attrs)
+ public Attributes (List<Attribute> attrs)
{
Attrs = attrs;
}
- public void AddAttributes (ArrayList attrs)
+ public void AddAttributes (List<Attribute> attrs)
{
Attrs.AddRange (attrs);
}
public Attributes Clone ()
{
- ArrayList al = new ArrayList (Attrs.Count);
+ var al = new List<Attribute> (Attrs.Count);
foreach (Attribute a in Attrs)
al.Add (a.Clone ());
/// </summary>
public Attribute[] SearchMulti (PredefinedAttribute t)
{
- ArrayList ar = null;
+ List<Attribute> ar = null;
foreach (Attribute a in Attrs) {
if (a.ResolveType () == t) {
if (ar == null)
- ar = new ArrayList ();
+ ar = new List<Attribute> (Attrs.Count);
ar.Add (a);
}
}
- return ar == null ? null : ar.ToArray (typeof (Attribute)) as Attribute[];
+ return ar == null ? null : ar.ToArray ();
}
public void Emit ()
{
CheckTargets ();
- ListDictionary ld = Attrs.Count > 1 ? new ListDictionary () : null;
+ Dictionary<Attribute, List<Attribute>> ld = Attrs.Count > 1 ? new Dictionary<Attribute, List<Attribute>> () : null;
foreach (Attribute a in Attrs)
a.Emit (ld);
if (ld == null || ld.Count == 0)
return;
- foreach (DictionaryEntry d in ld) {
+ foreach (var d in ld) {
if (d.Value == null)
continue;
- Report report = RootContext.ToplevelTypes.Compiler.Report;
- foreach (Attribute collision in (ArrayList)d.Value)
- report.SymbolRelatedToPreviousError (collision.Location, "");
+ Attribute a = d.Key;
- Attribute a = (Attribute)d.Key;
- report.Error (579, a.Location, "The attribute `{0}' cannot be applied multiple times",
+ foreach (Attribute collision in d.Value)
+ a.Report.SymbolRelatedToPreviousError (collision.Location, "");
+
+ a.Report.Error (579, a.Location, "The attribute `{0}' cannot be applied multiple times",
a.GetSignatureForError ());
}
}
/// </summary>
sealed class AttributeTester
{
- static PtrHashtable analyzed_types;
- static PtrHashtable analyzed_types_obsolete;
- static PtrHashtable analyzed_member_obsolete;
- static PtrHashtable analyzed_method_excluded;
- static PtrHashtable fixed_buffer_cache;
-
- static object TRUE = new object ();
- static object FALSE = new object ();
+ static Dictionary<Type, bool> analyzed_types;
+ static Dictionary<Type, ObsoleteAttribute> analyzed_types_obsolete;
+ static Dictionary<MemberInfo, ObsoleteAttribute> analyzed_member_obsolete;
+ static Dictionary<MethodBase, bool> analyzed_method_excluded;
+// static Dictionary<FieldInfo, IFixedBuffer> fixed_buffer_cache;
static AttributeTester ()
{
public static void Reset ()
{
- analyzed_types = new PtrHashtable ();
- analyzed_types_obsolete = new PtrHashtable ();
- analyzed_member_obsolete = new PtrHashtable ();
- analyzed_method_excluded = new PtrHashtable ();
- fixed_buffer_cache = new PtrHashtable ();
+ analyzed_types = new Dictionary<Type, bool> (ReferenceEquality<Type>.Default);
+ analyzed_types_obsolete = new Dictionary<Type, ObsoleteAttribute> (ReferenceEquality<Type>.Default);
+ analyzed_member_obsolete = new Dictionary<MemberInfo, ObsoleteAttribute> (ReferenceEquality<MemberInfo>.Default);
+ analyzed_method_excluded = new Dictionary<MethodBase, bool> (ReferenceEquality<MethodBase>.Default);
+// fixed_buffer_cache = new Dictionary<FieldInfo, IFixedBuffer> (ReferenceEquality<FieldInfo>.Default);
}
public enum Result {
if (type == null)
return true;
- object type_compliance = analyzed_types[type];
- if (type_compliance != null)
- return type_compliance == TRUE;
+ bool result;
+ if (analyzed_types.TryGetValue (type, out result))
+ return result;
if (type.IsPointer) {
- analyzed_types.Add (type, FALSE);
+ analyzed_types.Add (type, false);
return false;
}
- bool result;
if (type.IsArray) {
result = IsClsCompliant (TypeManager.GetElementType (type));
} else if (TypeManager.IsNullableType (type)) {
} else {
result = AnalyzeTypeCompliance (type);
}
- analyzed_types.Add (type, result ? TRUE : FALSE);
+ analyzed_types.Add (type, result);
return result;
}
-
- /// <summary>
- /// Returns IFixedBuffer implementation if field is fixed buffer else null.
- /// </summary>
- public static IFixedBuffer GetFixedBuffer (FieldInfo fi)
- {
- // Fixed buffer helper type is generated as value type
- if (TypeManager.IsReferenceType (fi.FieldType))
- return null;
-
- FieldBase fb = TypeManager.GetField (fi);
- if (fb != null) {
- return fb as IFixedBuffer;
- }
-
- if (TypeManager.GetConstant (fi) != null)
- return null;
-
- object o = fixed_buffer_cache [fi];
- if (o == null) {
- PredefinedAttribute pa = PredefinedAttributes.Get.FixedBuffer;
- if (!pa.IsDefined)
- return null;
-
- if (!fi.IsDefined (pa.Type, false)) {
- fixed_buffer_cache.Add (fi, FALSE);
- return null;
- }
-
- IFixedBuffer iff = new FixedFieldExternal (fi);
- fixed_buffer_cache.Add (fi, iff);
- return iff;
- }
-
- if (o == FALSE)
- return null;
-
- return (IFixedBuffer)o;
- }
public static void VerifyModulesClsCompliance (CompilerContext ctx)
{
/// </summary>
public static ObsoleteAttribute GetObsoleteAttribute (Type type)
{
- object type_obsolete = analyzed_types_obsolete [type];
- if (type_obsolete == FALSE)
- return null;
+ ObsoleteAttribute result;
+ if (analyzed_types_obsolete.TryGetValue (type, out result))
+ return result;
- if (type_obsolete != null)
- return (ObsoleteAttribute)type_obsolete;
-
- ObsoleteAttribute result = null;
if (TypeManager.HasElementType (type)) {
result = GetObsoleteAttribute (TypeManager.GetElementType (type));
} else if (TypeManager.IsGenericParameter (type))
}
// Cannot use .Add because of corlib bootstrap
- analyzed_types_obsolete [type] = result == null ? FALSE : result;
+ analyzed_types_obsolete [type] = result;
return result;
}
/// </summary>
public static ObsoleteAttribute GetMemberObsoleteAttribute (MemberInfo mi)
{
- object type_obsolete = analyzed_member_obsolete [mi];
- if (type_obsolete == FALSE)
- return null;
-
- if (type_obsolete != null)
- return (ObsoleteAttribute)type_obsolete;
+ ObsoleteAttribute oa;
+ if (analyzed_member_obsolete.TryGetValue (mi, out oa))
+ return oa;
if ((mi.DeclaringType is TypeBuilder) || TypeManager.IsGenericType (mi.DeclaringType))
return null;
if (!pa.IsDefined)
return null;
- ObsoleteAttribute oa = System.Attribute.GetCustomAttribute (mi, pa.Type, false)
- as ObsoleteAttribute;
- analyzed_member_obsolete.Add (mi, oa == null ? FALSE : oa);
+ oa = System.Attribute.GetCustomAttribute (mi, pa.Type, false) as ObsoleteAttribute;
+ analyzed_member_obsolete.Add (mi, oa);
return oa;
}
public static bool IsConditionalMethodExcluded (MethodBase mb, Location loc)
{
- object excluded = analyzed_method_excluded [mb];
- if (excluded != null)
- return excluded == TRUE ? true : false;
+ bool excluded;
+ if (analyzed_method_excluded.TryGetValue (mb, out excluded))
+ return excluded;
PredefinedAttribute pa = PredefinedAttributes.Get.Conditional;
if (!pa.IsDefined)
ConditionalAttribute[] attrs = mb.GetCustomAttributes (pa.Type, true)
as ConditionalAttribute[];
if (attrs.Length == 0) {
- analyzed_method_excluded.Add (mb, FALSE);
+ analyzed_method_excluded.Add (mb, false);
return false;
}
foreach (ConditionalAttribute a in attrs) {
if (loc.CompilationUnit.IsConditionalDefined (a.ConditionString)) {
- analyzed_method_excluded.Add (mb, FALSE);
+ analyzed_method_excluded.Add (mb, false);
return false;
}
}
- analyzed_method_excluded.Add (mb, TRUE);
+ analyzed_method_excluded.Add (mb, true);
return true;
}
// New in .NET 4.0
public readonly PredefinedAttribute Dynamic;
+ public readonly PredefinedAttribute DynamicTransform; // DynamicAttribute with transform arguments
//
// Optional types which are used as types and for member lookup
Extension = new PredefinedAttribute ("System.Runtime.CompilerServices", "ExtensionAttribute");
Dynamic = new PredefinedAttribute ("System.Runtime.CompilerServices", "DynamicAttribute");
+ DynamicTransform = new PredefinedAttribute ("System.Runtime.CompilerServices", "DynamicAttribute");
DefaultMember = new PredefinedAttribute ("System.Reflection", "DefaultMemberAttribute");
DecimalConstant = new PredefinedAttribute ("System.Runtime.CompilerServices", "DecimalConstantAttribute");
FieldOffset = new PredefinedAttribute ("System.Runtime.InteropServices", "FieldOffsetAttribute");
}
- public void Initialize ()
+ public void Initialize (CompilerContext ctx)
{
foreach (FieldInfo fi in GetType ().GetFields (BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) {
- ((PredefinedAttribute) fi.GetValue (this)).Resolve (true);
+ ((PredefinedAttribute) fi.GetValue (this)).Initialize (ctx, true);
}
}
CustomAttributeBuilder cab;
ConstructorInfo ctor;
readonly string ns, name;
+ CompilerContext compiler;
static readonly Type NotFound = typeof (PredefinedAttribute);
get { return type != null && type != NotFound; }
}
+ public void Initialize (CompilerContext ctx, bool canFail)
+ {
+ this.compiler = ctx;
+ Resolve (canFail);
+ }
+
public bool Resolve (bool canFail)
{
if (type != null) {
return false;
}
- type = TypeManager.CoreLookupType (RootContext.ToplevelTypes.Compiler, ns, name, Kind.Class, !canFail);
+ type = TypeManager.CoreLookupType (compiler, ns, name, MemberKind.Class, !canFail);
if (type == null) {
type = NotFound;
return false;