Beispiel #1
0
        public OverloadResolution(ICompilation compilation, ResolveResult[] arguments, string[] argumentNames = null, IType[] typeArguments = null, CSharpConversions conversions = null)
        {
            if (compilation == null)
            {
                throw new ArgumentNullException("compilation");
            }
            if (arguments == null)
            {
                throw new ArgumentNullException("arguments");
            }
            if (argumentNames == null)
            {
                argumentNames = new string[arguments.Length];
            }
            else if (argumentNames.Length != arguments.Length)
            {
                throw new ArgumentException("argumentsNames.Length must be equal to arguments.Length");
            }
            this.compilation   = compilation;
            this.arguments     = arguments;
            this.argumentNames = argumentNames;

            // keep explicitlyGivenTypeArguments==null when no type arguments were specified
            if (typeArguments != null && typeArguments.Length > 0)
            {
                this.explicitlyGivenTypeArguments = typeArguments;
            }

            this.conversions             = conversions ?? CSharpConversions.Get(compilation);
            this.AllowExpandingParams    = true;
            this.AllowOptionalParameters = true;
        }
Beispiel #2
0
 public void Run(AstNode rootNode, TransformContext context)
 {
     this.context     = context;
     this.conversions = CSharpConversions.Get(context.TypeSystem);
     InitializeContext(rootNode.Annotation <UsingScope>());
     rootNode.AcceptVisitor(this);
 }
Beispiel #3
0
        /// <remarks>
        /// See $7.10.10 of C# 4 Spec for details.
        /// </remarks>
        Value Visit(TypeIsResolveResult result)
        {
            var  importedType = NullableType.GetUnderlyingType(Import(result.TargetType));
            var  val          = Convert(result.Input);
            var  conversions  = CSharpConversions.Get(debuggerTypeSystem);
            bool evalResult   = false;

            if (!val.IsNull)
            {
                IType inputType = NullableType.GetUnderlyingType(val.Type);
                if (inputType.Equals(importedType))
                {
                    evalResult = true;
                }
                else if (conversions.IsImplicitReferenceConversion(inputType, importedType))
                {
                    evalResult = true;
                }
                else if (conversions.IsBoxingConversion(inputType, importedType))
                {
                    evalResult = true;
                }
            }
            return(Eval.CreateValue(evalThread, evalResult));
        }
            public override void VisitForeachStatement(ForeachStatement foreachStatement)
            {
                base.VisitForeachStatement(foreachStatement);
                var rr = ctx.Resolve(foreachStatement) as ForEachResolveResult;

                if (rr == null)
                {
                    return;
                }
                if (rr.ElementType.Kind == TypeKind.Unknown)
                {
                    return;
                }
                if (ReflectionHelper.GetTypeCode(rr.ElementType) == TypeCode.Object)
                {
                    return;
                }
                if (conversions == null)
                {
                    conversions = CSharpConversions.Get(ctx.Compilation);
                }
                Conversion c = conversions.ImplicitConversion(rr.ElementType, rr.ElementVariable.Type);

                if (c.IsValid)
                {
                    return;
                }
                var     csResolver   = ctx.GetResolverStateBefore(foreachStatement);
                var     builder      = new TypeSystemAstBuilder(csResolver);
                AstType elementType  = builder.ConvertType(rr.ElementType);
                AstType variableType = foreachStatement.VariableType;
                string  text         = ctx.TranslateString("Collection element type '{0}' is not implicitly convertible to '{1}'");

                AddIssue(variableType, string.Format(text, elementType.GetText(), variableType.GetText()));
            }
Beispiel #5
0
 public TypeInference(ICompilation compilation)
 {
     if (compilation == null)
     {
         throw new ArgumentNullException("compilation");
     }
     this.compilation = compilation;
     this.conversions = CSharpConversions.Get(compilation);
 }
Beispiel #6
0
 /// <summary>
 /// Validates whether the given type argument satisfies the constraints for the given type parameter.
 /// </summary>
 /// <param name="typeParameter">The type parameter.</param>
 /// <param name="typeArgument">The type argument.</param>
 /// <param name="substitution">The substitution that defines how type parameters are replaced with type arguments.
 /// The substitution is used to check constraints that depend on other type parameters (or recursively on the same type parameter).
 /// May be null if no substitution should be used.</param>
 /// <returns>True if the constraints are satisfied; false otherwise.</returns>
 public static bool ValidateConstraints(ITypeParameter typeParameter, IType typeArgument, TypeVisitor substitution = null)
 {
     if (typeParameter == null)
     {
         throw new ArgumentNullException("typeParameter");
     }
     if (typeArgument == null)
     {
         throw new ArgumentNullException("typeArgument");
     }
     return(ValidateConstraints(typeParameter, typeArgument, substitution, CSharpConversions.Get(typeParameter.Owner.Compilation)));
 }
