@@ -8,10 +8,10 @@ private object MacroCache {
8
8
val cache = mutable.Map .empty[Any , Any ]
9
9
def get (key : Any ) : Option [Any ] = cache.get(key)
10
10
def add [V <: Any ](key : Any , value : V ) : V = {cache += (key -> value); value}
11
- private var opInterceptValue : Option [ Any ] = None
12
- def setOpInterceptValue ( value : Any ) : Unit = opInterceptValue = Some (value)
13
- def getOpInterceptValue : Any = opInterceptValue
14
- def clearOpInterceptValue () : Unit = opInterceptValue = None
11
+ var errorCache : String = " "
12
+ def clearErrorCache ( ) : Unit = errorCache = " "
13
+ def setErrorCache ( msg : String ) : Unit = errorCache = msg
14
+ def getErrorMessage : String = errorCache
15
15
}
16
16
trait GeneralMacros {
17
17
val c : whitebox.Context
@@ -332,19 +332,6 @@ trait GeneralMacros {
332
332
VerboseTraversal (s " ${GREEN }${BOLD }caching ${RESET } $k -> $value" )
333
333
value
334
334
}
335
- def setOpInterceptCalc (calc : Calc ) : Unit = MacroCache .setOpInterceptValue(Left (calc))
336
- def setOpInterceptError (msg : String ) : Unit = MacroCache .setOpInterceptValue(Right (msg))
337
- def clearOpInterceptCalc () : Unit = MacroCache .clearOpInterceptValue()
338
- def getOpInterceptCalc : Option [Either [Calc , String ]] = {
339
- MacroCache .getOpInterceptValue.asInstanceOf [Option [Either [Calc , String ]]] match {
340
- case Some (Left (v)) => Some (Left (v match {
341
- case lit : CalcLit => CalcLit (lit.value) // reconstruct internal literal tree
342
- case nlit : CalcNLit => CalcNLit (nlit.primitive, deepCopyTree(nlit.tree))
343
- case c => c
344
- }))
345
- case v => v
346
- }
347
- }
348
335
}
349
336
// //////////////////////////////////////////////////////////////////
350
337
@@ -566,7 +553,7 @@ trait GeneralMacros {
566
553
}
567
554
568
555
case _ => // regular cases
569
- opCalc(Some (tp), funcType, aValue, bValue, cValue) match {
556
+ opCalc(funcType, aValue, bValue, cValue) match {
570
557
case (res : CalcVal ) => Some (res)
571
558
case u @ CalcUnknown (_,Some (_), _) => Some (u) // Accept unknown values with a tree
572
559
case oi @ CalcUnknown (_,_, true ) => Some (oi) // Accept unknown op interception
@@ -675,7 +662,7 @@ trait GeneralMacros {
675
662
def abort (msg : String , annotatedSym : Option [TypeSymbol ] = defaultAnnotatedSym, position : Position = c.enclosingPosition): Nothing = {
676
663
VerboseTraversal (s " !!!!!!aborted with: $msg at $annotatedSym, $defaultAnnotatedSym" )
677
664
if (annotatedSym.isDefined) setAnnotation(msg, annotatedSym.get)
678
- CalcCache .setOpInterceptError (msg) // propagating the error in case this is an inner implicit call for OpIntercept
665
+ MacroCache .setErrorCache (msg) // propagating the error in case this is an inner implicit call for OpIntercept
679
666
c.abort(position, msg)
680
667
}
681
668
@@ -907,56 +894,38 @@ trait GeneralMacros {
907
894
}
908
895
// /////////////////////////////////////////////////////////////////////////////////////////
909
896
910
- // /////////////////////////////////////////////////////////////////////////////////////////
911
- // OpInterept Result Caching
912
- // /////////////////////////////////////////////////////////////////////////////////////////
913
- def cacheOpInterceptResult [Out ](implicit ev0 : c.WeakTypeTag [Out ]) : Tree = {
914
- val outTpe = weakTypeOf[Out ]
915
- val outCalc = TypeCalc (outTpe)
916
- CalcCache .setOpInterceptCalc(outCalc)
917
- q " new _root_.singleton.ops.OpIntercept.CacheResult[ $outTpe]{} "
918
- }
919
- // /////////////////////////////////////////////////////////////////////////////////////////
920
-
921
897
// /////////////////////////////////////////////////////////////////////////////////////////
922
898
// Three operands (Generic)
923
899
// /////////////////////////////////////////////////////////////////////////////////////////
924
900
def materializeOpGen [F ](implicit ev0 : c.WeakTypeTag [F ]): MaterializeOpAuxGen =
925
901
new MaterializeOpAuxGen (weakTypeOf[F ])
926
902
927
- def opCalc (opTpe : Option [ Type ], funcType : TypeSymbol , aCalc : => Calc , bCalc : => Calc , cCalc : => Calc ) : Calc = {
903
+ def opCalc (funcType : TypeSymbol , aCalc : => Calc , bCalc : => Calc , cCalc : => Calc ) : Calc = {
928
904
lazy val a = aCalc
929
905
lazy val b = bCalc
930
906
lazy val cArg = cCalc
931
907
def unsupported () : Calc = {
932
- val cachedTpe = opTpe.get match {
933
- case TypeRef (pre, sym, args) => c.internal.typeRef(pre, sym, List (funcType.toType, a.tpe, b.tpe, cArg.tpe))
934
- }
935
- // calling OpIntercept for the operation should cache the expected result if executed correctly
936
- CalcCache .clearOpInterceptCalc()
937
- val implicitlyTree = q " implicitly[_root_.singleton.ops.OpIntercept[ $cachedTpe]] "
908
+ val opMacroTpe = typeOf[OpMacro [_,_,_,_]].typeConstructor
909
+ val opTpe = appliedType(opMacroTpe, List (funcType.toType, a.tpe, b.tpe, cArg.tpe))
910
+ val interceptTpe = typeOf[singleton.ops.OpIntercept [_]].typeConstructor
911
+ MacroCache .clearErrorCache()
938
912
try {
939
- c.typecheck(implicitlyTree, silent = false )
940
- val cachedCalc = CalcCache .getOpInterceptCalc match {
941
- case Some (calc) => calc
942
- case None => abort(" Missing a result cache for OpIntercept. Make sure you set `OpIntercept.CacheResult`" )
943
- }
944
- CalcCache .clearOpInterceptCalc()
945
- cachedCalc match {
946
- case Left (t : CalcUnknown ) =>
947
- t.copy(opIntercept = true ) // the unknown result must be marked properly so we allow it later
948
- case Left (t) => t
949
- case Right (msg) => abort(msg)
913
+ val itree = c.inferImplicitValue (
914
+ appliedType(interceptTpe, List (opTpe)),
915
+ silent = false
916
+ )
917
+ TypeCalc (itree.tpe.decls.head.info) match {
918
+ case t : CalcUnknown => t.copy(opIntercept = true ) // the unknown result must be marked properly so we allow it later
919
+ case t => t
950
920
}
951
921
} catch {
952
- case TypecheckException (pos, msg) =>
953
- CalcCache .getOpInterceptCalc match {
954
- case Some (Right (msg)) => abort(msg)
955
- case _ =>
956
- (a, b) match {
957
- case (_ : CalcVal , _ : CalcVal ) => abort(s " Unsupported operation $cachedTpe" )
958
- case _ => CalcUnknown (funcType.toType, None , opIntercept = false )
959
- }
922
+ case TypecheckException (_, _) =>
923
+ MacroCache .getErrorMessage match {
924
+ case m if m.nonEmpty => abort(m)
925
+ case _ => (a, b) match {
926
+ case (_ : CalcVal , _ : CalcVal ) => abort(s " Unsupported operation $opTpe" )
927
+ case _ => CalcUnknown (funcType.toType, None , opIntercept = false )
928
+ }
960
929
}
961
930
}
962
931
}
@@ -1557,7 +1526,7 @@ trait GeneralMacros {
1557
1526
}
1558
1527
}
1559
1528
1560
- val reqCalc = opCalc(None , funcTypes.Require , condCalc, msgCalc, CalcUnknown (typeOf[NoSym ], None , opIntercept = false ))
1529
+ val reqCalc = opCalc(funcTypes.Require , condCalc, msgCalc, CalcUnknown (typeOf[NoSym ], None , opIntercept = false ))
1561
1530
1562
1531
q """
1563
1532
(new $chkSym[ $condTpe, $msgTpe, $chkArgTpe]( $outTree.asInstanceOf[ $outTpe]))
@@ -1623,7 +1592,7 @@ trait GeneralMacros {
1623
1592
}
1624
1593
}
1625
1594
1626
- val reqCalc = opCalc(None , funcTypes.Require , condCalc, msgCalc, CalcUnknown (typeOf[NoSym ], None , opIntercept = false ))
1595
+ val reqCalc = opCalc(funcTypes.Require , condCalc, msgCalc, CalcUnknown (typeOf[NoSym ], None , opIntercept = false ))
1627
1596
1628
1597
q """
1629
1598
(new $chkSym[ $condTpe, $msgTpe, $chkArgTpe, $paramFaceTpe, $paramTpe]( $outTree.asInstanceOf[ $outTpe]))
0 commit comments