Example #1
0
 public override IAnalysisSet BinaryOperation(Node node, AnalysisUnit unit, PythonOperator operation, IAnalysisSet rhs) {
     switch (operation) {
         case PythonOperator.GreaterThan:
         case PythonOperator.LessThan:
         case PythonOperator.LessThanOrEqual:
         case PythonOperator.GreaterThanOrEqual:
         case PythonOperator.Equal:
         case PythonOperator.NotEqual:
         case PythonOperator.Is:
         case PythonOperator.IsNot:
             return ProjectState.ClassInfos[BuiltinTypeId.Bool].Instance;
         case PythonOperator.TrueDivide:
         case PythonOperator.Add:
         case PythonOperator.Subtract:
         case PythonOperator.Multiply:
         case PythonOperator.MatMultiply:
         case PythonOperator.Divide:
         case PythonOperator.Mod:
         case PythonOperator.BitwiseAnd:
         case PythonOperator.BitwiseOr:
         case PythonOperator.Xor:
         case PythonOperator.LeftShift:
         case PythonOperator.RightShift:
         case PythonOperator.Power:
         case PythonOperator.FloorDivide:
             return ConstantInfo.NumericOp(node, this, unit, operation, rhs) ?? CallReverseBinaryOp(node, unit, operation, rhs);
     }
     return CallReverseBinaryOp(node, unit, operation, rhs);
 }
        public override IAnalysisSet Call(Node node, AnalysisUnit unit, IAnalysisSet[] args, NameExpression[] keywordArgNames) {
            if (args.Length == 1) {
                var res = unit.Scope.GetOrMakeNodeValue(
                    node,
                    (node_) => MakeFromIndexes(node_, unit.ProjectEntry)
                ) as SequenceInfo;

                List<IAnalysisSet> seqTypes = new List<IAnalysisSet>();
                foreach (var type in args[0]) {
                    SequenceInfo seqInfo = type as SequenceInfo;
                    if (seqInfo != null) {
                        for (int i = 0; i < seqInfo.IndexTypes.Length; i++) {
                            if (seqTypes.Count == i) {
                                seqTypes.Add(seqInfo.IndexTypes[i].Types);
                            } else {
                                seqTypes[i] = seqTypes[i].Union(seqInfo.IndexTypes[i].Types);
                            }
                        }
                    } else {
                        var defaultIndexType = type.GetIndex(node, unit, ProjectState.GetConstant(0));
                        if (seqTypes.Count == 0) {
                            seqTypes.Add(defaultIndexType);
                        } else {
                            seqTypes[0] = seqTypes[0].Union(defaultIndexType);
                        }
                    }
                }

                res.AddTypes(unit, seqTypes.ToArray());

                return res;
            }

            return base.Call(node, unit, args, keywordArgNames);
        }
Example #3
0
 public override IAnalysisSet BinaryOperation(Node node, AnalysisUnit unit, PythonOperator operation, IAnalysisSet rhs) {
     var res = AnalysisSet.Empty;
     foreach (var member in _members) {
         res = res.Union(member.BinaryOperation(node, unit, operation, rhs));
     }
     return res;
 }
Example #4
0
 public override IAnalysisSet Call(Node node, AnalysisUnit unit, IAnalysisSet[] args, NameExpression[] keywordArgNames) {
     var res = AnalysisSet.Empty;
     foreach (var member in _members) {
         res = res.Union(member.Call(node, unit, args, keywordArgNames));
     }
     return res;
 }
Example #5
0
 /// <summary>
 /// Performs a SetMember operation for the given name and propagates the
 /// given values types for the provided member name.
 /// </summary>
 public static void SetMember(this IAnalysisSet self, Node node, AnalysisUnit unit, string name, IAnalysisSet value) {
     if (name != null && name.Length > 0) {
         foreach (var ns in self) {
             ns.SetMember(node, unit, name, value);
         }
     }
 }
Example #6
0
        public override IAnalysisSet BinaryOperation(Node node, AnalysisUnit unit, Parsing.PythonOperator operation, IAnalysisSet rhs) {
            if (_original == null) {
                return base.BinaryOperation(node, unit, operation, rhs);
            }

            return _original.BinaryOperation(node, unit, operation, rhs);
        }
