public override IEnumerable <KeyValuePair <IAnalysisSet, IAnalysisSet> > GetItems() { for (int i = 0; i < IndexTypes.Length; i++) { var value = IndexTypes[i]; yield return(new KeyValuePair <IAnalysisSet, IAnalysisSet>( ProjectState.GetConstant(i), value.Types )); } }
public override IAnalysisSet Call(Node node, AnalysisUnit unit, IAnalysisSet[] args, NameExpression[] keywordArgNames) { if (args.Length == 1) { var res = unit.Scope.GetOrMakeNodeValue( node, NodeValueKind.Sequence, (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)); }
public override IAnalysisSet UnaryOperation(Node node, AnalysisUnit unit, PythonOperator operation) { if (operation == PythonOperator.Negate && _value != null) { if (_value is int i) { return(ProjectState.GetConstant(-i)); } else if (_value is float f) { return(ProjectState.GetConstant(-f)); } else if (_value is double d) { return(ProjectState.GetConstant(-d)); } } return(_builtinInfo.UnaryOperation(node, unit, operation)); }
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 GetEnumerationValues(Node node, AnalysisUnit unit) { var res = AnalysisSet.Empty; if (_descriptors != null) { foreach (var kvp in _descriptors) { var key = kvp.Key; if (key == "prototype" || key == "__proto__") { // including prototype can cause things to explode, and it's not // enumerable anyway... This should be replaced w/ more general // support for non-enumerable properties. continue; } if (kvp.Value.Values != null) { var types = kvp.Value.Values.GetTypesNoCopy(unit, ProjectEntry); kvp.Value.Values.ClearOldValues(); if (kvp.Value.Values.VariableStillExists) { res = res.Add(ProjectState.GetConstant(kvp.Key).Proxy); } } if (kvp.Value.Getter != null) { foreach (var value in kvp.Value.Getter.GetTypesNoCopy(unit, ProjectEntry)) { res = res.Add(ProjectState.GetConstant(kvp.Key).Proxy); } } } } return(res); }
internal void AssignTo(Node assignStmt, Expression left, ISet <Namespace> values) { if (left is NameExpression) { var l = (NameExpression)left; var vars = LookupVariableByName(l.Name, l, false); if (vars == null) { vars = Scopes[Scopes.Length - 1].CreateVariable(left, _unit, l.Name, false); } vars.AddAssignment(left, _unit); vars.AddTypes(l, _unit, values); } else if (left is MemberExpression) { var l = (MemberExpression)left; foreach (var obj in Evaluate(l.Target)) { obj.SetMember(assignStmt, _unit, l.Name, values); } } else if (left is IndexExpression) { var l = (IndexExpression)left; var indexObj = Evaluate(l.Index); foreach (var obj in Evaluate(l.Target)) { obj.SetIndex(assignStmt, _unit, indexObj, values); } } else if (left is SequenceExpression) { // list/tuple var l = (SequenceExpression)left; foreach (var value in values.ToArray()) { for (int i = 0; i < l.Items.Count; i++) { AssignTo(assignStmt, l.Items[i], value.GetIndex(assignStmt, _unit, ProjectState.GetConstant(i))); } } } }
internal void AssignTo(Node assignStmt, Expression left, IAnalysisSet values) { if (left is ExpressionWithAnnotation) { left = ((ExpressionWithAnnotation)left).Expression; // "x:t=..." is a recommended pattern - we do not want to // actually assign the ellipsis in this case. if (values.Any(v => v.TypeId == BuiltinTypeId.Ellipsis)) { values = AnalysisSet.Create(values.Where(v => v.TypeId != BuiltinTypeId.Ellipsis), values.Comparer); } } if (left is NameExpression) { var l = (NameExpression)left; if (!string.IsNullOrEmpty(l.Name)) { Scope.AssignVariable( l.Name, l, _unit, values ); } } else if (left is MemberExpression) { var l = (MemberExpression)left; if (!string.IsNullOrEmpty(l.Name)) { foreach (var obj in Evaluate(l.Target).Resolve(_unit)) { obj.SetMember(l, _unit, l.Name, values.Resolve(_unit)); } } } else if (left is IndexExpression) { var l = (IndexExpression)left; var indexObj = Evaluate(l.Index); foreach (var obj in Evaluate(l.Target).Resolve(_unit)) { obj.SetIndex(assignStmt, _unit, indexObj, values.Resolve(_unit)); } } else if (left is SequenceExpression) { // list/tuple var l = (SequenceExpression)left; var valuesArr = values.ToArray(); for (var i = 0; i < l.Items.Count; i++) { if (valuesArr.Length > 0) { foreach (var value in valuesArr) { AssignTo(assignStmt, l.Items[i], value.GetIndex(assignStmt, _unit, ProjectState.GetConstant(i))); } } else { AssignTo(assignStmt, l.Items[i], AnalysisSet.Empty); } } } }
internal void AssignTo(Node assignStmt, Expression left, IAnalysisSet values) { if (left is NameExpression) { var l = (NameExpression)left; if (!string.IsNullOrEmpty(l.Name)) { Scope.AssignVariable( l.Name, l, _unit, values ); } } else if (left is MemberExpression) { var l = (MemberExpression)left; if (!string.IsNullOrEmpty(l.Name)) { foreach (var obj in Evaluate(l.Target)) { obj.SetMember(l, _unit, l.Name, values); } } } else if (left is IndexExpression) { var l = (IndexExpression)left; var indexObj = Evaluate(l.Index); foreach (var obj in Evaluate(l.Target)) { obj.SetIndex(assignStmt, _unit, indexObj, values); } } else if (left is SequenceExpression) { // list/tuple var l = (SequenceExpression)left; var valuesArr = values.ToArray(); for (int i = 0; i < l.Items.Count; i++) { if (valuesArr.Length > 0) { foreach (var value in valuesArr) { AssignTo(assignStmt, l.Items[i], value.GetIndex(assignStmt, _unit, ProjectState.GetConstant(i))); } } else { AssignTo(assignStmt, l.Items[i], AnalysisSet.Empty); } } } }