@@ -482,11 +482,11 @@ class MiscMacros(ctx: blackbox.Context) extends AbstractMacroCommons(ctx) {
482482 """
483483 }
484484
485- def assertLocal (tpe : Type ): Type = {
486- if (tpe.typeSymbol .pos.source != c.enclosingPosition.source) {
487- abort(s " Macro inspection of $tpe can only be done in the same source file where that type is defined " )
485+ def assertLocal (sym : Symbol ): Symbol = {
486+ if (sym .pos.source != c.enclosingPosition.source) {
487+ abort(s " Macro inspection of $sym can only be done in the same source file where it is defined " )
488488 }
489- tpe
489+ sym
490490 }
491491
492492 def safeAnnotTree (annot : Annot ): Tree = {
@@ -496,28 +496,77 @@ class MiscMacros(ctx: blackbox.Context) extends AbstractMacroCommons(ctx) {
496496 c.untypecheck(annot.tree)
497497 }
498498
499+ def classSymbol (sym : Symbol ): ClassSymbol = {
500+ if (sym.isClass) sym.asClass
501+ else abort(s " $sym is not a class " )
502+ }
503+
499504 def annotationOf [A : WeakTypeTag , T : WeakTypeTag ]: Tree = instrument {
500- val atpe = weakTypeOf[A ].dealias
501- val tpe = assertLocal(weakTypeOf[T ].dealias)
502- val annot = findAnnotation(tpe.typeSymbol, atpe)
503- .getOrElse(abort(s " No annotation of type $atpe found on $tpe" ))
505+ val atpe = weakTypeOf[A ]
506+ val tpe = weakTypeOf[T ]
507+ val sym = assertLocal(classSymbol(tpe.dealias.typeSymbol))
508+ val annot = findAnnotation(sym, atpe)
509+ .getOrElse(abort(s " No annotation of type $atpe found on $sym" ))
504510 q " $MiscPkg.AnnotationOf( ${safeAnnotTree(annot)}) "
505511 }
506512
507513 def optAnnotationOf [A : WeakTypeTag , T : WeakTypeTag ]: Tree = instrument {
508- val atpe = weakTypeOf[A ].dealias
509- val tpe = assertLocal(weakTypeOf[T ].dealias)
510- val annotTree = findAnnotation(tpe.typeSymbol, atpe)
514+ val atpe = weakTypeOf[A ]
515+ val tpe = weakTypeOf[T ]
516+ val sym = assertLocal(classSymbol(tpe.dealias.typeSymbol))
517+ val annotTree = findAnnotation(sym, atpe)
511518 .fold[Tree ](q " $MiscPkg.Opt.Empty " )(a => q " $MiscPkg.Opt( ${safeAnnotTree(a)}) " )
512519 q " $MiscPkg.OptAnnotationOf( $annotTree) "
513520 }
514521
515522 def annotationsOf [A : WeakTypeTag , T : WeakTypeTag ]: Tree = instrument {
516- val atpe = weakTypeOf[A ].dealias
517- val tpe = assertLocal(weakTypeOf[T ].dealias)
518- val annots = allAnnotations(tpe.typeSymbol, atpe).map(safeAnnotTree)
523+ val atpe = weakTypeOf[A ]
524+ val tpe = weakTypeOf[T ]
525+ val sym = assertLocal(classSymbol(tpe.dealias.typeSymbol))
526+ val annots = allAnnotations(sym, atpe).map(safeAnnotTree)
519527 q " $MiscPkg.AnnotationsOf( $ListObj(.. $annots)) "
520528 }
529+
530+ def hasAnnotation [A : WeakTypeTag , T : WeakTypeTag ]: Tree = instrument {
531+ val atpe = weakTypeOf[A ]
532+ val tpe = weakTypeOf[T ]
533+ val sym = assertLocal(classSymbol(tpe.dealias.typeSymbol))
534+ if (findAnnotation(sym, atpe).nonEmpty)
535+ q " $MiscPkg.HasAnnotation.create[ $atpe, $tpe] "
536+ else
537+ abort(s " No annotation of type $atpe found on $sym" )
538+ }
539+
540+ def classBeingConstructed : Symbol = {
541+ val ownerConstr = c.internal.enclosingOwner
542+ if (! ownerConstr.isConstructor) {
543+ abort(s " ${c.macroApplication.symbol} can only be used as super constructor argument " )
544+ }
545+ classSymbol(ownerConstr.owner)
546+ }
547+
548+ def selfAnnotation [A : WeakTypeTag ]: Tree = instrument {
549+ val atpe = weakTypeOf[A ]
550+ val sym = classBeingConstructed
551+ val annot = findAnnotation(sym, atpe)
552+ .getOrElse(abort(s " No annotation of type $atpe found on $sym" ))
553+ q " $MiscPkg.SelfAnnotation( ${safeAnnotTree(annot)}) "
554+ }
555+
556+ def selfOptAnnotation [A : WeakTypeTag ]: Tree = instrument {
557+ val atpe = weakTypeOf[A ]
558+ val sym = classBeingConstructed
559+ val annotTree = findAnnotation(sym, atpe)
560+ .fold[Tree ](q " $MiscPkg.Opt.Empty " )(a => q " $MiscPkg.Opt( ${safeAnnotTree(a)}) " )
561+ q " $MiscPkg.SelfOptAnnotation( $annotTree) "
562+ }
563+
564+ def selfAnnotations [A : WeakTypeTag ]: Tree = instrument {
565+ val atpe = weakTypeOf[A ]
566+ val sym = classBeingConstructed
567+ val annots = allAnnotations(sym, atpe).map(safeAnnotTree)
568+ q " $MiscPkg.SelfAnnotations( $ListObj(.. $annots)) "
569+ }
521570}
522571
523572class WhiteMiscMacros (ctx : whitebox.Context ) extends AbstractMacroCommons (ctx) {
0 commit comments