Example #7
0
        public override IAnalysisSet Call(Node node, AnalysisUnit unit, IAnalysisSet[] args, NameExpression[] keywordArgNames) {
            var res = (DictionaryInfo)unit.Scope.GetOrMakeNodeValue(
                node,
                NodeValueKind.Dictionary,
                (node_) => new DictionaryInfo(unit.ProjectEntry, node)
            );

            if (keywordArgNames.Length > 0) {
                for (int i = 0; i < keywordArgNames.Length; i++) {
                    var curName = keywordArgNames[i].Name;
                    var curArg = args[args.Length - keywordArgNames.Length + i];
                    if (curName == "**") {
                        foreach (var value in curArg) {
                            CopyFrom(args, res);
                        }
                    } else if (curName != "*") {
                        res.AddTypes(
                            node,
                            unit,
                            ProjectState.GetConstant(curName),
                            curArg
                        );
                    }
                }
            } else if (args.Length == 1) {
                foreach (var value in args[0]) {
                    CopyFrom(args, res);
                }
            }
            return res;
        }
Example #8
0
 internal bool AddTypes(Node node, AnalysisUnit unit, IAnalysisSet key, IAnalysisSet value, bool enqueue = true) {
     if (_keysAndValues.AddTypes(unit, key, value, enqueue)) {
         if (_keysVariable != null) {
             _keysVariable.MakeUnionStrongerIfMoreThan(ProjectState.Limits.DictKeyTypes, value);
             if (_keysVariable.AddTypes(unit, key, enqueue)) {
                 if (_keysIter != null) {
                     _keysIter.UnionType = null;
                 }
                 if (_keysList != null) {
                     _keysList.UnionType = null;
                 }
             }
         }
         if (_valuesVariable != null) {
             _valuesVariable.MakeUnionStrongerIfMoreThan(ProjectState.Limits.DictValueTypes, value);
             if (_valuesVariable.AddTypes(unit, value, enqueue)) {
                 if (_valuesIter != null) {
                     _valuesIter.UnionType = null;
                 }
                 if (_valuesList != null) {
                     _valuesList.UnionType = null;
                 }
             }
         }
         if (_keyValueTuple != null) {
             _keyValueTuple.IndexTypes[0].MakeUnionStrongerIfMoreThan(ProjectState.Limits.DictKeyTypes, key);
             _keyValueTuple.IndexTypes[1].MakeUnionStrongerIfMoreThan(ProjectState.Limits.DictValueTypes, value);
             _keyValueTuple.IndexTypes[0].AddTypes(unit, key, enqueue);
             _keyValueTuple.IndexTypes[1].AddTypes(unit, value, enqueue);
         }
         return true;
     }
     return false;
 }
        public override IAnalysisSet Construct(Node node, AnalysisUnit unit, IAnalysisSet[] args) {
            var result = Call(node, unit, _instance.Proxy, args);
            if (result.Count != 0) {
                // function returned a value, we want to return any values
                // which are typed to object.
                foreach (var resultValue in result) {
                    if (!resultValue.Value.IsObject) {
                        // we need to do some filtering
                        var tmpRes = AnalysisSet.Empty;
                        foreach (var resultValue2 in result) {
                            if (resultValue2.Value.IsObject) {
                                tmpRes = tmpRes.Add(resultValue2);
                            }
                        }
                        result = tmpRes;
                        break;
                    }
                }

                if (result.Count != 0) {
                    return result;
                }
            }
            // we didn't return a value or returned a non-object
            // value.  The result is our newly created instance object.
            return _instance.Proxy;
        }
        private static IAnalysisSet CloneSpecializationImpl(FunctionValue func, Node node, AnalysisUnit unit, IAnalysisSet @this, IAnalysisSet[] args) {
            if (args.Length > 0) {
                return args[0];
            }

            return AnalysisSet.Empty;            
        }
Example #11
0
        public override IAnalysisSet BinaryOperation(Node node, AnalysisUnit unit, PythonOperator operation, IAnalysisSet rhs) {
            IAnalysisSet res;

            switch (operation) {
                case PythonOperator.BitwiseOr:
                    var seq = (SetInfo)unit.Scope.GetOrMakeNodeValue(
                        node,
                        _ => new SetInfo(ProjectState, node, unit.ProjectEntry)
                    );
                    seq.AddTypes(unit, GetEnumeratorTypes(node, unit));
                    foreach (var type in rhs.Where(t => t.IsOfType(ClassInfo))) {
                        seq.AddTypes(unit, type.GetEnumeratorTypes(node, unit));
                    }
                    res = seq;
                    break;
                case PythonOperator.BitwiseAnd:
                case PythonOperator.ExclusiveOr:
                case PythonOperator.Subtract:
                    res = this;
                    break;
                default:
                    res = CallReverseBinaryOp(node, unit, operation, rhs);
                    break;
            }

            return res;
        }
