private IMember GetValueFromIndex(IndexExpression expr, LookupOptions options)
        {
            if (expr == null || expr.Target == null)
            {
                return(null);
            }

            if (expr.Index is SliceExpression || expr.Index is TupleExpression)
            {
                // When slicing, assume result is the same type
                return(GetValueFromExpression(expr.Target));
            }

            var type = GetTypeFromValue(GetValueFromExpression(expr.Target));

            if (type != null && type != _unknownType)
            {
                if (AstTypingModule.IsTypingType(type))
                {
                    return(type);
                }

                switch (type.TypeId)
                {
                case BuiltinTypeId.Bytes:
                    if (Ast.LanguageVersion.Is3x())
                    {
                        return(new AstPythonConstant(Interpreter.GetBuiltinType(BuiltinTypeId.Int), GetLoc(expr)));
                    }
                    else
                    {
                        return(new AstPythonConstant(Interpreter.GetBuiltinType(BuiltinTypeId.Bytes), GetLoc(expr)));
                    }

                case BuiltinTypeId.Unicode:
                    return(new AstPythonConstant(Interpreter.GetBuiltinType(BuiltinTypeId.Unicode), GetLoc(expr)));
                }

                if (type.MemberType == PythonMemberType.Class)
                {
                    // When indexing into a type, assume result is the type
                    // TODO: Proper generic handling
                    return(type);
                }

                _log?.Log(TraceLevel.Verbose, "UnknownIndex", type.TypeId, expr.ToCodeString(Ast, CodeFormattingOptions.Traditional).Trim());
            }
            else
            {
                _log?.Log(TraceLevel.Verbose, "UnknownIndex", expr.ToCodeString(Ast, CodeFormattingOptions.Traditional).Trim());
            }
            return(new AstPythonConstant(_unknownType, GetLoc(expr)));
        }
Exemplo n.º 2
0
        private IMember GetValueFromIndex(IndexExpression expr, LookupOptions options)
        {
            if (expr == null || expr.Target == null)
            {
                return(null);
            }

            if (expr.Index is SliceExpression || expr.Index is TupleExpression)
            {
                // When slicing, assume result is the same type
                return(GetValueFromExpression(expr.Target));
            }

            var type = GetTypeFromLiteral(expr.Target);

            if (type != null)
            {
                switch (type.TypeId)
                {
                case BuiltinTypeId.Bytes:
                    if (Ast.LanguageVersion.Is3x())
                    {
                        return(new AstPythonConstant(Interpreter.GetBuiltinType(BuiltinTypeId.Int), GetLoc(expr)));
                    }
                    else
                    {
                        return(new AstPythonConstant(Interpreter.GetBuiltinType(BuiltinTypeId.Bytes), GetLoc(expr)));
                    }

                case BuiltinTypeId.Unicode:
                    return(new AstPythonConstant(Interpreter.GetBuiltinType(BuiltinTypeId.Unicode), GetLoc(expr)));
                }
            }

            _log?.Log(TraceLevel.Verbose, "UnknownIndex", expr.ToCodeString(Ast).Trim());
            return(new AstPythonConstant(Interpreter.GetBuiltinType(BuiltinTypeId.Unknown), GetLoc(expr)));
        }