示例#1
0
 internal override AnalysisValue UnionMergeTypes(AnalysisValue ns, int strength) {
     var tns = ns as TestNamespace;
     if (tns == null || object.ReferenceEquals(this, tns)) {
         return this;
     }
     return new TestNamespace { _name = Name, Value = MergeCount > tns.MergeCount ? Value : tns.Value, MergeCount = MergeCount + tns.MergeCount };
 }
示例#2
0
 internal override bool UnionEquals(AnalysisValue ns, int strength) {
     var tns = ns as TestNamespace;
     if (tns == null) {
         return false;
     }
     return Name.Equals(tns.Name);
 }
示例#3
0
        internal override AnalysisValue UnionMergeTypes(AnalysisValue av, int strength) {
            if (strength >= MergeStrength.ToBaseClass) {
                return _analyzer._zeroIntValue;
            }

            return base.UnionMergeTypes(av, strength);
        }
        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;
        }
示例#5
0
文件: ListInfo.cs 项目: omnimark/PTVS
        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;
        }
示例#6
0
        internal override AnalysisValue UnionMergeTypes(AnalysisValue av, int strength) {
            if (strength >= StringUnionStrength) {
                return _analyzer._emptyStringValue;
            }

            return base.UnionMergeTypes(av, strength);
        }
示例#7
0
 private static string GetInstanceShortDescription(AnalysisValue ns) {
     var bci = ns as BuiltinClassInfo;
     if (bci != null) {
         return bci.Instance.ShortDescription;
     }
     return ns.ShortDescription;
 }
示例#8
0
 protected InterpreterScope(AnalysisValue av, InterpreterScope cloned, bool isCloned) {
     Debug.Assert(isCloned);
     _av = av;
     Children.AddRange(cloned.Children);
     _nodeScopes = cloned._nodeScopes;
     _nodeValues = cloned._nodeValues;
     _variables = cloned._variables;
     _linkedVariables = cloned._linkedVariables;
 }
        public ObjectValue(ProjectEntry projectEntry, AnalysisValue prototype = null, string description = null)
            : base(projectEntry) {
            if (prototype != null) {
                Add("__proto__", prototype.SelfSet);
            }
#if DEBUG
            _description = description;
#endif
        }
        internal override AnalysisValue UnionMergeTypes(AnalysisValue ns, int strength) {
            if (strength >= MergeStrength.ToObject) {
                if (ns is InstanceValue) {
                    return ProjectState._immutableObject;
                }
            }

            return base.UnionMergeTypes(ns, strength);
        }
        internal override bool UnionEquals(AnalysisValue ns, int strength) {
            if (strength >= MergeStrength.ToObject) {
                if (ns is InstanceValue) {
                    return true;
                }
            }

            return base.UnionEquals(ns, strength);
        }
示例#12
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>>();
        }
示例#13
0
        public override IAnalysisSet GetTypeMember(Node node, AnalysisUnit unit, string name) {
            var res = base.GetTypeMember(node, unit, name);

            if (name == "__iter__") {
                return _iterMethod = _iterMethod ?? new SpecializedCallable(
                    res.OfType<BuiltinNamespace<IPythonType>>().FirstOrDefault(),
                    IterableIter,
                    false
                );
            }

            return res;
        }
        internal static bool PushProtoLookup(AnalysisValue value){
            if (_hitCount == null) {
                _hitCount = new Dictionary<AnalysisValue, int>();
            }

            int count;
            if (!_hitCount.TryGetValue(value, out count)) {
                _hitCount[value] = 1;
                return true;
            } else {
                _hitCount[value] = count + 1;
            }
            return false;
        }
示例#15
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);

            if (name == "__iter__") {
                return _iterMethod = _iterMethod ?? new SpecializedCallable(
                    res.OfType<BuiltinNamespace<IPythonType>>().FirstOrDefault(),
                    SequenceIter,
                    false
                );
            }

            return res;
        }
示例#16
0
        internal override AnalysisValue UnionMergeTypes(AnalysisValue av, int strength) {
            if (strength >= MergeStrength.ToBaseClass) {
                var literal = av as ObjectLiteralValue;
                if (literal != null) {
                    return this;
                }
            }
            
            if (strength >= MergeStrength.ToBaseClass) {
                var literal = av as ObjectLiteralValue;
                if (literal != null && literal._node == _node) {
                    return this;
                }
            }

            return base.UnionMergeTypes(av, strength);
        }