Example #12
0
            public override IAnalysisSet GetIndex(Node node, AnalysisUnit unit, IAnalysisSet index) {
                var res = base.GetIndex(node, unit, index);

                var names = index.OfType<ConstantInfo>()
                    .Select(ci => ci.GetConstantValueAsString())
                    .Where(s => !string.IsNullOrEmpty(s))
                    .Distinct()
                    .ToArray();

                if (names.Length != 1) {
                    // Unless you request a specific module by string literal,
                    // you won't get any object out of sys.modules.
                    return AnalysisSet.Empty;
                }

                var name = names[0];

                lock (_owner.Modules) {
                    IAnalysisSet knownValues;
                    if (_owner.Modules.TryGetValue(name, out knownValues) &&
                        knownValues != null &&
                        knownValues.Any()
                    ) {
                        return knownValues;
                    }
                }

                ModuleReference modRef;
                if (unit.ProjectState.Modules.TryImport(name, out modRef)) {
                    return modRef.AnalysisModule;
                }

                return AnalysisSet.Empty;
            }
 public override IAnalysisSet BinaryOperation(Node node, AnalysisUnit unit, Parsing.PythonOperator operation, IAnalysisSet rhs) {
     var res = AnalysisSet.Empty;
     switch (operation) {
         case PythonOperator.Add:
             foreach (var type in rhs) {
                 if (type.IsOfType(ClassInfo)) {
                     res = res.Union(ClassInfo.Instance);
                 } else {
                     res = res.Union(type.ReverseBinaryOperation(node, unit, operation, SelfSet));
                 }
             }
             break;
         case PythonOperator.Mod:
             if (_supportsMod) {
                 res = SelfSet;
             }
             break;
         case PythonOperator.Multiply:
             foreach (var type in rhs) {
                 if (type.IsOfType(ProjectState.ClassInfos[BuiltinTypeId.Int]) || type.IsOfType(ProjectState.ClassInfos[BuiltinTypeId.Long])) {
                     res = res.Union(ClassInfo.Instance);
                 } else {
                     var partialRes = ConstantInfo.NumericOp(node, this, unit, operation, rhs);
                     if (partialRes != null) {
                         res = res.Union(partialRes);
                     }
                 }
             }
             break;
     }
     return res ?? base.BinaryOperation(node, unit, operation, rhs);
 }
Example #14
0
        private IAnalysisSet ListInsert(Node node, AnalysisUnit unit, IAnalysisSet[] args, NameExpression[] keywordArgNames) {
            if (args.Length == 2) {
                AppendItem(node, unit, args[1]);
            }

            return unit.ProjectState._noneInst;
        }
Example #15
0
        public override IAnalysisSet Call(Node node, AnalysisUnit unit, IAnalysisSet[] args, NameExpression[] keywordArgNames) {
            if (_original == null) {
                return base.Call(node, unit, args, keywordArgNames);
            }

            return _original.Call(node, unit, args, keywordArgNames);
        }
Example #16
0
        public override void AugmentAssign(AugmentedAssignStatement node, AnalysisUnit unit, IAnalysisSet value) {
            if (_original == null) {
                base.AugmentAssign(node, unit, value);
                return;
            }

            _original.AugmentAssign(node, unit, value);
        }
Example #17
0
        public override bool AssignVariable(string name, Parsing.Ast.Node location, AnalysisUnit unit, IAnalysisSet values) {
            if (base.AssignVariable(name, location, unit, values)) {
                Module.ModuleDefinition.EnqueueDependents();
                return true;
            }

            return false;
        }
        public override IAnalysisSet Call(Node node, AnalysisUnit unit, IAnalysisSet @this, IAnalysisSet[] args) {
            var res = _call(this, node, unit, @this, args);

            if (_callBase) {
                return res.Union(base.Call(node, unit, @this, args));
            }
            return res;
        }
 private IAnalysisSet SequenceIter(Node node, AnalysisUnit unit, IAnalysisSet[] args, NameExpression[] keywordArgNames) {
     if (_iterator == null) {
         var types = new [] { new VariableDef() };
         types[0].AddTypes(unit, _indexTypes, false);
         _iterator = new IteratorInfo(types, IteratorInfo.GetIteratorTypeFromType(ClassInfo, unit), node);
     }
     return _iterator ?? AnalysisSet.Empty;
 }
