Esempio n. 1
0
        public override void ApplyAttributeBuilder(Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
        {
            if (a.Target == AttributeTargets.Assembly)
            {
                assembly.ApplyAttributeBuilder(a, ctor, cdata, pa);
                return;
            }

            if (a.Type == pa.DefaultCharset)
            {
                switch (a.GetCharSetValue())
                {
                case CharSet.Ansi:
                case CharSet.None:
                    break;

                case CharSet.Auto:
                    DefaultCharSet     = CharSet.Auto;
                    DefaultCharSetType = TypeAttributes.AutoClass;
                    break;

                case CharSet.Unicode:
                    DefaultCharSet     = CharSet.Unicode;
                    DefaultCharSetType = TypeAttributes.UnicodeClass;
                    break;

                default:
                    Report.Error(1724, a.Location, "Value specified for the argument to `{0}' is not valid",
                                 a.GetSignatureForError());
                    break;
                }
            }
            else if (a.Type == pa.CLSCompliant)
            {
                Attribute cls = DeclaringAssembly.CLSCompliantAttribute;
                if (cls == null)
                {
                    Report.Warning(3012, 1, a.Location,
                                   "You must specify the CLSCompliant attribute on the assembly, not the module, to enable CLS compliance checking");
                }
                else if (DeclaringAssembly.IsCLSCompliant != a.GetBoolean())
                {
                    Report.SymbolRelatedToPreviousError(cls.Location, cls.GetSignatureForError());
                    Report.Warning(3017, 1, a.Location,
                                   "You cannot specify the CLSCompliant attribute on a module that differs from the CLSCompliant attribute on the assembly");
                    return;
                }
            }

            builder.SetCustomAttribute((ConstructorInfo)ctor.GetMetaInfo(), cdata);
        }
        //
        // When using assembly public key attributes InternalsVisibleTo key
        // was not checked, we have to do it later when we actually know what
        // our public key token is
        //
        void CheckReferencesPublicToken()
        {
            // TODO: It should check only references assemblies but there is
            // no working SRE API
            foreach (var entry in Importer.Assemblies)
            {
                var a = entry as ImportedAssemblyDefinition;
                if (a == null)
                {
                    continue;
                }

                if (public_key != null && !a.HasStrongName)
                {
                    Report.Error(1577, "Referenced assembly `{0}' does not have a strong name",
                                 a.FullName);
                }

                var ci = a.Assembly.GetName().CultureInfo;
                if (!ci.Equals(System.Globalization.CultureInfo.InvariantCulture))
                {
                    Report.Warning(1607, 1, "Referenced assembly `{0}' has different culture setting of `{1}'",
                                   a.Name, ci.Name);
                }

                if (!a.IsFriendAssemblyTo(this))
                {
                    continue;
                }

                var attr   = a.GetAssemblyVisibleToName(this);
                var atoken = attr.GetPublicKeyToken();

                if (ArrayComparer.IsEqual(GetPublicKeyToken(), atoken))
                {
                    continue;
                }

                Report.SymbolRelatedToPreviousError(a.Location);
                Report.Error(281,
                             "Friend access was granted to `{0}', but the output assembly is named `{1}'. Try adding a reference to `{0}' or change the output assembly name to match it",
                             attr.FullName, FullName);
            }
        }
Esempio n. 3
0
        protected override bool CheckBase()
        {
            if (!base.CheckBase())
            {
                return(false);
            }

            MemberSpec candidate;
            bool       overrides       = false;
            var        conflict_symbol = MemberCache.FindBaseMember(this, out candidate, ref overrides);

            if (conflict_symbol == null)
            {
                conflict_symbol = candidate;
            }

            if (conflict_symbol == null)
            {
                if ((ModFlags & Modifiers.NEW) != 0)
                {
                    Report.Warning(109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required",
                                   GetSignatureForError());
                }
            }
            else
            {
                if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE | Modifiers.BACKING_FIELD)) == 0)
                {
                    Report.SymbolRelatedToPreviousError(conflict_symbol);
                    Report.Warning(108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
                                   GetSignatureForError(), conflict_symbol.GetSignatureForError());
                }

                if (conflict_symbol.IsAbstract)
                {
                    Report.SymbolRelatedToPreviousError(conflict_symbol);
                    Report.Error(533, Location, "`{0}' hides inherited abstract member `{1}'",
                                 GetSignatureForError(), conflict_symbol.GetSignatureForError());
                }
            }

            return(true);
        }
        protected override bool DoDefineMembers()
        {
            var builtin_types = Compiler.BuiltinTypes;

            var ctor_parameters = ParametersCompiled.CreateFullyResolved(
                new [] {
                new Parameter(new TypeExpression(builtin_types.Object, Location), "object", Parameter.Modifier.NONE, null, Location),
                new Parameter(new TypeExpression(builtin_types.IntPtr, Location), "method", Parameter.Modifier.NONE, null, Location)
            },
                new [] {
                builtin_types.Object,
                builtin_types.IntPtr
            }
                );

            Constructor = new Constructor(this, Constructor.ConstructorName,
                                          Modifiers.PUBLIC, null, ctor_parameters, Location);
            Constructor.Define();

            //
            // Here the various methods like Invoke, BeginInvoke etc are defined
            //
            // First, call the `out of band' special method for
            // defining recursively any types we need:
            //
            var p = parameters;

            if (!p.Resolve(this))
            {
                return(false);
            }

            //
            // Invoke method
            //

            // Check accessibility
            foreach (var partype in p.Types)
            {
                if (!IsAccessibleAs(partype))
                {
                    Report.SymbolRelatedToPreviousError(partype);
                    Report.Error(59, Location,
                                 "Inconsistent accessibility: parameter type `{0}' is less accessible than delegate `{1}'",
                                 TypeManager.CSharpName(partype), GetSignatureForError());
                }
            }

            var ret_type = ReturnType.ResolveAsType(this);

            if (ret_type == null)
            {
                return(false);
            }

            //
            // We don't have to check any others because they are all
            // guaranteed to be accessible - they are standard types.
            //
            if (!IsAccessibleAs(ret_type))
            {
                Report.SymbolRelatedToPreviousError(ret_type);
                Report.Error(58, Location,
                             "Inconsistent accessibility: return type `" +
                             TypeManager.CSharpName(ret_type) + "' is less " +
                             "accessible than delegate `" + GetSignatureForError() + "'");
                return(false);
            }

            CheckProtectedModifier();

            if (Compiler.Settings.StdLib && ret_type.IsSpecialRuntimeType)
            {
                Method.Error1599(Location, ret_type, Report);
                return(false);
            }

            TypeManager.CheckTypeVariance(ret_type, Variance.Covariant, this);

            var resolved_rt = new TypeExpression(ret_type, Location);

            InvokeBuilder = new Method(this, resolved_rt, MethodModifiers, new MemberName(InvokeMethodName), p, null);
            InvokeBuilder.Define();

            //
            // Don't emit async method for compiler generated delegates (e.g. dynamic site containers)
            //
            if (!IsCompilerGenerated)
            {
                DefineAsyncMethods(Parameters.CallingConvention, resolved_rt);
            }

            return(true);
        }
        protected void ResolveAssemblySecurityAttributes()
        {
            string key_file      = null;
            string key_container = null;

            if (module.OptAttributes != null)
            {
                foreach (Attribute a in module.OptAttributes.Attrs)
                {
                    // cannot rely on any resolve-based members before you call Resolve
                    if (a.ExplicitTarget != "assembly")
                    {
                        continue;
                    }

                    // TODO: This code is buggy: comparing Attribute name without resolving is wrong.
                    //       However, this is invoked by CodeGen.Init, when none of the namespaces
                    //       are loaded yet.
                    // TODO: Does not handle quoted attributes properly
                    switch (a.Name)
                    {
                    case "AssemblyKeyFile":
                    case "AssemblyKeyFileAttribute":
                    case "System.Reflection.AssemblyKeyFileAttribute":
                        if (Compiler.Settings.StrongNameKeyFile != null)
                        {
                            Report.SymbolRelatedToPreviousError(a.Location, a.GetSignatureForError());
                            Report.Warning(1616, 1, "Option `{0}' overrides attribute `{1}' given in a source file or added module",
                                           "keyfile", "System.Reflection.AssemblyKeyFileAttribute");
                        }
                        else
                        {
                            string value = a.GetString();
                            if (!string.IsNullOrEmpty(value))
                            {
                                Error_ObsoleteSecurityAttribute(a, "keyfile");
                                key_file = value;
                            }
                        }
                        break;

                    case "AssemblyKeyName":
                    case "AssemblyKeyNameAttribute":
                    case "System.Reflection.AssemblyKeyNameAttribute":
                        if (Compiler.Settings.StrongNameKeyContainer != null)
                        {
                            Report.SymbolRelatedToPreviousError(a.Location, a.GetSignatureForError());
                            Report.Warning(1616, 1, "Option `{0}' overrides attribute `{1}' given in a source file or added module",
                                           "keycontainer", "System.Reflection.AssemblyKeyNameAttribute");
                        }
                        else
                        {
                            string value = a.GetString();
                            if (!string.IsNullOrEmpty(value))
                            {
                                Error_ObsoleteSecurityAttribute(a, "keycontainer");
                                key_container = value;
                            }
                        }
                        break;

                    case "AssemblyDelaySign":
                    case "AssemblyDelaySignAttribute":
                    case "System.Reflection.AssemblyDelaySignAttribute":
                        bool b = a.GetBoolean();
                        if (b)
                        {
                            Error_ObsoleteSecurityAttribute(a, "delaysign");
                        }

                        delay_sign = b;
                        break;
                    }
                }
            }

            // We came here only to report assembly attributes warnings
            if (public_key != null)
            {
                return;
            }

            //
            // Load the strong key file found in attributes when no
            // command line key was given
            //
            if (key_file != null || key_container != null)
            {
                LoadPublicKey(key_file, key_container);
            }
            else if (delay_sign)
            {
                Report.Warning(1607, 1, "Delay signing was requested but no key file was given");
            }
        }
        public void ApplyAttributeBuilder(Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
        {
            if (a.IsValidSecurityAttribute())
            {
                a.ExtractSecurityPermissionSet(ctor, ref declarative_security);
                return;
            }

            if (a.Type == pa.AssemblyCulture)
            {
                string value = a.GetString();
                if (value == null || value.Length == 0)
                {
                    return;
                }

                if (Compiler.Settings.Target == Target.Exe)
                {
                    a.Error_AttributeEmitError("The executables cannot be satelite assemblies, remove the attribute or keep it empty");
                    return;
                }

                if (value == "neutral")
                {
                    value = "";
                }

                if (Compiler.Settings.Target == Target.Module)
                {
                    SetCustomAttribute(ctor, cdata);
                }
                else
                {
                    builder_extra.SetCulture(value, a.Location);
                }

                IsSatelliteAssembly = true;
                return;
            }

            if (a.Type == pa.AssemblyVersion)
            {
                string value = a.GetString();
                if (value == null || value.Length == 0)
                {
                    return;
                }

                var vinfo = IsValidAssemblyVersion(value, true);
                if (vinfo == null)
                {
                    a.Error_AttributeEmitError(string.Format("Specified version `{0}' is not valid", value));
                    return;
                }

                if (Compiler.Settings.Target == Target.Module)
                {
                    SetCustomAttribute(ctor, cdata);
                }
                else
                {
                    builder_extra.SetVersion(vinfo, a.Location);
                }

                return;
            }

            if (a.Type == pa.AssemblyAlgorithmId)
            {
                const int pos = 2;                 // skip CA header
                uint      alg = (uint)cdata [pos];
                alg |= ((uint)cdata [pos + 1]) << 8;
                alg |= ((uint)cdata [pos + 2]) << 16;
                alg |= ((uint)cdata [pos + 3]) << 24;

                if (Compiler.Settings.Target == Target.Module)
                {
                    SetCustomAttribute(ctor, cdata);
                }
                else
                {
                    builder_extra.SetAlgorithmId(alg, a.Location);
                }

                return;
            }

            if (a.Type == pa.AssemblyFlags)
            {
                const int pos   = 2;               // skip CA header
                uint      flags = (uint)cdata[pos];
                flags |= ((uint)cdata [pos + 1]) << 8;
                flags |= ((uint)cdata [pos + 2]) << 16;
                flags |= ((uint)cdata [pos + 3]) << 24;

                // Ignore set PublicKey flag if assembly is not strongnamed
                if ((flags & (uint)AssemblyNameFlags.PublicKey) != 0 && public_key == null)
                {
                    flags &= ~(uint)AssemblyNameFlags.PublicKey;
                }

                if (Compiler.Settings.Target == Target.Module)
                {
                    SetCustomAttribute(ctor, cdata);
                }
                else
                {
                    builder_extra.SetFlags(flags, a.Location);
                }

                return;
            }

            if (a.Type == pa.TypeForwarder)
            {
                TypeSpec t = a.GetArgumentType();
                if (t == null || TypeManager.HasElementType(t))
                {
                    Report.Error(735, a.Location, "Invalid type specified as an argument for TypeForwardedTo attribute");
                    return;
                }

                if (emitted_forwarders == null)
                {
                    emitted_forwarders = new Dictionary <ITypeDefinition, Attribute> ();
                }
                else if (emitted_forwarders.ContainsKey(t.MemberDefinition))
                {
                    Report.SymbolRelatedToPreviousError(emitted_forwarders[t.MemberDefinition].Location, null);
                    Report.Error(739, a.Location, "A duplicate type forward of type `{0}'",
                                 TypeManager.CSharpName(t));
                    return;
                }

                emitted_forwarders.Add(t.MemberDefinition, a);

                if (t.MemberDefinition.DeclaringAssembly == this)
                {
                    Report.SymbolRelatedToPreviousError(t);
                    Report.Error(729, a.Location, "Cannot forward type `{0}' because it is defined in this assembly",
                                 TypeManager.CSharpName(t));
                    return;
                }

                if (t.IsNested)
                {
                    Report.Error(730, a.Location, "Cannot forward type `{0}' because it is a nested type",
                                 TypeManager.CSharpName(t));
                    return;
                }

                builder_extra.AddTypeForwarder(t.GetDefinition(), a.Location);
                return;
            }

            if (a.Type == pa.Extension)
            {
                a.Error_MisusedExtensionAttribute();
                return;
            }

            if (a.Type == pa.InternalsVisibleTo)
            {
                string assembly_name = a.GetString();
                if (assembly_name.Length == 0)
                {
                    return;
                }
#if STATIC
                ParsedAssemblyName  aname;
                ParseAssemblyResult r = Fusion.ParseAssemblyName(assembly_name, out aname);
                if (r != ParseAssemblyResult.OK)
                {
                    Report.Warning(1700, 3, a.Location, "Assembly reference `{0}' is invalid and cannot be resolved",
                                   assembly_name);
                    return;
                }

                if (aname.Version != null || aname.Culture != null || aname.ProcessorArchitecture != ProcessorArchitecture.None)
                {
                    Report.Error(1725, a.Location,
                                 "Friend assembly reference `{0}' is invalid. InternalsVisibleTo declarations cannot have a version, culture or processor architecture specified",
                                 assembly_name);

                    return;
                }

                if (public_key != null && !aname.HasPublicKey)
                {
                    Report.Error(1726, a.Location,
                                 "Friend assembly reference `{0}' is invalid. Strong named assemblies must specify a public key in their InternalsVisibleTo declarations",
                                 assembly_name);
                    return;
                }
#endif
            }
            else if (a.Type == pa.RuntimeCompatibility)
            {
                wrap_non_exception_throws_custom = true;
            }
            else if (a.Type == pa.AssemblyFileVersion)
            {
                string value = a.GetString();
                if (string.IsNullOrEmpty(value) || IsValidAssemblyVersion(value, false) == null)
                {
                    Report.Warning(1607, 1, a.Location, "The version number `{0}' specified for `{1}' is invalid",
                                   value, a.Name);
                    return;
                }
            }


            SetCustomAttribute(ctor, cdata);
        }
Esempio n. 7
0
 public static void Error_VariableOfStaticClass(Location loc, string variable_name, TypeSpec static_class, Report Report)
 {
     Report.SymbolRelatedToPreviousError(static_class);
     Report.Error(723, loc, "`{0}': cannot declare variables of static types",
                  variable_name);
 }
Esempio n. 8
0
		public static void Error_VariableOfStaticClass (Location loc, string variable_name, TypeSpec static_class, Report Report)
		{
			Report.SymbolRelatedToPreviousError (static_class);
			Report.Error (723, loc, "`{0}': cannot declare variables of static types",
				variable_name);
		}