Exemplo n.º 1
0
        /// <summary>
        /// Resolves the method if the expression was a member getter (obj.field or type::field).
        /// </summary>
        private void resolveGetMember(Context ctx, GetMemberNode node)
        {
            _InvocationSource = node.Expression;
            var type = _InvocationSource != null
                ? _InvocationSource.Resolve(ctx)
                : ctx.ResolveType(node.StaticType);

            checkTypeInSafeMode(ctx, type);

            if (node.TypeHints != null && node.TypeHints.Count > 0)
                _TypeHints = node.TypeHints.Select(x => ctx.ResolveType(x, true)).ToArray();

            try
            {
                // resolve a normal method
                try
                {
                    _Method = ctx.ResolveMethod(
                        type,
                        node.MemberName,
                        _ArgTypes,
                        _TypeHints,
                        (idx, types) => ctx.ResolveLambda(Arguments[idx] as LambdaNode, types)
                    );

                    if (_Method.IsStatic)
                        _InvocationSource = null;

                    return;
                }
                catch (KeyNotFoundException)
                {
                    if (_InvocationSource == null)
                        throw;
                }

                // resolve a callable field
                try
                {
                    ctx.ResolveField(type, node.MemberName);
                    resolveExpression(ctx, node);
                    return;
                }
                catch (KeyNotFoundException) { }

                // resolve a callable property
                try
                {
                    ctx.ResolveProperty(type, node.MemberName);
                    resolveExpression(ctx, node);
                    return;
                }
                catch (KeyNotFoundException) { }

                Arguments = (Arguments[0] is UnitNode)
                    ? new List<NodeBase> {_InvocationSource}
                    : new[] {_InvocationSource}.Union(Arguments).ToList();

                var oldArgTypes = _ArgTypes;
                _ArgTypes = Arguments.Select(a => a.Resolve(ctx)).ToArray();
                _InvocationSource = null;

                try
                {
                    // resolve a local function that is implicitly used as an extension method
                    _Method = ctx.ResolveMethod(
                        ctx.MainType.TypeInfo,
                        node.MemberName,
                        _ArgTypes,
                        resolver: (idx, types) => ctx.ResolveLambda(Arguments[idx] as LambdaNode, types)
                    );

                    return;
                }
                catch (KeyNotFoundException) { }

                // resolve a declared extension method
                // most time-consuming operation, therefore is last checked
                try
                {
                    if(!ctx.Options.AllowExtensionMethods)
                        throw new KeyNotFoundException();

                    _Method = ctx.ResolveExtensionMethod(
                        type,
                        node.MemberName,
                        oldArgTypes,
                        _TypeHints,
                        (idx, types) => ctx.ResolveLambda(Arguments[idx] as LambdaNode, types)
                    );
                }
                catch (KeyNotFoundException)
                {
                    var msg = node.StaticType != null
                        ? CompilerMessages.TypeStaticMethodNotFound
                        : CompilerMessages.TypeMethodNotFound;

                    error(msg, type, node.MemberName);
                }
            }
            catch (AmbiguousMatchException)
            {
                error(CompilerMessages.TypeMethodInvocationAmbiguous, type, node.MemberName);
            }
        }
Exemplo n.º 2
0
        private void resolveGetMember(Context ctx, GetMemberNode node)
        {
            m_InvocationSource = node.Expression;
            var type = m_InvocationSource != null
                ? m_InvocationSource.GetExpressionType(ctx)
                : ctx.ResolveType(node.StaticType);

            SafeModeCheckType(ctx, type);

            if (node.TypeHints.Any())
                m_TypeHints = node.TypeHints.Select(x => x.FullSignature == "_" ? null : ctx.ResolveType(x)).ToArray();

            try
            {
                // resolve a normal method
                try
                {
                    m_Method = ctx.ResolveMethod(type, node.MemberName, m_ArgTypes, m_TypeHints);

                    if (m_Method.IsStatic)
                        m_InvocationSource = null;

                    return;
                }
                catch (KeyNotFoundException)
                {
                    if (m_InvocationSource == null)
                        throw;
                }

                // resolve a callable field
                try
                {
                    ctx.ResolveField(type, node.MemberName);
                    resolveExpression(ctx, node);
                    return;
                }
                catch (KeyNotFoundException) { }

                // resolve a callable field
                try
                {
                    ctx.ResolveProperty(type, node.MemberName);
                    resolveExpression(ctx, node);
                    return;
                }
                catch (KeyNotFoundException) { }

                // resolve a local function that is implicitly used as an extension method
                // move invocation source to arguments
                if (Arguments[0] is UnitNode)
                    Arguments[0] = m_InvocationSource;
                else
                    Arguments.Insert(0, m_InvocationSource);

                var oldArgTypes = m_ArgTypes;
                m_ArgTypes = Arguments.Select(a => a.GetExpressionType(ctx)).ToArray();
                m_InvocationSource = null;

                try
                {
                    m_Method = ctx.ResolveMethod(ctx.MainType.TypeInfo, node.MemberName, m_ArgTypes);
                }
                catch (KeyNotFoundException)
                {
                    // resolve a declared extension method
                    // most time-consuming operation, therefore is last checked
                    if(ctx.Options.AllowExtensionMethods)
                        m_Method = ctx.ResolveExtensionMethod(type, node.MemberName, oldArgTypes, m_TypeHints);
                }
            }
            catch (AmbiguousMatchException)
            {
                Error(CompilerMessages.TypeMethodInvocationAmbiguous, type, node.MemberName);
            }
            catch (KeyNotFoundException)
            {
                var msg = node.StaticType != null
                    ? CompilerMessages.TypeStaticMethodNotFound
                    : CompilerMessages.TypeMethodNotFound;

                Error(msg, type, node.MemberName);
            }
        }