示例#17
0
        public override IAnalysisSet GetMember(Node node, AnalysisUnit unit, string name) {
            if (unit.ProjectState.LanguageVersion.Is2x() && name == "next" ||
                unit.ProjectState.LanguageVersion.Is3x() && name == "__next__") {
                return _next = _next ?? new SpecializedCallable(
                    null,
                    IteratorNext,
                    false
                );
            } else if (name == "__iter__") {
                return _iter = _iter ?? new SpecializedCallable(
                    null,
                    IteratorIter,
                    false
                );
            }

            return base.GetMember(node, unit, name);
        }
示例#18
0
        internal override bool UnionEquals(AnalysisValue av, int strength) {
            if (strength >= MergeStrength.ToBaseClass) {
                var literal = av as ObjectLiteralValue;
                if (literal != null) {
                    return true;
                }
            }

            if (strength >= MergeStrength.ToBaseClass) {
                var literal = av as ObjectLiteralValue;
                if (literal != null) {
                    // two literals from the same node, these
                    // literals were created by independent function
                    // analysis, merge them together now.
                    return literal._node == _node;
                }
            }
            return base.UnionEquals(av, strength);
        }
示例#19
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);

            if (unit.ProjectState.LanguageVersion.Is2x() && name == "next" ||
                unit.ProjectState.LanguageVersion.Is3x() && name == "__next__") {
                return _next = _next ?? new SpecializedCallable(
                    res.OfType<BuiltinNamespace<IPythonType>>().FirstOrDefault(),
                    IteratorNext,
                    false
                );
            } else if (name == "__iter__") {
                return _iter = _iter ?? new SpecializedCallable(
                    res.OfType<BuiltinNamespace<IPythonType>>().FirstOrDefault(),
                    IteratorIter,
                    false
                );
            }
            return res;
        }
示例#20
0
        public override IAnalysisSet GetMember(Node node, AnalysisUnit unit, string name) {
            var res = base.GetMember(node, unit, name);

            switch (name) {
                case "__new__":
                    return _new = _new ?? new SpecializedCallable(
                        res.OfType<BuiltinNamespace<IPythonType>>().FirstOrDefault(),
                        ObjectNew,
                        false
                    );
                case "__setattr__":
                    return _setattr = _setattr ?? new SpecializedCallable(
                        res.OfType<BuiltinNamespace<IPythonType>>().FirstOrDefault(),
                        ObjectSetAttr,
                        false
                    );
            }

            return res;
        }
示例#21
0
 protected abstract string MakeModuleDocumentation(AnalysisValue value);
示例#22
0
 protected abstract string MakeFunctionDocumentation(AnalysisValue value);
 public SpecializedCallable(AnalysisValue original, AnalysisValue inst, CallDelegate callable, bool mergeNormalAnalysis)
     : base(original, inst)
 {
     _callable = callable;
     _mergeOriginalAnalysis = mergeNormalAnalysis;
 }
示例#24
0
        internal override bool UnionEquals(AnalysisValue ns, int strength)
        {
            var dict = ProjectState.ClassInfos[BuiltinTypeId.Dict];

            if (strength < MergeStrength.IgnoreIterableNode && (this is DictionaryInfo || this == dict.Instance))
            {
                if (ns is DictionaryInfo || ns == dict.Instance)
                {
                    return(true);
                }
                var ci = ns as ConstantInfo;
                if (ci != null && ci.ClassInfo == dict)
                {
                    return(true);
                }
                return(false);
            }

            if (strength >= MergeStrength.ToObject)
            {
                if (TypeId == BuiltinTypeId.NoneType || ns.TypeId == BuiltinTypeId.NoneType)
                {
                    // BII + BII(None) => do not merge
                    // Unless both types are None, since they could be various
                    // combinations of BuiltinInstanceInfo or ConstantInfo that
                    // need to be merged.
                    return(TypeId == BuiltinTypeId.NoneType && ns.TypeId == BuiltinTypeId.NoneType);
                }

                var func = ProjectState.ClassInfos[BuiltinTypeId.Function];
                if (this == func.Instance)
                {
                    // FI + BII(function) => BII(function)
                    return(ns is FunctionInfo || ns is BuiltinFunctionInfo || ns == func.Instance);
                }
                else if (ns == func.Instance)
                {
                    return(false);
                }

                var type = ProjectState.ClassInfos[BuiltinTypeId.Type];
                if (this == type.Instance)
                {
                    // CI + BII(type) => BII(type)
                    // BCI + BII(type) => BII(type)
                    return(ns is ClassInfo || ns is BuiltinClassInfo || ns == type.Instance);
                }
                else if (ns == type.Instance)
                {
                    return(false);
                }

                /// BII + II => BII(object)
                /// BII + BII => BII(object)
                return(ns is InstanceInfo || ns is BuiltinInstanceInfo);
            }
            else if (strength >= MergeStrength.ToBaseClass)
            {
                var bii = ns as BuiltinInstanceInfo;
                if (bii != null)
                {
                    return(ClassInfo.UnionEquals(bii.ClassInfo, strength));
                }
                var ii = ns as InstanceInfo;
                if (ii != null)
                {
                    return(ClassInfo.UnionEquals(ii.ClassInfo, strength));
                }
            }
            else if (this is ConstantInfo || ns is ConstantInfo)
            {
                // ConI + BII => BII if CIs match
                var bii = ns as BuiltinInstanceInfo;
                return(bii != null && ClassInfo.Equals(bii.ClassInfo));
            }

            return(base.UnionEquals(ns, strength));
        }
