| ... |
... |
@@ -166,14 +166,16 @@ import qualified Data.ByteString.Lazy as LB |
|
166
|
166
|
import Data.Dynamic ( fromDynamic, toDyn )
|
|
167
|
167
|
import qualified Data.IntMap as IntMap
|
|
168
|
168
|
import qualified Data.Map as Map
|
|
169
|
|
-import Data.Typeable ( typeOf, Typeable, TypeRep, typeRep )
|
|
170
|
|
-import Data.Data (Data)
|
|
|
169
|
+import Data.Typeable ( typeOf, Typeable, TypeRep, typeRep, cast )
|
|
|
170
|
+import Data.Data (Data, gmapQ)
|
|
171
|
171
|
import Data.Proxy ( Proxy (..) )
|
|
172
|
172
|
import Data.IORef
|
|
173
|
173
|
import GHC.Parser.HaddockLex (lexHsDoc)
|
|
174
|
174
|
import GHC.Parser (parseIdentifier)
|
|
175
|
175
|
import GHC.Rename.Doc (rnHsDoc)
|
|
176
|
176
|
|
|
|
177
|
+import Debug.Trace
|
|
|
178
|
+
|
|
177
|
179
|
{-
|
|
178
|
180
|
Note [Template Haskell state diagram]
|
|
179
|
181
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| ... |
... |
@@ -668,14 +670,12 @@ Example: |
|
668
|
670
|
-}
|
|
669
|
671
|
|
|
670
|
672
|
-- None of these functions add constraints to the LIE
|
|
671
|
|
-
|
|
672
|
|
-tcTypedBracket :: HsExpr GhcRn -> LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
|
|
673
|
|
-tcUntypedBracket :: HsExpr GhcRn -> HsQuote GhcRn -> [PendingRnSplice] -> ExpRhoType
|
|
674
|
|
- -> TcM (HsExpr GhcTc)
|
|
675
|
|
-tcTypedSplice :: HsTypedSpliceResult -> HsTypedSplice GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
|
|
676
|
|
-
|
|
677
|
|
-getUntypedSpliceBody :: HsUntypedSpliceResult (HsExpr GhcRn) -> TcM (HsExpr GhcRn)
|
|
678
|
|
-runAnnotation :: CoreAnnTarget -> LHsExpr GhcRn -> TcM Annotation
|
|
|
673
|
+--
|
|
|
674
|
+-- * tcTypedBracket
|
|
|
675
|
+-- * tcUntypedBracket
|
|
|
676
|
+-- * tcTypedSplice
|
|
|
677
|
+-- * getUntypedSpliceBody
|
|
|
678
|
+-- * runAnnotation
|
|
679
|
679
|
|
|
680
|
680
|
{-
|
|
681
|
681
|
************************************************************************
|
| ... |
... |
@@ -686,6 +686,7 @@ runAnnotation :: CoreAnnTarget -> LHsExpr GhcRn -> TcM Annotation |
|
686
|
686
|
-}
|
|
687
|
687
|
|
|
688
|
688
|
-- See Note [How brackets and nested splices are handled]
|
|
|
689
|
+tcTypedBracket :: HsExpr GhcRn -> LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
|
|
689
|
690
|
tcTypedBracket rn_expr expr res_ty
|
|
690
|
691
|
= addErrCtxt (TypedTHBracketCtxt expr) $
|
|
691
|
692
|
do { cur_lvl <- getThLevel
|
| ... |
... |
@@ -728,6 +729,7 @@ tcTypedBracket rn_expr expr res_ty |
|
728
|
729
|
meta_ty res_ty }
|
|
729
|
730
|
|
|
730
|
731
|
-- See Note [Typechecking Overloaded Quotes]
|
|
|
732
|
+tcUntypedBracket :: HsExpr GhcRn -> HsQuote GhcRn -> [PendingRnSplice] -> ExpRhoType -> TcM (HsExpr GhcTc)
|
|
731
|
733
|
tcUntypedBracket rn_expr brack ps res_ty
|
|
732
|
734
|
= do { traceTc "tc_bracket untyped" (ppr brack $$ ppr ps)
|
|
733
|
735
|
|
| ... |
... |
@@ -929,6 +931,7 @@ tcCodeTy m_ty exp_ty |
|
929
|
931
|
-- getUntypedSpliceBody: the renamer has expanded the splice.
|
|
930
|
932
|
-- Just run the finalizers that it produced, and return
|
|
931
|
933
|
-- the renamed expression
|
|
|
934
|
+getUntypedSpliceBody :: HsUntypedSpliceResult (HsExpr GhcRn) -> TcM (HsExpr GhcRn)
|
|
932
|
935
|
getUntypedSpliceBody (HsUntypedSpliceTop { utsplice_result_finalizers = mod_finalizers
|
|
933
|
936
|
, utsplice_result = rn_expr })
|
|
934
|
937
|
= do { addModFinalizersWithLclEnv mod_finalizers
|
| ... |
... |
@@ -936,6 +939,7 @@ getUntypedSpliceBody (HsUntypedSpliceTop { utsplice_result_finalizers = mod_fina |
|
936
|
939
|
getUntypedSpliceBody (HsUntypedSpliceNested {})
|
|
937
|
940
|
= panic "tcTopUntypedSplice: invalid nested splice"
|
|
938
|
941
|
|
|
|
942
|
+tcTypedSplice :: HsTypedSpliceResult -> HsTypedSplice GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
|
|
939
|
943
|
tcTypedSplice HsTypedSpliceTop ctxt@(HsTypedSpliceExpr _ expr) res_ty
|
|
940
|
944
|
= addErrCtxt (TypedSpliceCtxt Nothing ctxt) $
|
|
941
|
945
|
setSrcSpan (getLocA expr) $
|
| ... |
... |
@@ -1082,6 +1086,7 @@ stubNestedSplice = warnPprTrace True "stubNestedSplice" empty $ |
|
1082
|
1086
|
************************************************************************
|
|
1083
|
1087
|
-}
|
|
1084
|
1088
|
|
|
|
1089
|
+runAnnotation :: CoreAnnTarget -> LHsExpr GhcRn -> TcM Annotation
|
|
1085
|
1090
|
runAnnotation target expr = do
|
|
1086
|
1091
|
-- Find the classes we want instances for in order to call toAnnotationWrapper
|
|
1087
|
1092
|
loc <- getSrcSpanM
|
| ... |
... |
@@ -1106,17 +1111,32 @@ runAnnotation target expr = do |
|
1106
|
1111
|
specialised_to_annotation_wrapper_expr expr'))
|
|
1107
|
1112
|
})
|
|
1108
|
1113
|
|
|
|
1114
|
+ traceM $ "runAnnotation: " ++ showPprUnsafe zonked_wrapped_expr'
|
|
|
1115
|
+
|
|
1109
|
1116
|
-- Run the appropriately wrapped expression to get the value of
|
|
1110
|
1117
|
-- the annotation and its dictionaries. The return value is of
|
|
1111
|
1118
|
-- type AnnotationWrapper by construction, so this conversion is
|
|
1112
|
1119
|
-- safe
|
|
1113
|
|
- serialized <- runMetaAW zonked_wrapped_expr'
|
|
|
1120
|
+ (names, serialized) <- runMetaAW zonked_wrapped_expr'
|
|
|
1121
|
+ -- See Note [Keeping things alive referenced by TH.Name from annotations]
|
|
|
1122
|
+ traceM $ "runAnnotation names: " ++ showPprUnsafe names
|
|
|
1123
|
+
|
|
|
1124
|
+ forM_ names $ \th_name -> do
|
|
|
1125
|
+ mb_name <- lookupThName_maybe th_name
|
|
|
1126
|
+ traceM $ "runAnnotation found: " ++ showPprUnsafe mb_name
|
|
|
1127
|
+ forM_ mb_name keepAlive -- TODO: do we need to do checks, like isExternalName here?
|
|
|
1128
|
+
|
|
1114
|
1129
|
return Annotation {
|
|
1115
|
1130
|
ann_target = target,
|
|
1116
|
1131
|
ann_value = serialized
|
|
1117
|
1132
|
}
|
|
1118
|
1133
|
|
|
1119
|
|
-convertAnnotationWrapper :: ForeignHValue -> TcM Serialized
|
|
|
1134
|
+extractTHNames :: Data a => a -> [TH.Name]
|
|
|
1135
|
+extractTHNames x = case cast x of
|
|
|
1136
|
+ Just n -> [n]
|
|
|
1137
|
+ Nothing -> concat (gmapQ extractTHNames x)
|
|
|
1138
|
+
|
|
|
1139
|
+convertAnnotationWrapper :: ForeignHValue -> TcM ([TH.Name], Serialized)
|
|
1120
|
1140
|
convertAnnotationWrapper fhv = do
|
|
1121
|
1141
|
interp <- tcGetInterp
|
|
1122
|
1142
|
case interpInstance interp of
|
| ... |
... |
@@ -1133,8 +1153,7 @@ convertAnnotationWrapper fhv = do |
|
1133
|
1153
|
-- annotation are exposed at this point. This is also why we are
|
|
1134
|
1154
|
-- doing all this stuff inside the context of runMeta: it has the
|
|
1135
|
1155
|
-- facilities to deal with user error in a meta-level expression
|
|
1136
|
|
- rnf serialized `seq` serialized
|
|
1137
|
|
-
|
|
|
1156
|
+ (extractTHNames value, rnf serialized `seq` serialized)
|
|
1138
|
1157
|
#endif
|
|
1139
|
1158
|
|
|
1140
|
1159
|
{-
|
| ... |
... |
@@ -1216,7 +1235,7 @@ defaultRunMeta (MetaAW r) |
|
1216
|
1235
|
|
|
1217
|
1236
|
----------------
|
|
1218
|
1237
|
runMetaAW :: LHsExpr GhcTc -- Of type AnnotationWrapper
|
|
1219
|
|
- -> TcM Serialized
|
|
|
1238
|
+ -> TcM ([TH.Name], Serialized)
|
|
1220
|
1239
|
runMetaAW = runMeta metaRequestAW
|
|
1221
|
1240
|
|
|
1222
|
1241
|
runMetaE :: LHsExpr GhcTc -- Of type (Q Exp)
|