-- | Case-sensitive names (varc-syntax LX13): Varc tells @point@ from @Point@, Verada's names -- below the front end do not (Ada folds case). A name declared in a Varc file whose spelling -- differs only in case from another name the file uses is given a key Ada can tell apart, -- @point_c0@: the name in lower case, then @_c@ and a mask of the letters that were upper -- case. Names other units see (@pub@ declarations) keep their spelling. module Verada.Varc.Names ( demangle , distinguishCase ) where import Verada.Varc.Syntax import Verada.Syntax.AST.Decl import Verada.Syntax.AST.Expr import Verada.Syntax.AST.Stmt import Verada.Syntax.AST.Type import Data.Bits (setBit, testBit) import Data.Char (isDigit, isUpper, toUpper) import Data.Data (Data, cast, gmapQ, gmapT) import Data.Map.Strict qualified as Map import Data.Maybe (fromMaybe, mapMaybe) import Data.Set qualified as Set import Data.Text (Text) import Data.Text qualified as Text import Text.Read (readMaybe) -- | A file with its names made distinct when folded. distinguishCase :: VarcFile -> VarcFile distinguishCase f | Map.null renamed = f | otherwise = rename f where used = Set.fromList (identifiers f) exported = Set.fromList (concatMap exportedNames f.units) byFold = Map.fromListWith Set.union [(Text.toCaseFold s, Set.singleton s) | s <- Set.toList used] renamed = Map.fromList [ (s, mangle s) | s <- Set.toList (Set.fromList (declared f)) , not (Set.member s exported) , maybe False ((> 1) . Set.size) (Map.lookup (Text.toCaseFold s) byFold) ] rename :: (Data a) => a -> a rename x = let x' = gmapT rename x in case cast x' of Just i -> fromMaybe x' (cast (renameIdentifier i)) Nothing -> x' renameIdentifier (i :: Identifier) = maybe i (\t -> Identifier {span = i.span, text = t}) (Map.lookup i.text renamed) -- | @point_c0@ for @point@, @poinT_c16@ for @poinT@. mangle :: Text -> Text mangle s = Text.toLower s <> "_c" <> Text.pack (show mask) where mask = foldl (\m (i, c) -> if isUpper c then setBit m i else m) (0 :: Integer) (zip [0 ..] (filter (/= '_') (Text.unpack s))) -- | The spelling a mangled name stands for (for messages), or the name itself. demangle :: Text -> Text demangle t = case Text.breakOnEnd "_c" t of (prefix, digits) | not (Text.null prefix) , not (Text.null digits) , Text.all isDigit digits , Text.toLower prefix == prefix , Just (mask :: Integer) <- readMaybe (Text.unpack digits) -> let base = Text.dropEnd 2 prefix in snd (Text.mapAccumL (\i c -> if c == '_' then (i, c) else (i + 1, if testBit mask i then toUpper c else c)) 0 base) _ -> t -- | Every identifier a tree mentions, as written. identifiers :: (Data a) => a -> [Text] identifiers x = case cast x of Just (i :: Identifier) -> [i.text] Nothing -> concat (gmapQ identifiers x) -- | The identifiers a tree declares, as written. declared :: (Data a) => a -> [Text] declared x = here <> concat (gmapQ declared x) where here = concat [ maybe [] (\(o :: ObjectDecl) -> map (.text) o.names) (cast x) , maybe [] (\(n :: NumberDecl) -> map (.text) n.names) (cast x) , maybe [] (\(t :: TypeDecl) -> t.name.text : [l.text | EnumIdentifier l <- literals t.definition]) (cast x) , maybe [] (\(s :: SubtypeDecl) -> [s.name.text]) (cast x) , maybe [] (\(s :: SubprogramSpec) -> nameText s.name) (cast x) , maybe [] (\(p :: Parameter) -> map (.text) p.names) (cast x) , maybe [] (\(c :: ComponentDecl) -> map (.text) c.names) (cast x) , maybe [] (\(d :: Discriminant) -> map (.text) d.names) (cast x) , maybe [] (\(it :: Iterator) -> [it.parameter.text]) (cast x) , maybe [] (\(r :: ObjectRenaming) -> [r.name.text]) (cast x) , maybe [] (\(e :: ExceptionDecl) -> map (.text) e.names) (cast x) , maybe [] (\(d :: DeclareItem) -> map (.text) d.names) (cast x) , maybe [] (\(r :: ExtendedReturn) -> [r.name.text]) (cast x) , maybe [] (\(l :: LoopStmt) -> maybe [] (pure . (.text)) l.label) (cast x) ] literals = \case Enumeration ls -> ls _ -> [] nameText e = case e.node of ENameRef i -> [i.text] _ -> [] -- | The names a unit's clients may use: what its @pub@ declarations declare, apart from -- what is inside subprogram bodies. exportedNames :: VarcUnit -> [Text] exportedNames u = case u.node of UnitPackage p -> fromPackage p UnitGenericPackage g -> fromPackage g.package UnitDeclaration d -> declared (headOf d.decl) UnitPlain _ -> [] where fromPackage p = nameOf p.name <> concat (mapMaybe fromItem p.items) fromItem i | i.visibility == Hidden = Nothing | otherwise = Just case i.node of ItemDecl d -> declared (headOf d) ItemStruct s -> declared s.declaration ItemPackage q -> fromPackage q ItemGenericPackage g -> fromPackage g.package _ -> [] nameOf e = case e.node of ENameRef i -> [i.text] _ -> [] -- | A declaration without its body (a subprogram's locals are not exported). headOf :: Decl -> Decl headOf d = case d.node of DSubprogramBody b -> Decl {span = d.span, node = DSubprogram SubprogramDecl {spec = b.spec, form = PlainSpec, aspects = []}} DGeneric g -> Decl {span = d.span, node = DGeneric GenericDecl {formals = g.formals, unit = headOf g.unit}} _ -> d