X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fmcs%2Fimport.cs;h=8474e29a5b44c1467bd04008a1afdc3dbb9169dd;hb=e2b2d181084848f3c5dde2788370db1b79893c69;hp=e3ada6de4f25359382ad5d09b654033892a91685;hpb=e097a6443206bcdf9866e0c65883ece765144d02;p=mono.git diff --git a/mcs/mcs/import.cs b/mcs/mcs/import.cs index e3ada6de4f2..8474e29a5b4 100644 --- a/mcs/mcs/import.cs +++ b/mcs/mcs/import.cs @@ -5,7 +5,8 @@ // // Dual licensed under the terms of the MIT X11 or GNU GPL // -// Copyright 2009, 2010 Novell, Inc +// Copyright 2009-2011 Novell, Inc +// Copyright 2011-2012 Xamarin, Inc (http://www.xamarin.com) // using System; @@ -31,7 +32,7 @@ namespace Mono.CSharp // Dynamic types reader with additional logic to reconstruct a dynamic // type using DynamicAttribute values // - struct DynamicTypeReader + protected struct DynamicTypeReader { static readonly bool[] single_attribute = { true }; @@ -87,10 +88,9 @@ namespace Mono.CSharp } if (cad.Count > 0) { - string ns, name; foreach (var ca in cad) { - importer.GetCustomAttributeTypeName (ca, out ns, out name); - if (name != "DynamicAttribute" && ns != CompilerServicesNamespace) + var dt = ca.Constructor.DeclaringType; + if (dt.Name != "DynamicAttribute" || dt.Namespace != CompilerServicesNamespace) continue; if (ca.ConstructorArguments.Count == 0) { @@ -120,11 +120,14 @@ namespace Mono.CSharp protected readonly Dictionary import_cache; protected readonly Dictionary compiled_types; protected readonly Dictionary assembly_2_definition; + protected readonly ModuleContainer module; public static readonly string CompilerServicesNamespace = "System.Runtime.CompilerServices"; - protected MetadataImporter () + protected MetadataImporter (ModuleContainer module) { + this.module = module; + import_cache = new Dictionary (1024, ReferenceEquality.Default); compiled_types = new Dictionary (40, ReferenceEquality.Default); assembly_2_definition = new Dictionary (ReferenceEquality.Default); @@ -146,7 +149,6 @@ namespace Mono.CSharp public abstract void AddCompiledType (TypeBuilder builder, TypeSpec spec); protected abstract MemberKind DetermineKindFromBaseType (MetaType baseType); protected abstract bool HasVolatileModifier (MetaType[] modifiers); - public abstract void GetCustomAttributeTypeName (CustomAttributeData cad, out string typeNamespace, out string typeName); public FieldSpec CreateField (FieldInfo fi, TypeSpec declaringType) { @@ -167,7 +169,8 @@ namespace Mono.CSharp break; default: // Ignore private fields (even for error reporting) to not require extra dependencies - if (IgnorePrivateMembers || HasAttribute (CustomAttributeData.GetCustomAttributes (fi), "CompilerGeneratedAttribute", CompilerServicesNamespace)) + if ((IgnorePrivateMembers && !declaringType.IsStruct) || + HasAttribute (CustomAttributeData.GetCustomAttributes (fi), "CompilerGeneratedAttribute", CompilerServicesNamespace)) return null; mod = Modifiers.PRIVATE; @@ -188,12 +191,14 @@ namespace Mono.CSharp var definition = new ImportedMemberDefinition (fi, field_type, this); if ((fa & FieldAttributes.Literal) != 0) { - var c = Constant.CreateConstantFromValue (field_type, fi.GetRawConstantValue (), Location.Null); + Constant c = field_type.Kind == MemberKind.MissingType ? + new NullConstant (InternalType.ErrorType, Location.Null) : + Constant.CreateConstantFromValue (field_type, fi.GetRawConstantValue (), Location.Null); return new ConstSpec (declaringType, definition, field_type, fi, mod, c); } if ((fa & FieldAttributes.InitOnly) != 0) { - if (field_type == TypeManager.decimal_type) { + if (field_type.BuiltinType == BuiltinTypeSpec.Type.Decimal) { var dc = ReadDecimalConstant (CustomAttributeData.GetCustomAttributes (fi)); if (dc != null) return new ConstSpec (declaringType, definition, field_type, fi, mod, dc); @@ -213,7 +218,7 @@ namespace Mono.CSharp if (declaringType.IsStruct && field_type.IsStruct && field_type.IsNested && HasAttribute (CustomAttributeData.GetCustomAttributes (fi), "FixedBufferAttribute", CompilerServicesNamespace)) { - // TODO: Sanity check on field_type (only few type are allowed) + // TODO: Sanity check on field_type (only few types are allowed) var element_field = CreateField (fi.FieldType.GetField (FixedField.FixedElementName), declaringType); return new FixedFieldSpec (declaringType, definition, fi, element_field, mod); } @@ -282,7 +287,7 @@ namespace Mono.CSharp var type = tparams[pos]; int index = pos - first; - tspec [index] = (TypeParameterSpec) CreateType (type, new DynamicTypeReader (), false); + tspec[index] = (TypeParameterSpec) CreateType (type, new DynamicTypeReader (), false); } return tspec; @@ -307,7 +312,7 @@ namespace Mono.CSharp throw new NotImplementedException ("Unknown element type " + type.ToString ()); } - spec = ArrayContainer.MakeType (spec, type.GetArrayRank ()); + spec = ArrayContainer.MakeType (module, spec, type.GetArrayRank ()); } else { spec = CreateType (type, dtype, true); @@ -323,7 +328,7 @@ namespace Mono.CSharp // if (!IsMissingType (type) && type.IsGenericTypeDefinition) { var targs = CreateGenericArguments (0, type.GetGenericArguments (), dtype); - spec = spec.MakeGenericType (targs); + spec = spec.MakeGenericType (module, targs); } } @@ -354,7 +359,7 @@ namespace Mono.CSharp TypeSpec returnType; if (mb.MemberType == MemberTypes.Constructor) { kind = MemberKind.Constructor; - returnType = TypeManager.void_type; + returnType = module.Compiler.BuiltinTypes.Void; } else { // // Detect operators and destructors @@ -371,7 +376,7 @@ namespace Mono.CSharp } } else if (parameters.IsEmpty && name == Destructor.MetadataName) { kind = MemberKind.Destructor; - if (declaringType == TypeManager.object_type) { + if (declaringType.BuiltinType == BuiltinTypeSpec.Type.Object) { mod &= ~Modifiers.OVERRIDE; mod |= Modifiers.VIRTUAL; } @@ -384,26 +389,45 @@ namespace Mono.CSharp // Cannot set to OVERRIDE without full hierarchy checks // this flag indicates that the method could be override // but further validation is needed - if ((mod & Modifiers.OVERRIDE) != 0 && kind == MemberKind.Method && declaringType.BaseType != null) { - var filter = MemberFilter.Method (name, tparams != null ? tparams.Length : 0, parameters, null); - var candidate = MemberCache.FindMember (declaringType.BaseType, filter, BindingRestriction.None); + if ((mod & Modifiers.OVERRIDE) != 0) { + bool is_real_override = false; + if (kind == MemberKind.Method && declaringType.BaseType != null) { + var btype = declaringType.BaseType; + if (IsOverrideMethodBaseTypeAccessible (btype)) { + var filter = MemberFilter.Method (name, tparams != null ? tparams.Length : 0, parameters, null); + var candidate = MemberCache.FindMember (btype, filter, BindingRestriction.None); + + // + // For imported class method do additional validation to be sure that metadata + // override flag was correct + // + // Difference between protected internal and protected is ok + // + const Modifiers conflict_mask = Modifiers.AccessibilityMask & ~Modifiers.INTERNAL; + if (candidate != null && (candidate.Modifiers & conflict_mask) == (mod & conflict_mask) && !candidate.IsStatic) { + is_real_override = true; + } + } + } - // - // For imported class method do additional validation to be sure that metadata - // override flag was correct - // - // Difference between protected internal and protected is ok - // - const Modifiers conflict_mask = Modifiers.AccessibilityMask & ~Modifiers.INTERNAL; - if (candidate == null || (candidate.Modifiers & conflict_mask) != (mod & conflict_mask) || candidate.IsStatic) { + if (!is_real_override) { mod &= ~Modifiers.OVERRIDE; + if ((mod & Modifiers.SEALED) != 0) + mod &= ~Modifiers.SEALED; + else + mod |= Modifiers.VIRTUAL; } } } IMemberDefinition definition; if (tparams != null) { - definition = new ImportedGenericMethodDefinition ((MethodInfo) mb, returnType, parameters, tparams, this); + var gmd = new ImportedGenericMethodDefinition ((MethodInfo) mb, returnType, parameters, tparams, this); + foreach (var tp in gmd.TypeParameters) { + ImportTypeParameterTypeConstraints (tp, tp.GetMetaInfo ()); + } + + definition = gmd; } else { definition = new ImportedParameterMemberDefinition (mb, returnType, parameters, this); } @@ -415,6 +439,30 @@ namespace Mono.CSharp return ms; } + bool IsOverrideMethodBaseTypeAccessible (TypeSpec baseType) + { + switch (baseType.Modifiers & Modifiers.AccessibilityMask) { + case Modifiers.PUBLIC: + return true; + case Modifiers.INTERNAL: + // + // Check whether imported method in base type is accessible from compiled + // context + // + return baseType.MemberDefinition.IsInternalAsPublic (module.DeclaringAssembly); + case Modifiers.PRIVATE: + return false; + default: + // protected + // protected internal + // + // Method accessibility checks will be done later based on context + // where the method is called (CS0122 error will be reported for inaccessible) + // + return true; + } + } + // // Imports System.Reflection parameters // @@ -443,7 +491,7 @@ namespace Mono.CSharp // var el = p.ParameterType.GetElementType (); types[i] = ImportType (el, new DynamicTypeReader (p)); // TODO: 1-based positio to be csc compatible - } else if (i == 0 && method.IsStatic && parent.IsStatic && parent.MemberDefinition.DeclaringAssembly.HasExtensionMethod && + } else if (i == 0 && method.IsStatic && (parent.Modifiers & Modifiers.METHOD_EXTENSION) != 0 && HasAttribute (CustomAttributeData.GetCustomAttributes (method), "ExtensionAttribute", CompilerServicesNamespace)) { mod = Parameter.Modifier.This; types[i] = ImportType (p.ParameterType); @@ -460,22 +508,22 @@ namespace Mono.CSharp if (!is_params && p.IsOptional) { object value = p.RawDefaultValue; var ptype = types[i]; - if ((p.Attributes & ParameterAttributes.HasDefault) != 0 && ptype.Kind != MemberKind.TypeParameter && (value != null || TypeManager.IsReferenceType (ptype))) { + if ((p.Attributes & ParameterAttributes.HasDefault) != 0 && ptype.Kind != MemberKind.TypeParameter && (value != null || TypeSpec.IsReferenceType (ptype))) { if (value == null) { - default_value = Constant.CreateConstant (null, ptype, null, Location.Null); + default_value = Constant.CreateConstant (ptype, null, Location.Null); } else { - default_value = ImportParameterConstant (value).Resolve (null); + default_value = ImportParameterConstant (value); if (ptype.IsEnum) { - default_value = new EnumConstant ((Constant) default_value, ptype).Resolve (null); + default_value = new EnumConstant ((Constant) default_value, ptype); } } } else if (value == Missing.Value) { default_value = EmptyExpression.MissingValue; } else if (value == null) { default_value = new DefaultValueExpression (new TypeExpression (ptype, Location.Null), Location.Null); - } else if (ptype == TypeManager.decimal_type) { - default_value = ImportParameterConstant (value).Resolve (null); + } else if (ptype.BuiltinType == BuiltinTypeSpec.Type.Decimal) { + default_value = ImportParameterConstant (value); } } } @@ -509,7 +557,7 @@ namespace Mono.CSharp bool is_valid_property = true; if (set != null) { - if (set.ReturnType != TypeManager.void_type) + if (set.ReturnType.Kind != MemberKind.Void) is_valid_property = false; var set_param_count = set.Parameters.Count - 1; @@ -572,33 +620,48 @@ namespace Mono.CSharp PropertySpec spec = null; if (!param.IsEmpty) { - var index_name = declaringType.MemberDefinition.GetAttributeDefaultMember (); - if (index_name == null) { - is_valid_property = false; - } else { - if (get != null) { - if (get.IsStatic) - is_valid_property = false; - if (get.Name.IndexOf (index_name, StringComparison.Ordinal) != 4) - is_valid_property = false; + if (is_valid_property) { + var index_name = declaringType.MemberDefinition.GetAttributeDefaultMember (); + if (index_name == null) { + is_valid_property = false; + } else { + if (get != null) { + if (get.IsStatic) + is_valid_property = false; + if (get.Name.IndexOf (index_name, StringComparison.Ordinal) != 4) + is_valid_property = false; + } + if (set != null) { + if (set.IsStatic) + is_valid_property = false; + if (set.Name.IndexOf (index_name, StringComparison.Ordinal) != 4) + is_valid_property = false; + } } - if (set != null) { - if (set.IsStatic) - is_valid_property = false; - if (set.Name.IndexOf (index_name, StringComparison.Ordinal) != 4) - is_valid_property = false; + + if (is_valid_property) { + spec = new IndexerSpec (declaringType, new ImportedParameterMemberDefinition (pi, type, param, this), type, param, pi, mod); + } else if (declaringType.MemberDefinition.IsComImport && param.FixedParameters[0].HasDefaultValue) { + // + // Enables support for properties with parameters (must have default value) of COM-imported types + // + is_valid_property = true; + + for (int i = 0; i < param.FixedParameters.Length; ++i) { + if (!param.FixedParameters[i].HasDefaultValue) { + is_valid_property = false; + break; + } + } } } - - if (is_valid_property) - spec = new IndexerSpec (declaringType, new ImportedParameterMemberDefinition (pi, type, param, this), type, param, pi, mod); } if (spec == null) spec = new PropertySpec (MemberKind.Property, declaringType, new ImportedMemberDefinition (pi, type, this), type, pi, mod); if (!is_valid_property) { - spec.IsNotRealProperty = true; + spec.IsNotCSharpCompatible = true; return spec; } @@ -631,13 +694,13 @@ namespace Mono.CSharp return CreateType (type, declaring_type, dtype, canImportBaseType); } - TypeSpec CreateType (MetaType type, TypeSpec declaringType, DynamicTypeReader dtype, bool canImportBaseType) + protected TypeSpec CreateType (MetaType type, TypeSpec declaringType, DynamicTypeReader dtype, bool canImportBaseType) { TypeSpec spec; if (import_cache.TryGetValue (type, out spec)) { - if (spec == TypeManager.object_type) { + if (spec.BuiltinType == BuiltinTypeSpec.Type.Object) { if (dtype.IsDynamicObject (this)) - return InternalType.Dynamic; + return module.Compiler.BuiltinTypes.Dynamic; return spec; } @@ -664,11 +727,16 @@ namespace Mono.CSharp if (type.IsGenericType && !type.IsGenericTypeDefinition) { var type_def = type.GetGenericTypeDefinition (); + + // Generic type definition can also be forwarded + if (compiled_types.TryGetValue (type_def, out spec)) + return spec; + var targs = CreateGenericArguments (0, type.GetGenericArguments (), dtype); if (declaringType == null) { // Simple case, no nesting spec = CreateType (type_def, null, new DynamicTypeReader (), canImportBaseType); - spec = spec.MakeGenericType (targs); + spec = spec.MakeGenericType (module, targs); } else { // // Nested type case, converting .NET types like @@ -683,7 +751,7 @@ namespace Mono.CSharp int targs_pos = 0; if (declaringType.Arity > 0) { - spec = declaringType.MakeGenericType (targs.Skip (targs_pos).Take (declaringType.Arity).ToArray ()); + spec = declaringType.MakeGenericType (module, targs.Skip (targs_pos).Take (declaringType.Arity).ToArray ()); targs_pos = spec.Arity; } else { spec = declaringType; @@ -693,7 +761,7 @@ namespace Mono.CSharp var t = nested_hierarchy [i - 1]; spec = MemberCache.FindNestedType (spec, t.Name, t.Arity); if (t.Arity > 0) { - spec = spec.MakeGenericType (targs.Skip (targs_pos).Take (spec.Arity).ToArray ()); + spec = spec.MakeGenericType (module, targs.Skip (targs_pos).Take (spec.Arity).ToArray ()); targs_pos += t.Arity; } } @@ -704,8 +772,11 @@ namespace Mono.CSharp name = name.Substring (0, index); spec = MemberCache.FindNestedType (spec, name, targs.Length - targs_pos); + if (spec == null) + return null; + if (spec.Arity > 0) { - spec = spec.MakeGenericType (targs.Skip (targs_pos).ToArray ()); + spec = spec.MakeGenericType (module, targs.Skip (targs_pos).ToArray ()); } } @@ -788,15 +859,9 @@ namespace Mono.CSharp kind = MemberKind.Class; } else if (kind == MemberKind.TypeParameter) { - // Return as type_cache was updated - return CreateTypeParameter (type, declaringType); + spec = CreateTypeParameter (type, declaringType); } else if (type.IsGenericTypeDefinition) { definition.TypeParameters = CreateGenericParameters (type, declaringType); - - // Constraints are not loaded on demand and can reference this type - if (import_cache.TryGetValue (type, out spec)) - return spec; - } else if (compiled_types.TryGetValue (type, out pt)) { // // Same type was found in inside compiled types. It's @@ -804,7 +869,7 @@ namespace Mono.CSharp // which point into just compiled assembly. // spec = pt; - BuildinTypeSpec bts = pt as BuildinTypeSpec; + BuiltinTypeSpec bts = pt as BuiltinTypeSpec; if (bts != null) bts.SetDefinition (definition, type, mod); } @@ -814,6 +879,13 @@ namespace Mono.CSharp import_cache.Add (type, spec); + if (kind == MemberKind.TypeParameter) { + if (canImportBaseType) + ImportTypeParameterTypeConstraints ((TypeParameterSpec) spec, type); + + return spec; + } + // // Two stage setup as the base type can be inflated declaring type or // another nested type inside same declaring type which has not been @@ -832,7 +904,7 @@ namespace Mono.CSharp if (!assembly_2_definition.TryGetValue (assembly, out found)) { // This can happen in dynamic context only - var def = new ImportedAssemblyDefinition (assembly, this); + var def = new ImportedAssemblyDefinition (assembly); assembly_2_definition.Add (assembly, def); def.ReadAttributes (); found = def; @@ -878,39 +950,12 @@ namespace Mono.CSharp TypeParameterSpec spec; var def = new ImportedTypeParameterDefinition (type, this); - if (type.DeclaringMethod != null) + if (type.DeclaringMethod != null) { spec = new TypeParameterSpec (type.GenericParameterPosition, def, special, variance, type); - else + } else { spec = new TypeParameterSpec (declaringType, type.GenericParameterPosition, def, special, variance, type); - - // Add it now, so any constraint can reference it and get same instance - import_cache.Add (type, spec); - - var constraints = type.GetGenericParameterConstraints (); - List tparams = null; - foreach (var ct in constraints) { - if (ct.IsGenericParameter) { - if (tparams == null) - tparams = new List (); - - tparams.Add (CreateType (ct)); - continue; - } - - if (!IsMissingType (ct) && ct.IsClass) { - spec.BaseType = CreateType (ct); - continue; - } - - spec.AddInterface (CreateType (ct)); } - if (spec.BaseType == null) - spec.BaseType = TypeManager.object_type; - - if (tparams != null) - spec.TypeArguments = tparams.ToArray (); - return spec; } @@ -918,15 +963,14 @@ namespace Mono.CSharp // Test for a custom attribute type match. Custom attributes are not really predefined globaly // they can be assembly specific therefore we do check based on names only // - public bool HasAttribute (IList attributesData, string attrName, string attrNamespace) + public static bool HasAttribute (IList attributesData, string attrName, string attrNamespace) { if (attributesData.Count == 0) return false; - string ns, name; foreach (var attr in attributesData) { - GetCustomAttributeTypeName (attr, out ns, out name); - if (name == attrName && ns == attrNamespace) + var dt = attr.Constructor.DeclaringType; + if (dt.Name == attrName && dt.Namespace == attrNamespace) return true; } @@ -936,7 +980,7 @@ namespace Mono.CSharp void ImportTypeBase (TypeSpec spec, MetaType type) { if (spec.Kind == MemberKind.Interface) - spec.BaseType = TypeManager.object_type; + spec.BaseType = module.Compiler.BuiltinTypes.Object; else if (type.BaseType != null) { TypeSpec base_type; if (!IsMissingType (type.BaseType) && type.BaseType.IsGenericType) @@ -951,13 +995,35 @@ namespace Mono.CSharp #if STATIC ifaces = type.__GetDeclaredInterfaces (); if (ifaces.Length != 0) { - foreach (var iface in ifaces) - spec.AddInterface (CreateType (iface)); + foreach (var iface in ifaces) { + var it = CreateType (iface); + if (it == null) + continue; + + spec.AddInterface (it); + + // Unfortunately not all languages expand inherited interfaces + var bifaces = it.Interfaces; + if (bifaces != null) { + foreach (var biface in bifaces) { + spec.AddInterface (biface); + } + } + } } if (spec.BaseType != null) { var bifaces = spec.BaseType.Interfaces; if (bifaces != null) { + // + // Before adding base class interfaces close defined interfaces + // on type parameter + // + var tp = spec as TypeParameterSpec; + if (tp != null && tp.InterfacesDefined == null) { + tp.InterfacesDefined = TypeSpec.EmptyTypes; + } + foreach (var iface in bifaces) spec.AddInterface (iface); } @@ -971,9 +1037,15 @@ namespace Mono.CSharp } } #endif + + if (spec.MemberDefinition.TypeParametersCount > 0) { + foreach (var tp in spec.MemberDefinition.TypeParameters) { + ImportTypeParameterTypeConstraints (tp, tp.GetMetaInfo ()); + } + } } - protected void ImportTypes (MetaType[] types, Namespace targetNamespace, bool hasExtensionTypes) + protected void ImportTypes (MetaType[] types, Namespace targetNamespace, bool importExtensionTypes) { Namespace ns = targetNamespace; string prev_namespace = null; @@ -997,50 +1069,82 @@ namespace Mono.CSharp prev_namespace = t.Namespace; } - ns.AddType (it); - - if (it.IsStatic && hasExtensionTypes && + // Cannot rely on assembly level Extension attribute or static modifier because they + // are not followed by other compilers (e.g. F#). + if (it.IsClass && it.Arity == 0 && importExtensionTypes && HasAttribute (CustomAttributeData.GetCustomAttributes (t), "ExtensionAttribute", CompilerServicesNamespace)) { it.SetExtensionMethodContainer (); } + + ns.AddType (module, it); + } + } + + void ImportTypeParameterTypeConstraints (TypeParameterSpec spec, MetaType type) + { + var constraints = type.GetGenericParameterConstraints (); + List tparams = null; + foreach (var ct in constraints) { + if (ct.IsGenericParameter) { + if (tparams == null) + tparams = new List (); + + tparams.Add (CreateType (ct)); + continue; + } + + var constraint_type = CreateType (ct); + if (constraint_type.IsClass) { + spec.BaseType = constraint_type; + continue; + } + + spec.AddInterface (constraint_type); } + + if (spec.BaseType == null) + spec.BaseType = module.Compiler.BuiltinTypes.Object; + + if (tparams != null) + spec.TypeArguments = tparams.ToArray (); } - static Constant ImportParameterConstant (object value) + Constant ImportParameterConstant (object value) { // // Get type of underlying value as int constant can be used for object // parameter type. This is not allowed in C# but other languages can do that // + var types = module.Compiler.BuiltinTypes; switch (System.Type.GetTypeCode (value.GetType ())) { case TypeCode.Boolean: - return new BoolConstant ((bool) value, Location.Null); + return new BoolConstant (types, (bool) value, Location.Null); case TypeCode.Byte: - return new ByteConstant ((byte) value, Location.Null); + return new ByteConstant (types, (byte) value, Location.Null); case TypeCode.Char: - return new CharConstant ((char) value, Location.Null); + return new CharConstant (types, (char) value, Location.Null); case TypeCode.Decimal: - return new DecimalConstant ((decimal) value, Location.Null); + return new DecimalConstant (types, (decimal) value, Location.Null); case TypeCode.Double: - return new DoubleConstant ((double) value, Location.Null); + return new DoubleConstant (types, (double) value, Location.Null); case TypeCode.Int16: - return new ShortConstant ((short) value, Location.Null); + return new ShortConstant (types, (short) value, Location.Null); case TypeCode.Int32: - return new IntConstant ((int) value, Location.Null); + return new IntConstant (types, (int) value, Location.Null); case TypeCode.Int64: - return new LongConstant ((long) value, Location.Null); + return new LongConstant (types, (long) value, Location.Null); case TypeCode.SByte: - return new SByteConstant ((sbyte) value, Location.Null); + return new SByteConstant (types, (sbyte) value, Location.Null); case TypeCode.Single: - return new FloatConstant ((float) value, Location.Null); + return new FloatConstant (types, (float) value, Location.Null); case TypeCode.String: - return new StringConstant ((string) value, Location.Null); + return new StringConstant (types, (string) value, Location.Null); case TypeCode.UInt16: - return new UShortConstant ((ushort) value, Location.Null); + return new UShortConstant (types, (ushort) value, Location.Null); case TypeCode.UInt32: - return new UIntConstant ((uint) value, Location.Null); + return new UIntConstant (types, (uint) value, Location.Null); case TypeCode.UInt64: - return new ULongConstant ((ulong) value, Location.Null); + return new ULongConstant (types, (ulong) value, Location.Null); } throw new NotImplementedException (value.GetType ().ToString ()); @@ -1059,11 +1163,11 @@ namespace Mono.CSharp var spec = ImportType (element, dtype); if (type.IsArray) - return ArrayContainer.MakeType (spec, type.GetArrayRank ()); + return ArrayContainer.MakeType (module, spec, type.GetArrayRank ()); if (type.IsByRef) - return ReferenceContainer.MakeType (spec); + return ReferenceContainer.MakeType (module, spec); if (type.IsPointer) - return PointerContainer.MakeType (spec); + return PointerContainer.MakeType (module, spec); throw new NotImplementedException ("Unknown element type " + type.ToString ()); } @@ -1090,10 +1194,9 @@ namespace Mono.CSharp if (attrs.Count == 0) return null; - string name, ns; foreach (var ca in attrs) { - GetCustomAttributeTypeName (ca, out ns, out name); - if (name != "DecimalConstantAttribute" || ns != CompilerServicesNamespace) + var dt = ca.Constructor.DeclaringType; + if (dt.Name != "DecimalConstantAttribute" || dt.Namespace != CompilerServicesNamespace) continue; var value = new decimal ( @@ -1103,7 +1206,7 @@ namespace Mono.CSharp (byte) ca.ConstructorArguments[1].Value != 0, (byte) ca.ConstructorArguments[0].Value); - return new DecimalConstant (value, Location.Null).Resolve (null); + return new DecimalConstant (module.Compiler.BuiltinTypes, value, Location.Null); } return null; @@ -1140,15 +1243,21 @@ namespace Mono.CSharp return mod; } + // It can be sealed and override if ((ma & MethodAttributes.Final) != 0) mod |= Modifiers.SEALED; - // It can be sealed and override if ((ma & MethodAttributes.Virtual) != 0) { - if ((ma & MethodAttributes.NewSlot) != 0 || !declaringType.IsClass) { - // No private virtual or sealed virtual - if ((mod & (Modifiers.PRIVATE | Modifiers.SEALED)) == 0) + // Not every member can be detected based on MethodAttribute, we + // set virtual or non-virtual only when we are certain. Further checks + // to really find out what `virtual' means for this member are done + // later + if ((ma & MethodAttributes.NewSlot) != 0) { + if ((mod & Modifiers.SEALED) != 0) { + mod &= ~Modifiers.SEALED; + } else { mod |= Modifiers.VIRTUAL; + } } else { mod |= Modifiers.OVERRIDE; } @@ -1168,7 +1277,7 @@ namespace Mono.CSharp public ObsoleteAttribute Obsolete; public string[] Conditionals; public string DefaultIndexerName; - public bool IsNotCLSCompliant; + public bool? CLSAttributeValue; public TypeSpec CoClass; public static AttributesBag Read (MemberInfo mi, MetadataImporter importer) @@ -1179,11 +1288,11 @@ namespace Mono.CSharp // It should not throw any loading exception IList attrs = CustomAttributeData.GetCustomAttributes (mi); - string ns, name; foreach (var a in attrs) { - importer.GetCustomAttributeTypeName (a, out ns, out name); + var dt = a.Constructor.DeclaringType; + string name = dt.Name; if (name == "ObsoleteAttribute") { - if (ns != "System") + if (dt.Namespace != "System") continue; if (bag == null) @@ -1203,7 +1312,7 @@ namespace Mono.CSharp } if (name == "ConditionalAttribute") { - if (ns != "System.Diagnostics") + if (dt.Namespace != "System.Diagnostics") continue; if (bag == null) @@ -1217,20 +1326,20 @@ namespace Mono.CSharp } if (name == "CLSCompliantAttribute") { - if (ns != "System") + if (dt.Namespace != "System") continue; if (bag == null) bag = new AttributesBag (); - bag.IsNotCLSCompliant = !(bool) a.ConstructorArguments[0].Value; + bag.CLSAttributeValue = (bool) a.ConstructorArguments[0].Value; continue; } // Type only attributes if (mi.MemberType == MemberTypes.TypeInfo || mi.MemberType == MemberTypes.NestedType) { if (name == "DefaultMemberAttribute") { - if (ns != "System.Reflection") + if (dt.Namespace != "System.Reflection") continue; if (bag == null) @@ -1241,7 +1350,7 @@ namespace Mono.CSharp } if (name == "AttributeUsageAttribute") { - if (ns != "System") + if (dt.Namespace != "System") continue; if (bag == null) @@ -1259,7 +1368,7 @@ namespace Mono.CSharp // Interface only attribute if (name == "CoClassAttribute") { - if (ns != "System.Runtime.InteropServices") + if (dt.Namespace != "System.Runtime.InteropServices") continue; if (bag == null) @@ -1323,12 +1432,13 @@ namespace Mono.CSharp return cattrs.Obsolete; } - public bool IsNotCLSCompliant () - { - if (cattrs == null) - ReadAttributes (); + public bool? CLSAttributeValue { + get { + if (cattrs == null) + ReadAttributes (); - return cattrs.IsNotCLSCompliant; + return cattrs.CLSAttributeValue; + } } protected void ReadAttributes () @@ -1351,12 +1461,10 @@ namespace Mono.CSharp { readonly Module module; bool cls_compliant; - readonly MetadataImporter importer; - public ImportedModuleDefinition (Module module, MetadataImporter importer) + public ImportedModuleDefinition (Module module) { this.module = module; - this.importer = importer; } #region Properties @@ -1379,11 +1487,10 @@ namespace Mono.CSharp { IList attrs = CustomAttributeData.GetCustomAttributes (module); - string ns, name; foreach (var a in attrs) { - importer.GetCustomAttributeTypeName (a, out ns, out name); - if (name == "CLSCompliantAttribute") { - if (ns != "System") + var dt = a.Constructor.DeclaringType; + if (dt.Name == "CLSCompliantAttribute") { + if (dt.Namespace != "System") continue; cls_compliant = (bool) a.ConstructorArguments[0].Value; @@ -1432,18 +1539,15 @@ namespace Mono.CSharp { readonly Assembly assembly; readonly AssemblyName aname; - readonly MetadataImporter importer; bool cls_compliant; - bool contains_extension_methods; List internals_visible_to; Dictionary internals_visible_to_cache; - public ImportedAssemblyDefinition (Assembly assembly, MetadataImporter importer) + public ImportedAssemblyDefinition (Assembly assembly) { this.assembly = assembly; this.aname = assembly.GetName (); - this.importer = importer; } #region Properties @@ -1460,12 +1564,6 @@ namespace Mono.CSharp } } - public bool HasExtensionMethod { - get { - return contains_extension_methods; - } - } - public bool HasStrongName { get { return aname.GetPublicKey ().Length != 0; @@ -1558,31 +1656,21 @@ namespace Mono.CSharp IList attrs = CustomAttributeData.GetCustomAttributes (assembly); - string ns, name; foreach (var a in attrs) { - importer.GetCustomAttributeTypeName (a, out ns, out name); - + var dt = a.Constructor.DeclaringType; + var name = dt.Name; if (name == "CLSCompliantAttribute") { - if (ns == "System") { - try { - cls_compliant = (bool) a.ConstructorArguments[0].Value; - } catch { - } + if (dt.Namespace == "System") { + cls_compliant = (bool) a.ConstructorArguments[0].Value; } continue; } if (name == "InternalsVisibleToAttribute") { - if (ns != MetadataImporter.CompilerServicesNamespace) + if (dt.Namespace != MetadataImporter.CompilerServicesNamespace) continue; - string s; - try { - s = a.ConstructorArguments[0].Value as string; - } catch { - s = null; - } - + string s = a.ConstructorArguments[0].Value as string; if (s == null) continue; @@ -1593,13 +1681,6 @@ namespace Mono.CSharp internals_visible_to.Add (an); continue; } - - if (name == "ExtensionAttribute") { - if (ns == MetadataImporter.CompilerServicesNamespace) - contains_extension_methods = true; - - continue; - } } } @@ -1702,6 +1783,29 @@ namespace Mono.CSharp } } + bool ITypeDefinition.IsComImport { + get { + return ((MetaType) provider).IsImport; + } + } + + + bool ITypeDefinition.IsPartial { + get { + return false; + } + } + + bool ITypeDefinition.IsTypeForwarder { + get { +#if STATIC + return ((MetaType) provider).__IsTypeForwarder; +#else + return false; +#endif + } + } + public override string Name { get { if (name == null) { @@ -1748,19 +1852,33 @@ namespace Mono.CSharp // or referenced from the user core in which case compilation error has to // be reported because compiler cannot continue anyway // - foreach (var t in types) { + for (int i = 0; i < types.Count; ++i) { + var t = types [i]; + + // + // Report missing types only once per type + // + if (i > 0 && types.IndexOf (t) < i) + continue; + string name = t.GetSignatureForError (); if (t.MemberDefinition.DeclaringAssembly == ctx.Module.DeclaringAssembly) { - ctx.Compiler.Report.Error (1683, loc, + ctx.Module.Compiler.Report.Error (1683, loc, "Reference to type `{0}' claims it is defined in this assembly, but it is not defined in source or any added modules", name); } else if (t.MemberDefinition.DeclaringAssembly.IsMissing) { - ctx.Compiler.Report.Error (12, loc, - "The type `{0}' is defined in an assembly that is not referenced. Consider adding a reference to assembly `{1}'", - name, t.MemberDefinition.DeclaringAssembly.FullName); + if (t.MemberDefinition.IsTypeForwarder) { + ctx.Module.Compiler.Report.Error (1070, loc, + "The type `{0}' has been forwarded to an assembly that is not referenced. Consider adding a reference to assembly `{1}'", + name, t.MemberDefinition.DeclaringAssembly.FullName); + } else { + ctx.Module.Compiler.Report.Error (12, loc, + "The type `{0}' is defined in an assembly that is not referenced. Consider adding a reference to assembly `{1}'", + name, t.MemberDefinition.DeclaringAssembly.FullName); + } } else { - ctx.Compiler.Report.Error (1684, loc, + ctx.Module.Compiler.Report.Error (1684, loc, "Reference to type `{0}' claims it is defined assembly `{1}', but it could not be found", name, t.MemberDefinition.DeclaringAssembly.FullName); } @@ -1843,10 +1961,16 @@ namespace Mono.CSharp var t = (MetaType) member; // Ignore compiler generated types, mostly lambda containers - if ((t.Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPrivate) + if ((t.Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPrivate && importer.IgnorePrivateMembers) continue; - imported = importer.CreateNestedType (t, declaringType); + try { + imported = importer.CreateNestedType (t, declaringType); + } catch (Exception e) { + throw new InternalErrorException (e, "Could not import nested type `{0}' from `{1}'", + t.FullName, declaringType.MemberDefinition.DeclaringAssembly.FullName); + } + cache.AddMemberImported (imported); } @@ -1856,13 +1980,22 @@ namespace Mono.CSharp var t = (MetaType) member; - if ((t.Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPrivate) + if ((t.Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPrivate && importer.IgnorePrivateMembers) continue; importer.ImportTypeBase (t); } } + // + // Load base interfaces first to minic behaviour of compiled members + // + if (declaringType.IsInterface && declaringType.Interfaces != null) { + foreach (var iface in declaringType.Interfaces) { + cache.AddInterface (iface); + } + } + if (!onlyTypes) { // // The logic here requires methods to be returned first which seems to work for both Mono and .NET @@ -1870,6 +2003,10 @@ namespace Mono.CSharp foreach (var member in all) { switch (member.MemberType) { case MemberTypes.Constructor: + if (declaringType.IsInterface) + continue; + + goto case MemberTypes.Method; case MemberTypes.Method: MethodBase mb = (MethodBase) member; var attrs = mb.Attributes; @@ -1883,7 +2020,7 @@ namespace Mono.CSharp continue; // Ignore compiler generated methods - if (importer.HasAttribute (CustomAttributeData.GetCustomAttributes (mb), "CompilerGeneratedAttribute", MetadataImporter.CompilerServicesNamespace)) + if (MetadataImporter.HasAttribute (CustomAttributeData.GetCustomAttributes (mb), "CompilerGeneratedAttribute", MetadataImporter.CompilerServicesNamespace)) continue; } @@ -1967,13 +2104,19 @@ namespace Mono.CSharp // if (imported_events != null) { // The backing event field should be private but it may not - int index = imported_events.FindIndex (l => l.Name == fi.Name); - if (index >= 0) { - event_spec = imported_events[index]; - event_spec.BackingField = (FieldSpec) imported; - imported_events.RemoveAt (index); - continue; + int i; + for (i = 0; i < imported_events.Count; ++i) { + var ev = imported_events[i]; + if (ev.Name == fi.Name) { + ev.BackingField = (FieldSpec) imported; + imported_events.RemoveAt (i); + i = -1; + break; + } } + + if (i < 0) + continue; } break; @@ -1984,13 +2127,10 @@ namespace Mono.CSharp throw new NotImplementedException (member.ToString ()); } - cache.AddMemberImported (imported); - } - } + if (imported.IsStatic && declaringType.IsInterface) + continue; - if (declaringType.IsInterface && declaringType.Interfaces != null) { - foreach (var iface in declaringType.Interfaces) { - cache.AddInterface (iface); + cache.AddMemberImported (imported); } } } @@ -2011,6 +2151,24 @@ namespace Mono.CSharp } } + bool ITypeDefinition.IsComImport { + get { + return false; + } + } + + bool ITypeDefinition.IsPartial { + get { + return false; + } + } + + bool ITypeDefinition.IsTypeForwarder { + get { + return false; + } + } + public string Namespace { get { return null;