Exemple #1
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;
        }
Exemple #2
0
 public FromImportStatement(ModuleName root, NameExpression/*!*/[] names, NameExpression[] asNames, bool fromFuture, bool forceAbsolute) {
     _root = root;
     _names = names;
     _asNames = asNames;
     _fromFuture = fromFuture;
     _forceAbsolute = forceAbsolute;
 }
        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);
        }
        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;
        }
        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;
        }
        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);
        }
Exemple #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;
 }
 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;
 }
Exemple #9
0
 internal void AddParameterReferences(AnalysisUnit caller, NameExpression[] names) {
     foreach (var name in names) {
         VariableDef param;
         if (name != null && TryGetVariable(name.Name, out param)) {
             param.AddReference(name, caller);
         }
     }
 }
Exemple #10
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;
        }
 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;
 }
Exemple #12
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;
 }
        /// <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;
        }
        public override INamespaceSet Call(Node node, AnalysisUnit unit, INamespaceSet[] args, NameExpression[] keywordArgNames) {
            var realArgs = args;
            if (_inst != null) {
                realArgs = Utils.Concat(_inst.SelfSet, args);
            }

            if (node is CallExpression) {
                return _call((CallExpression)node, unit, realArgs, keywordArgNames) ?? NamespaceSet.Empty;
            }
            return NamespaceSet.Empty;
        }
Exemple #15
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);
        }
Exemple #16
0
        public FunctionDefinition(NameExpression name, Parameter[] parameters, Statement body, DecoratorStatement decorators = null) {
            if (name == null) {
                _name = new NameExpression("<lambda>");
                _isLambda = true;
            } else {
                _name = name;
            }

            _parameters = parameters;
            _body = body;
            _decorators = decorators;
        }
        public override void PostWalk(NameExpression node) {
            if (IsInRange(node)) {
                if(_ast.LanguageVersion.Is2x()) {
                    // In 2.7 True and False are constants, we made an exception to not show them Autos window.
                    if(node.Name == "True" || node.Name == "False") {
                        return;
                    }
                }

                _expressions.Add(node, null);
            }

        }
Exemple #18
0
        public override IAnalysisSet Call(Node node, AnalysisUnit unit, IAnalysisSet[] args, NameExpression[] keywordArgNames) {
            var realArgs = args;
            if (_inst != null) {
                realArgs = Utils.Concat(_inst.SelfSet, args);
            }

            var res = _callable(node, unit, args, keywordArgNames);
            if (_mergeOriginalAnalysis && _original != null) {
                return res.Union(_original.Call(node, unit, args, keywordArgNames));
            }

            return res;
        }
Exemple #19
0
 private IAnalysisSet IterableIter(Node node, AnalysisUnit unit, IAnalysisSet[] args, NameExpression[] keywordArgNames) {
     if (args.Length == 0) {
         return unit.Scope.GetOrMakeNodeValue(
             node,
             n => new IteratorInfo(
                 _indexTypes,
                 IteratorInfo.GetIteratorTypeFromType(ClassInfo, unit),
                 n
             )
         );
     }
     return AnalysisSet.Empty;
 }
 private IAnalysisSet ObjectNew(Node node, Analysis.AnalysisUnit unit, IAnalysisSet[] args, NameExpression[] keywordArgNames) {
     if (args.Length >= 1) {
         var instance = AnalysisSet.Empty;
         foreach (var n in args[0]) {
             var bci = n as BuiltinClassInfo;
             var ci = n as ClassInfo;
             if (bci != null) {
                 instance = instance.Union(bci.Instance);
             } else if (ci != null) {
                 instance = instance.Union(ci.Instance);
             }
         }
         return instance;
     }
     return ProjectState.ClassInfos[BuiltinTypeId.Object].Instance;
 }
Exemple #21
0
        private IAnalysisSet DictionaryIterValues(Node node, Analysis.AnalysisUnit unit, IAnalysisSet[] args, NameExpression[] keywordArgNames) {
            _keysAndValues.AddDependency(unit);

            if (_valuesIter == null) {
                _valuesIter = new IteratorInfo(
                    new[] { ValuesVariable },
                    unit.ProjectState.ClassInfos[BuiltinTypeId.DictValues],
                    node
                );
            }
            return _valuesIter;
        }
