Skip to content

Commit 25b3ccc

Browse files
authored
AddedSourceRange and SourcePosition (#297)
Resolves issues #293, #291 and #290 This involved modifications to any source files that used the "old" SourceLocation.
1 parent bff3905 commit 25b3ccc

31 files changed

+298
-166
lines changed

src/Samples/Kaleidoscope/Chapter9/CodeGenerator.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,8 @@ private void EmitLocation( IAstNode? node )
512512
if(scope != null)
513513
{
514514
loc = new DILocation( InstructionBuilder.Context
515-
, (uint)(node?.Location.StartLine ?? 0)
516-
, (uint)(node?.Location.StartColumn ?? 0)
515+
, (uint)(node?.Location.Start.Line ?? 0)
516+
, (uint)(node?.Location.Start.Column ?? 0)
517517
, scope
518518
);
519519
}
@@ -561,11 +561,11 @@ private Function GetOrDeclareFunction( Prototype prototype )
561561
, name: prototype.Name
562562
, linkageName: null
563563
, file: debugFile
564-
, line: (uint)prototype.Location.StartLine
564+
, line: (uint)prototype.Location.Start.Line
565565
, signature
566566
, isLocalToUnit: false
567567
, isDefinition: true
568-
, scopeLine: (uint)lastParamLocation.EndLine
568+
, scopeLine: (uint)lastParamLocation.End.Line
569569
, debugFlags: prototype.IsCompilerGenerated ? DebugInfoFlags.Artificial : DebugInfoFlags.Prototyped
570570
, isOptimized: false
571571
);
@@ -590,8 +590,8 @@ private void AddDebugInfoForAlloca( Alloca argSlot, Function function, Parameter
590590
{
591591
Debug.Assert( CurrentDIBuilder is not null, "Internal error CurrentDIBuilder should be set in Generate already" );
592592

593-
uint line = ( uint )param.Location.StartLine;
594-
uint col = ( uint )param.Location.StartColumn;
593+
uint line = ( uint )param.Location.Start.Line;
594+
uint col = ( uint )param.Location.Start.Column;
595595

596596
// Keep compiler happy on null checks by asserting on expectations
597597
// The items were created in this file with all necessary info so
@@ -620,8 +620,8 @@ private void AddDebugInfoForAlloca( Alloca argSlot, Function function, LocalVari
620620
{
621621
Debug.Assert( CurrentDIBuilder is not null, "Internal error CurrentDIBuilder should be set in Generate already" );
622622

623-
uint line = ( uint )localVar.Location.StartLine;
624-
uint col = ( uint )localVar.Location.StartColumn;
623+
uint line = ( uint )localVar.Location.Start.Line;
624+
uint col = ( uint )localVar.Location.Start.Column;
625625

626626
// Keep compiler happy on null checks by asserting on expectations
627627
// The items were created in this file with all necessary info so

src/Samples/Kaleidoscope/Kaleidoscope.Grammar/ANTLR/KaleidoscopeParser.BinaryPrototypeContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ internal partial class BinaryPrototypeContext
2020
{
2121
public IToken OpToken => userdefinedop().Start;
2222

23-
public override IEnumerable<(string Name, int Index, SourceLocation Span)> Parameters
24-
=> Identifier().Select( ( id, i ) => (id.GetText(), i, id.GetSourceLocation()) );
23+
public override IEnumerable<(string Name, int Index, SourceRange Span)> Parameters
24+
=> Identifier().Select( ( id, i ) => (id.GetText(), i, id.GetSourceRange()) );
2525

2626
public int Precedence => (int)double.Parse( Number().GetText(), CultureInfo.InvariantCulture );
2727
}

src/Samples/Kaleidoscope/Kaleidoscope.Grammar/ANTLR/KaleidoscopeParser.FunctionPrototypeContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ internal partial class FunctionPrototypeContext
1515
{
1616
public override string Name => Identifier( 0 ).GetText();
1717

18-
public override IEnumerable<(string Name, int Index, SourceLocation Span)> Parameters
19-
=> Identifier().Skip( 1 ).Select( ( id, i ) => (id.GetText(), i, id.GetSourceLocation()) );
18+
public override IEnumerable<(string Name, int Index, SourceRange Span)> Parameters
19+
=> Identifier().Skip( 1 ).Select( ( id, i ) => (id.GetText(), i, id.GetSourceRange()) );
2020
}
2121
}
2222
}

src/Samples/Kaleidoscope/Kaleidoscope.Grammar/ANTLR/KaleidoscopeParser.PrototypeContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ internal partial class KaleidoscopeParser
1313
// base type for all prototypes (technically abstract, but the generator doesn't apply that)
1414
internal partial class PrototypeContext
1515
{
16-
public virtual IEnumerable<(string Name, int Index, SourceLocation Span)> Parameters
17-
=> Enumerable.Empty<(string, int, SourceLocation)>();
16+
public virtual IEnumerable<(string Name, int Index, SourceRange Span)> Parameters
17+
=> Enumerable.Empty<(string, int, SourceRange)>();
1818

1919
public virtual string Name => string.Empty;
2020
}

src/Samples/Kaleidoscope/Kaleidoscope.Grammar/ANTLR/KaleidoscopeParser.UnaryPrototypeContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ internal partial class KaleidoscopeParser
1414
{
1515
internal partial class UnaryPrototypeContext
1616
{
17-
public override IEnumerable<(string Name, int Index, SourceLocation Span)> Parameters
17+
public override IEnumerable<(string Name, int Index, SourceRange Span)> Parameters
1818
{
1919
get
2020
{
21-
yield return (Identifier().GetText(), 0, Identifier().GetSourceLocation());
21+
yield return (Identifier().GetText(), 0, Identifier().GetSourceRange());
2222
}
2323
}
2424

src/Samples/Kaleidoscope/Kaleidoscope.Grammar/AST/AstBuilder.cs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public AstBuilder( DynamicRuntimeState globalState )
2626

2727
public override IAstNode VisitErrorNode( IErrorNode node )
2828
{
29-
return new ErrorNode( node.GetSourceLocation(), $"Syntax Error: {node}" );
29+
return new ErrorNode( node.GetSourceRange(), $"Syntax Error: {node}" );
3030
}
3131

3232
public override IAstNode VisitParenExpression( ParenExpressionContext context )
@@ -36,23 +36,23 @@ public override IAstNode VisitParenExpression( ParenExpressionContext context )
3636

3737
public override IAstNode VisitConstExpression( ConstExpressionContext context )
3838
{
39-
return new ConstantExpression( context.GetSourceLocation(), context.Value );
39+
return new ConstantExpression( context.GetSourceRange(), context.Value );
4040
}
4141

4242
public override IAstNode VisitVariableExpression( VariableExpressionContext context )
4343
{
4444
string varName = context.Name;
4545
return NamedValues.TryGetValue( varName, out IVariableDeclaration? declaration )
46-
? new VariableReferenceExpression( context.GetSourceLocation(), declaration )
47-
: new ErrorNode( context.GetSourceLocation(), $"Unknown variable name: {varName}" );
46+
? new VariableReferenceExpression( context.GetSourceRange(), declaration )
47+
: new ErrorNode( context.GetSourceRange(), $"Unknown variable name: {varName}" );
4848
}
4949

5050
public override IAstNode VisitFunctionCallExpression( FunctionCallExpressionContext context )
5151
{
5252
Prototype? function = FindCallTarget( context.CaleeName );
5353
if(function is null)
5454
{
55-
return new ErrorNode( context.GetSourceLocation(), $"Call to unknown function '{context.CaleeName}'" );
55+
return new ErrorNode( context.GetSourceRange(), $"Call to unknown function '{context.CaleeName}'" );
5656
}
5757

5858
var argNodes = ( from expCtx in context.expression( )
@@ -67,7 +67,7 @@ select expCtx.Accept( this )
6767
}
6868
}
6969

70-
return new FunctionCallExpression( context.GetSourceLocation(), function, argNodes.Cast<IExpression>() );
70+
return new FunctionCallExpression( context.GetSourceRange(), function, argNodes.Cast<IExpression>() );
7171
}
7272

7373
public override IAstNode VisitExpression( ExpressionContext context )
@@ -126,7 +126,7 @@ public override IAstNode VisitFunctionDefinition( FunctionDefinitionContext cont
126126
return body;
127127
}
128128

129-
var retVal = new FunctionDefinition( context.GetSourceLocation( )
129+
var retVal = new FunctionDefinition( context.GetSourceRange( )
130130
, sig
131131
, exp
132132
, LocalVariables.ToImmutableArray( )
@@ -150,7 +150,7 @@ public override IAstNode VisitFunctionDefinition( FunctionDefinitionContext cont
150150
public override IAstNode VisitTopLevelExpression( TopLevelExpressionContext context )
151151
{
152152
BeginFunctionDefinition();
153-
var sig = new Prototype( context.GetSourceLocation( ), RuntimeState.GenerateAnonymousName( ), true );
153+
var sig = new Prototype( context.GetSourceRange( ), RuntimeState.GenerateAnonymousName( ), true );
154154
var bodyNode = context.expression( ).Accept( this );
155155
var errors = bodyNode.CollectErrors();
156156

@@ -160,7 +160,7 @@ public override IAstNode VisitTopLevelExpression( TopLevelExpressionContext cont
160160
return bodyNode;
161161
}
162162

163-
var retVal = new FunctionDefinition( context.GetSourceLocation( ), sig, bodyExp, true );
163+
var retVal = new FunctionDefinition( context.GetSourceRange( ), sig, bodyExp, true );
164164
RuntimeState.FunctionDefinitions.AddOrReplaceItem( retVal );
165165
return retVal;
166166
}
@@ -172,18 +172,18 @@ public override IAstNode VisitUnaryOpExpression( UnaryOpExpressionContext contex
172172
var opKind = RuntimeState.GetUnaryOperatorInfo( context.Op ).Kind;
173173
if(opKind == OperatorKind.None)
174174
{
175-
return new ErrorNode( context.GetSourceLocation(), $"invalid unary operator {context.Op}" );
175+
return new ErrorNode( context.GetSourceRange(), $"invalid unary operator {context.Op}" );
176176
}
177177

178178
string calleeName = CreateUnaryFunctionName( context.OpToken );
179179
var function = FindCallTarget( calleeName );
180180
if(function == null)
181181
{
182-
return new ErrorNode( context.GetSourceLocation(), $"reference to unknown unary operator function {calleeName}" );
182+
return new ErrorNode( context.GetSourceRange(), $"reference to unknown unary operator function {calleeName}" );
183183
}
184184

185185
var arg = context.Rhs.Accept( this );
186-
return arg is not IExpression exp ? arg : new FunctionCallExpression( context.GetSourceLocation(), function, exp );
186+
return arg is not IExpression exp ? arg : new FunctionCallExpression( context.GetSourceRange(), function, exp );
187187
}
188188
#endregion
189189

@@ -193,12 +193,12 @@ public override IAstNode VisitFullsrc( FullsrcContext context )
193193
where child is not TopLevelSemicolonContext
194194
select child.Accept( this );
195195

196-
return new RootNode( context.GetSourceLocation(), children );
196+
return new RootNode( context.GetSourceRange(), children );
197197
}
198198

199199
public override IAstNode VisitConditionalExpression( ConditionalExpressionContext context )
200200
{
201-
var expressionSpan = context.GetSourceLocation( );
201+
var expressionSpan = context.GetSourceRange( );
202202
var condition = context.Condition.Accept( this );
203203
if(condition is not IExpression conditionExp)
204204
{
@@ -258,7 +258,7 @@ public override IAstNode VisitForExpression( ForExpressionContext context )
258258
return bodyNode;
259259
}
260260

261-
retVal = new ForInExpression( context.GetSourceLocation(), initializer, conditionExp, stepExp, bodyExp );
261+
retVal = new ForInExpression( context.GetSourceRange(), initializer, conditionExp, stepExp, bodyExp );
262262
}
263263

264264
return retVal;
@@ -277,7 +277,7 @@ public override IAstNode VisitVarInExpression( VarInExpressionContext context )
277277
}
278278

279279
var bodyNode = context.Scope.Accept( this );
280-
return bodyNode is not IExpression bodyExp ? bodyNode : new VarInExpression( context.GetSourceLocation(), localVariables, bodyExp );
280+
return bodyNode is not IExpression bodyExp ? bodyNode : new VarInExpression( context.GetSourceRange(), localVariables, bodyExp );
281281
}
282282
}
283283

@@ -288,8 +288,8 @@ public override IAstNode VisitFunctionPrototype( FunctionPrototypeContext contex
288288

289289
public override IAstNode VisitInitializer( InitializerContext context )
290290
{
291-
var value = ( IExpression )(context.Value?.Accept( this ) ?? new ConstantExpression( context.GetSourceLocation( ), 0.0 ));
292-
return new LocalVariableDeclaration( context.GetSourceLocation()
291+
var value = ( IExpression )(context.Value?.Accept( this ) ?? new ConstantExpression( context.GetSourceRange( ), 0.0 ));
292+
return new LocalVariableDeclaration( context.GetSourceRange()
293293
, context.Name
294294
, value
295295
);
@@ -328,25 +328,25 @@ private IAstNode CreateBinaryOperatorNode( IExpression lhs, BinaryopContext op,
328328
switch(op.OpToken.Type)
329329
{
330330
case LEFTANGLE:
331-
return new BinaryOperatorExpression( op.GetSourceLocation(), lhs, BuiltInOperatorKind.Less, rhs );
331+
return new BinaryOperatorExpression( op.GetSourceRange(), lhs, BuiltInOperatorKind.Less, rhs );
332332

333333
case CARET:
334-
return new BinaryOperatorExpression( op.GetSourceLocation(), lhs, BuiltInOperatorKind.Pow, rhs );
334+
return new BinaryOperatorExpression( op.GetSourceRange(), lhs, BuiltInOperatorKind.Pow, rhs );
335335

336336
case PLUS:
337-
return new BinaryOperatorExpression( op.GetSourceLocation(), lhs, BuiltInOperatorKind.Add, rhs );
337+
return new BinaryOperatorExpression( op.GetSourceRange(), lhs, BuiltInOperatorKind.Add, rhs );
338338

339339
case MINUS:
340-
return new BinaryOperatorExpression( op.GetSourceLocation(), lhs, BuiltInOperatorKind.Subtract, rhs );
340+
return new BinaryOperatorExpression( op.GetSourceRange(), lhs, BuiltInOperatorKind.Subtract, rhs );
341341

342342
case ASTERISK:
343-
return new BinaryOperatorExpression( op.GetSourceLocation(), lhs, BuiltInOperatorKind.Multiply, rhs );
343+
return new BinaryOperatorExpression( op.GetSourceRange(), lhs, BuiltInOperatorKind.Multiply, rhs );
344344

345345
case SLASH:
346-
return new BinaryOperatorExpression( op.GetSourceLocation(), lhs, BuiltInOperatorKind.Divide, rhs );
346+
return new BinaryOperatorExpression( op.GetSourceRange(), lhs, BuiltInOperatorKind.Divide, rhs );
347347

348348
case ASSIGN:
349-
return new BinaryOperatorExpression( op.GetSourceLocation(), lhs, BuiltInOperatorKind.Assign, rhs );
349+
return new BinaryOperatorExpression( op.GetSourceRange(), lhs, BuiltInOperatorKind.Assign, rhs );
350350

351351
#region UserBinaryOpExpression
352352
default:
@@ -355,14 +355,14 @@ private IAstNode CreateBinaryOperatorNode( IExpression lhs, BinaryopContext op,
355355
var opKind = RuntimeState.GetBinOperatorInfo( op.OpToken.Type ).Kind;
356356
if(opKind != OperatorKind.InfixLeftAssociative && opKind != OperatorKind.InfixRightAssociative)
357357
{
358-
return new ErrorNode( op.GetSourceLocation(), $"Invalid binary operator '{op.OpToken.Text}'" );
358+
return new ErrorNode( op.GetSourceRange(), $"Invalid binary operator '{op.OpToken.Text}'" );
359359
}
360360

361361
string calleeName = CreateBinaryFunctionName( op.OpToken );
362362
Prototype? callTarget = FindCallTarget( calleeName );
363363
return callTarget is null
364-
? new ErrorNode( op.GetSourceLocation(), $"Unary operator function '{calleeName}' not found" )
365-
: new FunctionCallExpression( op.GetSourceLocation(), callTarget, lhs, rhs );
364+
? new ErrorNode( op.GetSourceRange(), $"Unary operator function '{calleeName}' not found" )
365+
: new FunctionCallExpression( op.GetSourceRange(), callTarget, lhs, rhs );
366366
}
367367
#endregion
368368
}
@@ -385,7 +385,7 @@ private IAstNode BuildPrototype( PrototypeContext context, string name )
385385
name = context.Name;
386386
}
387387

388-
var retVal = new Prototype( context.GetSourceLocation()
388+
var retVal = new Prototype( context.GetSourceRange()
389389
, name
390390
, false
391391
, context.Parent is ExternalDeclarationContext
@@ -397,7 +397,7 @@ private IAstNode BuildPrototype( PrototypeContext context, string name )
397397
{
398398
if(existingPrototype.Parameters.Count != retVal.Parameters.Count)
399399
{
400-
return new ErrorNode( context.GetSourceLocation(), "Declaration incompatible with previous declaration" );
400+
return new ErrorNode( context.GetSourceRange(), "Declaration incompatible with previous declaration" );
401401
}
402402
}
403403

src/Samples/Kaleidoscope/Kaleidoscope.Grammar/AST/BinaryOperatorExpression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public sealed class BinaryOperatorExpression
2929
/// <param name="lhs">Left hand side expression for the operator</param>
3030
/// <param name="op">Operator type</param>
3131
/// <param name="rhs">Right hand side expression for the operator</param>
32-
public BinaryOperatorExpression( SourceLocation location, IExpression lhs, BuiltInOperatorKind op, IExpression rhs )
32+
public BinaryOperatorExpression( SourceRange location, IExpression lhs, BuiltInOperatorKind op, IExpression rhs )
3333
: base( location )
3434
{
3535
Left = lhs;

src/Samples/Kaleidoscope/Kaleidoscope.Grammar/AST/ConditionalExpression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public sealed class ConditionalExpression
1111
: AstNode
1212
, IExpression
1313
{
14-
public ConditionalExpression( SourceLocation location
14+
public ConditionalExpression( SourceRange location
1515
, IExpression condition
1616
, IExpression thenExpression
1717
, IExpression elseExpression

src/Samples/Kaleidoscope/Kaleidoscope.Grammar/AST/ConstantExpression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public sealed class ConstantExpression
1212
: AstNode
1313
, IExpression
1414
{
15-
public ConstantExpression( SourceLocation location, double value )
15+
public ConstantExpression( SourceRange location, double value )
1616
: base( location )
1717
{
1818
Value = value;

src/Samples/Kaleidoscope/Kaleidoscope.Grammar/AST/ForInExpression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public sealed class ForInExpression
1111
: AstNode
1212
, IExpression
1313
{
14-
public ForInExpression( SourceLocation location
14+
public ForInExpression( SourceRange location
1515
, LocalVariableDeclaration loopVariable
1616
, IExpression condition
1717
, IExpression step

0 commit comments

Comments
 (0)