Skip to content

Commit f4d1980

Browse files
committed
[Heavy] Add dump-source-loc
1 parent 7f706b4 commit f4d1980

File tree

7 files changed

+50
-0
lines changed

7 files changed

+50
-0
lines changed

heavy/include/heavy/Context.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ using llvm::dyn_cast;
4848
using llvm::dyn_cast_or_null;
4949
using llvm::isa;
5050

51+
class SourceManager;
5152
class OpGen;
5253
class Context;
5354

@@ -128,6 +129,7 @@ class Context : public ContinuationStack<Context>,
128129
mlir::Operation* ModuleOp = nullptr;
129130

130131
public:
132+
heavy::SourceManager* SourceManager = nullptr;
131133
heavy::OpGen* OpGen = nullptr;
132134
heavy::OpEvalPtr OpEval;
133135

heavy/include/heavy/Value.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,8 @@ class SourceValue : public ValueBase,
697697
ValueWithSource(L)
698698
{ }
699699

700+
using ValueWithSource::getSourceLocation;
701+
700702
static bool classof(Value V) {
701703
return V.getKind() == ValueKind::SourceValue;
702704
}

heavy/include/heavy/base.sld

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
include-ci
3232
include-library-declarations
3333
source-loc
34+
dump-source-loc
3435
parse-source-file
3536
+
3637
-

heavy/lib/Builtins.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "heavy/Context.h"
1616
#include "heavy/Dialect.h"
1717
#include "heavy/OpGen.h"
18+
#include "heavy/SourceManager.h"
1819
#include "heavy/Value.h"
1920
#include "llvm/ADT/StringExtras.h"
2021
#include "llvm/Support/Casting.h"
@@ -88,6 +89,7 @@ heavy::ExternFunction error;
8889
heavy::ExternFunction dynamic_wind;
8990
heavy::ExternFunction load_module;
9091
heavy::ExternFunction source_loc;
92+
heavy::ExternFunction dump_source_loc;
9193
heavy::ExternFunction make_syntactic_closure;
9294

9395
heavy::ExternFunction eval;
@@ -402,6 +404,37 @@ void source_loc(Context& C, ValueRefs Args) {
402404
C.Cont(C.CreateSourceValue(Loc));
403405
}
404406

407+
void dump_source_loc(Context& C, ValueRefs Args) {
408+
if (Args.size() != 1)
409+
return C.RaiseError("invalid arity");
410+
if (!C.SourceManager)
411+
return C.RaiseError("source manager not available");
412+
413+
// TODO
414+
// This could be generalize to take an optional message to work with
415+
// errors, warnings, notes, etc.
416+
417+
heavy::SourceLocation Loc = Args[0].getSourceLocation();
418+
heavy::FullSourceLocation SL = C.SourceManager->getFullSourceLocation(Loc);
419+
if (SL.isValid()) {
420+
heavy::SourceLineContext LineContext = SL.getLineContext();
421+
llvm::errs() << LineContext.FileName
422+
<< ':' << LineContext.LineNumber
423+
<< ':' << LineContext.Column
424+
// TODO optional message could go here
425+
<< '\n'
426+
<< LineContext.LineRange << '\n';
427+
// Display the caret pointing to the point of interest.
428+
for (unsigned i = 1; i < LineContext.Column; i++) {
429+
llvm::errs() << ' ';
430+
}
431+
llvm::errs() << "^\n";
432+
} else {
433+
llvm::errs() << "<<INVALID SOURCE LOCATION>>\n";
434+
}
435+
C.Cont();
436+
}
437+
405438
} // end of namespace heavy::builtins
406439

407440
namespace heavy {
@@ -1217,6 +1250,7 @@ void HEAVY_BASE_INIT(heavy::Context& Context) {
12171250

12181251

12191252
HEAVY_BASE_VAR(source_loc) = heavy::builtins::source_loc;
1253+
HEAVY_BASE_VAR(dump_source_loc) = heavy::builtins::dump_source_loc;
12201254
HEAVY_BASE_VAR(parse_source_file).init(Context);
12211255

12221256
// functions
@@ -1311,6 +1345,7 @@ void HEAVY_BASE_LOAD_MODULE(heavy::Context& Context) {
13111345
HEAVY_BASE_VAR(include_library_declarations)},
13121346

13131347
{"source-loc", HEAVY_BASE_VAR(source_loc)},
1348+
{"dump-source-loc", HEAVY_BASE_VAR(dump_source_loc)},
13141349
{"parse-source-file",
13151350
HEAVY_BASE_VAR(parse_source_file).get(Context)},
13161351

heavy/lib/Context.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ Context::Context()
8080
EnvStack(Empty()),
8181
DialectRegistry(std::make_unique<mlir::DialectRegistry>()),
8282
MLIRContext(std::make_unique<mlir::MLIRContext>(*DialectRegistry)),
83+
SourceManager(nullptr),
8384
OpGen(nullptr)
8485
{
8586
NameForImportVar = HEAVY_IMPORT_VAR;
@@ -365,6 +366,13 @@ class Writer : public ValueVisitor<Writer> {
365366
<< ">";
366367
}
367368

369+
void VisitSourceValue(SourceValue* SV) {
370+
heavy::SourceLocation Loc = SV->getSourceLocation();
371+
OS << "\n#<SourceValue {"
372+
<< Loc.getEncoding()
373+
<< "}>";
374+
}
375+
368376
void VisitUndefined(Undefined U) {
369377
OS << "#<Undefined";
370378
if (Value Tracer = U.getTracer()) {

heavy/lib/HeavyScheme.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ void HeavyScheme::ProcessTopLevelCommands(
6060
heavy::tok Terminator) {
6161
auto& Context = getContext();
6262
auto& SM = getSourceManager();
63+
Context.SourceManager = &SM;
6364
auto ParserPtr = std::make_unique<Parser>(Lexer, Context);
6465
Parser& Parser = *ParserPtr;
6566
heavy::Environment* Env = Context.DefaultEnv.get();

heavy/lib/SourceManager.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ SourceManager::getClosestLineStart(SourceFile File,
9696

9797
SourceLineContext
9898
FullSourceLocation::getLineContext() const {
99+
assert(isValid() && "expecting valid source location");
99100
char const* const FileStartPos = File.Buffer.begin();
100101
char const* const FileEndPos = File.Buffer.end();
101102
// We could store SourceLineStarts to help with speeding

0 commit comments

Comments
 (0)