Example #1
0
        /// <summary>
        /// Checks if 'symbol' is accessible from within named type 'within'.  If 'symbol' is accessed off
        /// of an expression then 'throughTypeOpt' is the type of that expression. This is needed to
        /// properly do protected access checks.
        /// </summary>
        public static bool IsSymbolAccessible(
            Symbol symbol,
            NamedTypeSymbol within,
            NamedTypeSymbol throughTypeOpt = null)
        {
            if ((object)symbol == null)
            {
                throw new ArgumentNullException("symbol");
            }

            if ((object)within == null)
            {
                throw new ArgumentNullException("within");
            }

            HashSet <DiagnosticInfo> useSiteDiagnostics = null;

            return(AccessCheck.IsSymbolAccessible(
                       symbol,
                       within,
                       ref useSiteDiagnostics,
                       throughTypeOpt));
        }
Example #2
0
        public static bool IsSymbolAccessible(
            Symbol symbol,
            NamedTypeSymbol within,
            NamedTypeSymbol throughTypeOpt = null)
        {
            if ((object)symbol == null)
            {
                throw new ArgumentNullException(nameof(symbol));
            }

            if ((object)within == null)
            {
                throw new ArgumentNullException(nameof(within));
            }

            var discardedUseSiteInfo = CompoundUseSiteInfo <AssemblySymbol> .Discarded;

            return(AccessCheck.IsSymbolAccessible(
                       symbol,
                       within,
                       ref discardedUseSiteInfo,
                       throughTypeOpt));
        }
Example #3
0
        internal static BoundCall GenerateObjectConstructorInitializer(MethodSymbol constructor, DiagnosticBag diagnostics)
        {
            NamedTypeSymbol objectType = constructor.ContainingType.BaseTypeNoUseSiteDiagnostics;

            Debug.Assert(objectType.SpecialType == SpecialType.System_Object);
            MethodSymbol     objectConstructor = null;
            LookupResultKind resultKind        = LookupResultKind.Viable;

            foreach (MethodSymbol objectCtor in objectType.InstanceConstructors)
            {
                if (objectCtor.ParameterCount == 0)
                {
                    objectConstructor = objectCtor;
                    break;
                }
            }

            // UNDONE: If this happens then something is deeply wrong. Should we give a better error?
            if ((object)objectConstructor == null)
            {
                diagnostics.Add(ErrorCode.ERR_BadCtorArgCount, constructor.Locations[0], objectType, /*desired param count*/ 0);
                return(null);
            }

            // UNDONE: If this happens then something is deeply wrong. Should we give a better error?
            bool hasErrors = false;
            HashSet <DiagnosticInfo> useSiteDiagnostics = null;

            if (!AccessCheck.IsSymbolAccessible(objectConstructor, constructor.ContainingType, ref useSiteDiagnostics))
            {
                diagnostics.Add(ErrorCode.ERR_BadAccess, constructor.Locations[0], objectConstructor);
                resultKind = LookupResultKind.Inaccessible;
                hasErrors  = true;
            }

            if (!useSiteDiagnostics.IsNullOrEmpty())
            {
                diagnostics.Add(constructor.Locations.IsEmpty ? NoLocation.Singleton : constructor.Locations[0], useSiteDiagnostics);
            }

            CSharpSyntaxNode syntax = constructor.GetNonNullSyntaxNode();

            BoundExpression receiver = new BoundThisReference(syntax, constructor.ContainingType)
            {
                WasCompilerGenerated = true
            };

            return(new BoundCall(
                       syntax: syntax,
                       receiverOpt: receiver,
                       method: objectConstructor,
                       arguments: ImmutableArray <BoundExpression> .Empty,
                       argumentNamesOpt: ImmutableArray <string> .Empty,
                       argumentRefKindsOpt: ImmutableArray <RefKind> .Empty,
                       isDelegateCall: false,
                       expanded: false,
                       invokedAsExtensionMethod: false,
                       argsToParamsOpt: ImmutableArray <int> .Empty,
                       resultKind: resultKind,
                       type: objectType,
                       hasErrors: hasErrors)
            {
                WasCompilerGenerated = true
            });
        }
Example #4
0
        private FieldSymbol GetActualInstanceField(Symbol member, NamedTypeSymbol type)
        {
            if (!member.IsStatic)
            {
                HashSet <DiagnosticInfo> useSiteDiagnostics = null;

                switch (member.Kind)
                {
                case SymbolKind.Field:
                    var field = (FieldSymbol)member;
                    if (!field.IsFixed && ((object)this.accessor == null || AccessCheck.IsSymbolAccessible(field, this.accessor, ref useSiteDiagnostics)))
                    {
                        return(field.AsMember(type));
                    }
                    break;

                case SymbolKind.Event:
                    EventSymbol eventSymbol = (EventSymbol)member;
                    if (eventSymbol.HasAssociatedField && ((object)this.accessor == null || AccessCheck.IsSymbolAccessible(eventSymbol, this.accessor, ref useSiteDiagnostics)))
                    {
                        return(eventSymbol.AssociatedField.AsMember(type));
                    }
                    break;
                }
            }
            return(null);
        }