Example #20
0
        public override IAnalysisSet GetIndex(Node node, AnalysisUnit unit, IAnalysisSet index) {
            IAnalysisSet res;
            if (TryInvokeMethod(node, unit, "__getitem__", new[] { index }, out res)) {
                return res;
            }

            return _instances.GetIndex(node, unit, index);
        }
Example #21
0
        private IAnalysisSet ListExtend(Node node, AnalysisUnit unit, IAnalysisSet[] args, NameExpression[] keywordArgNames) {
            if (args.Length == 1) {
                foreach (var type in args[0]) {
                    AppendItem(node, unit, type.GetEnumeratorTypes(node, unit));
                }
            }

            return unit.ProjectState._noneInst;
        }
Example #22
0
 public SequenceBuiltinClassInfo(IPythonType classObj, PythonAnalyzer projectState)
     : base(classObj, projectState) {
     var seqType = classObj as IPythonSequenceType;
     if (seqType != null && seqType.IndexTypes != null) {
         _indexTypes = projectState.GetAnalysisSetFromObjects(seqType.IndexTypes).GetInstanceType();
     } else {
         _indexTypes = AnalysisSet.Empty;
     }
 }
Example #23
0
 private IAnalysisSet IterableIter(Node node, AnalysisUnit unit, IAnalysisSet[] args, NameExpression[] keywordArgNames) {
     if (args.Length == 0) {
         return unit.Scope.GetOrMakeNodeValue(
             node,
             NodeValueKind.Iterator,
             n => MakeIteratorInfo(n, unit)
         );
     }
     return AnalysisSet.Empty;
 }
Example #24
0
        public override bool AssignVariable(string name, Node location, AnalysisUnit unit, IAnalysisSet values) {
            var res = base.AssignVariable(name, location, unit, values);

            if (name == "__metaclass__") {
                // assignment to __metaclass__, save it in our metaclass variable
                Class.GetOrCreateMetaclassVariable().AddTypes(unit, values);
            }

            return res;
        }
Example #25
0
 internal void AddReturnTypes(Node node, AnalysisUnit unit, IAnalysisSet types, bool enqueue = true) {
     if (Coroutine != null) {
         Coroutine.AddReturn(node, unit, types, enqueue);
     } else if (Generator != null) {
         Generator.AddReturn(node, unit, types, enqueue);
     } else {
         ReturnValue.MakeUnionStrongerIfMoreThan(unit.ProjectState.Limits.ReturnTypes, types);
         ReturnValue.AddTypes(unit, types, enqueue);
     }
 }
Example #26
0
 private IAnalysisSet ObjectSetAttr(Node node, Analysis.AnalysisUnit unit, IAnalysisSet[] args, NameExpression[] keywordArgNames) {
     if (args.Length >= 3) {
         foreach (var ii in args[0].OfType<InstanceInfo>()) {
             foreach (var key in args[1].GetConstantValueAsString()) {
                 ii.SetMember(node, unit, key, args[2]);
             }
         }
     }
     return AnalysisSet.Empty;
 }