示例#25
0
 protected abstract string MakeConstantDocumentation(AnalysisValue value);
示例#26
0
 protected override string MakeConstantDocumentation(AnalysisValue value) => $"```python\n{value.Description}\n```";
示例#27
0
 protected override string MakeClassDocumentation(AnalysisValue value) => FromDocAndDescription(value);
 public SpecializedNamespace(AnalysisValue original, AnalysisValue inst)
 {
     _original = original;
     _inst     = inst;
 }
 public SpecializedNamespace(AnalysisValue original)
 {
     _original = original;
 }
 protected abstract SpecializedNamespace Clone(AnalysisValue original, AnalysisValue instance);
        public override IAnalysisSet GetDescriptor(Node node, AnalysisValue instance, AnalysisValue context, AnalysisUnit unit)
        {
            if (_original == null)
            {
                return(base.GetDescriptor(node, instance, context, unit));
            }

            if (_descriptor == null)
            {
                var res = _original.GetDescriptor(node, instance, context, unit);
                // TODO: This kinda sucks...
                if (Object.ReferenceEquals(res, _original))
                {
                    _descriptor = SelfSet;
                }
                else if (res.Count >= 1)
                {
                    // TODO: Dictionary per-instance

                    _descriptor = Clone(res.First(), instance);
                }
                else
                {
                    _descriptor = Clone(_original, instance);
                }
            }
            return(_descriptor);
        }
