@@ -445,7 +445,7 @@ convertBinder fileName = go
445445
446446convertDeclaration :: String -> Declaration a -> [AST. Declaration ]
447447convertDeclaration fileName decl = case decl of
448- DeclData _ (DataHead _ a vars) bd -> do
448+ DeclData _ (DataHead _ a vars) bd deriveClauses -> do
449449 let
450450 ctrs :: SourceToken -> DataCtor b -> [(SourceToken , DataCtor b )] -> [AST. DataConstructorDeclaration ]
451451 ctrs st (DataCtor _ name fields) tl
@@ -454,15 +454,17 @@ convertDeclaration fileName decl = case decl of
454454 [] -> []
455455 (st', ctor) : tl' -> ctrs st' ctor tl'
456456 )
457- pure $ AST. DataDeclaration ann Env. Data (nameValue a) (goTypeVar <$> vars) (maybe [] (\ (st, Separated hd tl) -> ctrs st hd tl) bd)
457+ AST. DataDeclaration ann Env. Data (nameValue a) (goTypeVar <$> vars) (maybe [] (\ (st, Separated hd tl) -> ctrs st hd tl) bd)
458+ : convertDeriveClauses fileName (nameValue a) deriveClauses
458459 DeclType _ (DataHead _ a vars) _ bd ->
459460 pure $ AST. TypeSynonymDeclaration ann
460461 (nameValue a)
461462 (goTypeVar <$> vars)
462463 (convertType fileName bd)
463- DeclNewtype _ (DataHead _ a vars) st x ys -> do
464+ DeclNewtype _ (DataHead _ a vars) st x ys deriveClauses -> do
464465 let ctrs = [AST. DataConstructorDeclaration (sourceAnnCommented fileName st (snd $ declRange decl)) (nameValue x) [(headDef (internalError " No constructor name" ) ctrFields, convertType fileName ys)]]
465- pure $ AST. DataDeclaration ann Env. Newtype (nameValue a) (goTypeVar <$> vars) ctrs
466+ AST. DataDeclaration ann Env. Newtype (nameValue a) (goTypeVar <$> vars) ctrs
467+ : convertDeriveClauses fileName (nameValue a) deriveClauses
466468 DeclClass _ (ClassHead _ sup name vars fdeps) bd -> do
467469 let
468470 goTyVar (TypeVarKinded (Wrapped _ (Labeled (_, a) _ _) _)) = nameValue a
@@ -553,25 +555,8 @@ convertDeclaration fileName decl = case decl of
553555
554556 mkPartialInstanceName :: Maybe (Name Ident , SourceToken ) -> QualifiedName (N. ProperName 'N.ClassName ) -> [Type a ] -> Either Text. Text N. Ident
555557 mkPartialInstanceName nameSep cls args =
556- maybe (Left genName ) (Right . ident . nameValue . fst ) nameSep
558+ maybe (Left (genInstanceName cls ( foldMap argName args)) ) (Right . ident . nameValue . fst ) nameSep
557559 where
558- -- truncate to 25 chars to reduce verbosity
559- -- of name and still keep it readable
560- -- name will be used to create a GenIdent
561- -- in desugaring process
562- genName :: Text. Text
563- genName = Text. take 25 (className <> typeArgs)
564-
565- className :: Text. Text
566- className
567- = foldMap (uncurry Text. cons . first toLower)
568- . Text. uncons
569- . N. runProperName
570- $ qualName cls
571-
572- typeArgs :: Text. Text
573- typeArgs = foldMap argName args
574-
575560 argName :: Type a -> Text. Text
576561 argName = \ case
577562 -- These are only useful to disambiguate between overlapping instances
@@ -619,6 +604,36 @@ convertDeclaration fileName decl = case decl of
619604 else
620605 (fst $ qualRange cls, snd $ typeRange $ last args)
621606
607+ convertDeriveClauses
608+ :: String
609+ -> N. ProperName 'N.TypeName
610+ -> [DeriveClause ]
611+ -> [AST. Declaration ]
612+ convertDeriveClauses fileName tyName = concatMap go
613+ where
614+ go (DeriveClause _ (Wrapped _ classes _)) = map convertClass (toList classes)
615+ convertClass (DeriveClass cls) =
616+ AST. TypeInstanceDeclaration clsAnn clsAnn chainId 0 (Left genName)
617+ []
618+ (qualified cls)
619+ [tyCon]
620+ AST. DerivedInstance
621+ where
622+ clsAnn = uncurry (sourceAnnCommented fileName) (qualRange cls)
623+ chainId = mkChainId fileName (Pos. spanStart (fst clsAnn))
624+ tyCon = T. TypeConstructor clsAnn (N. Qualified N. ByNullSourcePos tyName)
625+ genName = genInstanceName cls (N. runProperName tyName)
626+
627+ genInstanceName :: QualifiedName (N. ProperName 'N.ClassName ) -> Text. Text -> Text. Text
628+ genInstanceName cls typeArgs = Text. take 25 (className <> typeArgs)
629+ where
630+ className :: Text. Text
631+ className
632+ = foldMap (uncurry Text. cons . first toLower)
633+ . Text. uncons
634+ . N. runProperName
635+ $ qualName cls
636+
622637convertSignature :: String -> Labeled (Name Ident ) (Type a ) -> AST. Declaration
623638convertSignature fileName (Labeled a _ b) = do
624639 let
0 commit comments