Пример #1
0
 protected MethodGeneratorBase(T type, Method method, ISymbolMap symbols, INamespaceSet namespaces)
 {
     Type       = type ?? throw new ArgumentNullException(nameof(type));
     Method     = method ?? throw new ArgumentNullException(nameof(method));
     Symbols    = symbols ?? throw new ArgumentNullException(nameof(symbols));
     Namespaces = namespaces ?? throw new ArgumentNullException(nameof(namespaces));
 }
Пример #2
0
 public override INamespaceSet Call(Node node, AnalysisUnit unit, INamespaceSet[] args, NameExpression[] keywordArgNames)
 {
     if (args.Length == 0) {
         return unit.Scope.GetOrMakeNodeValue(node, n => new IteratorInfo(_indexTypes, _iterClass, n));
     }
     return NamespaceSet.Empty;
 }
Пример #3
0
        internal bool MakeUnion(int strength)
        {
            bool wasChanged;

            _types = _types.AsUnion(strength, out wasChanged);
            return(wasChanged);
        }
Пример #4
0
        public static ParameterListSyntax GetParameterListSyntax(this Method method, INamespaceSet namespaces, ISymbolMap symbols)
        {
            method     = method ?? throw new ArgumentNullException(nameof(method));
            namespaces = namespaces ?? throw new ArgumentNullException(nameof(namespaces));
            symbols    = symbols ?? throw new ArgumentNullException(nameof(symbols));

            return(SF.ParameterList(SF.SeparatedList(GetParameters())));

            IEnumerable <ParameterSyntax> GetParameters()
            {
                if (method.Parameters == null)
                {
                    yield break;
                }

                foreach (Parameter parameter in method.Parameters)
                {
                    namespaces.Add(parameter.Type);

                    yield return(SF.Parameter(
                                     SF.List <AttributeListSyntax>(),
                                     SF.TokenList(),
                                     symbols.GetTypeSyntax(parameter.Type),
                                     symbols.GetNameSyntaxToken(parameter),
                                     null
                                     ));
                }
            }
        }
Пример #5
0
        public bool AddType(Namespace ns)
        {
            bool wasChanged;

            _types = _types.Add(ns, out wasChanged);
            return(wasChanged);
        }
Пример #6
0
 public override INamespaceSet BinaryOperation(Node node, AnalysisUnit unit, JOperator operation, INamespaceSet rhs)
 {
     switch (operation) {
         case JOperator.GreaterThan:
         case JOperator.LessThan:
         case JOperator.LessThanOrEqual:
         case JOperator.GreaterThanOrEqual:
         case JOperator.Equal:
         case JOperator.NotEqual:
         case JOperator.Is:
         case JOperator.IsNot:
             return ProjectState._boolType.Instance;
         case JOperator.TrueDivide:
         case JOperator.Add:
         case JOperator.Subtract:
         case JOperator.Multiply:
         case JOperator.Divide:
         case JOperator.Mod:
         case JOperator.BitwiseAnd:
         case JOperator.BitwiseOr:
         case JOperator.Xor:
         case JOperator.LeftShift:
         case JOperator.RightShift:
         case JOperator.Power:
         case JOperator.FloorDivide:
             return ConstantInfo.NumericOp(node, this, unit, operation, rhs) ?? base.BinaryOperation(node, unit, operation, rhs);
     }
     return base.BinaryOperation(node, unit, operation, rhs);
 }
Пример #7
0
        public override INamespaceSet Call(Node node, AnalysisUnit unit, INamespaceSet[] args, NameExpression[] keywordArgNames)
        {
            if (args.Length == 2) {
                _list.AppendItem(node, unit, args[1]);
            }

            return ProjectState._noneInst.SelfSet;
        }
 public AbstractClassProxyGenerator(string package, ClassType type, ISymbolMap symbols,
                                    INamespaceSet namespaces = null)
     : base(package, type, symbols, namespaces)
 {
     if (!type.IsAbstract)
     {
         throw new ArgumentException("Class type must be abstract.", nameof(type));
     }
 }
