public abstract string[] ValidAttributeTargets { get; }
};
- public class Attribute {
+ public class Attribute : Expression
+ {
public readonly string ExplicitTarget;
public AttributeTargets Target;
public readonly Expression LeftExpr;
public readonly string Identifier;
- readonly ArrayList PosArguments;
+ ArrayList PosArguments;
ArrayList NamedArguments;
- public readonly Location Location;
-
- public Type Type;
-
bool resolve_error;
readonly bool nameEscaped;
// It can contain more onwers when the attribute is applied to multiple fiels.
- Attributable[] owners;
+ protected Attributable[] owners;
static readonly AttributeUsageAttribute DefaultUsageAttribute = new AttributeUsageAttribute (AttributeTargets.All);
static Assembly orig_sec_assembly;
PosArguments = (ArrayList)args [0];
NamedArguments = (ArrayList)args [1];
}
- Location = loc;
+ this.loc = loc;
ExplicitTarget = target;
this.nameEscaped = nameEscaped;
}
att_cache = new PtrHashtable ();
}
- public void AttachTo (Attributable owner)
+ public virtual void AttachTo (Attributable owner)
{
if (this.owners == null) {
this.owners = new Attributable[1] { owner };
return Type;
}
- public string GetSignatureForError ()
+ public override string GetSignatureForError ()
{
if (Type != null)
return TypeManager.CSharpName (Type);
return LeftExpr == null ? Identifier : LeftExpr.GetSignatureForError () + "." + Identifier;
}
- bool IsValidArgumentType (Type t)
+ public bool HasSecurityAttribute {
+ get {
+ return TypeManager.security_attr_type != null &&
+ TypeManager.IsSubclassOf (type, TypeManager.security_attr_type);
+ }
+ }
+
+ public bool IsValidSecurityAttribute ()
+ {
+ return HasSecurityAttribute && IsSecurityActionValid (false);
+ }
+
+ static bool IsValidArgumentType (Type t)
{
if (t.IsArray)
- t = t.GetElementType ();
+ t = TypeManager.GetElementType (t);
- return TypeManager.IsPrimitiveType (t) ||
+ return t == TypeManager.string_type ||
+ TypeManager.IsPrimitiveType (t) ||
TypeManager.IsEnumType (t) ||
- t == TypeManager.string_type ||
t == TypeManager.object_type ||
t == TypeManager.type_type;
}
}
Attributable owner = Owner;
- EmitContext ec = new EmitContext (owner.ResolveContext, owner.ResolveContext.DeclContainer, owner.ResolveContext.DeclContainer,
- Location, null, null, owner.ResolveContext.DeclContainer.ModFlags, false);
+ DeclSpace ds = owner.ResolveContext as DeclSpace;
+ if (ds == null)
+ ds = owner.ResolveContext.DeclContainer;
+
+ EmitContext ec = new EmitContext (owner.ResolveContext, ds, owner.ResolveContext.DeclContainer,
+ Location, null, typeof (Attribute), owner.ResolveContext.DeclContainer.ModFlags, false);
ec.IsAnonymousMethodAllowed = false;
ConstructorInfo ctor = ResolveConstructor (ec);
CustomAttributeBuilder cb;
try {
+ // SRE does not allow private ctor but we want to report all source code errors
+ if (ctor.IsPrivate)
+ return null;
+
if (NamedArguments == null) {
cb = new CustomAttributeBuilder (ctor, pos_values);
return null;
}
}
-
- Expression mg = Expression.MemberLookup (ec.ContainerType,
+
+ MethodGroupExpr mg = MemberLookupFinal (ec, ec.ContainerType,
Type, ".ctor", MemberTypes.Constructor,
BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
- Location);
+ Location) as MethodGroupExpr;
if (mg == null)
return null;
- MethodBase constructor = ((MethodGroupExpr)mg).OverloadResolve (
- ec, PosArguments, false, Location);
-
- if (constructor == null)
+ mg = mg.OverloadResolve (ec, ref PosArguments, false, Location);
+ if (mg == null)
return null;
+
+ ConstructorInfo constructor = (ConstructorInfo)mg;
+ // TODO: move to OverloadResolve
ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (constructor);
if (oa != null && !Owner.ResolveContext.IsInObsoleteScope) {
AttributeTester.Report_ObsoleteMessage (oa, mg.GetSignatureForError (), mg.Location);
if (PosArguments == null) {
pos_values = EmptyObject;
- return (ConstructorInfo)constructor;
+ return constructor;
}
ParameterData pd = TypeManager.GetParameterData (constructor);
int pos_arg_count = PosArguments.Count;
- int last_real_param = pd.Count;
-
pos_values = new object [pos_arg_count];
-
- if (pd.HasParams) {
- // When the params is not filled we need to put one
- if (last_real_param > pos_arg_count) {
- object [] new_pos_values = new object [pos_arg_count + 1];
- pos_values.CopyTo (new_pos_values, 0);
- new_pos_values [pos_arg_count] = new object [] {} ;
- pos_values = new_pos_values;
- }
- last_real_param--;
- }
-
for (int j = 0; j < pos_arg_count; ++j) {
Argument a = (Argument) PosArguments [j];
if (!a.Expr.GetAttributableValue (a.Type, out pos_values [j]))
return null;
-
- if (j < last_real_param)
- continue;
-
- if (j == last_real_param) {
- object [] array = new object [pos_arg_count - last_real_param];
- array [0] = pos_values [j];
- pos_values [j] = array;
- continue;
- }
-
- object [] params_array = (object []) pos_values [last_real_param];
- params_array [j - last_real_param] = pos_values [j];
- }
-
- // Adjust the size of the pos_values if it had params
- if (last_real_param != pos_arg_count) {
- object [] new_pos_values = new object [last_real_param + 1];
- Array.Copy (pos_values, new_pos_values, last_real_param + 1);
- pos_values = new_pos_values;
}
// Here we do the checks which should be done by corlib or by runtime.
return null;
}
- return (ConstructorInfo)constructor;
+ return constructor;
}
protected virtual bool ResolveNamedArguments (EmitContext ec)
return false;
}
- if (!IsValidArgumentType (pi.PropertyType)) {
+ if (!IsValidArgumentType (member.Type)) {
Report.SymbolRelatedToPreviousError (pi);
Error_InvalidNamedAgrumentType (member_name);
return false;
}
object value;
- if (!a.Expr.GetAttributableValue (pi.PropertyType, out value))
+ if (!a.Expr.GetAttributableValue (member.Type, out value))
return false;
PropertyBase pb = TypeManager.GetProperty (pi);
return false;
}
- if (!IsValidArgumentType (fi.FieldType)) {
+ if (!IsValidArgumentType (member.Type)) {
Report.SymbolRelatedToPreviousError (fi);
Error_InvalidNamedAgrumentType (member_name);
return false;
}
object value;
- if (!a.Expr.GetAttributableValue (fi.FieldType, out value))
+ if (!a.Expr.GetAttributableValue (member.Type, out value))
return false;
FieldBase fb = TypeManager.GetField (fi);
public string GetValidTargets ()
{
StringBuilder sb = new StringBuilder ();
- AttributeTargets targets = GetAttributeUsage ().ValidOn;
+ AttributeTargets targets = GetAttributeUsage (Type).ValidOn;
if ((targets & AttributeTargets.Assembly) != 0)
sb.Append ("assembly, ");
}
/// <summary>
- /// Returns AttributeUsage attribute for this type
+ /// Returns AttributeUsage attribute based on types hierarchy
/// </summary>
- AttributeUsageAttribute GetAttributeUsage ()
+ static AttributeUsageAttribute GetAttributeUsage (Type type)
{
- AttributeUsageAttribute ua = usage_attr_cache [Type] as AttributeUsageAttribute;
+ AttributeUsageAttribute ua = usage_attr_cache [type] as AttributeUsageAttribute;
if (ua != null)
return ua;
- Class attr_class = TypeManager.LookupClass (Type);
+ Class attr_class = TypeManager.LookupClass (type);
if (attr_class == null) {
- object[] usage_attr = Type.GetCustomAttributes (TypeManager.attribute_usage_type, true);
+ object[] usage_attr = type.GetCustomAttributes (TypeManager.attribute_usage_type, true);
ua = (AttributeUsageAttribute)usage_attr [0];
- usage_attr_cache.Add (Type, ua);
+ usage_attr_cache.Add (type, ua);
return ua;
}
- Attribute a = attr_class.OptAttributes == null
- ? null
- : attr_class.OptAttributes.Search (TypeManager.attribute_usage_type);
+ Attribute a = null;
+ if (attr_class.OptAttributes != null)
+ a = attr_class.OptAttributes.Search (TypeManager.attribute_usage_type);
- ua = a == null
- ? DefaultUsageAttribute
- : a.GetAttributeUsageAttribute ();
+ if (a == null) {
+ if (attr_class.TypeBuilder.BaseType != TypeManager.attribute_type)
+ ua = GetAttributeUsage (attr_class.TypeBuilder.BaseType);
+ else
+ ua = DefaultUsageAttribute;
+ } else {
+ ua = a.GetAttributeUsageAttribute ();
+ }
- usage_attr_cache.Add (Type, ua);
+ usage_attr_cache.Add (type, ua);
return ua;
}
/// <summary>
/// Tests permitted SecurityAction for assembly or other types
/// </summary>
- public bool CheckSecurityActionValidity (bool for_assembly)
+ protected virtual bool IsSecurityActionValid (bool for_assembly)
{
SecurityAction action = GetSecurityActionValue ();
if (cb == null)
return;
- AttributeUsageAttribute usage_attr = GetAttributeUsage ();
+ AttributeUsageAttribute usage_attr = GetAttributeUsage (Type);
if ((usage_attr.ValidOn & Target) == 0) {
Report.Error (592, Location, "The attribute `{0}' is not valid on this declaration type. " +
"It is valid on `{1}' declarations only",
public string GetString ()
{
Expression e = GetValue ();
- if (e is StringLiteral)
- return (e as StringLiteral).Value;
+ if (e is StringConstant)
+ return ((StringConstant)e).Value;
return null;
}
public bool GetBoolean ()
{
Expression e = GetValue ();
- if (e is BoolLiteral)
- return (e as BoolLiteral).Value;
+ if (e is BoolConstant)
+ return ((BoolConstant)e).Value;
return false;
}
return null;
return e.TypeArgument;
}
+
+ public override Expression DoResolve (EmitContext ec)
+ {
+ throw new NotImplementedException ();
+ }
+
+ public override void Emit (EmitContext ec)
+ {
+ throw new NotImplementedException ();
+ }
}
base (target, left_expr, identifier, args, loc, nameEscaped)
{
this.ns = ns;
+ this.owners = new Attributable[1];
+ }
+
+ public override void AttachTo (Attributable owner)
+ {
+ if (ExplicitTarget == "assembly") {
+ owners [0] = CodeGen.Assembly;
+ return;
+ }
+ if (ExplicitTarget == "module") {
+ owners [0] = CodeGen.Module;
+ return;
+ }
+ throw new NotImplementedException ("Unknown global explicit target " + ExplicitTarget);
}
void Enter ()
RootContext.ToplevelTypes.NamespaceEntry = ns;
}
+ protected override bool IsSecurityActionValid (bool for_assembly)
+ {
+ return base.IsSecurityActionValid (true);
+ }
+
void Leave ()
{
RootContext.ToplevelTypes.NamespaceEntry = null;
Report.SymbolRelatedToPreviousError (collision.Location, "");
Attribute a = (Attribute)d.Key;
- Report.Error (579, a.Location, "The attribute `{0}' cannot be applied multiple times", a.GetSignatureForError ());
+ Report.Error (579, a.Location, "The attribute `{0}' cannot be applied multiple times",
+ a.GetSignatureForError ());
}
}
static PtrHashtable analyzed_types_obsolete;
static PtrHashtable analyzed_member_obsolete;
static PtrHashtable analyzed_method_excluded;
-
-#if NET_2_0
static PtrHashtable fixed_buffer_cache;
-#endif
static object TRUE = new object ();
static object FALSE = new object ();
analyzed_types_obsolete = new PtrHashtable ();
analyzed_member_obsolete = new PtrHashtable ();
analyzed_method_excluded = new PtrHashtable ();
-#if NET_2_0
fixed_buffer_cache = new PtrHashtable ();
-#endif
}
public enum Result {
}
bool result;
- if (type.IsArray || type.IsByRef) {
+ if (type.IsArray || type.IsByRef) {
result = IsClsCompliant (TypeManager.GetElementType (type));
+ } else if (TypeManager.IsNullableType (type)) {
+ result = IsClsCompliant (TypeManager.GetTypeArguments (type) [0]);
} else {
result = AnalyzeTypeCompliance (type);
}
if (fb != null) {
return fb as IFixedBuffer;
}
+
+ if (TypeManager.GetConstant (fi) != null)
+ return null;
-#if NET_2_0
object o = fixed_buffer_cache [fi];
if (o == null) {
- if (System.Attribute.GetCustomAttribute (fi, TypeManager.fixed_buffer_attr_type) == null) {
+ if (TypeManager.fixed_buffer_attr_type == null)
+ return null;
+
+ if (!fi.IsDefined (TypeManager.fixed_buffer_attr_type, false)) {
fixed_buffer_cache.Add (fi, FALSE);
return null;
}
return null;
return (IFixedBuffer)o;
-#else
- return null;
-#endif
}
public static void VerifyModulesClsCompliance ()
// The first module is generated assembly
for (int i = 1; i < modules.Length; ++i) {
Module module = modules [i];
- if (!IsClsCompliant (module)) {
+ if (!GetClsCompliantAttributeValue (module, null)) {
Report.Error (3013, "Added modules must be marked with the CLSCompliant attribute " +
"to match the assembly", module.Name);
return;
return null;
}
- static bool IsClsCompliant (ICustomAttributeProvider attribute_provider)
+ static bool GetClsCompliantAttributeValue (ICustomAttributeProvider attribute_provider, Assembly a)
{
- object[] CompliantAttribute = attribute_provider.GetCustomAttributes (TypeManager.cls_compliant_attribute_type, false);
- if (CompliantAttribute.Length == 0)
+ if (TypeManager.cls_compliant_attribute_type == null)
return false;
- return ((CLSCompliantAttribute)CompliantAttribute[0]).IsCompliant;
+ object[] cls_attr = attribute_provider.GetCustomAttributes (TypeManager.cls_compliant_attribute_type, false);
+ if (cls_attr.Length == 0) {
+ if (a == null)
+ return false;
+
+ return GetClsCompliantAttributeValue (a, null);
+ }
+
+ return ((CLSCompliantAttribute)cls_attr [0]).IsCompliant;
}
static bool AnalyzeTypeCompliance (Type type)
if (TypeManager.IsGenericParameter (type))
return true;
- object[] CompliantAttribute = type.GetCustomAttributes (TypeManager.cls_compliant_attribute_type, false);
- if (CompliantAttribute.Length == 0)
- return IsClsCompliant (type.Assembly);
-
- return ((CLSCompliantAttribute)CompliantAttribute[0]).IsCompliant;
+ return GetClsCompliantAttributeValue (type, type.Assembly);
}
// Registers the core type as we assume that they will never be obsolete which
// Type is external, we can get attribute directly
if (type_ds == null) {
- object[] attribute = type.GetCustomAttributes (TypeManager.obsolete_attribute_type, false);
- if (attribute.Length == 1)
- result = (ObsoleteAttribute)attribute [0];
+ if (TypeManager.obsolete_attribute_type != null) {
+ object [] attribute = type.GetCustomAttributes (TypeManager.obsolete_attribute_type, false);
+ if (attribute.Length == 1)
+ result = (ObsoleteAttribute) attribute [0];
+ }
} else {
result = type_ds.GetObsoleteAttribute ();
}
if ((mi.DeclaringType is TypeBuilder) || TypeManager.IsGenericType (mi.DeclaringType))
return null;
+ if (TypeManager.obsolete_attribute_type == null)
+ return null;
+
ObsoleteAttribute oa = System.Attribute.GetCustomAttribute (mi, TypeManager.obsolete_attribute_type, false)
as ObsoleteAttribute;
analyzed_member_obsolete.Add (mi, oa == null ? FALSE : oa);
return;
}
- if (oa.Message == null) {
+ if (oa.Message == null || oa.Message.Length == 0) {
Report.Warning (612, 1, loc, "`{0}' is obsolete", member);
return;
}
public static bool IsConditionalMethodExcluded (MethodBase mb)
{
- mb = TypeManager.DropGenericMethodArguments (mb);
- if ((mb is MethodBuilder) || (mb is ConstructorBuilder))
- return false;
-
object excluded = analyzed_method_excluded [mb];
if (excluded != null)
return excluded == TRUE ? true : false;
+ if (TypeManager.conditional_attribute_type == null)
+ return false;
+
ConditionalAttribute[] attrs = mb.GetCustomAttributes (TypeManager.conditional_attribute_type, true)
as ConditionalAttribute[];
if (attrs.Length == 0) {
// TODO: add caching
// TODO: merge all Type bases attribute caching to one cache to save memory
- if (class_decl == null) {
+ if (class_decl == null && TypeManager.conditional_attribute_type != null) {
object[] attributes = type.GetCustomAttributes (TypeManager.conditional_attribute_type, false);
foreach (ConditionalAttribute ca in attributes) {
if (RootContext.AllDefines.Contains (ca.ConditionString))
{
TypeContainer tc = TypeManager.LookupInterface (type);
if (tc == null) {
+ if (TypeManager.coclass_attr_type == null)
+ return null;
+
object[] o = type.GetCustomAttributes (TypeManager.coclass_attr_type, false);
if (o.Length < 1)
return null;
return ((System.Runtime.InteropServices.CoClassAttribute)o[0]).CoClass;
}
- if (tc.OptAttributes == null)
+ if (tc.OptAttributes == null || TypeManager.coclass_attr_type == null)
return null;
Attribute a = tc.OptAttributes.Search (TypeManager.coclass_attr_type);