Beispiel #7
0
        internal MetadataContext(HackedSimpleCompilation compilation, EmitSettings settings)
        {
            this.hackedCompilation = compilation;
            Settings          = settings ?? new EmitSettings();
            CSharpConversions = CSharpConversions.Get(compilation);
            moduleMap         = compilation.Modules.ToDictionary(m => new ModuleSignature(m.Name));
            Modules           = moduleMap.Keys.ToImmutableArray();
            MainModule        = Modules[0];
            mutableModule     = (VirtualModule)moduleMap[MainModule];

            Debug.Assert(MainModule.Name == this.Compilation.MainModule.Name);
        }
Beispiel #8
0
        private ExpressionWithResolveResult HandleImplicitConversion(IMethod method, TranslatedExpression argument)
        {
            var   conversions = CSharpConversions.Get(expressionBuilder.compilation);
            IType targetType  = method.ReturnType;
            var   conv        = conversions.ImplicitConversion(argument.Type, targetType);

            if (!(conv.IsUserDefined && conv.Method.Equals(method)))
            {
                // implicit conversion to targetType isn't directly possible, so first insert a cast to the argument type
                argument = argument.ConvertTo(method.Parameters[0].Type, expressionBuilder);
                conv     = conversions.ImplicitConversion(argument.Type, targetType);
            }
            return(new CastExpression(expressionBuilder.ConvertType(targetType), argument.Expression)
                   .WithRR(new ConversionResolveResult(targetType, argument.ResolveResult, conv)));
        }
Beispiel #9
0
            public override void VisitIsExpression(IsExpression isExpression)
            {
                base.VisitIsExpression(isExpression);

                var conversions  = CSharpConversions.Get(ctx.Compilation);
                var exprType     = ctx.Resolve(isExpression.Expression).Type;
                var providedType = ctx.ResolveType(isExpression.Type);

                if (exprType.Kind == TypeKind.Unknown || providedType.Kind == TypeKind.Unknown)
                {
                    return;
                }
                if (IsValidReferenceOrBoxingConversion(exprType, providedType))
                {
                    return;
                }

                var exprTP     = exprType as ITypeParameter;
                var providedTP = providedType as ITypeParameter;

                if (exprTP != null)
                {
                    if (IsValidReferenceOrBoxingConversion(exprTP.EffectiveBaseClass, providedType) &&
                        exprTP.EffectiveInterfaceSet.All(i => IsValidReferenceOrBoxingConversion(i, providedType)))
                    {
                        return;
                    }
                }
                if (providedTP != null)
                {
                    if (IsValidReferenceOrBoxingConversion(exprType, providedTP.EffectiveBaseClass))
                    {
                        return;
                    }
                }

                AddIssue(isExpression, ctx.TranslateString("Given expression is never of the provided type"));
            }
 public GatherVisitor(BaseRefactoringContext ctx)
     : base(ctx)
 {
     conversions = CSharpConversions.Get(ctx.Compilation);
 }
        public override void VisitIndexerExpression(IndexerExpression indexerExpression)
        {
            base.VisitIndexerExpression(indexerExpression);

            var localResolveResult = context.Resolve(indexerExpression.Target) as LocalResolveResult;

            if (localResolveResult == null)
            {
                return;
            }
            var resolveResult = context.Resolve(indexerExpression);

            if (localResolveResult == null)
            {
                return;
            }
            var parent = indexerExpression.Parent;

            while (parent is ParenthesizedExpression)
            {
                parent = parent.Parent;
            }
            if (parent is DirectionExpression)
            {
                // The only types which are indexable and where the indexing expression
                // results in a variable is an actual array type
                AddCriterion(localResolveResult.Variable, new IsArrayTypeCriterion());
            }
            else if (resolveResult is ArrayAccessResolveResult)
            {
                var arrayResolveResult = (ArrayAccessResolveResult)resolveResult;
                var arrayType          = arrayResolveResult.Array.Type as ArrayType;
                if (arrayType != null)
                {
                    var parameterTypes = arrayResolveResult.Indexes.Select(index => index.Type);
                    var criterion      = new SupportsIndexingCriterion(arrayType.ElementType, parameterTypes, CSharpConversions.Get(context.Compilation));
                    AddCriterion(localResolveResult.Variable, criterion);
                }
            }
            else if (resolveResult is CSharpInvocationResolveResult)
            {
                var invocationResolveResult = (CSharpInvocationResolveResult)resolveResult;
                var parameterTypes          = invocationResolveResult.Arguments.Select(arg => arg.Type);
                var criterion = new SupportsIndexingCriterion(invocationResolveResult.Member.ReturnType, parameterTypes, CSharpConversions.Get(context.Compilation));
                AddCriterion(localResolveResult.Variable, criterion);
            }
        }
Beispiel #12
0
        void AssertDoesNotMatch(IType candidateType, IType elementType, bool isWriteAccess, params IType[] indexTypes)
        {
            var criterion = new SupportsIndexingCriterion(elementType, indexTypes, CSharpConversions.Get(compilation), isWriteAccess);

            Assert.IsFalse(criterion.SatisfiedBy(candidateType));
        }