示例#32
0
        internal override bool UnionEquals(AnalysisValue ns, int strength) {
            if (strength >= MergeStrength.ToObject) {
                if (ns.TypeId == BuiltinTypeId.NoneType) {
                    // II + BII(None) => do not merge
                    return false;
                }

                // II + II => BII(object)
                // II + BII => BII(object)
                var obj = ProjectState.ClassInfos[BuiltinTypeId.Object];
                return ns is InstanceInfo || 
                    (ns is BuiltinInstanceInfo && ns.TypeId != BuiltinTypeId.Type && ns.TypeId != BuiltinTypeId.Function) ||
                    ns == obj.Instance;

            } else if (strength >= MergeStrength.ToBaseClass) {
                var ii = ns as InstanceInfo;
                if (ii != null) {
                    return ii.ClassInfo.UnionEquals(ClassInfo, strength);
                }
                var bii = ns as BuiltinInstanceInfo;
                if (bii != null) {
                    return bii.ClassInfo.UnionEquals(ClassInfo, strength);
                }
            }

            return base.UnionEquals(ns, strength);
        }
        internal override bool UnionEquals(AnalysisValue ns, int strength) {
            if (strength >= MergeStrength.ToObject) {
                // II + II => BII(object)
                // II + BII => BII(object)
#if FALSE
                var obj = ProjectState.ClassInfos[BuiltinTypeId.Object];
                return ns is InstanceInfo || 
                    //(ns is BuiltinInstanceInfo && ns.TypeId != BuiltinTypeId.Type && ns.TypeId != BuiltinTypeId.Function) ||
                    ns == obj.Instance;
#endif
#if FALSE
            } else if (strength >= MergeStrength.ToBaseClass) {
                var ii = ns as InstanceInfo;
                if (ii != null) {
                    return ii.ClassInfo.UnionEquals(ClassInfo, strength);
                }
                var bii = ns as BuiltinInstanceInfo;
                if (bii != null) {
                    return bii.ClassInfo.UnionEquals(ClassInfo, strength);
                }
#endif
            }

            return base.UnionEquals(ns, strength);
        }
 public override IAnalysisSet GetDescriptor(Node node, AnalysisValue instance, AnalysisValue context, AnalysisUnit unit)
 {
     return(AnalysisSet.UnionAll(_protocols.Select(p => p.GetDescriptor(node, instance, context, unit)).WhereNotNull()));
 }
 internal static void PopProtoLookup(AnalysisValue value) {
     int count = _hitCount[value];
     if (count == 1) {
         _hitCount.Remove(value);
     } else {
         _hitCount[value] = count - 1;
     }
 }
 public override IAnalysisSet GetDescriptor(PythonAnalyzer projectState, AnalysisValue instance, AnalysisValue context)
 {
     return(AnalysisSet.UnionAll(_protocols.Select(p => p.GetDescriptor(projectState, instance, context)).WhereNotNull()));
 }
 protected override SpecializedNamespace Clone(AnalysisValue original, AnalysisValue instance)
 {
     return(new SpecializedCallable(original, instance, _callable, _mergeOriginalAnalysis));
 }
 internal override bool UnionEquals(AnalysisValue av, int strength)
 => av is ProtocolInfo pi && ObjectComparer.Instance.Equals(_protocols, pi._protocols);
示例#39
0
 private static bool IsTestCaseClass(AnalysisValue cls)
 {
     return(IsTestCaseClass(cls?.PythonType));
 }
示例#40
0
 internal override AnalysisValue UnionMergeTypes(AnalysisValue ns, int strength) {
     if (strength < MergeStrength.IgnoreIterableNode) {
         return ClassInfo.Instance;
     }
     return base.UnionMergeTypes(ns, strength);
 }
示例#41
0
        internal static AnalysisValue GetFirstCommonBase(PythonAnalyzer state, AnalysisValue ns1, AnalysisValue ns2)
        {
            if (ns1.MemberType != PythonMemberType.Class || ns2.MemberType != PythonMemberType.Class)
            {
                return(null);
            }

            (ns1.Mro as Mro)?.RecomputeIfNecessary();
            (ns2.Mro as Mro)?.RecomputeIfNecessary();

            var mro1 = ns1.Mro.SelectMany().ToArray();
            var mro2 = ns2.Mro.SelectMany().ToArray();

            if (!IsFirstForMroUnion(ns1, ns2))
            {
                var tmp = mro1;
                mro1 = mro2;
                mro2 = tmp;
            }

            var mro2Set    = new HashSet <AnalysisValue>(mro2.MaybeEnumerate().SelectMany(), ObjectComparer.Instance);
            var commonBase = mro1.MaybeEnumerate().SelectMany().Where(v => v is ClassInfo || v is BuiltinClassInfo).FirstOrDefault(mro2Set.Contains);

            if (commonBase == null || commonBase.TypeId == BuiltinTypeId.Object || commonBase.TypeId == BuiltinTypeId.Type)
            {
                return(null);
            }
            if (commonBase.Push())
            {
                try {
#if FULL_VALIDATION
                    Validation.Assert(GetFirstCommonBase(state, ns1, commonBase) != null, $"No common base between {ns1} and {commonBase}");
                    Validation.Assert(GetFirstCommonBase(state, ns2, commonBase) != null, $"No common base between {ns2} and {commonBase}");
#endif
                    if (GetFirstCommonBase(state, ns1, commonBase) == null || GetFirstCommonBase(state, ns2, commonBase) == null)
                    {
                        return(null);
                    }
                } finally {
                    commonBase.Pop();
                }
            }
            return(commonBase);
        }
示例#42
0
 private static bool IsTestCaseClass(AnalysisValue cls) {
     if (cls == null ||
         cls.DeclaringModule != null ||
         cls.PythonType == null ||
         cls.PythonType.DeclaringModule == null) {
         return false;
     }
     var mod = cls.PythonType.DeclaringModule.Name;
     return (mod == "unittest" || mod.StartsWith("unittest.")) && cls.Name == "TestCase";
 }
