Skip to content

Commit 214eb45

Browse files
authored
Merge pull request #3762 from bio-aeon/add-getfc-elab
[ elab ] Add `getFC` for call-site source location
2 parents 29d925c + 4731e6b commit 214eb45

7 files changed

Lines changed: 29 additions & 0 deletions

File tree

CHANGELOG_NEXT.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ should target this file (`CHANGELOG_NEXT`).
7373
* Better messages for errors inside string interpolation.
7474
* Added execution time logging for elaboration scripts.
7575
* Optimised the passing of local variables during compile-time normalisation.
76+
* Added `getFC` to elaborator reflection, exposing the macro call-site source
77+
location.
7678

7779
### Building/Packaging changes
7880

CONTRIBUTORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Aleksei Volkov
88
Alex Asafov
99
Alex Gryzlov
1010
Alex Silva
11+
Alexandr Chigrinets
1112
Alissa Tung
1213
Andor Penzes
1314
Andre Kuhlenschmidt

libs/base/Language/Reflection.idr

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ data Elab : Type -> Type where
9797
GetReferredFns : Name -> Elab (List Name)
9898
-- Get the name of the current and outer functions, if it is applicable
9999
GetCurrentFn : Elab (SnocList Name)
100+
-- Get the source location of the macro call site
101+
GetFC : Elab FC
100102
-- Check a group of top level declarations
101103
Declare : List Decl -> Elab ()
102104

@@ -203,6 +205,9 @@ interface Monad m => Elaboration m where
203205
||| Get the name of the current and outer functions, if we are in a function
204206
getCurrentFn : m (SnocList Name)
205207

208+
||| Get the source location of the macro call site.
209+
getFC : m FC
210+
206211
||| Make some top level declarations
207212
declare : List Decl -> m ()
208213

@@ -253,6 +258,7 @@ Elaboration Elab where
253258
getCons = GetCons
254259
getReferredFns = GetReferredFns
255260
getCurrentFn = GetCurrentFn
261+
getFC = GetFC
256262
declare = Declare
257263
readFile = ReadFile
258264
writeFile = WriteFile
@@ -281,6 +287,7 @@ Elaboration m => MonadTrans t => Monad (t m) => Elaboration (t m) where
281287
getCons = lift . getCons
282288
getReferredFns = lift . getReferredFns
283289
getCurrentFn = lift getCurrentFn
290+
getFC = lift getFC
284291
declare = lift . declare
285292
readFile = lift .: readFile
286293
writeFile d = lift .: writeFile d

src/TTImp/Elab/RunElab.idr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,8 @@ elabScript rig fc nest env script@(NDCon nfc nm t ar args) exp
317317
elabCon defs "GetCurrentFn" []
318318
= do defs <- get Ctxt
319319
scriptRet defs.defsStack
320+
elabCon defs "GetFC" []
321+
= scriptRet fc
320322
elabCon defs "Declare" [d]
321323
= do d' <- evalClosure defs d
322324
decls <- reify defs d'
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import Language.Reflection
2+
3+
%language ElabReflection
4+
5+
%macro
6+
here : Elab String
7+
here = do
8+
MkFC (PhysicalIdrSrc _) (l, c) _ <- getFC
9+
| _ => fail "expected a physical source location"
10+
pure $ "line " ++ show l ++ ", col " ++ show c
11+
12+
main : IO ()
13+
main = putStrLn here
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
line 12, col 16
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
. ../../../testutils.sh
2+
3+
run GetFC.idr

0 commit comments

Comments
 (0)