/
Macros.scala
1319 lines (1212 loc) · 62.2 KB
/
Macros.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package scala.tools.nsc
package typechecker
import symtab.Flags._
import scala.tools.nsc.util._
import scala.tools.nsc.util.ClassPath._
import scala.reflect.ReflectionUtils
import scala.collection.mutable.ListBuffer
import scala.compat.Platform.EOL
import scala.reflect.makro.runtime.{Context => MacroContext}
import scala.reflect.runtime.Mirror
import util.Statistics._
import scala.reflect.makro.util._
/**
* Code to deal with macros, namely with:
* * Compilation of macro definitions
* * Expansion of macro applications
*
* Say we have in a class C:
*
* def foo[T](xs: List[T]): T = macro fooBar
*
* Then fooBar needs to point to a static method of the following form:
*
* def fooBar[T: c.TypeTag]
* (c: scala.reflect.makro.Context)
* (xs: c.Expr[List[T]])
* : c.mirror.Tree = {
* ...
* }
*
* Then, if foo is called in qual.foo[Int](elems), where qual: D,
* the macro application is expanded to a reflective invocation of fooBar with parameters
*
* (simpleMacroContext{ type PrefixType = D; val prefix = qual })
* (Expr(elems))
* (TypeTag(Int))
*/
trait Macros extends Traces {
self: Analyzer =>
import global._
import definitions._
def globalSettings = global.settings
val globalMacroCache = collection.mutable.Map[Any, Any]()
val perRunMacroCache = perRunCaches.newMap[Symbol, collection.mutable.Map[Any, Any]]
/** A list of compatible macro implementation signatures.
*
* In the example above:
* (c: scala.reflect.makro.Context)(xs: c.Expr[List[T]]): c.Expr[T]
*
* @param macroDef The macro definition symbol
* @param tparams The type parameters of the macro definition
* @param vparamss The value parameters of the macro definition
* @param retTpe The return type of the macro definition
*/
private def macroImplSigs(macroDef: Symbol, tparams: List[TypeDef], vparamss: List[List[ValDef]], retTpe: Type): (List[List[List[Symbol]]], Type) = {
// had to move method's body to an object because of the recursive dependencies between sigma and param
object SigGenerator {
val hasThis = macroDef.owner.isClass
val ownerTpe = macroDef.owner match {
case owner if owner.isModuleClass => new UniqueThisType(macroDef.owner)
case owner if owner.isClass => macroDef.owner.tpe
case _ => NoType
}
val hasTparams = !tparams.isEmpty
def sigma(tpe: Type): Type = {
class SigmaTypeMap extends TypeMap {
def apply(tp: Type): Type = tp match {
case TypeRef(pre, sym, args) =>
val pre1 = pre match {
case ThisType(sym) if sym == macroDef.owner =>
SingleType(SingleType(SingleType(NoPrefix, paramsCtx(0)), MacroContextPrefix), ExprValue)
case SingleType(NoPrefix, sym) =>
mfind(vparamss)(_.symbol == sym) match {
case Some(macroDefParam) =>
SingleType(SingleType(NoPrefix, param(macroDefParam)), ExprValue)
case _ =>
pre
}
case _ =>
pre
}
val args1 = args map mapOver
TypeRef(pre1, sym, args1)
case _ =>
mapOver(tp)
}
}
new SigmaTypeMap() apply tpe
}
def makeParam(name: Name, pos: Position, tpe: Type, flags: Long = 0L) =
macroDef.newValueParameter(name, pos, flags) setInfo tpe
val ctxParam = makeParam(nme.macroContext, macroDef.pos, MacroContextClass.tpe, SYNTHETIC)
def implType(isType: Boolean, origTpe: Type): Type =
if (isRepeatedParamType(origTpe))
appliedType(
RepeatedParamClass.typeConstructor,
List(implType(isType, sigma(origTpe.typeArgs.head))))
else {
val tsym = getMember(MacroContextClass, if (isType) tpnme.TypeTag else tpnme.Expr)
typeRef(singleType(NoPrefix, ctxParam), tsym, List(sigma(origTpe)))
}
val paramCache = collection.mutable.Map[Symbol, Symbol]()
def param(tree: Tree): Symbol =
paramCache.getOrElseUpdate(tree.symbol, {
// [Eugene] deskolemization became necessary once I implemented inference of macro def return type
// please, verify this solution, but for now I'll leave it here - cargo cult for the win
val sym = tree.symbol.deSkolemize
val sigParam = makeParam(sym.name, sym.pos, implType(sym.isType, sym.tpe))
if (sym.isSynthetic) sigParam.flags |= SYNTHETIC
sigParam
})
val paramsCtx = List(ctxParam)
val paramsThis = List(makeParam(nme.macroThis, macroDef.pos, implType(false, ownerTpe), SYNTHETIC))
val paramsTparams = tparams map param
val paramssParams = mmap(vparamss)(param)
var paramsss = List[List[List[Symbol]]]()
// tparams are no longer part of a signature, they get into macro implementations via context bounds
// if (hasTparams && hasThis) paramsss :+= paramsCtx :: paramsThis :: paramsTparams :: paramssParams
// if (hasTparams) paramsss :+= paramsCtx :: paramsTparams :: paramssParams
// _this params are no longer part of a signature, its gets into macro implementations via Context.prefix
// if (hasThis) paramsss :+= paramsCtx :: paramsThis :: paramssParams
paramsss :+= paramsCtx :: paramssParams
val tsym = getMember(MacroContextClass, tpnme.Expr)
val implRetTpe = typeRef(singleType(NoPrefix, ctxParam), tsym, List(sigma(retTpe)))
}
import SigGenerator._
macroTraceVerbose("generating macroImplSigs for: ")(macroDef)
macroTraceVerbose("tparams are: ")(tparams)
macroTraceVerbose("vparamss are: ")(vparamss)
macroTraceVerbose("retTpe is: ")(retTpe)
macroTraceVerbose("macroImplSigs are: ")(paramsss, implRetTpe)
}
private def transformTypeTagEvidenceParams(paramss: List[List[Symbol]], transform: (Symbol, Symbol) => Option[Symbol]): List[List[Symbol]] = {
if (paramss.length == 0)
return paramss
val wannabe = if (paramss.head.length == 1) paramss.head.head else NoSymbol
val contextParam = if (wannabe != NoSymbol && wannabe.tpe <:< definitions.MacroContextClass.tpe) wannabe else NoSymbol
val lastParamList0 = paramss.lastOption getOrElse Nil
val lastParamList = lastParamList0 flatMap (param => param.tpe match {
case TypeRef(SingleType(NoPrefix, contextParam), sym, List(tparam)) =>
var wannabe = sym
while (wannabe.isAliasType) wannabe = wannabe.info.typeSymbol
if (wannabe != definitions.TypeTagClass && wannabe != definitions.ConcreteTypeTagClass)
List(param)
else
transform(param, tparam.typeSymbol) map (_ :: Nil) getOrElse Nil
case _ =>
List(param)
})
var result = paramss.dropRight(1) :+ lastParamList
if (lastParamList0.isEmpty ^ lastParamList.isEmpty) {
result = result dropRight 1
}
result
}
/** As specified above, body of a macro definition must reference its implementation.
* This function verifies that the body indeed refers to a method, and that
* the referenced macro implementation is compatible with the given macro definition.
*
* This means that macro implementation (fooBar in example above) must:
* 1) Refer to a statically accessible, non-overloaded method.
* 2) Have the right parameter lists as outlined in the SIP / in the doc comment of this class.
*
* @return typechecked rhs of the given macro definition
*/
def typedMacroBody(typer: Typer, ddef: DefDef): Tree = {
import typer.context
macroLogVerbose("typechecking macro def %s at %s".format(ddef.symbol, ddef.pos))
if (!typer.checkFeature(ddef.pos, MacrosFeature, immediate = true)) {
macroLogVerbose("typecheck terminated unexpectedly: language.experimental.macros feature is not enabled")
ddef.symbol setFlag IS_ERROR
return EmptyTree
}
implicit class AugmentedString(s: String) {
def abbreviateCoreAliases: String = { // hack!
var result = s
result = result.replace("c.mirror.TypeTag", "c.TypeTag")
result = result.replace("c.mirror.Expr", "c.Expr")
result
}
}
var hasErrors = false
def reportError(pos: Position, msg: String) = {
hasErrors = true
context.error(pos, msg)
}
val macroDef = ddef.symbol
val defpos = macroDef.pos
val implpos = ddef.rhs.pos
assert(macroDef.isTermMacro, ddef)
def invalidBodyError() =
reportError(defpos,
"macro body has wrong shape:" +
"\n required: macro <reference to implementation object>.<implementation method name>" +
"\n or : macro <implementation method name>")
def validatePreTyper(rhs: Tree): Unit = rhs match {
// we do allow macro invocations inside macro bodies
// personally I don't mind if pre-typer tree is a macro invocation
// that later resolves to a valid reference to a macro implementation
// however, I don't think that invalidBodyError() should hint at that
// let this be an Easter Egg :)
case Apply(_, _) => ;
case TypeApply(_, _) => ;
case Super(_, _) => ;
case This(_) => ;
case Ident(_) => ;
case Select(_, _) => ;
case _ => invalidBodyError()
}
def validatePostTyper(rhs1: Tree): Unit = {
def loop(tree: Tree): Unit = {
def errorNotStatic() =
reportError(implpos, "macro implementation must be in statically accessible object")
def ensureRoot(sym: Symbol) =
if (!sym.isModule && !sym.isModuleClass) errorNotStatic()
def ensureModule(sym: Symbol) =
if (!sym.isModule) errorNotStatic()
tree match {
case TypeApply(fun, _) =>
loop(fun)
case Super(qual, _) =>
ensureRoot(macroDef.owner)
loop(qual)
case This(_) =>
ensureRoot(tree.symbol)
case Select(qual, name) if name.isTypeName =>
loop(qual)
case Select(qual, name) if name.isTermName =>
if (tree.symbol != rhs1.symbol) ensureModule(tree.symbol)
loop(qual)
case Ident(name) if name.isTypeName =>
;
case Ident(name) if name.isTermName =>
if (tree.symbol != rhs1.symbol) ensureModule(tree.symbol)
case _ =>
invalidBodyError()
}
}
loop(rhs1)
}
val rhs = ddef.rhs
validatePreTyper(rhs)
if (hasErrors) macroTraceVerbose("macro def failed to satisfy trivial preconditions: ")(macroDef)
// we use typed1 instead of typed, because otherwise adapt is going to mess us up
// if adapt sees <qualifier>.<method>, it will want to perform eta-expansion and will fail
// unfortunately, this means that we have to manually trigger macro expansion
// because it's adapt which is responsible for automatic expansion during typechecking
def typecheckRhs(rhs: Tree): Tree = {
try {
val prevNumErrors = reporter.ERROR.count // [Eugene] funnily enough, the isErroneous check is not enough
var rhs1 = if (hasErrors) EmptyTree else typer.typed1(rhs, EXPRmode, WildcardType)
def typecheckedWithErrors = (rhs1 exists (_.isErroneous)) || reporter.ERROR.count != prevNumErrors
def rhsNeedsMacroExpansion = rhs1.symbol != null && rhs1.symbol.isTermMacro && !rhs1.symbol.isErroneous
while (!typecheckedWithErrors && rhsNeedsMacroExpansion) {
rhs1 = macroExpand1(typer, rhs1) match {
case Success(expanded) =>
try {
val typechecked = typer.typed1(expanded, EXPRmode, WildcardType)
macroLogVerbose("typechecked1:%n%s%n%s".format(typechecked, showRaw(typechecked)))
typechecked
} finally {
openMacros = openMacros.tail
}
case Fallback(fallback) =>
typer.typed1(fallback, EXPRmode, WildcardType)
case Other(result) =>
result
}
}
rhs1
} catch {
case ex: TypeError =>
typer.reportTypeError(context, rhs.pos, ex)
typer.infer.setError(rhs)
}
}
val prevNumErrors = reporter.ERROR.count // funnily enough, the isErroneous check is not enough
var rhs1 = typecheckRhs(rhs)
def typecheckedWithErrors = (rhs1 exists (_.isErroneous)) || reporter.ERROR.count != prevNumErrors
hasErrors = hasErrors || typecheckedWithErrors
if (typecheckedWithErrors) macroTraceVerbose("body of a macro def failed to typecheck: ")(ddef)
val macroImpl = rhs1.symbol
macroDef withAnnotation AnnotationInfo(MacroImplAnnotation.tpe, List(rhs1), Nil)
if (!hasErrors) {
if (macroImpl == null) {
invalidBodyError()
} else {
if (!macroImpl.isMethod)
invalidBodyError()
if (macroImpl.isOverloaded)
reportError(implpos, "macro implementation cannot be overloaded")
if (!macroImpl.typeParams.isEmpty && (!rhs1.isInstanceOf[TypeApply]))
reportError(implpos, "macro implementation reference needs type arguments")
if (!hasErrors)
validatePostTyper(rhs1)
}
if (hasErrors)
macroTraceVerbose("macro def failed to satisfy trivial preconditions: ")(macroDef)
}
if (!hasErrors) {
def checkCompatibility(reqparamss: List[List[Symbol]], actparamss: List[List[Symbol]], reqres: Type, actres: Type): List[String] = {
var hasErrors = false
var errors = List[String]()
def compatibilityError(msg: String) {
hasErrors = true
errors :+= msg
}
val flatreqparams = reqparamss.flatten
val flatactparams = actparamss.flatten
val tparams = macroImpl.typeParams
val tvars = tparams map freshVar
def lengthMsg(which: String, extra: Symbol) =
"parameter lists have different length, "+which+" extra parameter "+extra.defString
if (actparamss.length != reqparamss.length)
compatibilityError("number of parameter sections differ")
def checkSubType(slot: String, reqtpe: Type, acttpe: Type): Unit = {
val ok = if (macroDebugVerbose) {
if (reqtpe eq acttpe) println(reqtpe + " <: " + acttpe + "?" + EOL + "true")
withTypesExplained(reqtpe <:< acttpe)
} else reqtpe <:< acttpe
if (!ok) {
compatibilityError("type mismatch for %s: %s does not conform to %s".format(slot, reqtpe.toString.abbreviateCoreAliases, acttpe.toString.abbreviateCoreAliases))
}
}
if (!hasErrors) {
try {
for ((rparams, aparams) <- reqparamss zip actparamss) {
if (rparams.length < aparams.length)
compatibilityError(lengthMsg("found", aparams(rparams.length)))
if (aparams.length < rparams.length)
compatibilityError(lengthMsg("required", rparams(aparams.length)).abbreviateCoreAliases)
}
// if the implementation signature is already deemed to be incompatible, we bail out
// otherwise, high-order type magic employed below might crash in weird ways
if (!hasErrors) {
for ((rparams, aparams) <- reqparamss zip actparamss) {
for ((rparam, aparam) <- rparams zip aparams) {
def isRepeated(param: Symbol) = param.tpe.typeSymbol == RepeatedParamClass
if (rparam.name != aparam.name && !rparam.isSynthetic) {
val rparam1 = rparam
val aparam1 = aparam
compatibilityError("parameter names differ: "+rparam.name+" != "+aparam.name)
}
if (isRepeated(rparam) && !isRepeated(aparam))
compatibilityError("types incompatible for parameter "+rparam.name+": corresponding is not a vararg parameter")
if (!isRepeated(rparam) && isRepeated(aparam))
compatibilityError("types incompatible for parameter "+aparam.name+": corresponding is not a vararg parameter")
if (!hasErrors) {
var atpe = aparam.tpe.substSym(flatactparams, flatreqparams).instantiateTypeParams(tparams, tvars)
atpe = atpe.dealias // SI-5706
// strip the { type PrefixType = ... } refinement off the Context or otherwise we get compatibility errors
atpe = atpe match {
case RefinedType(List(tpe), Scope(sym)) if tpe == MacroContextClass.tpe && sym.allOverriddenSymbols.contains(MacroContextPrefixType) => tpe
case _ => atpe
}
checkSubType("parameter " + rparam.name, rparam.tpe, atpe)
}
}
}
}
if (!hasErrors) {
val atpe = actres.substSym(flatactparams, flatreqparams).instantiateTypeParams(tparams, tvars)
checkSubType("return type", atpe, reqres)
}
if (!hasErrors) {
val targs = solvedTypes(tvars, tparams, tparams map varianceInType(actres), false,
lubDepth(flatactparams map (_.tpe)) max lubDepth(flatreqparams map (_.tpe)))
val boundsOk = typer.silent(_.infer.checkBounds(ddef, NoPrefix, NoSymbol, tparams, targs, ""))
boundsOk match {
case SilentResultValue(true) => ;
case SilentResultValue(false) | SilentTypeError(_) =>
val bounds = tparams map (tp => tp.info.instantiateTypeParams(tparams, targs).bounds)
compatibilityError("type arguments " + targs.mkString("[", ",", "]") +
" do not conform to " + tparams.head.owner + "'s type parameter bounds " +
(tparams map (_.defString)).mkString("[", ",", "]"))
}
}
} catch {
case ex: NoInstance =>
compatibilityError(
"type parameters "+(tparams map (_.defString) mkString ", ")+" cannot be instantiated\n"+
ex.getMessage)
}
}
errors.toList
}
var actparamss = macroImpl.paramss
actparamss = transformTypeTagEvidenceParams(actparamss, (param, tparam) => None)
val rettpe = if (!ddef.tpt.isEmpty) typer.typedType(ddef.tpt).tpe else computeMacroDefTypeFromMacroImpl(ddef, macroDef, macroImpl)
val (reqparamsss0, reqres0) = macroImplSigs(macroDef, ddef.tparams, ddef.vparamss, rettpe)
var reqparamsss = reqparamsss0
// prohibit implicit params on macro implementations
// we don't have to do this, but it appears to be more clear than allowing them
val implicitParams = actparamss.flatten filter (_.isImplicit)
if (implicitParams.length > 0) {
reportError(implicitParams.head.pos, "macro implementations cannot have implicit parameters other than TypeTag evidences")
macroTraceVerbose("macro def failed to satisfy trivial preconditions: ")(macroDef)
}
if (!hasErrors) {
val reqres = reqres0
val actres = macroImpl.tpe.finalResultType
def showMeth(pss: List[List[Symbol]], restpe: Type, abbreviate: Boolean) = {
var argsPart = (pss map (ps => ps map (_.defString) mkString ("(", ", ", ")"))).mkString
if (abbreviate) argsPart = argsPart.abbreviateCoreAliases
var retPart = restpe.toString
if (abbreviate || ddef.tpt.tpe == null) retPart = retPart.abbreviateCoreAliases
argsPart + ": " + retPart
}
def compatibilityError(addendum: String) =
reportError(implpos,
"macro implementation has wrong shape:"+
"\n required: "+showMeth(reqparamsss.head, reqres, true) +
(reqparamsss.tail map (paramss => "\n or : "+showMeth(paramss, reqres, true)) mkString "")+
"\n found : "+showMeth(actparamss, actres, false)+
"\n"+addendum)
macroTraceVerbose("considering " + reqparamsss.length + " possibilities of compatible macro impl signatures for macro def: ")(ddef.name)
val results = reqparamsss map (checkCompatibility(_, actparamss, reqres, actres))
if (macroDebugVerbose) (reqparamsss zip results) foreach { case (reqparamss, result) =>
println("%s %s".format(if (result.isEmpty) "[ OK ]" else "[FAILED]", reqparamss))
result foreach (errorMsg => println(" " + errorMsg))
}
if (results forall (!_.isEmpty)) {
var index = reqparamsss indexWhere (_.length == actparamss.length)
if (index == -1) index = 0
val mostRelevantMessage = results(index).head
compatibilityError(mostRelevantMessage)
} else {
assert((results filter (_.isEmpty)).length == 1, results)
if (macroDebugVerbose) (reqparamsss zip results) filter (_._2.isEmpty) foreach { case (reqparamss, result) =>
println("typechecked macro impl as: " + reqparamss)
}
}
}
}
// if this macro definition is erroneous, then there's no sense in expanding its usages
// in the previous prototype macro implementations were magically generated from macro definitions
// so macro definitions and its usages couldn't be compiled in the same compilation run
// however, now definitions and implementations are decoupled, so it's everything is possible
// hence, we now use IS_ERROR flag to serve as an indicator that given macro definition is broken
if (hasErrors) {
macroDef setFlag IS_ERROR
}
rhs1
}
def computeMacroDefTypeFromMacroImpl(macroDdef: DefDef, macroDef: Symbol, macroImpl: Symbol): Type = {
// get return type from method type
def unwrapRet(tpe: Type): Type = {
def loop(tpe: Type) = tpe match {
case NullaryMethodType(ret) => ret
case mtpe @ MethodType(_, ret) => unwrapRet(ret)
case _ => tpe
}
tpe match {
case PolyType(_, tpe) => loop(tpe)
case _ => loop(tpe)
}
}
var metaType = unwrapRet(macroImpl.tpe)
// downgrade from metalevel-0 to metalevel-1
def inferRuntimeType(metaType: Type): Type = metaType match {
case TypeRef(pre, sym, args) if sym.name == tpnme.Expr && args.length == 1 =>
args.head
case _ =>
AnyClass.tpe
}
var runtimeType = inferRuntimeType(metaType)
// transform type parameters of a macro implementation into type parameters of a macro definition
runtimeType = runtimeType map {
case TypeRef(pre, sym, args) =>
// [Eugene] not sure which of these deSkolemizes are necessary
// sym.paramPos is unreliable (see another case below)
val tparams = macroImpl.typeParams map (_.deSkolemize)
val paramPos = tparams indexOf sym.deSkolemize
val sym1 = if (paramPos == -1) sym else {
val ann = macroDef.getAnnotation(MacroImplAnnotation)
ann match {
case Some(ann) =>
val TypeApply(_, implRefTargs) = ann.args(0)
val implRefTarg = implRefTargs(paramPos).tpe.typeSymbol
implRefTarg
case None =>
sym
}
}
TypeRef(pre, sym1, args)
case tpe =>
tpe
}
// as stated in the spec, before being matched to macroimpl, type and value parameters of macrodef
// undergo a special transformation, sigma, that adapts them to the different metalevel macroimpl lives in
// as a result, we need to reverse this transformation when inferring macrodef ret from macroimpl ret
def unsigma(tpe: Type): Type = {
// unfortunately, we cannot dereference ``paramss'', because we're in the middle of inferring a type for ``macroDef''
// val defParamss = macroDef.paramss
val defParamss = mmap(macroDdef.vparamss)(_.symbol)
var implParamss = macroImpl.paramss
implParamss = transformTypeTagEvidenceParams(implParamss, (param, tparam) => None)
val implCtxParam = if (implParamss.length > 0 && implParamss(0).length > 0) implParamss(0)(0) else null
def implParamToDefParam(implParam: Symbol): Symbol = {
val indices = (((implParamss drop 1).zipWithIndex) map { case (implParams, index) => (index, implParams indexOf implParam) } filter (_._2 != -1)).headOption
val defParam = indices flatMap {
case (plistIndex, pIndex) =>
if (defParamss.length <= plistIndex) None
else if (defParamss(plistIndex).length <= pIndex) None
else Some(defParamss(plistIndex)(pIndex))
}
defParam.orNull
}
class UnsigmaTypeMap extends TypeMap {
def apply(tp: Type): Type = tp match {
case TypeRef(pre, sym, args) =>
val pre1 = pre match {
case SingleType(SingleType(SingleType(NoPrefix, param), prefix), value) if param == implCtxParam && prefix == MacroContextPrefix && value == ExprValue =>
ThisType(macroDef.owner)
case SingleType(SingleType(NoPrefix, param), value) if implParamToDefParam(param) != null && value == ExprValue =>
val macroDefParam = implParamToDefParam(param)
SingleType(NoPrefix, macroDefParam)
case _ =>
pre
}
val args1 = args map mapOver
TypeRef(pre1, sym, args1)
case _ =>
mapOver(tp)
}
}
new UnsigmaTypeMap() apply tpe
}
runtimeType = unsigma(runtimeType)
runtimeType
}
/** Primary mirror that is used to resolve and run macro implementations.
* Loads classes from -Xmacro-primary-classpath, or from -cp if the option is not specified.
*/
private lazy val primaryMirror: Mirror = {
if (global.forMSIL)
throw new UnsupportedOperationException("Scala reflection not available on this platform")
val libraryClassLoader = {
if (settings.XmacroPrimaryClasspath.value != "") {
macroLogVerbose("primary macro mirror: initializing from -Xmacro-primary-classpath: %s".format(settings.XmacroPrimaryClasspath.value))
val classpath = toURLs(settings.XmacroFallbackClasspath.value)
ScalaClassLoader.fromURLs(classpath, self.getClass.getClassLoader)
} else {
macroLogVerbose("primary macro mirror: initializing from -cp: %s".format(global.classPath.asURLs))
val classpath = global.classPath.asURLs
var loader: ClassLoader = ScalaClassLoader.fromURLs(classpath, self.getClass.getClassLoader)
// [Eugene] a heuristic to detect REPL
if (global.settings.exposeEmptyPackage.value) {
import scala.tools.nsc.interpreter._
val virtualDirectory = global.settings.outputDirs.getSingleOutput.get
loader = new AbstractFileClassLoader(virtualDirectory, loader) {}
}
loader
}
}
new Mirror(libraryClassLoader) { override def toString = "<primary macro mirror>" }
}
/** Fallback mirror that is used to resolve and run macro implementations.
* Loads classes from -Xmacro-fallback-classpath aka "macro fallback classpath".
*/
private lazy val fallbackMirror: Mirror = {
if (global.forMSIL)
throw new UnsupportedOperationException("Scala reflection not available on this platform")
val fallbackClassLoader = {
macroLogVerbose("fallback macro mirror: initializing from -Xmacro-fallback-classpath: %s".format(settings.XmacroFallbackClasspath.value))
val classpath = toURLs(settings.XmacroFallbackClasspath.value)
ScalaClassLoader.fromURLs(classpath, self.getClass.getClassLoader)
}
new Mirror(fallbackClassLoader) { override def toString = "<fallback macro mirror>" }
}
/** Produces a function that can be used to invoke macro implementation for a given macro definition:
* 1) Looks up macro implementation symbol in this universe.
* 2) Loads its enclosing class from the primary mirror.
* 3) Loads the companion of that enclosing class from the primary mirror.
* 4) Resolves macro implementation within the loaded companion.
* 5) If 2-4 fails, repeats them for the fallback mirror.
*
* @return Some(runtime) if macro implementation can be loaded successfully from either of the mirrors,
* None otherwise.
*/
private type MacroRuntime = List[Any] => Any
private val macroRuntimesCache = perRunCaches.newWeakMap[Symbol, Option[MacroRuntime]]
private lazy val fastTrack: Map[Symbol, MacroRuntime] = {
import scala.reflect.api.Universe
import scala.reflect.makro.internal._
Map( // challenge: how can we factor out the common code? Does not seem to be easy.
MacroInternal_materializeArrayTag -> (args => {
assert(args.length == 3, args)
val c = args(0).asInstanceOf[MacroContext]
materializeArrayTag_impl(c)(args(1).asInstanceOf[c.Expr[Universe]])(args(2).asInstanceOf[c.TypeTag[_]])
}),
MacroInternal_materializeErasureTag -> (args => {
assert(args.length == 3, args)
val c = args(0).asInstanceOf[MacroContext]
materializeErasureTag_impl(c)(args(1).asInstanceOf[c.Expr[Universe]])(args(2).asInstanceOf[c.TypeTag[_]])
}),
MacroInternal_materializeClassTag -> (args => {
assert(args.length == 3, args)
val c = args(0).asInstanceOf[MacroContext]
materializeClassTag_impl(c)(args(1).asInstanceOf[c.Expr[Universe]])(args(2).asInstanceOf[c.TypeTag[_]])
}),
MacroInternal_materializeTypeTag -> (args => {
assert(args.length == 3, args)
val c = args(0).asInstanceOf[MacroContext]
materializeTypeTag_impl(c)(args(1).asInstanceOf[c.Expr[Universe]])(args(2).asInstanceOf[c.TypeTag[_]])
}),
MacroInternal_materializeConcreteTypeTag -> (args => {
assert(args.length == 3, args)
val c = args(0).asInstanceOf[MacroContext]
materializeConcreteTypeTag_impl(c)(args(1).asInstanceOf[c.Expr[Universe]])(args(2).asInstanceOf[c.TypeTag[_]])
})
)
}
private def macroRuntime(macroDef: Symbol): Option[MacroRuntime] = {
macroTraceVerbose("looking for macro implementation: ")(macroDef)
if (fastTrack contains macroDef) {
macroLogVerbose("macro expansion is serviced by a fast track")
Some(fastTrack(macroDef))
} else {
macroRuntimesCache.getOrElseUpdate(macroDef, {
val runtime = {
macroTraceVerbose("macroDef is annotated with: ")(macroDef.annotations)
val ann = macroDef.getAnnotation(MacroImplAnnotation)
if (ann == None) { macroTraceVerbose("@macroImpl annotation is missing (this means that macro definition failed to typecheck)")(macroDef); return None }
val macroImpl = ann.get.args(0).symbol
if (macroImpl == NoSymbol) { macroTraceVerbose("@macroImpl annotation is malformed (this means that macro definition failed to typecheck)")(macroDef); return None }
macroLogVerbose("resolved implementation %s at %s".format(macroImpl, macroImpl.pos))
if (macroImpl.isErroneous) { macroTraceVerbose("macro implementation is erroneous (this means that either macro body or macro implementation signature failed to typecheck)")(macroDef); return None }
def loadMacroImpl(macroMirror: Mirror): Option[(Object, macroMirror.Symbol)] = {
try {
// this logic relies on the assumptions that were valid for the old macro prototype
// namely that macro implementations can only be defined in top-level classes and modules
// with the new prototype that materialized in a SIP, macros need to be statically accessible, which is different
// for example, a macro def could be defined in a trait that is implemented by an object
// there are some more clever cases when seemingly non-static method ends up being statically accessible
// however, the code below doesn't account for these guys, because it'd take a look of time to get it right
// for now I leave it as a todo and move along to more the important stuff
macroTraceVerbose("loading implementation class: ")(macroImpl.owner.fullName)
macroTraceVerbose("classloader is: ")(ReflectionUtils.show(macroMirror.classLoader))
// [Eugene] relies on the fact that macro implementations can only be defined in static classes
// [Martin to Eugene] There's similar logic buried in Symbol#flatname. Maybe we can refactor?
def classfile(sym: Symbol): String = {
def recur(sym: Symbol): String = sym match {
case sym if sym.owner.isPackageClass =>
val suffix = if (sym.isModuleClass) "$" else ""
sym.fullName + suffix
case sym =>
val separator = if (sym.owner.isModuleClass) "" else "$"
recur(sym.owner) + separator + sym.javaSimpleName.toString
}
if (sym.isClass || sym.isModule) recur(sym)
else recur(sym.enclClass)
}
// [Eugene] this doesn't work for inner classes
// neither does macroImpl.owner.javaClassName, so I had to roll my own implementation
//val receiverName = macroImpl.owner.fullName
val implClassName = classfile(macroImpl.owner)
val implClassSymbol: macroMirror.Symbol = macroMirror.symbolForName(implClassName)
if (macroDebugVerbose) {
println("implClassSymbol is: " + implClassSymbol.fullNameString)
if (implClassSymbol != macroMirror.NoSymbol) {
val implClass = macroMirror.classToJava(implClassSymbol)
val implSource = implClass.getProtectionDomain.getCodeSource
println("implClass is %s from %s".format(implClass, implSource))
println("implClassLoader is %s".format(implClass.getClassLoader, ReflectionUtils.show(implClass.getClassLoader)))
}
}
val implObjSymbol = implClassSymbol.companionModule
macroTraceVerbose("implObjSymbol is: ")(implObjSymbol.fullNameString)
if (implObjSymbol == macroMirror.NoSymbol) None
else {
// yet another reflection method that doesn't work for inner classes
//val receiver = macroMirror.companionInstance(receiverClass)
val implObj = try {
val implObjClass = java.lang.Class.forName(implClassName, true, macroMirror.classLoader)
implObjClass getField "MODULE$" get null
} catch {
case ex: NoSuchFieldException => macroTraceVerbose("exception when loading implObj: ")(ex); null
case ex: NoClassDefFoundError => macroTraceVerbose("exception when loading implObj: ")(ex); null
case ex: ClassNotFoundException => macroTraceVerbose("exception when loading implObj: ")(ex); null
}
if (implObj == null) None
else {
val implMethSymbol = implObjSymbol.info.member(macroMirror.newTermName(macroImpl.name.toString))
macroLogVerbose("implMethSymbol is: " + implMethSymbol.fullNameString)
macroLogVerbose("jimplMethSymbol is: " + macroMirror.methodToJava(implMethSymbol))
if (implMethSymbol == macroMirror.NoSymbol) None
else {
macroLogVerbose("successfully loaded macro impl as (%s, %s)".format(implObj, implMethSymbol))
Some((implObj, implMethSymbol))
}
}
}
} catch {
case ex: ClassNotFoundException =>
macroTraceVerbose("implementation class failed to load: ")(ex.toString)
None
case ex: NoSuchMethodException =>
macroTraceVerbose("implementation method failed to load: ")(ex.toString)
None
}
}
val primary = loadMacroImpl(primaryMirror)
primary match {
case Some((implObj, implMethSymbol)) =>
def runtime(args: List[Any]) = primaryMirror.invoke(implObj, implMethSymbol)(args: _*).asInstanceOf[Any]
Some(runtime _)
case None =>
if (settings.XmacroFallbackClasspath.value != "") {
macroLogVerbose("trying to load macro implementation from the fallback mirror: %s".format(settings.XmacroFallbackClasspath.value))
val fallback = loadMacroImpl(fallbackMirror)
fallback match {
case Some((implObj, implMethSymbol)) =>
def runtime(args: List[Any]) = fallbackMirror.invoke(implObj, implMethSymbol)(args: _*).asInstanceOf[Any]
Some(runtime _)
case None =>
None
}
} else {
None
}
}
}
if (runtime == None) macroDef setFlag IS_ERROR
runtime
})
}
}
/** Should become private again once we're done with migrating typetag generation from implicits */
def macroContext(typer: Typer, prefixTree: Tree, expandeeTree: Tree): MacroContext { val mirror: global.type } =
new {
val mirror: global.type = global
val callsiteTyper: mirror.analyzer.Typer = typer.asInstanceOf[global.analyzer.Typer]
// todo. infer precise typetag for this Expr, namely the PrefixType member of the Context refinement
val prefix = Expr(prefixTree)(TypeTag.Nothing)
val expandee = expandeeTree
} with MacroContext {
override def toString = "MacroContext(%s@%s +%d)".format(expandee.symbol.name, expandee.pos, enclosingMacros.length - 1 /* exclude myself */)
}
/** Calculate the arguments to pass to a macro implementation when expanding the provided tree.
*
* This includes inferring the exact type and instance of the macro context to pass, and also
* allowing for missing parameter sections in macro implementation (see ``macroImplParamsss'' for more info).
*
* @return list of runtime objects to pass to the implementation obtained by ``macroRuntime''
*/
private def macroArgs(typer: Typer, expandee: Tree): Option[List[Any]] = {
val macroDef = expandee.symbol
val runtime = macroRuntime(macroDef) orElse { return None }
var prefixTree: Tree = EmptyTree
var typeArgs = List[Tree]()
val exprArgs = ListBuffer[List[Expr[_]]]()
def collectMacroArgs(tree: Tree): Unit = tree match {
case Apply(fn, args) =>
// todo. infer precise typetag for this Expr, namely the declared type of the corresponding macro impl argument
exprArgs.prepend(args map (Expr(_)(TypeTag.Nothing)))
collectMacroArgs(fn)
case TypeApply(fn, args) =>
typeArgs = args
collectMacroArgs(fn)
case Select(qual, name) =>
prefixTree = qual
case _ =>
}
collectMacroArgs(expandee)
val context = expandee.attachmentOpt[MacroAttachment].flatMap(_.macroContext).getOrElse(macroContext(typer, prefixTree, expandee))
var argss: List[List[Any]] = List(context) :: exprArgs.toList
macroTraceVerbose("argss: ")(argss)
val ann = macroDef.getAnnotation(MacroImplAnnotation).getOrElse(throw new Error("assertion failed. %s: %s".format(macroDef, macroDef.annotations)))
val macroImpl = ann.args(0).symbol
var paramss = macroImpl.paramss
val tparams = macroImpl.typeParams
macroTraceVerbose("paramss: ")(paramss)
// we need to take care of all possible combos of nullary/empty-paramlist macro defs vs nullary/empty-arglist invocations
// nullary def + nullary invocation => paramss and argss match, everything is okay
// nullary def + empty-arglist invocation => illegal Scala code, impossible, everything is okay
// empty-paramlist def + nullary invocation => uh-oh, we need to append a List() to argss
// empty-paramlist def + empty-arglist invocation => paramss and argss match, everything is okay
// that's almost it, but we need to account for the fact that paramss might have context bounds that mask the empty last paramlist
val paramss_without_evidences = transformTypeTagEvidenceParams(paramss, (param, tparam) => None)
val isEmptyParamlistDef = paramss_without_evidences.nonEmpty && paramss_without_evidences.last.isEmpty
val isEmptyArglistInvocation = argss.nonEmpty && argss.last.isEmpty
if (isEmptyParamlistDef && !isEmptyArglistInvocation) {
macroLogVerbose("isEmptyParamlistDef && !isEmptyArglistInvocation: appending a List() to argss")
argss = argss :+ Nil
}
// nb! check partial application against paramss without evidences
val numParamLists = paramss_without_evidences.length
val numArgLists = argss.length
if (numParamLists != numArgLists) {
typer.TyperErrorGen.MacroPartialApplicationError(expandee)
return None
}
// if paramss have typetag context bounds, add an arglist to argss if necessary and instantiate the corresponding evidences
// consider the following example:
//
// class D[T] {
// class C[U] {
// def foo[V] = macro Impls.foo[T, U, V]
// }
// }
//
// val outer1 = new D[Int]
// val outer2 = new outer1.C[String]
// outer2.foo[Boolean]
//
// then T and U need to be inferred from the lexical scope of the call using ``asSeenFrom''
// whereas V won't be resolved by asSeenFrom and need to be loaded directly from ``expandee'' which needs to contain a TypeApply node
// also, macro implementation reference may contain a regular type as a type argument, then we pass it verbatim
val resolved = collection.mutable.Map[Symbol, Type]()
paramss = transformTypeTagEvidenceParams(paramss, (param, tparam) => {
val TypeApply(_, implRefTargs) = ann.args(0)
var implRefTarg = implRefTargs(tparam.paramPos).tpe.typeSymbol
val tpe = if (implRefTarg.isTypeParameterOrSkolem) {
if (implRefTarg.owner == macroDef) {
// [Eugene] doesn't work when macro def is compiled separately from its usages
// then implRefTarg is not a skolem and isn't equal to any of macroDef.typeParams
// val paramPos = implRefTarg.deSkolemize.paramPos
val paramPos = macroDef.typeParams.indexWhere(_.name == implRefTarg.name)
typeArgs(paramPos).tpe
} else
implRefTarg.tpe.asSeenFrom(
if (prefixTree == EmptyTree) macroDef.owner.tpe else prefixTree.tpe,
macroDef.owner)
} else
implRefTarg.tpe
macroLogVerbose("resolved tparam %s as %s".format(tparam, tpe))
resolved(tparam) = tpe
param.tpe.typeSymbol match {
case definitions.TypeTagClass =>
// do nothing
case definitions.ConcreteTypeTagClass =>
if (!tpe.isConcrete) context.abort(context.enclosingPosition, "cannot create ConcreteTypeTag from a type %s having unresolved type parameters".format(tpe))
// otherwise do nothing
case _ =>
throw new Error("unsupported tpe: " + tpe)
}
Some(tparam)
})
val tags = paramss.last takeWhile (_.isType) map (resolved(_)) map (tpe => {
// generally speaking, it's impossible to calculate erasure from a tpe here
// the tpe might be compiled by this run, so its jClass might not exist yet
// hence I just pass `null` instead and leave this puzzle to macro programmers
val ttag = TypeTag(tpe, null)
if (ttag.isConcrete) ttag.toConcrete else ttag
})
if (paramss.lastOption map (params => !params.isEmpty && params.forall(_.isType)) getOrElse false) argss = argss :+ Nil
argss = argss.dropRight(1) :+ (tags ++ argss.last) // todo. add support for context bounds in argss
assert(argss.length == paramss.length, "argss: %s, paramss: %s".format(argss, paramss))
val rawArgss = for ((as, ps) <- argss zip paramss) yield {
if (isVarArgsList(ps)) as.take(ps.length - 1) :+ as.drop(ps.length - 1)
else as
}
val rawArgs = rawArgss.flatten
macroTraceVerbose("rawArgs: ")(rawArgs)
Some(rawArgs)
}
/** Keeps track of macros in-flight.
* See more informations in comments to ``openMacros'' in ``scala.reflect.makro.Context''.
*/
var openMacros = List[MacroContext]()
def enclosingMacroPosition = openMacros map (_.macroApplication.pos) find (_ ne NoPosition) getOrElse NoPosition
/** Performs macro expansion:
* 1) Checks whether the expansion needs to be delayed (see ``mustDelayMacroExpansion'')
* 2) Loads macro implementation using ``macroMirror''
* 3) Synthesizes invocation arguments for the macro implementation
* 4) Checks that the result is a tree bound to this universe
* 5) Typechecks the result against the return type of the macro definition
*
* If -Ymacro-debug-lite is enabled, you will get basic notifications about macro expansion
* along with macro expansions logged in the form that can be copy/pasted verbatim into REPL.
*
* If -Ymacro-debug-verbose is enabled, you will get detailed log of how exactly this function
* performs class loading and method resolution in order to load the macro implementation.
* The log will also include other non-trivial steps of macro expansion.
*
* @return
* the expansion result if the expansion has been successful,
* the fallback method invocation if the expansion has been unsuccessful, but there is a fallback,
* the expandee unchanged if the expansion has been delayed,
* the expandee fully expanded if the expansion has been delayed before and has been expanded now,
* the expandee with an error marker set if the expansion has been cancelled due malformed arguments or implementation
* the expandee with an error marker set if there has been an error
*/
def macroExpand(typer: Typer, expandee: Tree, mode: Int = EXPRmode, pt: Type = WildcardType): Tree = {
def fail(what: String, tree: Tree): Tree = {
val err = typer.context.errBuffer.head
this.fail(typer, tree, err.errPos, "failed to %s: %s".format(what, err.errMsg))
return expandee
}
val start = startTimer(macroExpandNanos)
incCounter(macroExpandCount)
try {
macroExpand1(typer, expandee) match {
case Success(expanded0) =>
try {
val expanded = expanded0 // virtpatmat swallows the local for expandee from the match
// so I added this dummy local for the ease of debugging
var expectedTpe = expandee.tpe
// [Eugene] weird situation. what's the conventional way to deal with it?
val isNullaryInvocation = expandee match {
case TypeApply(Select(_, _), _) => true
case TypeApply(Ident(_), _) => true
case Select(_, _) => true
case Ident(_) => true
case _ => false
}
if (isNullaryInvocation) expectedTpe match {
case NullaryMethodType(restpe) =>
macroTraceVerbose("nullary invocation of a nullary method. unwrapping expectedTpe from " + expectedTpe + " to: ")(restpe)
expectedTpe = restpe
case MethodType(Nil, restpe) =>