示例#43
0
 protected abstract string MakeClassDocumentation(AnalysisValue value);
示例#44
0
文件: ClassInfo.cs 项目: krus/PTVS
 public void ClearBases()
 {
     _bases.Clear();
     _baseUserType = null;
     _mro.Recompute();
 }
示例#45
0
        public AnalysisHashSet Remove(AnalysisValue key)
        {
            bool dummy;

            return(Remove(key, out dummy));
        }
示例#46
0
        /// <summary>
        /// Add helper that works over a single set of buckets.  Used for
        /// both the normal add case as well as the resize case.
        /// </summary>
        private bool Add(Bucket[] buckets, AnalysisValue key)
        {
            int hc = _comparer.GetHashCode(key) & Int32.MaxValue;

            return(AddOne(buckets, key, hc));
        }
        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 "get":
                return(_getMethod = _getMethod ?? new SpecializedCallable(
                           res.OfType <BuiltinNamespace <IPythonType> >().FirstOrDefault(),
                           DictionaryGet,
                           false
                           ));

            case "items":
                return(_itemsMethod = _itemsMethod ?? new SpecializedCallable(
                           res.OfType <BuiltinNamespace <IPythonType> >().FirstOrDefault(),
                           unit.ProjectState.LanguageVersion.Is3x() ? (CallDelegate)DictionaryIterItems : DictionaryItems,
                           false
                           ));

            case "keys":
                return(_keysMethod = _keysMethod ?? new SpecializedCallable(
                           res.OfType <BuiltinNamespace <IPythonType> >().FirstOrDefault(),
                           unit.ProjectState.LanguageVersion.Is3x() ? (CallDelegate)DictionaryIterKeys : DictionaryKeys,
                           false
                           ));

            case "values":
                return(_valuesMethod = _valuesMethod ?? new SpecializedCallable(
                           res.OfType <BuiltinNamespace <IPythonType> >().FirstOrDefault(),
                           unit.ProjectState.LanguageVersion.Is3x() ? (CallDelegate)DictionaryIterValues : DictionaryValues,
                           false
                           ));

            case "pop":
                return(_popMethod = _popMethod ?? new SpecializedCallable(
                           res.OfType <BuiltinNamespace <IPythonType> >().FirstOrDefault(),
                           DictionaryPop,
                           false
                           ));

            case "popitem":
                return(_popItemMethod = _popItemMethod ?? new SpecializedCallable(
                           res.OfType <BuiltinNamespace <IPythonType> >().FirstOrDefault(),
                           DictionaryPopItem,
                           false
                           ));

            case "iterkeys":
                if (unit.ProjectState.LanguageVersion.Is2x())
                {
                    return(_iterKeysMethod = _iterKeysMethod ?? new SpecializedCallable(
                               res.OfType <BuiltinNamespace <IPythonType> >().FirstOrDefault(),
                               DictionaryIterKeys,
                               false
                               ));
                }
                break;

            case "itervalues":
                if (unit.ProjectState.LanguageVersion.Is2x())
                {
                    return(_iterValuesMethod = _iterValuesMethod ?? new SpecializedCallable(
                               res.OfType <BuiltinNamespace <IPythonType> >().FirstOrDefault(),
                               DictionaryIterValues,
                               false
                               ));
                }
                break;

            case "iteritems":
                if (unit.ProjectState.LanguageVersion.Is2x())
                {
                    return(_iterItemsMethod = _iterItemsMethod ?? new SpecializedCallable(
                               res.OfType <BuiltinNamespace <IPythonType> >().FirstOrDefault(),
                               DictionaryIterItems,
                               false
                               ));
                }
                break;

            case "update":
                return(_updateMethod = _updateMethod ?? new SpecializedCallable(
                           res.OfType <BuiltinNamespace <IPythonType> >().FirstOrDefault(),
                           DictionaryUpdate,
                           false
                           ));
            }

            return(res);
        }
