Skip to content

Commit 608a01d

Browse files
committed
major issues all fixed. doesn't resolve calls to default instance members ("static classes").
1 parent 551accf commit 608a01d

File tree

1 file changed

+60
-22
lines changed

1 file changed

+60
-22
lines changed

Rubberduck.Parsing/Symbols/IdentifierReferenceResolver.cs

Lines changed: 60 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,23 @@ private Declaration ResolveType(VBAParser.ComplexTypeContext context)
158158
// note: inter-project references won't work, but we can qualify VbaStandardLib types:
159159
if (libraryName == _qualifiedModuleName.ProjectName || libraryName == "VBA")
160160
{
161-
return _declarations[identifier.GetText()].SingleOrDefault(item =>
162-
item.ProjectName == libraryName
163-
&& (_moduleTypes.Contains(item.DeclarationType)) || item.DeclarationType == DeclarationType.UserDefinedType);
161+
var matches = _declarations[identifier.GetText()];
162+
try
163+
{
164+
return matches.SingleOrDefault(item =>
165+
item.ProjectName == libraryName
166+
&& (item.Accessibility == Accessibility.Public
167+
|| item.Accessibility == Accessibility.Global
168+
|| item.Accessibility == Accessibility.Friend
169+
|| item.Accessibility == Accessibility.Implicit)
170+
&& (_moduleTypes.Contains(item.DeclarationType))
171+
|| (item.DeclarationType == DeclarationType.UserDefinedType
172+
&& item.ComponentName == _currentScope.ComponentName));
173+
}
174+
catch (InvalidOperationException)
175+
{
176+
return null;
177+
}
164178
}
165179

166180
return null;
@@ -240,12 +254,15 @@ private Declaration ResolveInternal(ParserRuleContext callSiteContext, Declarati
240254
}
241255

242256
var identifierName = callSiteContext.GetText();
243-
Declaration callee;
257+
Declaration callee = null;
244258
if (localScope.DeclarationType == DeclarationType.Variable)
245259
{
246-
// localScope is a UDT
260+
// localScope is probably a UDT
247261
var udt = ResolveType(localScope);
248-
callee = _declarations[identifierName].SingleOrDefault(item => item.Context != null && item.Context.Parent == udt.Context);
262+
if (udt != null)
263+
{
264+
callee = _declarations[identifierName].SingleOrDefault(item => item.Context != null && item.Context.Parent == udt.Context);
265+
}
249266
}
250267
else
251268
{
@@ -689,7 +706,7 @@ public void Resolve(VBAParser.VsAssignContext context)
689706
{
690707
// named parameter reference must be scoped to called procedure
691708
var callee = FindParentCall(context);
692-
ResolveInternal(context.implicitCallStmt_InStmt(), callee);
709+
ResolveInternal(context.implicitCallStmt_InStmt(), callee, ContextAccessorType.GetValueOrReference);
693710
}
694711

695712
private Declaration FindParentCall(VBAParser.VsAssignContext context)
@@ -738,11 +755,18 @@ private Declaration FindLocalScopeDeclaration(string identifierName, Declaration
738755
}
739756

740757
var matches = _declarations[identifierName];
741-
var parent = matches.SingleOrDefault(item =>
742-
item.ParentScope == localScope.Scope
743-
&& !_moduleTypes.Contains(item.DeclarationType));
744758

745-
return parent;
759+
try
760+
{
761+
return matches.SingleOrDefault(item =>
762+
item.ParentScope == localScope.Scope
763+
&& localScope.Context.GetSelection().Contains(item.Selection)
764+
&& !_moduleTypes.Contains(item.DeclarationType));
765+
}
766+
catch (InvalidOperationException)
767+
{
768+
return null;
769+
}
746770
}
747771

748772
private Declaration FindModuleScopeDeclaration(string identifierName, Declaration localScope = null)
@@ -753,10 +777,17 @@ private Declaration FindModuleScopeDeclaration(string identifierName, Declaratio
753777
}
754778

755779
var matches = _declarations[identifierName];
756-
return matches.SingleOrDefault(item =>
757-
item.ParentScope == localScope.ParentScope
758-
&& !item.DeclarationType.HasFlag(DeclarationType.Member)
759-
&& !_moduleTypes.Contains(item.DeclarationType));
780+
try
781+
{
782+
return matches.SingleOrDefault(item =>
783+
item.ParentScope == localScope.ParentScope
784+
&& !item.DeclarationType.HasFlag(DeclarationType.Member)
785+
&& !_moduleTypes.Contains(item.DeclarationType));
786+
}
787+
catch (InvalidOperationException)
788+
{
789+
return null;
790+
}
760791
}
761792

762793
private Declaration FindModuleScopeProcedure(string identifierName, Declaration localScope, ContextAccessorType accessorType, bool isAssignmentTarget = false)
@@ -774,20 +805,27 @@ private Declaration FindModuleScopeProcedure(string identifierName, Declaration
774805
&& item.ComponentName == localScope.ComponentName
775806
&& (IsProcedure(item, localScope) || IsPropertyAccessor(item, accessorType, localScope, isAssignmentTarget)));
776807
}
777-
catch (InvalidOperationException e)
808+
catch (InvalidOperationException)
778809
{
779810
return null;
780811
}
781812
}
782813

783814
private Declaration FindProjectScopeDeclaration(string identifierName)
784815
{
785-
// assume unqualified variable call, i.e. unique declaration.
786-
return _declarations[identifierName].SingleOrDefault(item =>
787-
!item.DeclarationType.HasFlag(DeclarationType.Member)
788-
&& (item.Accessibility == Accessibility.Public
789-
|| item.Accessibility == Accessibility.Global
790-
|| _moduleTypes.Contains(item.DeclarationType) /* because static classes are accessed just like modules */));
816+
var matches = _declarations[identifierName];
817+
try
818+
{
819+
return matches.SingleOrDefault(item =>
820+
!item.DeclarationType.HasFlag(DeclarationType.Member)
821+
&& (item.Accessibility == Accessibility.Public
822+
|| item.Accessibility == Accessibility.Global
823+
|| _moduleTypes.Contains(item.DeclarationType) /* because static classes are accessed just like modules */));
824+
}
825+
catch (InvalidOperationException)
826+
{
827+
return null;
828+
}
791829
}
792830

793831
private bool IsProcedure(Declaration item, Declaration localScope)

0 commit comments

Comments
 (0)