@@ -14,6 +14,17 @@ object Show extends Phase[CoreTransformed, CoreTransformed] {
1414 override val phaseName : String = " show"
1515
1616 private final val FUNCTION_NAME : String = " show"
17+ private final val FUNCTION_BUILTIN_NAME : String = " showBuiltin"
18+ private final val STRING_CONCAT_NAME : String = " infixPlusPlus"
19+
20+ /** Names that [[Deadcode ]] must preserve for the [[Show ]] pass to function correctly. */
21+ def requiredNames (core : ModuleDecl ): Set [Id ] = {
22+ val requiredExternNames = Set (FUNCTION_NAME , FUNCTION_BUILTIN_NAME , STRING_CONCAT_NAME )
23+
24+ core.externs.collect {
25+ case Extern .Def (id, _, _, _, _, _, _, _) if requiredExternNames.contains(id.name.name) => id
26+ }.toSet
27+ }
1728
1829 case class ShowContext (showNames : collection.mutable.Map [ValueType , Id ], showDefns : collection.mutable.Map [ValueType , Toplevel .Def ], tparamLookup : collection.mutable.Map [Id , ValueType ]) {
1930
@@ -270,7 +281,7 @@ object Show extends Phase[CoreTransformed, CoreTransformed] {
270281 }
271282
272283 def findExternShowDef (valueType : ValueType )(using dctx : DeclarationContext )(using Context ): Block .BlockVar =
273- findExternDef(" showBuiltin " , List (valueType))
284+ findExternDef(FUNCTION_BUILTIN_NAME , List (valueType))
274285
275286 def findExternDef (name : String , vts : List [ValueType ])(using dctx : DeclarationContext )(using Context ): Block .BlockVar =
276287 dctx.findExternDef(name, vts) match
@@ -334,7 +345,7 @@ object Show extends Phase[CoreTransformed, CoreTransformed] {
334345
335346 def constructorStmt (constr : Constructor )(using ctx : ShowContext , dctx : DeclarationContext )(using Context ): Stmt = constr match
336347 case Constructor (id, tparams, fields) =>
337- val infixConcatBlockVar : Block .BlockVar = findExternDef(" infixPlusPlus " , List (TString , TString ))
348+ val infixConcatBlockVar : Block .BlockVar = findExternDef(STRING_CONCAT_NAME , List (TString , TString ))
338349 val pureFields = fields map fieldPure
339350 val concatenated = PureApp (infixConcatBlockVar, List .empty, List (Literal (id.name.name ++ " (" , TString ), concatPure(pureFields)))
340351
@@ -351,7 +362,7 @@ object Show extends Phase[CoreTransformed, CoreTransformed] {
351362 // =>
352363 // PureApp(concat, List(Literal("Just("), PureApp(concat, List(PureApp(show, x), PureApp(concat, List(Literal(", "), ...))))
353364 def concatPure (pures : List [(Id , Stmt .App )])(using ctx : ShowContext )(using Context , DeclarationContext ): Expr =
354- val infixConcatDef = findExternDef(" infixPlusPlus " , List (TString , TString ))
365+ val infixConcatDef = findExternDef(STRING_CONCAT_NAME , List (TString , TString ))
355366 pures match
356367 case (fieldId, _) :: next :: rest => PureApp (infixConcatDef, List .empty, List (Expr .ValueVar (fieldId, TString ), PureApp (infixConcatDef, List .empty, List (Literal (" , " , TString ), concatPure(next :: rest)))))
357368 case (fieldId, _) :: Nil => PureApp (infixConcatDef, List .empty, List (Expr .ValueVar (fieldId, TString ), Literal (" )" , TString )))
0 commit comments