示例#48
0
        /// <summary>
        /// Add helper which adds the given key/value (where the key is not null) with
        /// a pre-computed hash code.
        /// </summary>
        private bool AddOne(Bucket[] buckets, AnalysisValue /*!*/ key, int hc)
        {
            Debug.Assert(key != null);

            Debug.Assert(_count < buckets.Length);
            int index      = hc % buckets.Length;
            int startIndex = index;
            int addIndex   = -1;

            for (; ;)
            {
                Bucket cur         = buckets[index];
                var    existingKey = cur.Key;
                if (existingKey == null || existingKey == _removed || !existingKey.IsAlive)
                {
                    if (addIndex == -1)
                    {
                        addIndex = index;
                    }
                    if (cur.Key == null)
                    {
                        break;
                    }
                }
                else if (Object.ReferenceEquals(key, existingKey))
                {
                    return(false);
                }
                else if (cur.HashCode == hc && _comparer.Equals(key, existingKey))
                {
                    var uc = _comparer as UnionComparer;
                    if (uc == null)
                    {
                        return(false);
                    }
                    bool changed;
                    var  newKey = uc.MergeTypes(existingKey, key, out changed);
                    if (!changed)
                    {
                        return(false);
                    }
                    // merging values has changed the one we should store, so
                    // replace it.
                    var newHc = _comparer.GetHashCode(newKey) & Int32.MaxValue;
                    if (newHc != buckets[index].HashCode)
                    {
                        // The hash code should not change, but if it does, we
                        // need to keep things consistent
                        Debug.Fail("Hash code changed when merging AnalysisValues");
                        Thread.MemoryBarrier();
                        buckets[index].Key = _removed;
                        AddOne(buckets, newKey, newHc);
                        return(true);
                    }
                    Thread.MemoryBarrier();
                    buckets[index].Key = newKey;
                    return(true);
                }

                index = ProbeNext(buckets, index);

                if (index == startIndex)
                {
                    break;
                }
            }

            if (buckets[addIndex].Key != null &&
                buckets[addIndex].Key != _removed &&
                !buckets[addIndex].Key.IsAlive)
            {
                _count--;
            }
            buckets[addIndex].HashCode = hc;
            Thread.MemoryBarrier();
            // we write the key last so that we can check for null to
            // determine if a bucket is available.
            buckets[addIndex].Key = key;

            return(true);
        }
 private IAnalysisSet GetRecurse(AnalysisValue protoStart, Node node, AnalysisUnit unit, string name, bool addRef) {
     var prototypes = protoStart.GetPrototype(unit.ProjectEntry);
     IAnalysisSet protovalue = AnalysisSet.Empty;
     if (prototypes != null) {
         try {
             foreach (var proto in prototypes) {
                 if (PushProtoLookup(proto.Value)) {
                     var property = proto.Value.GetProperty(node, unit, name);
                     if (property != null) {
                         var value = property.GetValue(
                             node,
                             unit,
                             proto.Value.DeclaringModule,
                             this.SelfSet,
                             addRef
                         );
                         protovalue = protovalue.Union(value);
                         if (property.IsEphemeral) {
                             protovalue = protovalue.Union(GetRecurse(proto.Value, node, unit, name, addRef));
                         }
                     } else {
                         // keep searching the prototype chain...
                         protovalue = protovalue.Union(GetRecurse(proto.Value, node, unit, name, addRef));
                     }
                 }
             }
         } finally {
             foreach (var proto in prototypes) {
                 PopProtoLookup(proto.Value);
             }
         }
     }
    
     return protovalue;
 }
示例#50
0
 public override IAnalysisSet GetDescriptor(Node node, AnalysisValue instance, AnalysisValue context, AnalysisUnit unit)
 {
     return(_builtinInfo.GetDescriptor(node, instance, context, unit));
 }
        internal override AnalysisValue UnionMergeTypes(AnalysisValue ns, int strength) {
            if (strength >= MergeStrength.ToObject) {
                // II + II => BII(object)
                // II + BII => BII(object)
                //return ProjectState.ClassInfos[BuiltinTypeId.Object].Instance;
#if FALSE
            } else if (strength >= MergeStrength.ToBaseClass) {
                var ii = ns as InstanceInfo;
                if (ii != null) {
                    return ii.ClassInfo.UnionMergeTypes(ClassInfo, strength).GetInstanceType().Single();
                }
                var bii = ns as BuiltinInstanceInfo;
                if (bii != null) {
                    return bii.ClassInfo.UnionMergeTypes(ClassInfo, strength).GetInstanceType().Single();
                }
#endif
            }

            return base.UnionMergeTypes(ns, strength);
        }
