Skip to content

Commit 0035fa3

Browse files
authored
Merge pull request #5862 from MDoerner/StopBailingOutCompletelyOnFailedSupertypeResolution
Stop bailing out completely on failed supertype resolution
2 parents b55e3b7 + 770c568 commit 0035fa3

File tree

6 files changed

+129
-31
lines changed

6 files changed

+129
-31
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Antlr4.Runtime;
2+
using Antlr4.Runtime.Tree;
3+
using NLog;
4+
using Rubberduck.Parsing.Symbols;
5+
6+
namespace Rubberduck.Parsing.Binding
7+
{
8+
public abstract class BindingContextBase : IBindingContext
9+
{
10+
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
11+
12+
13+
14+
protected IExpressionBinding HandleUnexpectedExpressionType(ParserRuleContext expression)
15+
{
16+
Logger.Warn($"Unexpected context type {expression.GetType()}");
17+
return new FailedExpressionBinding(expression);
18+
}
19+
20+
public abstract IBoundExpression Resolve(Declaration module,
21+
Declaration parent,
22+
ParserRuleContext expression,
23+
IBoundExpression withBlockVariable,
24+
StatementResolutionContext statementContext,
25+
bool requiresLetCoercion = false,
26+
bool isLetAssignment = false);
27+
28+
public abstract IExpressionBinding BuildTree(Declaration module,
29+
Declaration parent,
30+
ParserRuleContext expression,
31+
IBoundExpression withBlockVariable,
32+
StatementResolutionContext statementContext,
33+
bool requiresLetCoercion = false,
34+
bool isLetAssignment = false);
35+
}
36+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Antlr4.Runtime;
2+
3+
namespace Rubberduck.Parsing.Binding
4+
{
5+
public sealed class FailedExpressionBinding : IExpressionBinding
6+
{
7+
private readonly ParserRuleContext _context;
8+
9+
public FailedExpressionBinding(ParserRuleContext context)
10+
{
11+
_context = context;
12+
}
13+
14+
public IBoundExpression Resolve()
15+
{
16+
return new ResolutionFailedExpression(_context);
17+
}
18+
}
19+
}

Rubberduck.Parsing/Binding/DefaultBindingContext.cs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
using Rubberduck.Parsing.Symbols;
44
using System;
55
using System.Collections.Generic;
6-
using Antlr4.Runtime.Tree;
76
using Rubberduck.Parsing.VBA.DeclarationCaching;
87

98
namespace Rubberduck.Parsing.Binding
109
{
11-
public sealed class DefaultBindingContext : IBindingContext
10+
public sealed class DefaultBindingContext : BindingContextBase
1211
{
1312
private readonly DeclarationFinder _declarationFinder;
1413
private readonly IBindingContext _typeBindingContext;
@@ -24,19 +23,24 @@ public DefaultBindingContext(
2423
_procedurePointerBindingContext = procedurePointerBindingContext;
2524
}
2625

27-
public IBoundExpression Resolve(Declaration module, Declaration parent, IParseTree expression, IBoundExpression withBlockVariable, StatementResolutionContext statementContext, bool requiresLetCoercion, bool isLetAssignment)
26+
public override IBoundExpression Resolve(Declaration module,
27+
Declaration parent,
28+
ParserRuleContext expression,
29+
IBoundExpression withBlockVariable,
30+
StatementResolutionContext statementContext,
31+
bool requiresLetCoercion = false,
32+
bool isLetAssignment = false)
2833
{
2934
var bindingTree = BuildTree(module, parent, expression, withBlockVariable, statementContext, requiresLetCoercion, isLetAssignment);
3035
return bindingTree?.Resolve();
3136
}
3237

33-
public IExpressionBinding BuildTree(
34-
Declaration module,
35-
Declaration parent,
36-
IParseTree expression,
37-
IBoundExpression withBlockVariable,
38+
public override IExpressionBinding BuildTree(Declaration module,
39+
Declaration parent,
40+
ParserRuleContext expression,
41+
IBoundExpression withBlockVariable,
3842
StatementResolutionContext statementContext,
39-
bool requiresLetCoercion = false,
43+
bool requiresLetCoercion = false,
4044
bool isLetAssignment = false)
4145
{
4246
return Visit(
@@ -49,7 +53,7 @@ public IExpressionBinding BuildTree(
4953
isLetAssignment);
5054
}
5155

52-
public IExpressionBinding Visit(Declaration module, Declaration parent, IParseTree expression, IBoundExpression withBlockVariable, StatementResolutionContext statementContext, bool requiresLetCoercion = false, bool isLetAssignment = false)
56+
public IExpressionBinding Visit(Declaration module, Declaration parent, ParserRuleContext expression, IBoundExpression withBlockVariable, StatementResolutionContext statementContext, bool requiresLetCoercion = false, bool isLetAssignment = false)
5357
{
5458
if (requiresLetCoercion && expression is ParserRuleContext context)
5559
{
@@ -76,7 +80,7 @@ public IExpressionBinding Visit(Declaration module, Declaration parent, IParseTr
7680
case VBAParser.UnqualifiedObjectPrintStmtContext unqualifiedObjectPrintStmtContext:
7781
return Visit(module, parent, unqualifiedObjectPrintStmtContext, withBlockVariable);
7882
default:
79-
throw new NotSupportedException($"Unexpected context type {expression.GetType()}");
83+
return HandleUnexpectedExpressionType(expression);
8084
}
8185
}
8286

@@ -160,7 +164,7 @@ private IExpressionBinding Visit(Declaration module, Declaration parent, VBAPars
160164
return Visit(module, parent, builtInTypeExprContext, statementContext);
161165
//We do not handle the VBAParser.TypeofexprContext because that should only ever appear as a child of an IS relational operator expression and is specifically handled there.
162166
default:
163-
throw new NotSupportedException($"Unexpected expression type {expression.GetType()}");
167+
return HandleUnexpectedExpressionType(expression);
164168
}
165169
}
166170

@@ -187,7 +191,7 @@ private IExpressionBinding Visit(Declaration module, Declaration parent, VBAPars
187191
case VBAParser.ObjectPrintExprContext objectPrintExprContext:
188192
return Visit(module, parent, objectPrintExprContext, withBlockVariable);
189193
default:
190-
throw new NotSupportedException($"Unexpected lExpression type {expression.GetType()}");
194+
return HandleUnexpectedExpressionType(expression);
191195
}
192196
}
193197

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
1-
using Antlr4.Runtime.Tree;
1+
using Antlr4.Runtime;
22
using Rubberduck.Parsing.Symbols;
33

44
namespace Rubberduck.Parsing.Binding
55
{
66
public interface IBindingContext
77
{
8-
IBoundExpression Resolve(Declaration module, Declaration parent, IParseTree expression, IBoundExpression withBlockVariable, StatementResolutionContext statementContext, bool requiresLetCoercion = false, bool isLetAssignment = false);
9-
IExpressionBinding BuildTree(Declaration module, Declaration parent, IParseTree expression, IBoundExpression withBlockVariable, StatementResolutionContext statementContext, bool requiresLetCoercion = false, bool isLetAssignment = false);
8+
IBoundExpression Resolve(
9+
Declaration module,
10+
Declaration parent,
11+
ParserRuleContext expression,
12+
IBoundExpression withBlockVariable,
13+
StatementResolutionContext statementContext,
14+
bool requiresLetCoercion = false,
15+
bool isLetAssignment = false);
16+
IExpressionBinding BuildTree(
17+
Declaration module,
18+
Declaration parent,
19+
ParserRuleContext expression,
20+
IBoundExpression withBlockVariable,
21+
StatementResolutionContext statementContext,
22+
bool requiresLetCoercion = false,
23+
bool isLetAssignment = false);
1024
}
1125
}

Rubberduck.Parsing/Binding/ProcedurePointerBindingContext.cs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
using System;
2-
using Antlr4.Runtime.Tree;
1+
using Antlr4.Runtime;
32
using Rubberduck.Parsing.Grammar;
43
using Rubberduck.Parsing.Symbols;
54
using Rubberduck.Parsing.VBA.DeclarationCaching;
65

76
namespace Rubberduck.Parsing.Binding
87
{
9-
public sealed class ProcedurePointerBindingContext : IBindingContext
8+
public sealed class ProcedurePointerBindingContext : BindingContextBase
109
{
1110
private readonly DeclarationFinder _declarationFinder;
1211

@@ -15,7 +14,14 @@ public ProcedurePointerBindingContext(DeclarationFinder declarationFinder)
1514
_declarationFinder = declarationFinder;
1615
}
1716

18-
public IBoundExpression Resolve(Declaration module, Declaration parent, IParseTree expression, IBoundExpression withBlockVariable, StatementResolutionContext statementContext, bool requiresLetCoercion = false, bool isLetAssignment = false)
17+
public override IBoundExpression Resolve(
18+
Declaration module,
19+
Declaration parent,
20+
ParserRuleContext expression,
21+
IBoundExpression withBlockVariable,
22+
StatementResolutionContext statementContext,
23+
bool requiresLetCoercion = false,
24+
bool isLetAssignment = false)
1925
{
2026
IExpressionBinding bindingTree = BuildTree(module, parent, expression, withBlockVariable, statementContext);
2127
if (bindingTree != null)
@@ -25,7 +31,14 @@ public IBoundExpression Resolve(Declaration module, Declaration parent, IParseTr
2531
return null;
2632
}
2733

28-
public IExpressionBinding BuildTree(Declaration module, Declaration parent, IParseTree expression, IBoundExpression withBlockVariable, StatementResolutionContext statementContext, bool requiresLetCoercion = false, bool isLetAssignment = false)
34+
public override IExpressionBinding BuildTree(
35+
Declaration module,
36+
Declaration parent,
37+
ParserRuleContext expression,
38+
IBoundExpression withBlockVariable,
39+
StatementResolutionContext statementContext,
40+
bool requiresLetCoercion = false,
41+
bool isLetAssignment = false)
2942
{
3043
switch (expression)
3144
{
@@ -36,7 +49,7 @@ public IExpressionBinding BuildTree(Declaration module, Declaration parent, IPar
3649
case VBAParser.AddressOfExpressionContext addressOfExpressionContext:
3750
return Visit(module, parent, addressOfExpressionContext);
3851
default:
39-
throw new NotSupportedException($"Unexpected context type {expression.GetType()}");
52+
return HandleUnexpectedExpressionType(expression);
4053
}
4154
}
4255

@@ -52,7 +65,7 @@ private IExpressionBinding Visit(Declaration module, Declaration parent, VBAPars
5265
case VBAParser.LExprContext lExprContext:
5366
return Visit(module, parent, lExprContext.lExpression());
5467
default:
55-
throw new NotSupportedException($"Unexpected expression type {expression.GetType()}");
68+
return HandleUnexpectedExpressionType(expression);
5669
}
5770
}
5871

@@ -65,7 +78,7 @@ private IExpressionBinding Visit(Declaration module, Declaration parent, VBAPars
6578
case VBAParser.MemberAccessExprContext memberAccessExprContext:
6679
return Visit(module, parent, memberAccessExprContext);
6780
default:
68-
throw new NotSupportedException($"Unexpected lExpression type {expression.GetType()}");
81+
return HandleUnexpectedExpressionType(expression);
6982
}
7083
}
7184

Rubberduck.Parsing/Binding/TypeBindingContext.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
using System;
2-
using Antlr4.Runtime.Tree;
1+
using Antlr4.Runtime;
32
using Rubberduck.Parsing.Grammar;
43
using Rubberduck.Parsing.Symbols;
54
using Rubberduck.Parsing.VBA.DeclarationCaching;
65

76
namespace Rubberduck.Parsing.Binding
87
{
9-
public sealed class TypeBindingContext : IBindingContext
8+
public sealed class TypeBindingContext : BindingContextBase
109
{
1110
private readonly DeclarationFinder _declarationFinder;
1211

@@ -15,13 +14,26 @@ public TypeBindingContext(DeclarationFinder declarationFinder)
1514
_declarationFinder = declarationFinder;
1615
}
1716

18-
public IBoundExpression Resolve(Declaration module, Declaration parent, IParseTree expression, IBoundExpression withBlockVariable, StatementResolutionContext statementContext, bool requiresLetCoercion = false, bool isLetAssignment = false)
17+
public override IBoundExpression Resolve(
18+
Declaration module,
19+
Declaration parent, ParserRuleContext expression,
20+
IBoundExpression withBlockVariable,
21+
StatementResolutionContext statementContext,
22+
bool requiresLetCoercion = false,
23+
bool isLetAssignment = false)
1924
{
2025
IExpressionBinding bindingTree = BuildTree(module, parent, expression, withBlockVariable, statementContext);
2126
return bindingTree?.Resolve();
2227
}
2328

24-
public IExpressionBinding BuildTree(Declaration module, Declaration parent, IParseTree expression, IBoundExpression withBlockVariable, StatementResolutionContext statementContext, bool requiresLetCoercion = false, bool isLetAssignment = false)
29+
public override IExpressionBinding BuildTree(
30+
Declaration module,
31+
Declaration parent,
32+
ParserRuleContext expression,
33+
IBoundExpression withBlockVariable,
34+
StatementResolutionContext statementContext,
35+
bool requiresLetCoercion = false,
36+
bool isLetAssignment = false)
2537
{
2638
switch (expression)
2739
{
@@ -32,7 +44,7 @@ public IExpressionBinding BuildTree(Declaration module, Declaration parent, IPar
3244
case VBAParser.BuiltInTypeExprContext builtInTypeExprContext:
3345
return Visit(builtInTypeExprContext.builtInType());
3446
default:
35-
throw new NotSupportedException($"Unexpected context type {expression.GetType()}");
47+
return HandleUnexpectedExpressionType(expression);
3648
}
3749
}
3850

@@ -52,7 +64,7 @@ private IExpressionBinding Visit(Declaration module, Declaration parent, VBAPars
5264
case VBAParser.IndexExprContext indexExprContext:
5365
return Visit(module, parent, indexExprContext);
5466
default:
55-
throw new NotSupportedException($"Unexpected lExpression type {expression.GetType()}");
67+
return HandleUnexpectedExpressionType(expression);
5668
}
5769
}
5870

0 commit comments

Comments
 (0)