1616
1717using System ;
1818using System . Collections . Generic ;
19+ using System . Diagnostics ;
1920using System . Linq ;
2021using System . Text ;
2122using System . Threading . Tasks ;
2627namespace Microsoft . PythonTools . Analysis {
2728 public class OverloadResult : IOverloadResult {
2829 private readonly ParameterResult [ ] _parameters ;
29- private readonly IAnalysisSet _returnTypes ;
30+ private readonly string [ ] _returnType ;
3031
31- public OverloadResult ( ParameterResult [ ] parameters , string name , string documentation , IAnalysisSet returnTypes ) {
32+ public OverloadResult ( ParameterResult [ ] parameters , string name , string documentation , IEnumerable < string > returnType ) {
3233 _parameters = parameters ;
3334 Name = name ;
3435 Documentation = documentation ;
35- _returnTypes = returnTypes ;
36+ _returnType = returnType . MaybeEnumerate ( ) . ToArray ( ) ;
3637 }
3738
3839 public string Name { get ; }
39- public virtual IReadOnlyList < string > ReturnType => _returnTypes . Select ( t => t . Name ) . ToArray ( ) ;
40- public virtual IAnalysisSet ReturnTypes => _returnTypes ;
40+ public virtual IReadOnlyList < string > ReturnType => _returnType ;
4141 public virtual string Documentation { get ; }
4242 public virtual ParameterResult [ ] Parameters => _parameters ;
4343
4444 internal virtual OverloadResult WithNewParameters ( ParameterResult [ ] newParameters )
45- => new OverloadResult ( newParameters , Name , Documentation , ReturnTypes ) ;
45+ => new OverloadResult ( newParameters , Name , Documentation , _returnType ) ;
4646
4747 internal virtual OverloadResult WithoutLeadingParameters ( int skipCount = 1 )
48- => new OverloadResult ( _parameters . Skip ( skipCount ) . ToArray ( ) , Name , Documentation , _returnTypes ) ;
48+ => new OverloadResult ( _parameters . Skip ( skipCount ) . ToArray ( ) , Name , Documentation , _returnType ) ;
4949
5050 private static string Longest ( string x , string y ) {
5151 if ( x == null ) {
@@ -115,8 +115,8 @@ public static OverloadResult Merge(IEnumerable<OverloadResult> overloads) {
115115 return res ;
116116 } ) ;
117117
118- var returnTypes = overloads . SelectMany ( o => o . ReturnTypes ) . Distinct ( ) ;
119- return new OverloadResult ( parameters , name , doc , AnalysisSet . Create ( returnTypes ) ) ;
118+ var returnType = overloads . SelectMany ( o => o . ReturnType ) . Distinct ( ) ;
119+ return new OverloadResult ( parameters , name , doc , returnType ) ;
120120 }
121121
122122 public override string ToString ( ) {
@@ -131,7 +131,7 @@ public override string ToString() {
131131
132132 var returnType = "{0}{1}{2}" . FormatInvariant (
133133 ReturnType . Count > 1 ? "[" : string . Empty ,
134- ReturnType . Count > 0 ? string . Join ( "," , ReturnType . OrderBy ( k => k ) ) : "None" ,
134+ ReturnType . Count > 0 ? string . Join ( ", " , ReturnType . OrderBy ( k => k ) ) : "None" ,
135135 ReturnType . Count > 1 ? "]" : string . Empty
136136 ) ;
137137
@@ -146,7 +146,7 @@ class AccumulatedOverloadResult {
146146 private string [ ] _pnames ;
147147 private IAnalysisSet [ ] _ptypes ;
148148 private string [ ] _pdefaults ;
149- private IAnalysisSet _rtypes ;
149+ private readonly HashSet < string > _rtypes ;
150150
151151 public AccumulatedOverloadResult ( string name , string documentation , int parameters ) {
152152 _name = name ;
@@ -155,7 +155,7 @@ public AccumulatedOverloadResult(string name, string documentation, int paramete
155155 _ptypes = new IAnalysisSet [ parameters ] ;
156156 _pdefaults = new string [ parameters ] ;
157157 ParameterCount = parameters ;
158- _rtypes = AnalysisSet . Empty ;
158+ _rtypes = new HashSet < string > ( ) ;
159159 }
160160
161161 public int ParameterCount { get ; }
@@ -184,12 +184,12 @@ private IAnalysisSet ChooseBest(IAnalysisSet x, IAnalysisSet y) {
184184 return ( y == null ) ? AnalysisSet . Empty : y ;
185185 }
186186 if ( y == null || y . IsObjectOrUnknown ( ) ) {
187- return null ;
187+ return AnalysisSet . Empty ;
188188 }
189- return MergeTypes ( x , y ) ;
189+ return x . Union ( y ) ;
190190 }
191191
192- public bool TryAddOverload ( string name , string documentation , string [ ] names , IAnalysisSet [ ] types , string [ ] defaults , IAnalysisSet returnTypes ) {
192+ public bool TryAddOverload ( string name , string documentation , string [ ] names , IAnalysisSet [ ] types , string [ ] defaults , IEnumerable < string > returnTypes ) {
193193 if ( names . Length != _pnames . Length || types . Length != _ptypes . Length ) {
194194 return false ;
195195 }
@@ -203,7 +203,7 @@ public bool TryAddOverload(string name, string documentation, string[] names, IA
203203 return false ;
204204 }
205205
206- for ( int i = 0 ; i < _pnames . Length ; ++ i ) {
206+ for ( var i = 0 ; i < _pnames . Length ; ++ i ) {
207207 _pnames [ i ] = ChooseBest ( _pnames [ i ] , names [ i ] ) ;
208208 _ptypes [ i ] = ChooseBest ( _ptypes [ i ] , types [ i ] ) ;
209209 _pdefaults [ i ] = ChooseBest ( _pdefaults [ i ] , defaults [ i ] ) ;
@@ -217,7 +217,7 @@ public bool TryAddOverload(string name, string documentation, string[] names, IA
217217 }
218218
219219 if ( returnTypes != null ) {
220- _rtypes = ChooseBest ( _rtypes , returnTypes ) ;
220+ _rtypes . UnionWith ( returnTypes ) ;
221221 }
222222
223223 return true ;
@@ -241,27 +241,6 @@ public OverloadResult ToOverloadResult() {
241241 }
242242 return new OverloadResult ( parameters , _name , _doc , _rtypes ) ;
243243 }
244-
245- private IAnalysisSet MergeTypes ( IAnalysisSet x , IAnalysisSet y ) {
246- // Merge types so we get simple parameter description such as
247- // list[int] rather than duplicates such as list[int],list[int, int, int].
248- var cmp = UnionComparer . Instances [ 1 ] ;
249- var xA = x . ToArray ( ) ;
250- var yA = y . ToArray ( ) ;
251- var mergedValues = new List < AnalysisValue > ( ) ;
252- for ( var i = 0 ; i < xA . Length ; i ++ ) {
253- // Order types for merging in the order of description length
254- // so more complex type would possibly fold into the simpler type
255- // such as list[int, int, int] will fold into the list[int].
256-
257- // TODO: this is not bulletproof.
258- // For example, it will merge user type derived from int into the int.
259- var xd = string . Join ( "," , xA [ i ] . GetShortDescriptions ( ) ) ;
260- var yd = string . Join ( "," , yA [ i ] . GetShortDescriptions ( ) ) ;
261- mergedValues . Add ( xd . Length < yd . Length ? cmp . MergeTypes ( xA [ i ] , yA [ i ] , out _ ) : cmp . MergeTypes ( yA [ i ] , xA [ i ] , out _ ) ) ;
262- }
263- return AnalysisSet . CreateUnion ( mergedValues , cmp ) ;
264- }
265244 }
266245
267246 class BuiltinFunctionOverloadResult : OverloadResult {
0 commit comments