@@ -298,6 +298,12 @@ data ContextEntry : Type where
298298 Coded : Binary -> ContextEntry
299299 Decoded : GlobalDef -> ContextEntry
300300
301+ data PossibleName : Type where
302+ Direct : Name -> Int -> PossibleName -- full name and resolved name id
303+ Alias : Name -> -- aliased name (from "import as ")
304+ Name -> Int -> -- real full name and resolved name , as above
305+ PossibleName
306+
301307-- All the GlobalDefs. We can only have one context , because name references
302308-- point at locations in here, and if we have more than one the indices won't
303309-- match up . So, this isn't polymorphic.
@@ -309,7 +315,7 @@ record Context where
309315 -- Map from full name to its position in the context
310316 resolvedAs : NameMap Int
311317 -- Map from strings to all the possible names in all namespaces
312- possibles : StringMap (List (Name, Int) )
318+ possibles : StringMap (List PossibleName )
313319 -- Reference to the actual content, indexed by Int
314320 content : Ref Arr (IOArray ContextEntry )
315321 -- Branching depth , in a backtracking elaborator . 0 is top level ; at lower
@@ -327,6 +333,7 @@ record Context where
327333 allPublic : Bool -- treat everything as public . This is only intended
328334 -- for checking partially evaluated definitions
329335 inlineOnly : Bool -- only return things with the 'alwaysReduce' flag
336+ hidden : NameMap () -- Never return these
330337
331338export
332339getContent : Context -> Ref Arr (IOArray ContextEntry )
@@ -348,21 +355,31 @@ initCtxtS : Int -> Core Context
348355initCtxtS s
349356 = do arr <- coreLift $ newArray s
350357 aref <- newRef Arr arr
351- pure (MkContext 0 0 empty empty aref 0 empty [["_PE"]] False False )
358+ pure (MkContext 0 0 empty empty aref 0 empty [["_PE"]] False False empty )
352359
353360export
354361initCtxt : Core Context
355362initCtxt = initCtxtS initSize
356363
357364addPossible : Name -> Int ->
358- StringMap (List (Name, Int)) -> StringMap (List (Name, Int) )
365+ StringMap (List PossibleName ) -> StringMap (List PossibleName )
359366addPossible n i ps
360367 = case userNameRoot n of
361368 Nothing => ps
362369 Just nr =>
363370 case lookup nr ps of
364- Nothing => insert nr [(n, i)] ps
365- Just nis => insert nr ((n, i) :: nis) ps
371+ Nothing => insert nr [Direct n i] ps
372+ Just nis => insert nr (Direct n i :: nis) ps
373+
374+ addAlias : Name -> Name -> Int ->
375+ StringMap (List PossibleName ) -> StringMap (List PossibleName )
376+ addAlias alias full i ps
377+ = case userNameRoot alias of
378+ Nothing => ps
379+ Just nr =>
380+ case lookup nr ps of
381+ Nothing => insert nr [Alias alias full i ] ps
382+ Just nis => insert nr (Alias alias full i :: nis) ps
366383
367384export
368385newEntry : Name -> Context -> Core (Int, Context)
@@ -390,6 +407,11 @@ getPosition n ctxt
390407 do pure (idx, ctxt)
391408 Nothing => newEntry n ctxt
392409
410+ newAlias : Name -> Name -> Context -> Core Context
411+ newAlias alias full ctxt
412+ = do (idx, ctxt) <- getPosition full ctxt
413+ pure $ record { possibles $= addAlias alias full idx } ctxt
414+
393415export
394416getNameID : Name -> Context -> Maybe Int
395417getNameID (Resolved idx ) ctxt = Just idx
@@ -498,27 +520,41 @@ lookupCtxtName n ctxt
498520 Just r =>
499521 do let Just ps = lookup r (possibles ctxt)
500522 | Nothing => pure []
501- ps' <- the (Core (List (Maybe (Name , Int , GlobalDef )))) $
502- traverse (\ (n, i) =>
503- do Just res <- lookupCtxtExact (Resolved i) ctxt
504- | _ => pure Nothing
505- pure (Just (n, i, res))) ps
506- getMatches ps'
523+ lookupPossibles [] ps
507524 where
508- matches : Name -> ( Name, Int, a) -> Bool
509- matches (NS ns _ ) (NS cns _ , _ , _ ) = ns `isPrefixOf` cns
525+ matches : Name -> Name -> Bool
526+ matches (NS ns _ ) (NS cns _ ) = ns `isPrefixOf` cns
510527 matches (NS _ _ ) _ = True -- no in library name, so root doesn't match
511528 matches _ _ = True -- no prefix, so root must match, so good
512529
513- getMatches : List (Maybe (Name, Int, GlobalDef)) ->
514- Core (List (Name, Int, GlobalDef))
515- getMatches [] = pure []
516- getMatches (Nothing :: rs) = getMatches rs
517- getMatches (Just r :: rs)
518- = if matches n r
519- then do rs' <- getMatches rs
520- pure (r :: rs')
521- else getMatches rs
530+ resn : (Name, Int, GlobalDef) -> Int
531+ resn (_ , i, _ ) = i
532+
533+ lookupPossibles : List (Name, Int, GlobalDef) -> -- accumulator
534+ List PossibleName ->
535+ Core (List (Name , Int , GlobalDef ))
536+ lookupPossibles acc [] = pure (reverse acc)
537+ lookupPossibles acc (Direct fulln i :: ps)
538+ = case lookup fulln (hidden ctxt) of
539+ Nothing =>
540+ do Just res <- lookupCtxtExact (Resolved i) ctxt
541+ | Nothing => lookupPossibles acc ps
542+ if matches n fulln && not (i `elem` map resn acc)
543+ then lookupPossibles ((fulln, i, res) :: acc) ps
544+ else lookupPossibles acc ps
545+ _ => lookupPossibles acc ps
546+ lookupPossibles acc (Alias asn fulln i :: ps)
547+ = case lookup fulln (hidden ctxt) of
548+ Nothing =>
549+ do Just res <- lookupCtxtExact (Resolved i) ctxt
550+ | Nothing => lookupPossibles acc ps
551+ if (matches n asn) && not (i `elem` map resn acc)
552+ then lookupPossibles ((fulln, i, res) :: acc) ps
553+ else lookupPossibles acc ps
554+ _ => lookupPossibles acc ps
555+
556+ hideName : Name -> Context -> Context
557+ hideName n ctxt = record { hidden $= insert n () } ctxt
522558
523559branchCtxt : Context -> Core Context
524560branchCtxt ctxt = pure (record { branchDepth $= S } ctxt)
@@ -918,6 +954,38 @@ clearCtxt
918954 resetElab : Options -> Options
919955 resetElab = record { elabDirectives = defaultElab }
920956
957+ -- Get the canonical name of something that might have been aliased via
958+ -- import as
959+ export
960+ canonicalName : {auto c : Ref Ctxt Defs} ->
961+ FC -> Name -> Core Name
962+ canonicalName fc n
963+ = do defs <- get Ctxt
964+ case ! (lookupCtxtName n (gamma defs)) of
965+ [] => throw (UndefinedName fc n)
966+ [(n, _ , _ )] => pure n
967+ ns => throw (AmbiguousName fc (map fst ns))
968+
969+ -- If the name is aliased, get the alias
970+ export
971+ aliasName : {auto c : Ref Ctxt Defs} ->
972+ Name -> Core Name
973+ aliasName fulln
974+ = do defs <- get Ctxt
975+ let Just r = userNameRoot fulln
976+ | Nothing => pure fulln
977+ let Just ps = lookup r (possibles (gamma defs))
978+ | Nothing => pure fulln
979+ findAlias ps
980+ where
981+ findAlias : List PossibleName -> Core Name
982+ findAlias [] = pure fulln
983+ findAlias (Alias as full i :: ps)
984+ = if full == fulln
985+ then pure as
986+ else findAlias ps
987+ findAlias (_ :: ps) = findAlias ps
988+
921989-- Beware: if your hashable thing contains (potentially resolved) names,
922990-- it'll be better to use addHashWithNames to make the hash independent
923991-- of the internal numbering of names.
@@ -992,6 +1060,16 @@ addContextEntry n def
9921060 put Ctxt (record { gamma = gam' } defs)
9931061 pure idx
9941062
1063+ export
1064+ addContextAlias : {auto c : Ref Ctxt Defs} ->
1065+ Name -> Name -> Core ()
1066+ addContextAlias alias full
1067+ = do defs <- get Ctxt
1068+ Nothing <- lookupCtxtExact alias (gamma defs)
1069+ | _ => pure () -- Don't add the alias if the name exists already
1070+ gam' <- newAlias alias full (gamma defs)
1071+ put Ctxt (record { gamma = gam' } defs)
1072+
9951073export
9961074addBuiltin : {arity : _} ->
9971075 {auto x : Ref Ctxt Defs} ->
@@ -1347,7 +1425,7 @@ hide fc n
13471425 [(nsn, _ )] <- lookupCtxtName n (gamma defs)
13481426 | [] => throw (UndefinedName fc n)
13491427 | res => throw (AmbiguousName fc (map fst res))
1350- setVisibility fc nsn Private
1428+ put Ctxt (record { gamma $= hideName nsn } defs)
13511429
13521430export
13531431getVisibility : {auto c : Ref Ctxt Defs} ->
0 commit comments