示例#52
0
        public void Recompute()
        {
            var mroList = new List <AnalysisValue> {
                _classInfo
            };
            var isValid = true;

            var bases = _classInfo.Bases;

            if (bases.Any())
            {
                var mergeList = new List <List <AnalysisValue> >();
                var finalMro  = new List <AnalysisValue>();

                foreach (var baseClass in bases.SelectMany())
                {
                    var klass        = baseClass as ClassInfo;
                    var builtInClass = baseClass as BuiltinClassInfo;
                    if (klass != null && klass.Push())
                    {
                        try {
                            if (!klass._mro.IsValid)
                            {
                                isValid = false;
                                break;
                            }
                            finalMro.Add(klass);
                            mergeList.Add(klass.Mro.SelectMany().ToList());
                        } finally {
                            klass.Pop();
                        }
                    }
                    else if (builtInClass != null && builtInClass.Push())
                    {
                        try {
                            finalMro.Add(builtInClass);
                            mergeList.Add(builtInClass.Mro.SelectMany().ToList());
                        } finally {
                            builtInClass.Pop();
                        }
                    }
                }

                if (isValid)
                {
                    mergeList.Add(finalMro);
                    mergeList.RemoveAll(mro => mro.Count == 0);

                    while (mergeList.Count > 0)
                    {
                        AnalysisValue nextInMro = null;

                        for (int i = 0; i < mergeList.Count; ++i)
                        {
                            // Select candidate head
                            var candidate = mergeList[i][0];

                            // Look for the candidate in the tails of every other MRO
                            if (!mergeList.Any(baseMro => baseMro.Skip(1).Contains(candidate)))
                            {
                                // Candidate is good, so stop searching.
                                nextInMro = candidate;
                                break;
                            }
                        }

                        // No valid MRO for this class
                        if (nextInMro == null)
                        {
                            isValid = false;
                            break;
                        }

                        mroList.Add(nextInMro);

                        // Remove all instances of that class from potentially being returned again
                        foreach (var mro in mergeList)
                        {
                            mro.RemoveAll(ns => ns == nextInMro);
                        }

                        // Remove all lists that are now empty.
                        mergeList.RemoveAll(mro => mro.Count == 0);
                    }
                }
            }

            // If the MRO is invalid, we only want the class itself to be there so that we
            // will show all members defined in it, but nothing else.
            if (!isValid)
            {
                mroList.Clear();
                mroList.Add(_classInfo);
            }

            if (_isValid != isValid || !_mroList.SequenceEqual(mroList))
            {
                _isValid = isValid;
                _mroList = mroList;
                EnqueueDependents();
            }
        }
示例#53
0
 private static void AppendDescription(StringBuilder result, AnalysisValue key)
 {
     result.Append(key.ShortDescription);
 }
 public override IAnalysisSet GetDescriptor(Node node, AnalysisValue instance, AnalysisValue context, AnalysisUnit unit)
 {
     if (Push())
     {
         try {
             var getter = GetTypeMember(node, unit, "__get__");
             if (getter.Count > 0)
             {
                 var get = getter.GetDescriptor(node, this, _classInfo, unit);
                 return(get.Call(node, unit, new[] { instance, context }, ExpressionEvaluator.EmptyNames));
             }
         } finally {
             Pop();
         }
     }
     return(SelfSet);
 }