Пример #9
0
        public static INamespaceSet BinaryOperation(this INamespaceSet self, Node node, AnalysisUnit unit, JOperator operation, INamespaceSet rhs)
        {
            var res = NamespaceSet.Empty;
            foreach (var ns in self) {
                res = res.Union(ns.BinaryOperation(node, unit, operation, rhs));
            }

            return res;
        }
Пример #10
0
        internal void AppendItem(Node node, AnalysisUnit unit, INamespaceSet set)
        {
            if (IndexTypes.Length == 0) {
                IndexTypes = new[] { new VariableDef() };
            }

            IndexTypes[0].AddTypes(unit, set);

            UnionType = null;
        }
        public override INamespaceSet Call(Node node, AnalysisUnit unit, INamespaceSet[] args, NameExpression[] keywordArgNames)
        {
            if (args.Length == 1) {
                _generator.AddSend(node, unit, args[0]);
            }

            _generator.Yields.AddDependency(unit);

            return _generator.Yields.Types;
        }
Пример #12
0
        public override INamespaceSet Call(Node node, AnalysisUnit unit, INamespaceSet[] args, NameExpression[] keywordArgNames)
        {
            if (args.Length == 1) {
                foreach (var type in args[0]) {
                    _list.AppendItem(node, unit, type.GetEnumeratorTypes(node, unit));
                }
            }

            return ProjectState._noneInst.SelfSet;
        }
Пример #13
0
        protected TypeGeneratorBase(string package, T type, ISymbolMap symbols, INamespaceSet namespaces)
        {
            Package = package ?? throw new ArgumentNullException(nameof(package));

            Type    = type ?? throw new ArgumentNullException(nameof(type));
            Symbols = symbols ?? throw new ArgumentNullException(nameof(symbols));

            NameSyntax namespaceSyntax = Symbols.GetNamespaceSyntax(Type);

            Namespaces = namespaces ?? new NamespaceSet(Symbols, namespaceSyntax);
        }
Пример #14
0
        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;
        }
Пример #15
0
        public override INamespaceSet GetIndex(Node node, AnalysisUnit unit, INamespaceSet 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._intType.SelfSet;
        }
Пример #16
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 INamespaceSet Call(this INamespaceSet self, Node node, AnalysisUnit unit, INamespaceSet[] args, NameExpression[] keywordArgNames)
        {
            var res = NamespaceSet.Empty;
            foreach (var ns in self) {
                var call = ns.Call(node, unit, args, keywordArgNames);
                Debug.Assert(call != null);

                res = res.Union(call);
            }

            return res;
        }
        bool INamespaceSet.SetEquals(INamespaceSet other)
        {
            if (other.Count != 1)
            {
                return(false);
            }
            var ns = other as Namespace;

            if (ns != null)
            {
                return(((INamespaceSet)this).Comparer.Equals(this, ns));
            }

            return(((INamespaceSet)this).Comparer.Equals(this, other.First()));
        }
