Esempio n. 1
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;
 }
Esempio n. 2
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);
 }
Esempio n. 3
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;
 }
Esempio n. 4
0
 public DictionaryInfo(ProjectEntry declaringModule, Node node)
     : base(declaringModule.ProjectState.ClassInfos[BuiltinTypeId.Dict]) {
     _keysAndValues = new DependentKeyValue();
     _declaringModule = declaringModule;
     _declVersion = declaringModule.AnalysisVersion;
     _node = node;
 }
Esempio n. 5
0
        public override IAnalysisSet GetMember(Node node, AnalysisUnit unit, string name) {
            // Must unconditionally call the base implementation of GetMember
            var res = base.GetMember(node, unit, name);

            switch(name) {
                case "next":
                    if (unit.ProjectState.LanguageVersion.Is2x()) {
                        return _nextMethod = _nextMethod ?? new SpecializedCallable(
                            res.OfType<BuiltinNamespace<IPythonType>>().FirstOrDefault(),
                            GeneratorNext,
                            false
                        );
                    }
                    break;
                case "__next__":
                    if (unit.ProjectState.LanguageVersion.Is3x()) {
                        return _nextMethod = _nextMethod ?? new SpecializedCallable(
                            res.OfType<BuiltinNamespace<IPythonType>>().FirstOrDefault(),
                            GeneratorNext,
                            false
                        );
                    }
                    break;
                case "send":
                    return _sendMethod = _sendMethod ?? new SpecializedCallable(
                        res.OfType<BuiltinNamespace<IPythonType>>().FirstOrDefault(),
                        GeneratorSend,
                        false
                    );
            }

            return res;
        }
Esempio n. 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);
        }
Esempio n. 7
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;
 }
Esempio n. 8
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);
        }
Esempio n. 9
0
        public override ISet<Namespace> Call(Node node, AnalysisUnit unit, ISet<Namespace>[] args, NameExpression[] keywordArgNames) {
            _generator.Callers.AddDependency(unit);

            _generator.AddReturn(node, unit, base.Call(node, unit, args, keywordArgNames));
            
            return _generator.SelfSet;
        }
Esempio n. 10
0
 private bool ShouldWalkWorker(Node node) {
     if (node is ScopeStatement) {
         _suites.Add(null);  // marker for a function/class boundary
         _parents.Add((ScopeStatement)node);
     }
     return _selectedSpan.IntersectsWith(Span.FromBounds(node.StartIndex, node.EndIndex));
 }
Esempio n. 11
0
 /// <summary>
 /// Performs a delete index operation propagating the index types into
 /// the provided object.
 /// </summary>
 public static void DeleteMember(this IAnalysisSet self, Node node, AnalysisUnit unit, string name) {
     if (name != null && name.Length > 0) {
         foreach (var ns in self) {
             ns.DeleteMember(node, unit, name);
         }
     }
 }
Esempio n. 12
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;
        }
Esempio n. 13
0
        public override IAnalysisSet GetMember(Node node, AnalysisUnit unit, string name) {
            // Must unconditionally call the base implementation of GetMember
            var res = base.GetMember(node, unit, name);

            switch (name) {
                case "append":
                    return _appendMethod = _appendMethod ?? new SpecializedCallable(
                        res.OfType<BuiltinNamespace<IPythonType>>().FirstOrDefault(),
                        ListAppend,
                        false
                    );
                case "pop":
                    return _popMethod = _popMethod ?? new SpecializedCallable(
                        res.OfType<BuiltinNamespace<IPythonType>>().FirstOrDefault(),
                        ListPop,
                        false
                    );
                case "insert":
                    return _insertMethod = _insertMethod ?? new SpecializedCallable(
                        res.OfType<BuiltinNamespace<IPythonType>>().FirstOrDefault(),
                        ListInsert,
                        false
                    );
                case "extend":
                    return _extendMethod = _extendMethod ?? new SpecializedCallable(
                        res.OfType<BuiltinNamespace<IPythonType>>().FirstOrDefault(),
                        ListExtend,
                        false
                    );
            }

            return res;
        }
Esempio n. 14
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;
        }
Esempio n. 15
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;
        }
Esempio n. 16
0
        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);
        }
Esempio n. 17
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;
            }
Esempio n. 18
0
 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);
 }
Esempio n. 19
0
        internal override void AddReference(Node node, AnalysisUnit analysisUnit) {
            if (_original == null) {
                base.AddReference(node, analysisUnit);
                return;
            }

            _original.AddReference(node, analysisUnit);
        }
Esempio n. 20
0
 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;
 }
Esempio n. 21
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);
        }
Esempio n. 22
0
 internal bool TryGetAttribute(Node node, object key, out object value) {
     Dictionary<object, object> nodeAttrs;
     if (_attributes.TryGetValue(node, out nodeAttrs)) {
         return nodeAttrs.TryGetValue(key, out value);
     } else {
         value = null;
     }
     return false;
 }
Esempio n. 23
0
 internal override SequenceInfo MakeFromIndexes(Node node, ProjectEntry entry) {
     if (_indexTypes.Count > 0) {
         var vals = new[] { new VariableDef() };
         vals[0].AddTypes(entry, _indexTypes, false);
         return new ListInfo(vals, this, node, entry);
     } else {
         return new ListInfo(VariableDef.EmptyArray, this, node, entry);
     }
 }
Esempio n. 24
0
        internal LocationInfo GetLoc(Node node) {
            if (node == null || node.StartIndex >= node.EndIndex) {
                return null;
            }

            var start = node.GetStart(_ast);
            var end = node.GetEnd(_ast);
            return new LocationInfo(_filePath, start.Line, start.Column, end.Line, end.Column);
        }
Esempio n. 25
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;
        }
Esempio n. 26
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;
 }
Esempio n. 27
0
        public InterpreterScope(AnalysisValue av, Node ast, InterpreterScope outerScope) {
            _av = av;
            _node = ast;
            OuterScope = outerScope;

            _nodeScopes = new AnalysisDictionary<Node, InterpreterScope>();
            _nodeValues = new AnalysisDictionary<Node, IAnalysisSet>();
            _variables = new AnalysisDictionary<string, VariableDef>();
            _linkedVariables = new AnalysisDictionary<string, HashSet<VariableDef>>();
        }
Esempio n. 28
0
 /// <summary>
 /// Performs a GetMember operation for the given name and returns the
 /// types of variables which are associated with that name.
 /// </summary>
 public static IAnalysisSet GetMember(this IAnalysisSet self, Node node, AnalysisUnit unit, string name) {
     var res = AnalysisSet.Empty;
     // name can be empty if we have "fob."
     if (name != null && name.Length > 0) {
         foreach (var ns in self) {
             res = res.Union(ns.GetMember(node, unit, name));
         }
     }
     return res;
 }
Esempio n. 29
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;
 }
Esempio n. 30
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;
        }