示例#55
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 "get":
                    return _getMethod = _getMethod ?? new SpecializedCallable(
                        res.OfType<BuiltinNamespace<IPythonType>>().FirstOrDefault(),
                        DictionaryGet,
                        false
                    );
                case "items":
                    return _itemsMethod = _itemsMethod ?? new SpecializedCallable(
                        res.OfType<BuiltinNamespace<IPythonType>>().FirstOrDefault(),
                        unit.ProjectState.LanguageVersion.Is3x() ? (CallDelegate)DictionaryIterItems : DictionaryItems,
                        false
                    );
                case "keys":
                    return _keysMethod = _keysMethod ?? new SpecializedCallable(
                        res.OfType<BuiltinNamespace<IPythonType>>().FirstOrDefault(),
                        unit.ProjectState.LanguageVersion.Is3x() ? (CallDelegate)DictionaryIterKeys : DictionaryKeys,
                        false
                    );
                case "values":
                    return _valuesMethod = _valuesMethod ?? new SpecializedCallable(
                        res.OfType<BuiltinNamespace<IPythonType>>().FirstOrDefault(),
                        unit.ProjectState.LanguageVersion.Is3x() ? (CallDelegate)DictionaryIterValues : DictionaryValues,
                        false
                    );
                case "pop":
                    return _popMethod = _popMethod ?? new SpecializedCallable(
                        res.OfType<BuiltinNamespace<IPythonType>>().FirstOrDefault(),
                        DictionaryPop,
                        false
                    );
                case "popitem":
                    return _popItemMethod = _popItemMethod ?? new SpecializedCallable(
                        res.OfType<BuiltinNamespace<IPythonType>>().FirstOrDefault(),
                        DictionaryPopItem,
                        false
                    );
                case "iterkeys":
                    if (unit.ProjectState.LanguageVersion.Is2x()) {
                        return _iterKeysMethod = _iterKeysMethod ?? new SpecializedCallable(
                            res.OfType<BuiltinNamespace<IPythonType>>().FirstOrDefault(),
                            DictionaryIterKeys,
                            false
                        );
                    }
                    break;
                case "itervalues":
                    if (unit.ProjectState.LanguageVersion.Is2x()) {
                        return _iterValuesMethod = _iterValuesMethod ?? new SpecializedCallable(
                            res.OfType<BuiltinNamespace<IPythonType>>().FirstOrDefault(),
                            DictionaryIterValues,
                            false
                        );
                    }
                    break;
                case "iteritems":
                    if (unit.ProjectState.LanguageVersion.Is2x()) {
                        return _iterItemsMethod = _iterItemsMethod ?? new SpecializedCallable(
                            res.OfType<BuiltinNamespace<IPythonType>>().FirstOrDefault(),
                            DictionaryIterItems,
                            false
                        );
                    }
                    break;
                case "update":
                    return _updateMethod = _updateMethod ?? new SpecializedCallable(
                        res.OfType<BuiltinNamespace<IPythonType>>().FirstOrDefault(),
                        DictionaryUpdate,
                        false
                    );
            }

            return res;
        }
示例#56
0
        public override IAnalysisSet GetDescriptor(Node node, AnalysisValue instance, AnalysisValue context, AnalysisUnit unit)
        {
            if (instance == ProjectState._noneInst)
            {
                return(base.GetDescriptor(node, instance, context, unit));
            }

            if (_boundMethod == null)
            {
                _boundMethod = new BoundBuiltinMethodInfo(this);
            }

            return(_boundMethod.SelfSet);
        }
示例#57
0
 internal override bool UnionEquals(AnalysisValue ns, int strength) {
     if (strength < MergeStrength.IgnoreIterableNode) {
         if (ns == ProjectState.ClassInfos[BuiltinTypeId.Dict].Instance) {
             return true;
         }
         var ci = ns as ConstantInfo;
         if (ci != null && ci.ClassInfo == ProjectState.ClassInfos[BuiltinTypeId.Dict]) {
             return true;
         }
         var di = ns as DictionaryInfo;
         if (di != null) {
             return di._node.Equals(_node);
         }
         return false;
     }
     return base.UnionEquals(ns, strength);
 }
 public TypingTypeInfo(string baseName, AnalysisValue innerValue) : this(baseName, innerValue, Array.Empty <IAnalysisSet>())
 {
 }
示例#59
0
        /// <summary>
        /// Get Test Case Members for a class.  If the class has 'test*' tests 
        /// return those.  If there aren't any 'test*' tests return (if one at 
        /// all) the runTest overridden method
        /// </summary>
        private static IEnumerable<KeyValuePair<string, IAnalysisSet>> GetTestCaseMembers(
            ModuleAnalysis analysis,
            AnalysisValue classValue
        ) {
            var methodFunctions = classValue.GetAllMembers(analysis.InterpreterContext)
                .Where(v => v.Value.Any(m => m.MemberType == PythonMemberType.Function || m.MemberType == PythonMemberType.Method));

            var tests = methodFunctions.Where(v => v.Key.StartsWith("test"));
            var runTest = methodFunctions.Where(v => v.Key.Equals("runTest"));

            if (tests.Any()) {
                return tests;
            } else {
                return runTest;
            }
        }
 private TypingTypeInfo(string baseName, AnalysisValue innerValue, IReadOnlyList <IAnalysisSet> args)
 {
     _baseName   = baseName;
     _innerValue = innerValue;
     _args       = args;
 }