@@ -48,14 +48,14 @@ trait MacroCommons { bundle =>
4848 final lazy val MapSym = typeOf[scala.collection.immutable.Map [_, _]].typeSymbol
4949 final lazy val FutureSym = typeOf[scala.concurrent.Future [_]].typeSymbol
5050 final lazy val OptionClass = definitions.OptionClass
51- final lazy val AnnotationAggregateType = getType (tq " $CommonsPkg.annotation.AnnotationAggregate " )
52- final lazy val DefaultsToNameAT = getType (tq " $CommonsPkg.annotation.defaultsToName " )
53- final lazy val InferAT : Type = getType (tq " $CommonsPkg.meta.infer " )
54- final lazy val NotInheritedFromSealedTypes = getType (tq " $CommonsPkg.annotation.NotInheritedFromSealedTypes " )
51+ final lazy val AnnotationAggregateType = staticType (tq " $CommonsPkg.annotation.AnnotationAggregate " )
52+ final lazy val DefaultsToNameAT = staticType (tq " $CommonsPkg.annotation.defaultsToName " )
53+ final lazy val InferAT : Type = staticType (tq " $CommonsPkg.meta.infer " )
54+ final lazy val NotInheritedFromSealedTypes = staticType (tq " $CommonsPkg.annotation.NotInheritedFromSealedTypes " )
5555 final lazy val SeqCompanionSym = typeOf[scala.collection.Seq .type ].termSymbol
56- final lazy val PositionedAT = getType (tq " $CommonsPkg.annotation.positioned " )
57- final lazy val ImplicitNotFoundAT = getType (tq " $ScalaPkg.annotation.implicitNotFound " )
58- final lazy val ImplicitNotFoundSym = getType (tq " $MiscPkg.ImplicitNotFound[_] " ).typeSymbol
56+ final lazy val PositionedAT = staticType (tq " $CommonsPkg.annotation.positioned " )
57+ final lazy val ImplicitNotFoundAT = staticType (tq " $ScalaPkg.annotation.implicitNotFound " )
58+ final lazy val ImplicitNotFoundSym = staticType (tq " $MiscPkg.ImplicitNotFound[_] " ).typeSymbol
5959
6060 final lazy val NothingTpe : Type = typeOf[Nothing ]
6161 final lazy val StringPFTpe : Type = typeOf[PartialFunction [String , Any ]]
@@ -72,12 +72,12 @@ trait MacroCommons { bundle =>
7272 definitions.ScalaPackageClass .toType.member(TermName (" scalajs" )) != NoSymbol
7373
7474 final lazy val ownerChain = {
75- val sym = c. typecheck(q " val ${c.freshName(TermName (" " ))} = null " ).symbol
75+ val sym = typecheck(q " val ${c.freshName(TermName (" " ))} = null " ).symbol
7676 Iterator .iterate(sym)(_.owner).takeWhile(_ != NoSymbol ).drop(1 ).toList
7777 }
7878
7979 final lazy val enclosingClasses = {
80- val enclosingSym = c. typecheck(q " this " , silent = true ) match {
80+ val enclosingSym = typecheck(q " this " , silent = true ) match {
8181 case EmptyTree => Iterator .iterate(c.internal.enclosingOwner)(_.owner).find(_.isModuleClass).get
8282 case tree => tree.tpe.typeSymbol
8383 }
@@ -89,16 +89,51 @@ trait MacroCommons { bundle =>
8989 case _ => false
9090 }
9191
92+ final val statsEnabled : Boolean =
93+ c.settings.contains(" statsEnabled" )
94+
9295 def debug (msg : => String ): Unit =
9396 if (debugEnabled) {
9497 error(msg)
9598 }
9699
97- def showOnDebug (tree : Tree ): Tree = {
98- debug(show(tree))
99- tree
100+ def instrument (tree : => Tree ): Tree = measure(c.macroApplication.symbol.fullName) {
101+ val result = tree
102+ debug(show(result))
103+ result
100104 }
101105
106+ private [this ] var measureStack = List .empty[String ]
107+
108+ def measure [T ](what : String )(expr : => T ): T =
109+ if (! statsEnabled || measureStack.contains(what)) expr else {
110+ measureStack ::= what
111+ val start = System .nanoTime()
112+ try expr finally {
113+ echo(s " $what ${(System .nanoTime() - start) / 1000 }" )
114+ measureStack = measureStack.tail
115+ }
116+ }
117+
118+ def inferImplicitValue (
119+ pt : Type ,
120+ silent : Boolean = true ,
121+ withMacrosDisabled : Boolean = false ,
122+ pos : Position = c.enclosingPosition
123+ ): Tree = {
124+ debug(s " macro implicit search for $pt" )
125+ measure(" implicit" )(c.inferImplicitValue(pt, silent, withMacrosDisabled, pos))
126+ }
127+
128+ def typecheck (
129+ tree : Tree ,
130+ mode : c.TypecheckMode = c.TERMmode ,
131+ pt : Type = WildcardType ,
132+ silent : Boolean = false ,
133+ withImplicitViewsDisabled : Boolean = false ,
134+ withMacrosDisabled : Boolean = false
135+ ): Tree = measure(" typecheck" )(c.typecheck(tree, mode, pt, silent, withImplicitViewsDisabled, withMacrosDisabled))
136+
102137 def containsInaccessibleThises (tree : Tree ): Boolean = tree.exists {
103138 case t@ This (_) if ! t.symbol.isPackageClass && ! enclosingClasses.contains(t.symbol) => true
104139 case _ => false
@@ -232,7 +267,8 @@ trait MacroCommons { bundle =>
232267 if (withSupers) withSuperSymbols(s) else Iterator (s)
233268
234269 def allAnnotations (s : Symbol , tpeFilter : Type ,
235- seenFrom : Type = NoType , withInherited : Boolean = true , fallback : List [Tree ] = Nil ): List [Annot ] = {
270+ seenFrom : Type = NoType , withInherited : Boolean = true , fallback : List [Tree ] = Nil
271+ ): List [Annot ] = measure(" annotations" ) {
236272
237273 val initSym = orConstructorParam(s)
238274 def inherited (annot : Annotation , superSym : Symbol ): Boolean =
@@ -247,8 +283,8 @@ trait MacroCommons { bundle =>
247283 }
248284
249285 def findAnnotation (s : Symbol , tpe : Type ,
250- seenFrom : Type = NoType , withInherited : Boolean = true , fallback : List [Tree ] = Nil ) : Option [ Annot ] = {
251-
286+ seenFrom : Type = NoType , withInherited : Boolean = true , fallback : List [Tree ] = Nil
287+ ) : Option [ Annot ] = measure( " annotations " ) {
252288 val initSym = orConstructorParam(s)
253289 def find (annots : List [Annot ]): Option [Annot ] = annots match {
254290 case head :: tail =>
@@ -283,7 +319,7 @@ trait MacroCommons { bundle =>
283319 ownerChain.filter(_.isConstructor).map(_.owner.asClass.module).find(_ != NoSymbol ).getOrElse(NoSymbol )
284320
285321 lazy val companionReplacement : Symbol =
286- c. typecheck(q " $companionReplacementName" , silent = true ) match {
322+ typecheck(q " $companionReplacementName" , silent = true ) match {
287323 case EmptyTree => NoSymbol
288324 case _ => enclosingConstructorCompanion
289325 }
@@ -371,7 +407,7 @@ trait MacroCommons { bundle =>
371407
372408 def tryInferCachedImplicit (tpe : Type ): Option [TermName ] = {
373409 def compute : Option [TermName ] =
374- Option (c. inferImplicitValue(tpe)).filter(_ != EmptyTree ).map { found =>
410+ Option (inferImplicitValue(tpe)).filter(_ != EmptyTree ).map { found =>
375411 def newCachedImplicit (): TermName = {
376412 val name = c.freshName(TermName (" cachedImplicit" ))
377413 inferredImplicitTypes(name) = found.tpe
@@ -456,7 +492,7 @@ trait MacroCommons { bundle =>
456492 }
457493
458494 def implicitNotFoundMsg (tpe : Type ): String =
459- implicitNotFoundMsg(Nil , tpe, c. inferImplicitValue(getType(tq " $ImplicitNotFoundCls[ $tpe] " )))
495+ implicitNotFoundMsg(Nil , tpe, inferImplicitValue(getType(tq " $ImplicitNotFoundCls[ $tpe] " )))
460496
461497 class treeOps [T <: Tree ](t : T ) {
462498 def debug : T = {
@@ -512,7 +548,7 @@ trait MacroCommons { bundle =>
512548 def warning (msg : String ): Unit =
513549 c.warning(c.enclosingPosition, msg)
514550
515- def ensure (condition : Boolean , msg : String ): Unit =
551+ def ensure (condition : Boolean , msg : => String ): Unit =
516552 if (! condition) {
517553 abort(msg)
518554 }
@@ -583,8 +619,11 @@ trait MacroCommons { bundle =>
583619 }
584620 }
585621
622+ def staticType (tpeTree : Tree ): Type =
623+ getType(tpeTree)
624+
586625 def getType (typeTree : Tree ): Type =
587- c. typecheck(typeTree, c.TYPEmode ).tpe
626+ measure( " typecheck.getType " )( typecheck(typeTree, c.TYPEmode ).tpe)
588627
589628 def pathTo (sym : Symbol ): Tree =
590629 if (enclosingClasses.contains(sym)) This (sym)
@@ -937,7 +976,7 @@ trait MacroCommons { bundle =>
937976 def applyUnapplyFor (tpe : Type ): Option [ApplyUnapply ] =
938977 typedCompanionOf(tpe).flatMap(comp => applyUnapplyFor(tpe, comp))
939978
940- def applyUnapplyFor (tpe : Type , typedCompanion : Tree ): Option [ApplyUnapply ] = {
979+ def applyUnapplyFor (tpe : Type , typedCompanion : Tree ): Option [ApplyUnapply ] = measure( " applyUnapplyFor " ) {
941980 val dtpe = tpe.dealias
942981 val ts = dtpe.typeSymbol.asType
943982 val caseClass = ts.isClass && ts.asClass.isCaseClass
@@ -990,7 +1029,7 @@ trait MacroCommons { bundle =>
9901029 }
9911030 }
9921031
993- def singleValueFor (tpe : Type ): Option [Tree ] = tpe match {
1032+ def singleValueFor (tpe : Type ): Option [Tree ] = measure( " singleValueFor " )( tpe match {
9941033 case ThisType (sym) if enclosingClasses.contains(sym) =>
9951034 Some (This (sym))
9961035 case ThisType (sym) if sym.isModuleClass =>
@@ -1007,15 +1046,15 @@ trait MacroCommons { bundle =>
10071046 singleValueFor(pre).map(prefix => Select (prefix, sym.asClass.module))
10081047 case _ =>
10091048 None
1010- }
1049+ })
10111050
10121051 def typedCompanionOf (tpe : Type ): Option [Tree ] = {
10131052 val result = tpe match {
10141053 case TypeRef (pre, sym, _) if sym.companion != NoSymbol =>
10151054 singleValueFor(pre).map(Select (_, sym.companion)) orElse singleValueFor(tpe.companion)
10161055 case _ => singleValueFor(tpe.companion)
10171056 }
1018- result.map(c. typecheck(_))
1057+ result.map(typecheck(_))
10191058 }
10201059
10211060 def typeOfTypeSymbol (sym : TypeSymbol ): Type = sym.toType match {
@@ -1056,7 +1095,7 @@ trait MacroCommons { bundle =>
10561095 s " it resides in separate file than macro invocation and has no @positioned annotation " )
10571096 })
10581097
1059- def knownSubtypes (tpe : Type , ordered : Boolean = false ): Option [List [Type ]] = {
1098+ def knownSubtypes (tpe : Type , ordered : Boolean = false ): Option [List [Type ]] = measure( " knownSubtypes " ) {
10601099 val dtpe = tpe.dealias
10611100 val (tpeSym, refined) = dtpe match {
10621101 case RefinedType (List (single), scope) =>
@@ -1093,7 +1132,7 @@ trait MacroCommons { bundle =>
10931132 val methodName = c.freshName(TermName (" m" ))
10941133 val typeDefs = typeParams.map(typeSymbolToTypeDef(_, forMethod = true ))
10951134
1096- val tree = c. typecheck(
1135+ val tree = typecheck(
10971136 q """
10981137 def $methodName[.. $typeDefs](f: ${treeForType(undetTpe)} => $UnitCls): $UnitCls = ()
10991138 $methodName((_: $detTpe) => ())
0 commit comments