Exemple #22
0
        private IAnalysisSet DictionaryValues(Node node, Analysis.AnalysisUnit unit, IAnalysisSet[] args, NameExpression[] keywordArgNames) {
            _keysAndValues.AddDependency(unit);

            if (_valuesList == null) {
                _valuesList = new ListInfo(
                    new[] { ValuesVariable },
                    unit.ProjectState.ClassInfos[BuiltinTypeId.List],
                    node,
                    unit.ProjectEntry
                );
            }
            return _valuesList;
        }
Exemple #23
0
        private IAnalysisSet DictionaryPop(Node node, Analysis.AnalysisUnit unit, IAnalysisSet[] args, NameExpression[] keywordArgNames) {
            _keysAndValues.AddDependency(unit);

            return _keysAndValues.AllValueTypes;
        }
Exemple #24
0
        private IAnalysisSet DictionaryUpdate(Node node, Analysis.AnalysisUnit unit, IAnalysisSet[] args, NameExpression[] keywordArgNames) {
            if (args.Length >= 1) {
                foreach (var otherDict in args[0].OfType<DictionaryInfo>()) {
                    if (!Object.ReferenceEquals(otherDict, this)) {
                        _keysAndValues.CopyFrom(otherDict._keysAndValues);
                    }
                }
            }
            // TODO: Process keyword args and add those values to our dictionary, plus a string key

            return AnalysisSet.Empty;
        }
Exemple #25
0
        public override IAnalysisSet Call(Node node, AnalysisUnit unit, IAnalysisSet[] args, NameExpression[] keywordArgNames) {
            var res = base.Call(node, unit, args, keywordArgNames);

            if (Push()) {
                try {
                    var callRes = GetTypeMember(node, unit, "__call__");
                    if (callRes.Any()) {
                        res = res.Union(callRes.Call(node, unit, args, keywordArgNames));
                    }
                } finally {
                    Pop();
                }
            }

            return res;
        }
Exemple #26
0
 public FunctionDefinition(NameExpression name, Parameter[] parameters)
     : this(name, parameters, (Statement)null)
 {
 }
Exemple #27
0
 public override bool Walk(NameExpression node)
 {
     node.AddVariableReference(_globalScope, _bindRefs, Reference(node.Name));
     return(true);
 }
Exemple #28
0
 public FunctionDefinition(NameExpression name, Parameter[] parameters)
     : this(name, parameters, (Statement)null) {            
 }
