Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.
15 changes: 3 additions & 12 deletions src/Analysis/Engine/Impl/EncodedLocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,9 @@ public EncodedLocation(ILocationResolver resolver, object location) {

public bool IsAlive => (Resolver as ICanExpire)?.IsAlive ?? true;

public override int GetHashCode() {
return (Resolver?.GetHashCode() ?? 0) ^ (Location?.GetHashCode() ?? 0);
}
public override int GetHashCode() => (Resolver?.GetHashCode() ?? 0) ^ (Location?.GetHashCode() ?? 0);

public override bool Equals(object obj) {
return obj is EncodedLocation location && Equals(location);
}
public override bool Equals(object obj) => obj is EncodedLocation location && Equals(location);

#region IEquatable<EncodedLocation> Members

Expand All @@ -71,11 +67,6 @@ public bool Equals(EncodedLocation other) {

#endregion

public ILocationInfo GetLocationInfo() {
if (Resolver == null) {
return Location as ILocationInfo;
}
return Resolver.ResolveLocation(Location);
}
public ILocationInfo GetLocationInfo() => Resolver?.ResolveLocation(Location) ?? Location as ILocationInfo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using System.Collections.Generic;
using Microsoft.PythonTools.Analysis;
using Microsoft.PythonTools.Analysis.Infrastructure;

namespace Microsoft.PythonTools.Interpreter.Ast {
class AstPythonBoundMethod : IPythonBoundFunction, ILocatedMember {
Expand All @@ -27,6 +28,6 @@ public AstPythonBoundMethod(IPythonFunction function, IPythonType selfType) {
public IPythonFunction Function { get; }
public IPythonType SelfType { get; }
public PythonMemberType MemberType => PythonMemberType.Method;
public IEnumerable<ILocationInfo> Locations => (Function as ILocatedMember)?.Locations;
public IEnumerable<ILocationInfo> Locations => (Function as ILocatedMember)?.Locations.MaybeEnumerate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public string Documentation {
public Uri DocumentUri { get; }
public PythonMemberType MemberType => PythonMemberType.Module;
public Dictionary<object, object> Properties { get; } = new Dictionary<object, object>();
public IEnumerable<ILocationInfo> Locations { get; }
public IEnumerable<ILocationInfo> Locations { get; } = Enumerable.Empty<ILocationInfo>();

public int AnalysisVersion => 1;
public IModuleContext AnalysisContext => null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public AstPythonProperty(
ILocationInfo location
) {
IsReadOnly = true;
Locations = new[] { location };
Locations = location != null ? new[] { location } : Enumerable.Empty<ILocationInfo>();
FunctionDefinition = definition;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Analysis/Engine/Impl/Interpreter/Ast/AstPythonType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private AstPythonType(string name, Dictionary<string, IMember> members, IEnumera
_members = members;
_mro = Array.Empty<IPythonType>();
DeclaringModule = NoDeclModule;
Locations = locations;
Locations = locations ?? Enumerable.Empty<ILocationInfo>();
}

internal void AddMembers(IEnumerable<KeyValuePair<string, IMember>> members, bool overwrite) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public virtual IEnumerable<string> GetMemberNames(IModuleContext moduleContext)
internal PythonAst Ast { get; private set; }

#if DEBUG
public IEnumerable<ILocationInfo> Locations { get; private set; } = new LocationInfo[0];
public IEnumerable<ILocationInfo> Locations { get; private set; } = Enumerable.Empty<ILocationInfo>();
#endif

protected virtual List<string> GetScrapeArguments(IPythonInterpreterFactory factory) {
Expand Down
2 changes: 1 addition & 1 deletion src/Analysis/Engine/Impl/MemberResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ internal MemberResult(string name, Func<IEnumerable<AnalysisValue>> vars, Func<P
///
/// New in 1.5.
/// </summary>
public IEnumerable<ILocationInfo> Locations => Values.SelectMany(ns => ns.Locations);
public IEnumerable<ILocationInfo> Locations => Values.SelectMany(ns => ns.Locations.MaybeEnumerate()).ExcludeDefault();

public IEnumerable<AnalysisValue> Values => _vars.Value;

Expand Down
2 changes: 1 addition & 1 deletion src/Analysis/Engine/Impl/ModuleAnalysis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ internal static IEnumerable<AnalysisVariable> ToVariables(IReferenceable referen
varType = VariableType.Definition;
}

foreach (var loc in type.Locations.WhereNotNull()) {
foreach (var loc in type.Locations.MaybeEnumerate().WhereNotNull()) {
yield return new AnalysisVariable(def, varType, loc);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Analysis/Engine/Impl/ModuleTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ internal override bool ModuleContainsMember(IModuleContext context, string name)
return false;
}

foreach (var location in type.Locations) {
foreach (var location in type.Locations.MaybeEnumerate().WhereNotNull()) {
if (location.FilePath != modInfo.ProjectEntry.FilePath) {
// declared in another module
return false;
Expand Down
6 changes: 3 additions & 3 deletions src/Analysis/Engine/Impl/Values/BoundMethodInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
// MERCHANTABILITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.

using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.PythonTools.Analysis.Infrastructure;
using Microsoft.PythonTools.Interpreter;
using Microsoft.PythonTools.Parsing.Ast;

Expand Down Expand Up @@ -45,7 +45,7 @@ public override IAnalysisSet Call(Node node, AnalysisUnit unit, IAnalysisSet[] a

public override IPythonProjectEntry DeclaringModule => Function.DeclaringModule;
public override int DeclaringVersion => Function.DeclaringVersion;
public override IEnumerable<ILocationInfo> Locations => Function.Locations;
public override IEnumerable<ILocationInfo> Locations => Function.Locations.MaybeEnumerate();
public override BuiltinTypeId TypeId => BuiltinTypeId.Function;

public IEnumerable<KeyValuePair<string, string>> GetRichDescription() {
Expand Down
10 changes: 1 addition & 9 deletions src/Analysis/Engine/Impl/Values/BuiltinNamespace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,7 @@ public MemberContainerType ContainedValue {

public virtual ILocatedMember GetLocatedMember() => null;

public override IEnumerable<ILocationInfo> Locations {
get {
var locatedMem = GetLocatedMember();
if (locatedMem != null) {
return locatedMem.Locations;
}
return LocationInfo.Empty;
}
}
public override IEnumerable<ILocationInfo> Locations => GetLocatedMember()?.Locations.MaybeEnumerate();

public override bool Equals(object obj) {
if (obj is BuiltinNamespace<MemberContainerType> bn && GetType() == bn.GetType()) {
Expand Down
3 changes: 2 additions & 1 deletion src/Analysis/Engine/Impl/Values/MemberReferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.PythonTools.Analysis.Analyzer;
using Microsoft.PythonTools.Analysis.Infrastructure;
using Microsoft.PythonTools.Interpreter;
using Microsoft.PythonTools.Parsing.Ast;

Expand Down Expand Up @@ -62,7 +63,7 @@ public IEnumerable<IReferenceable> GetDefinitions(string name, IMemberContainer

var member = innerContainer.GetMember(context, name) as ILocatedMember;
if (member != null) {
res.AddRange(member.Locations.Select(loc => new DefinitionList(loc)));
res.AddRange(member.Locations.MaybeEnumerate().Select(loc => new DefinitionList(loc)));
}

return res;
Expand Down
2 changes: 1 addition & 1 deletion src/Analysis/Engine/Impl/Values/MultipleMemberInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public override string ShortDescription {
}
}

public override IEnumerable<ILocationInfo> Locations => _members.SelectMany(m => m.Locations);
public override IEnumerable<ILocationInfo> Locations => _members.SelectMany(m => m.Locations.MaybeEnumerate());

IModule IModule.GetChildPackage(IModuleContext context, string name) {
var children = new List<AnalysisValue>();
Expand Down
43 changes: 13 additions & 30 deletions src/Analysis/Engine/Impl/Values/ProtocolInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,15 @@ public override PythonMemberType MemberType {
}


internal override void AddReference(Node node, AnalysisUnit analysisUnit) {
_references.GetReferences(analysisUnit.ProjectEntry as ProjectEntry)?.AddReference(new EncodedLocation(analysisUnit, node));
}
internal override void AddReference(Node node, AnalysisUnit analysisUnit)
=> _references.GetReferences(analysisUnit.ProjectEntry as ProjectEntry)?.AddReference(new EncodedLocation(analysisUnit, node));

public override IEnumerable<ILocationInfo> Locations {
get {
ReferenceList defns;
if (!_references.TryGetValue(DeclaringModule, out defns)) {
if (!_references.TryGetValue(DeclaringModule, out var defns)) {
return Enumerable.Empty<ILocationInfo>();
}
return defns.Definitions.Select(l => l.GetLocationInfo()).Where(l => l != null);
return defns.Definitions.Select(l => l.GetLocationInfo()).ExcludeDefault();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we have two almost identical methods: ExcludeDefaults and WhereNotNull.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes ExcludeDefault came from RTVS extensions, looks like WhereNotNull is local thing

}
}

Expand All @@ -151,17 +149,14 @@ public override void AugmentAssign(AugmentedAssignStatement node, AnalysisUnit u
}
}

public override IAnalysisSet Await(Node node, AnalysisUnit unit) {
return AnalysisSet.UnionAll(_protocols.Select(p => p.Await(node, unit)));
}
public override IAnalysisSet Await(Node node, AnalysisUnit unit)
=> AnalysisSet.UnionAll(_protocols.Select(p => p.Await(node, unit)));

public override IAnalysisSet BinaryOperation(Node node, AnalysisUnit unit, PythonOperator operation, IAnalysisSet rhs) {
return AnalysisSet.UnionAll(_protocols.Select(p => p.BinaryOperation(node, unit, operation, rhs)));
}
public override IAnalysisSet BinaryOperation(Node node, AnalysisUnit unit, PythonOperator operation, IAnalysisSet rhs)
=> AnalysisSet.UnionAll(_protocols.Select(p => p.BinaryOperation(node, unit, operation, rhs)));

public override IAnalysisSet Call(Node node, AnalysisUnit unit, IAnalysisSet[] args, NameExpression[] keywordArgNames) {
return AnalysisSet.UnionAll(_protocols.Select(p => p.Call(node, unit, args, keywordArgNames)));
}
public override IAnalysisSet Call(Node node, AnalysisUnit unit, IAnalysisSet[] args, NameExpression[] keywordArgNames)
=> AnalysisSet.UnionAll(_protocols.Select(p => p.Call(node, unit, args, keywordArgNames)));

public override void DeleteMember(Node node, AnalysisUnit unit, string name) {
foreach (var p in _protocols) {
Expand Down Expand Up @@ -273,22 +268,10 @@ public override bool Equals(object obj) {

public override int GetHashCode() => ObjectComparer.Instance.GetHashCode(_protocols);

internal override bool UnionEquals(AnalysisValue av, int strength) {
if (av is ProtocolInfo pi) {
if (strength > 0) {
return Name == pi.Name;
}
return ObjectComparer.Instance.Equals(_protocols, pi._protocols);
}
return false;
}
internal override bool UnionEquals(AnalysisValue av, int strength)
Comment thread
MikhailArkhipov marked this conversation as resolved.
Resolved
=> av is ProtocolInfo pi && ObjectComparer.Instance.Equals(_protocols, pi._protocols);

internal override int UnionHashCode(int strength) {
if (strength > 0) {
return Name.GetHashCode();
}
return GetHashCode();
}
internal override int UnionHashCode(int strength) => strength > 0 ? Name.GetHashCode() : GetHashCode();

internal override AnalysisValue UnionMergeTypes(AnalysisValue av, int strength) {
if (strength > 0 && av is ProtocolInfo pi && pi.Push()) {
Expand Down
50 changes: 25 additions & 25 deletions src/Analysis/Engine/Impl/Values/Protocols.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,19 @@ protected IAnalysisSet MakeMethod(string qualname, IReadOnlyList<IAnalysisSet> a

public override PythonMemberType MemberType => PythonMemberType.Unknown;

// Do not return any default values from protocols. We call these directly and handle null.
public override IAnalysisSet Await(Node node, AnalysisUnit unit) => null;
public override IAnalysisSet BinaryOperation(Node node, AnalysisUnit unit, PythonOperator operation, IAnalysisSet rhs) => null;
public override IAnalysisSet GetAsyncEnumeratorTypes(Node node, AnalysisUnit unit) => null;
public override IAnalysisSet GetAsyncIterator(Node node, AnalysisUnit unit) => null;
public override IAnalysisSet GetDescriptor(Node node, AnalysisValue instance, AnalysisValue context, AnalysisUnit unit) => null;
public override IAnalysisSet GetDescriptor(PythonAnalyzer projectState, AnalysisValue instance, AnalysisValue context) => null;
public override IAnalysisSet GetEnumeratorTypes(Node node, AnalysisUnit unit) => null;
public override IAnalysisSet GetIndex(Node node, AnalysisUnit unit, IAnalysisSet index) => null;
public override IAnalysisSet GetInstanceType() => null;
public override IEnumerable<KeyValuePair<IAnalysisSet, IAnalysisSet>> GetItems() => null;
public override IAnalysisSet GetIterator(Node node, AnalysisUnit unit) => null;
public override IAnalysisSet GetReturnForYieldFrom(Node node, AnalysisUnit unit) => null;
public override IAnalysisSet UnaryOperation(Node node, AnalysisUnit unit, PythonOperator operation) => null;
public override IAnalysisSet Await(Node node, AnalysisUnit unit) => AnalysisSet.Empty;
public override IAnalysisSet BinaryOperation(Node node, AnalysisUnit unit, PythonOperator operation, IAnalysisSet rhs) => AnalysisSet.Empty;
public override IAnalysisSet GetAsyncEnumeratorTypes(Node node, AnalysisUnit unit) => AnalysisSet.Empty;
public override IAnalysisSet GetAsyncIterator(Node node, AnalysisUnit unit) => AnalysisSet.Empty;
public override IAnalysisSet GetDescriptor(Node node, AnalysisValue instance, AnalysisValue context, AnalysisUnit unit) => AnalysisSet.Empty;
public override IAnalysisSet GetDescriptor(PythonAnalyzer projectState, AnalysisValue instance, AnalysisValue context) => AnalysisSet.Empty;
public override IAnalysisSet GetEnumeratorTypes(Node node, AnalysisUnit unit) => AnalysisSet.Empty;
public override IAnalysisSet GetIndex(Node node, AnalysisUnit unit, IAnalysisSet index) => AnalysisSet.Empty;
public override IAnalysisSet GetInstanceType() => AnalysisSet.Empty;
public override IEnumerable<KeyValuePair<IAnalysisSet, IAnalysisSet>> GetItems() => Enumerable.Empty< KeyValuePair<IAnalysisSet, IAnalysisSet>>();
public override IAnalysisSet GetIterator(Node node, AnalysisUnit unit) => AnalysisSet.Empty;
public override IAnalysisSet GetReturnForYieldFrom(Node node, AnalysisUnit unit) => AnalysisSet.Empty;
public override IAnalysisSet UnaryOperation(Node node, AnalysisUnit unit, PythonOperator operation) => AnalysisSet.Empty;

public override IDictionary<string, IAnalysisSet> GetAllMembers(IModuleContext moduleContext, GetMemberOptions options = GetMemberOptions.None) {
EnsureMembers();
Expand Down Expand Up @@ -131,21 +130,22 @@ class NameProtocol : Protocol {
private readonly BuiltinTypeId _typeId;
private List<KeyValuePair<string, string>> _richDescription;

public NameProtocol(ProtocolInfo self, string name, string documentation = null, BuiltinTypeId typeId = BuiltinTypeId.Unknown, PythonMemberType memberType = PythonMemberType.Unknown) : base(self) {
public NameProtocol(
ProtocolInfo self,
string name,
string documentation = null,
BuiltinTypeId typeId = BuiltinTypeId.Unknown,
PythonMemberType memberType = PythonMemberType.Unknown)
: base(self) {
_name = name;
_doc = documentation;
_typeId = typeId;
MemberType = memberType;
_richDescription = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>(WellKnownRichDescriptionKinds.Type, _name) };
}

public NameProtocol(ProtocolInfo self, IPythonType type) : base(self) {
_name = type.Name;
_doc = type.Documentation;
_typeId = type.TypeId;
MemberType = type.MemberType;
_richDescription = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>(WellKnownRichDescriptionKinds.Type, _name) };
}
public NameProtocol(ProtocolInfo self, IPythonType type)
: this(self, type.Name, type.Documentation, type.TypeId, type.MemberType) { }

public void ExtendDescription(KeyValuePair<string, string> part) {
_richDescription.Add(part);
Expand Down Expand Up @@ -851,12 +851,12 @@ public override IAnalysisSet GetDescriptor(PythonAnalyzer projectState, Analysis
public override IEnumerable<KeyValuePair<IAnalysisSet, IAnalysisSet>> GetItems() => _actualType.GetItems();
public override IAnalysisSet GetIterator(Node node, AnalysisUnit unit) => _actualType.GetIterator(node, unit);
public override IAnalysisSet GetReturnForYieldFrom(Node node, AnalysisUnit unit) => _actualType.GetReturnForYieldFrom(node, unit);
public override IEnumerable<ILocationInfo> Locations => _actualType.Locations;
public override IEnumerable<ILocationInfo> Locations => _actualType.Locations.MaybeEnumerate();
public override IMro Mro => _actualType.Mro;
public override IEnumerable<OverloadResult> Overloads => _actualType.Overloads;
public override IEnumerable<OverloadResult> Overloads => _actualType.Overloads.MaybeEnumerate();
public override int? GetLength() => _actualType.GetLength();
internal override IAnalysisSet Resolve(AnalysisUnit unit, ResolutionContext context) => _actualType.Resolve(unit, context);
internal override IEnumerable<ILocationInfo> References => _actualType.References;
internal override IEnumerable<ILocationInfo> References => _actualType.References.MaybeEnumerate();
public override IAnalysisSet ReverseBinaryOperation(Node node, AnalysisUnit unit, PythonOperator operation, IAnalysisSet rhs)
=> _actualType.ReverseBinaryOperation(node, unit, operation, rhs);

Expand Down
Loading