Example #27
0
        internal static IAnalysisSet NumericOp(Node node, BuiltinInstanceInfo lhs, AnalysisUnit unit, PythonOperator operation, IAnalysisSet rhs) {
            var res = AnalysisSet.Empty;
            var lhsType = lhs.TypeId;

            foreach(var ns in rhs) {
                var rhsType = ns.TypeId;

                // First handle string operations
                if (lhsType == BuiltinTypeId.Bytes || lhsType == BuiltinTypeId.Unicode) {
                    if (operation == PythonOperator.Mod) {
                        res = res.Union(lhs.ClassInfo.Instance);
                    } else if (operation == PythonOperator.Add &&
                        (rhsType == BuiltinTypeId.Bytes || rhsType == BuiltinTypeId.Unicode)) {
                        res = res.Union(lhs.ClassInfo.Instance);
                    } else if (operation == PythonOperator.Multiply &&
                        (rhsType == BuiltinTypeId.Int || rhsType == BuiltinTypeId.Long)) {
                        res = res.Union(lhs.ClassInfo.Instance);
                    }
                    continue;
                } else if (operation == PythonOperator.Multiply &&
                           (lhsType == BuiltinTypeId.Int || lhsType == BuiltinTypeId.Long)) {
                    if (rhsType == BuiltinTypeId.Str || rhsType == BuiltinTypeId.Bytes || rhsType == BuiltinTypeId.Unicode ||
                        rhsType == BuiltinTypeId.Tuple || rhsType == BuiltinTypeId.List) {
                        res = res.Union(unit.ProjectState.ClassInfos[rhsType].Instance);
                        continue;
                    }
                }

                // These specializations change rhsType before type promotion
                // rules are applied.
                if ((operation == PythonOperator.TrueDivide || 
                    (operation == PythonOperator.Divide && unit.ProjectState.LanguageVersion.Is3x())) &&
                    (lhsType == BuiltinTypeId.Int || lhsType == BuiltinTypeId.Long) &&
                    (rhsType == BuiltinTypeId.Int || rhsType == BuiltinTypeId.Long)) {
                    rhsType = BuiltinTypeId.Float;
                }

                // Type promotion rules are applied 
                if (lhsType == BuiltinTypeId.Unknown || lhsType > BuiltinTypeId.Complex || 
                    rhsType == BuiltinTypeId.Unknown || rhsType > BuiltinTypeId.Complex) {
                    // Non-numeric types require the reverse operation
                    res = res.Union(ns.ReverseBinaryOperation(node, unit, operation, lhs));
                } else if (lhsType == BuiltinTypeId.Complex || rhsType == BuiltinTypeId.Complex) {
                    res = res.Union(unit.ProjectState.ClassInfos[BuiltinTypeId.Complex].Instance);
                } else if (lhsType == BuiltinTypeId.Float || rhsType == BuiltinTypeId.Float) {
                    res = res.Union(unit.ProjectState.ClassInfos[BuiltinTypeId.Float].Instance);
                } else if (lhsType == BuiltinTypeId.Long || rhsType == BuiltinTypeId.Long) {
                    res = res.Union(unit.ProjectState.ClassInfos[BuiltinTypeId.Long].Instance);
                } else {
                    res = res.Union(unit.ProjectState.ClassInfos[BuiltinTypeId.Int].Instance);
                }
            }

            return res.Count > 0 ? res : null;
        }
Example #28
0
        /// <summary>
        /// Performs a call operation propagating the argument types into any
        /// user defined functions or classes and returns the set of types which
        /// result from the call.
        /// </summary>
        public static IAnalysisSet Call(this IAnalysisSet self, Node node, AnalysisUnit unit, IAnalysisSet[] args, NameExpression[] keywordArgNames) {
            var res = AnalysisSet.Empty;
            foreach (var ns in self) {
                var call = ns.Call(node, unit, args, keywordArgNames);
                Debug.Assert(call != null);

                res = res.Union(call);
            }

            return res;
        }
Example #29
0
        public override IAnalysisSet Call(Node node, AnalysisUnit unit, IAnalysisSet[] args, NameExpression[] keywordArgNames) {
            var newArgs = _args.Take(_args.Length - _keywordArgNames.Length)
                .Concat(args.Take(args.Length - keywordArgNames.Length))
                .Concat(_args.Skip(_args.Length - _keywordArgNames.Length))
                .Concat(args.Skip(args.Length - keywordArgNames.Length))
                .ToArray();

            var newKwArgs = _keywordArgNames.Concat(keywordArgNames).ToArray();

            return _function.Call(node, unit, newArgs, newKwArgs);
        }
Example #30
0
        public override IAnalysisSet GetIndex(Node node, AnalysisUnit unit, IAnalysisSet index) {
            // TODO: Return correct index value if we have a constant
            /*int? constIndex = SequenceInfo.GetConstantIndex(index);

            if (constIndex != null && constIndex.Value < _indexTypes.Count) {
                // TODO: Warn if outside known index and no appends?
                return _indexTypes[constIndex.Value];
            }*/

            return ProjectState.ClassInfos[BuiltinTypeId.Int].Instance;
        }