Пример #18
0
 public override INamespaceSet Call(Node node, AnalysisUnit unit, INamespaceSet[] args, NameExpression[] keywordArgNames)
 {
     if (args.Length >= 1) {
         var instance = NamespaceSet.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._objectType.Instance;
 }
Пример #19
0
        public override INamespaceSet Call(Node node, AnalysisUnit unit, INamespaceSet[] args, NameExpression[] keywordArgNames)
        {
            var realArgs = args;
            if (_inst != null) {
                realArgs = Utils.Concat(_inst.SelfSet, args);
            }

            var analyzed = _original.Call(node, unit, args, keywordArgNames);
            var res = _call((CallExpression)node, unit, realArgs, keywordArgNames);
            if (res == null) {
                return analyzed;
            } else if (analyzed.Count == 0) {
                return res;
            } else {
                return res.Union(analyzed);
            }
        }
Пример #20
0
        public override INamespaceSet BinaryOperation(Node node, AnalysisUnit unit, JOperator operation, INamespaceSet rhs)
        {
            switch (operation) {
                case JOperator.Multiply:
                    INamespaceSet res = NamespaceSet.Empty;
                    foreach (var type in rhs) {
                        var typeId = type.TypeId;

                        if (typeId == BuiltinTypeId.Int || typeId == BuiltinTypeId.Long) {
                            res = res.Union(this);
                        } else {
                            var partialRes = type.ReverseBinaryOperation(node, unit, operation, SelfSet);
                            if (partialRes != null) {
                                res = res.Union(partialRes);
                            }
                        }

                    }
                    return res;
            }
            return base.BinaryOperation(node, unit, operation, rhs);
        }
Пример #21
0
        public override INamespaceSet Call(Node node, AnalysisUnit unit, INamespaceSet[] args, NameExpression[] keywordArgNames)
        {
            if (args.Length == 1) {
                var res = unit.Scope.GetOrMakeNodeValue(
                    node,
                    (node_) => MakeFromIndexes(node_)
                ) as SequenceInfo;

                List<INamespaceSet> seqTypes = new List<INamespaceSet>();
                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);
        }
Пример #22
0
 protected TypeProxyGeneratorBase(string package, T type, ISymbolMap symbols, INamespaceSet namespaces)
     : base(package, type, symbols, namespaces)
 {
 }
Пример #23
0
 public override void SetMember(Node node, AnalysisUnit unit, string name, INamespaceSet value)
 {
     foreach (var member in _members) {
         member.SetMember(node, unit, name, value);
     }
 }
Пример #24
0
 public override INamespaceSet Call(Node node, AnalysisUnit unit, INamespaceSet[] args, NameExpression[] keywordArgNames)
 {
     return _original.Call(node, unit, args, keywordArgNames);
 }
Пример #25
0
 public override INamespaceSet GetIndex(Node node, AnalysisUnit unit, INamespaceSet index)
 {
     return _original.GetIndex(node, unit, index);
 }
Пример #26
0
        internal INamespaceSet[] GetParameterTypes(int unionStrength = 0)
        {
            var result = new INamespaceSet[FunctionDefinition.Parameters.Count];
            var units = new HashSet<AnalysisUnit>();
            units.Add(AnalysisUnit);
            if (_allCalls != null) {
                units.UnionWith(_allCalls.Values);
            }

            for (int i = 0; i < result.Length; ++i) {
                result[i] = (unionStrength >= 0 && unionStrength <= UnionComparer.MAX_STRENGTH)
                    ? NamespaceSet.CreateUnion(UnionComparer.Instances[unionStrength])
                    : NamespaceSet.Empty;

                VariableDef param;
                foreach (var unit in units) {
                    if (unit != null && unit.Scope != null && unit.Scope.Variables.TryGetValue(FunctionDefinition.Parameters[i].Name, out param)) {
                        result[i] = result[i].Union(param.TypesNoCopy);
                    }
                }
            }

            return result;
        }
Пример #27
0
        public InterfaceProxyMethodGenerator(InterfaceType type, Method method, ISymbolMap symbols, INamespaceSet namespaces)
            : base(type, method, symbols, namespaces)
        {
            if (!method.IsAbstract)
            {
                throw new ArgumentException("Interface methods must be abstract", nameof(method));
            }

            if (method.IsProtected)
            {
                throw new ArgumentException("Protected methods are not allowed on interfaces", nameof(method));
            }

            if (method.IsStatic)
            {
                throw new ArgumentException(
                          $"Method {type.Name}.{method.Name} is marked as static, but interfaces must not contain static members.",
                          nameof(method)
                          );
            }
        }
 public virtual void SetMember(Node node, AnalysisUnit unit, string name, INamespaceSet value)
 {
 }
Пример #29
0
        public override INamespaceSet Call(Node node, AnalysisUnit unit, INamespaceSet[] args, NameExpression[] keywordArgNames)
        {
            var callArgs = ArgumentSet.FromArgs(FunctionDefinition, unit, args, keywordArgNames);

            if (_allCalls == null) {
                _allCalls = new Dictionary<CallChain, FunctionAnalysisUnit>();
            }

            FunctionAnalysisUnit calledUnit;
            bool updateArguments = true;

            if (callArgs.Count == 0 || (ProjectState.Limits.UnifyCallsToNew && Name == "__new__")) {
                calledUnit = (FunctionAnalysisUnit)AnalysisUnit;
            } else {
                var chain = new CallChain(node, unit, unit.ProjectState.Limits.CallDepth);
                if (!_allCalls.TryGetValue(chain, out calledUnit)) {
                    if (unit.ForEval) {
                        // Call expressions that weren't analyzed get the union result
                        // of all calls to this function.
                        var res = NamespaceSet.Empty;
                        foreach (var call in _allCalls.Values) {
                            res = res.Union(call.ReturnValue.TypesNoCopy);
                        }
                        return res;
                    } else {
                        _allCalls[chain] = calledUnit = new FunctionAnalysisUnit((FunctionAnalysisUnit)AnalysisUnit, chain, callArgs);
                        updateArguments = false;
                    }
                }
            }

            if (updateArguments && calledUnit.UpdateParameters(callArgs)) {
            #if DEBUG
                // Checks whether these arguments can be added ad nauseum.
                if (calledUnit.UpdateParameters(callArgs) && calledUnit.UpdateParameters(callArgs) && calledUnit.UpdateParameters(callArgs)) {
                    AnalysisLog.Add("BadArgs", calledUnit, callArgs);
                }
            #endif
                AnalysisLog.UpdateUnit(calledUnit);
            }

            calledUnit.ReturnValue.AddDependency(unit);
            return calledUnit.ReturnValue.Types;
        }
 /// <summary>
 /// Provides implementation of __r*__methods (__radd__, __rsub__, etc...)
 ///
 /// This is dispatched to when the LHS doesn't understand the RHS.  Unlike normal Python it's currently
 /// the LHS responsibility to dispatch to this.
 /// </summary>
 public virtual INamespaceSet ReverseBinaryOperation(Node node, AnalysisUnit unit, PythonOperator operation, INamespaceSet rhs)
 {
     return(NamespaceSet.Empty);
 }
Пример #31
0
 public ClassPropertyGenerator(ClassType type, Property property, ISymbolMap symbols, INamespaceSet namespaces)
     : base(type, property, symbols, namespaces)
 {
 }
Пример #32
0
 public override void SetIndex(Node node, AnalysisUnit unit, INamespaceSet index, INamespaceSet value)
 {
     _original.SetIndex(node, unit, index, value);
 }
 public virtual INamespaceSet GetIndex(Node node, AnalysisUnit unit, INamespaceSet index)
 {
     return(GetMember(node, unit, "__getitem__").Call(node, unit, new[] { index }, ExpressionEvaluator.EmptyNames));
 }
Пример #34
0
 protected PropertyGeneratorBase(T type, Property property, ISymbolMap symbols, INamespaceSet namespaces)
 {
     Type       = type ?? throw new ArgumentNullException(nameof(type));
     Property   = property ?? throw new ArgumentNullException(nameof(property));
     Symbols    = symbols ?? throw new ArgumentNullException(nameof(symbols));
     Namespaces = namespaces ?? throw new ArgumentNullException(nameof(namespaces));
 }
 public virtual void SetIndex(Node node, AnalysisUnit unit, INamespaceSet index, INamespaceSet value)
 {
 }
Пример #36
0
 public TypedDependencyInfo(int version, INamespaceSet emptySet)
     : base(version)
 {
     _types = emptySet;
 }
        public InterfaceProxyPropertyGenerator(InterfaceType type, Property property, ISymbolMap symbols, INamespaceSet namespaces)
            : base(type, property, symbols, namespaces)
        {
            if (!property.IsAbstract)
            {
                throw new ArgumentException("Interface properties must be abstract", nameof(property));
            }

            if (property.IsProtected)
            {
                throw new ArgumentException("Protected properties are not allowed on interfaces", nameof(property));
            }

            if (property.IsStatic())
            {
                throw new ArgumentException(
                          $"Property {type.Name}.{property.Name} is marked as static, but interfaces must not contain static members.",
                          nameof(property)
                          );
            }
        }
        public virtual INamespaceSet BinaryOperation(Node node, AnalysisUnit unit, PythonOperator operation, INamespaceSet rhs)
        {
            switch (operation)
            {
            case PythonOperator.Is:
            case PythonOperator.IsNot:
            case PythonOperator.In:
            case PythonOperator.NotIn:
                return(unit.DeclaringModule.ProjectEntry.ProjectState.ClassInfos[BuiltinTypeId.Bool].Instance);

            default:
                var res = NamespaceSet.Empty;
                foreach (var value in rhs)
                {
                    res = res.Union(value.ReverseBinaryOperation(node, unit, operation, SelfSet));
                }

                return(res);
            }
        }
Пример #39
0
 public InterfaceGenerator(string package, InterfaceType type, ISymbolMap symbols, INamespaceSet namespaces = null)
     : base(package, type, symbols, namespaces)
 {
 }
 public virtual void AugmentAssign(AugmentedAssignStatement node, AnalysisUnit unit, INamespaceSet value)
 {
 }
Пример #41
0
        public override void SetMember(Node node, AnalysisUnit unit, string name, INamespaceSet value)
        {
            if (_functionAttrs == null) {
                _functionAttrs = new Dictionary<string, VariableDef>();
            }

            VariableDef varRef;
            if (!_functionAttrs.TryGetValue(name, out varRef)) {
                _functionAttrs[name] = varRef = new VariableDef();
            }
            varRef.AddAssignment(node, unit);
            varRef.AddTypes(unit, value);
        }
Пример #42
0
 public ClassMethodGenerator(ClassType type, Method method, ISymbolMap symbols, INamespaceSet namespaces)
     : base(type, method, symbols, namespaces)
 {
 }
Пример #43
0
 public SuperInfo(ClassInfo classInfo, INamespaceSet instances = null)
 {
     _classInfo = classInfo;
     _instances = instances ?? NamespaceSet.Empty;
 }
Пример #44
0
 public override INamespaceSet BinaryOperation(Node node, AnalysisUnit unit, JOperator operation, INamespaceSet rhs)
 {
     var res = NamespaceSet.Empty;
     foreach (var member in _members) {
         res = res.Union(member.BinaryOperation(node, unit, operation, rhs));
     }
     return res;
 }
Пример #45
0
 public override void AugmentAssign(AugmentedAssignStatement node, AnalysisUnit unit, INamespaceSet value)
 {
     _original.AugmentAssign(node, unit, value);
 }
Пример #46
0
 public override INamespaceSet Call(Node node, AnalysisUnit unit, INamespaceSet[] args, NameExpression[] keywordArgNames)
 {
     var res = NamespaceSet.Empty;
     foreach (var member in _members) {
         res = res.Union(member.Call(node, unit, args, keywordArgNames));
     }
     return res;
 }
Пример #47
0
        public override INamespaceSet GetDescriptor(Node node, Namespace instance, Namespace context, AnalysisUnit 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;
        }
Пример #48
0
 public override INamespaceSet GetIndex(Node node, AnalysisUnit unit, INamespaceSet index)
 {
     var res = NamespaceSet.Empty;
     foreach (var member in _members) {
         res = res.Union(member.GetIndex(node, unit, index));
     }
     return res;
 }
Пример #49
0
 public override INamespaceSet ReverseBinaryOperation(Node node, AnalysisUnit unit, Parsing.JOperator operation, INamespaceSet rhs)
 {
     return _original.ReverseBinaryOperation(node, unit, operation, rhs);
 }
Пример #50
0
 public override void SetIndex(Node node, AnalysisUnit unit, INamespaceSet index, INamespaceSet value)
 {
     foreach (var member in _members) {
         member.SetIndex(node, unit, index, value);
     }
 }
Пример #51
0
 public override void SetMember(Node node, AnalysisUnit unit, string name, INamespaceSet value)
 {
     _original.SetMember(node, unit, name, value);
 }
Пример #52
0
 public EnumGenerator(string package, EnumType type, ISymbolMap symbols, INamespaceSet namespaces = null)
     : base(package, type, symbols, namespaces)
 {
 }