Exemple #29
0
        /// <summary>
        /// Returns a new FromImport statement that is identical to this one but has
        /// removed the specified import statement.  Otherwise preserves any attributes
        /// for the statement.
        ///
        /// New in 1.1.
        /// <param name="ast">The parent AST whose attributes should be updated for the new node.</param>
        /// <param name="index">The index in Names of the import to be removed.</param>
        /// </summary>
        public FromImportStatement RemoveImport(PythonAst ast, int index)
        {
            if (index < 0 || index >= Names.Count)
            {
                throw new ArgumentOutOfRangeException("index");
            }
            if (ast == null)
            {
                throw new ArgumentNullException("ast");
            }

            NameExpression[] names            = new NameExpression[Names.Count - 1];
            NameExpression[] asNames          = AsNames == null ? null : new NameExpression[AsNames.Count - 1];
            var           asNameWhiteSpace    = this.GetNamesWhiteSpace(ast);
            List <string> newAsNameWhiteSpace = new List <string>();
            int           importIndex         = ImportIndex;
            int           asIndex             = 0;

            for (int i = 0, write = 0; i < Names.Count; i++)
            {
                bool includingCurrentName = i != index;

                // track the white space, this needs to be kept in sync w/ ToCodeString and how the
                // parser creates the white space.

                if (asNameWhiteSpace != null && asIndex < asNameWhiteSpace.Length)
                {
                    if (write > 0)
                    {
                        if (includingCurrentName)
                        {
                            newAsNameWhiteSpace.Add(asNameWhiteSpace[asIndex++]);
                        }
                        else
                        {
                            asIndex++;
                        }
                    }
                    else if (i > 0)
                    {
                        asIndex++;
                    }
                }

                if (asNameWhiteSpace != null && asIndex < asNameWhiteSpace.Length)
                {
                    if (includingCurrentName)
                    {
                        if (newAsNameWhiteSpace.Count == 0)
                        {
                            // no matter what we want the 1st entry to have the whitespace after the import keyword
                            newAsNameWhiteSpace.Add(asNameWhiteSpace[0]);
                            asIndex++;
                        }
                        else
                        {
                            newAsNameWhiteSpace.Add(asNameWhiteSpace[asIndex++]);
                        }
                    }
                    else
                    {
                        asIndex++;
                    }
                }

                if (includingCurrentName)
                {
                    names[write] = Names[i];

                    if (AsNames != null)
                    {
                        asNames[write] = AsNames[i];
                    }

                    write++;
                }

                if (AsNames != null && AsNames[i] != null)
                {
                    if (asNameWhiteSpace != null && asIndex < asNameWhiteSpace.Length)
                    {
                        if (i != index)
                        {
                            newAsNameWhiteSpace.Add(asNameWhiteSpace[asIndex++]);
                        }
                        else
                        {
                            asIndex++;
                        }
                    }

                    if (AsNames[i].Name.Length != 0)
                    {
                        if (asNameWhiteSpace != null && asIndex < asNameWhiteSpace.Length)
                        {
                            if (i != index)
                            {
                                newAsNameWhiteSpace.Add(asNameWhiteSpace[asIndex++]);
                            }
                            else
                            {
                                asIndex++;
                            }
                        }
                    }
                    else
                    {
                        asIndex++;
                    }
                }
            }

            if (asNameWhiteSpace != null && asIndex < asNameWhiteSpace.Length)
            {
                // trailing comma
                newAsNameWhiteSpace.Add(asNameWhiteSpace[asNameWhiteSpace.Length - 1]);
            }

            var res = new FromImportStatement(Root, names, asNames, IsFromFuture, ForceAbsolute, importIndex);

            ast.CopyAttributes(this, res);
            ast.SetAttribute(res, NodeAttributes.NamesWhiteSpace, newAsNameWhiteSpace.ToArray());

            return(res);
        }
Exemple #30
0
        private IAnalysisSet DictionaryGet(Node node, Analysis.AnalysisUnit unit, IAnalysisSet[] args, NameExpression[] keywordArgNames) {
            _keysAndValues.AddDependency(unit);

            if (args.Length == 1) {
                return _keysAndValues.GetValueType(args[0]);
            } else if (args.Length >= 2) {
                return _keysAndValues.GetValueType(args[0]).Union(args[1]);
            }

            return AnalysisSet.Empty;
        }
 internal void AddNamedParameterReferences(AnalysisUnit caller, NameExpression[] names) {
     ((FunctionScope)Scope).AddParameterReferences(caller, names);
 }
Exemple #32
0
 public PartialFunctionInfo(ProjectEntry declProjEntry, IAnalysisSet function, IAnalysisSet[] args, NameExpression[] keywordArgNames) {
     _declProjEntry = declProjEntry;
     _function = function;
     _args = args;
     _keywordArgNames = keywordArgNames;
 }
Exemple #33
0
 public override bool Walk(NameExpression node)
 {
     _binder.DefineDeleted(node.Name);
     return(false);
 }
Exemple #34
0
 private static bool IsImplicitlyDefinedName(NameExpression nameExpr) {
     return nameExpr.Name == "__all__" ||
         nameExpr.Name == "__file__" ||
         nameExpr.Name == "__doc__" ||
         nameExpr.Name == "__name__";
 }
Exemple #35
0
        private PythonVariable _classVariable;      // Variable for the classes __class__ cell var on 3.x

        public ClassDefinition(NameExpression /*!*/ name, Arg[] bases, Statement body)
        {
            _name  = name;
            _bases = bases;
            _body  = body;
        }