@@ -23,7 +23,7 @@ import util.Statistics._
23
23
* @author Martin Odersky
24
24
* @version 1.0
25
25
*/
26
- trait Implicits {
26
+ trait Implicits extends SourceContextUtils {
27
27
self : Analyzer =>
28
28
29
29
import global ._
@@ -1112,126 +1112,6 @@ trait Implicits {
1112
1112
implicitInfoss1
1113
1113
}
1114
1114
1115
- def contextSourceInfoChain (ctx : Context ,
1116
- stopAt : Context ,
1117
- prevValDef : Option [String ]): List [(String , Int )] = {
1118
- if (ctx == stopAt)
1119
- List ()
1120
- else ctx.tree match {
1121
- case vd @ ValDef (_, name, _, _) if prevValDef.isEmpty || (! prevValDef.get.equals(name.toString)) =>
1122
- (name.toString, vd.pos.line) :: contextSourceInfoChain(ctx.outer, stopAt, Some (name.toString))
1123
- // case app @ Apply(fun, args) if fun.symbol.isMethod =>
1124
- // (fun.symbol.nameString, fun.pos.line) :: contextSourceInfoChain(ctx.outer, stopAt)
1125
- case _ =>
1126
- contextSourceInfoChain(ctx.outer, stopAt, None )
1127
- }
1128
- }
1129
-
1130
- def contextInfoChain = context0.tree match {
1131
- case vd @ ValDef (_, name, _, _) =>
1132
- // println("current context tree is ValDef "+name)
1133
- contextSourceInfoChain(context0, context0.enclClass, None )
1134
- case _ =>
1135
- // println("current context tree: "+context0.tree)
1136
- val l = tree.pos match {
1137
- case NoPosition => 0
1138
- case _ => tree.pos.line
1139
- }
1140
- (null , l) :: contextSourceInfoChain(context0.outer, context0.outer.enclClass, None )
1141
- }
1142
-
1143
- def sourceInfoTree (chain : List [(String , Int )]): Tree = chain match {
1144
- case (name, line) :: rest =>
1145
- val pairTree = gen.mkTuple(List (Literal (Constant (name)), Literal (Constant (line))))
1146
- Apply (Select (gen.mkAttributedRef(ListModule ), nme.apply), List (pairTree))
1147
- case List () =>
1148
- gen.mkNil
1149
- }
1150
-
1151
- /** Creates a tree that calls the factory method called constructor in object reflect.SourceContext */
1152
- def sourceInfoFactoryCall (constructor : String , args : Tree * ): Tree =
1153
- if (args contains EmptyTree ) EmptyTree
1154
- else typedPos(tree.pos.focus) {
1155
- Apply (
1156
- Select (gen.mkAttributedRef(SourceContextModule ), constructor),
1157
- args.toList
1158
- )
1159
- }
1160
-
1161
- private def methodNameOf (tree : Tree ) = {
1162
- tree match {
1163
- case Apply (TypeApply (s, _), _) => s.symbol.name
1164
- case Apply (s@ Select (_, _), _) => s.symbol.name
1165
- case Apply (s@ Ident (_), _) => s.symbol.name
1166
- case Apply (Apply (s, _), _) => s.symbol.name
1167
- case s@ Select (_, _) => s.symbol.name
1168
- case other => " "
1169
- }
1170
- }
1171
-
1172
- private def receiverOptOf (tree : Tree ) = {
1173
- try {
1174
- tree match {
1175
- case Apply (TypeApply (Select (recv, _), _), _) => Some (recv.symbol.name)
1176
- case Apply (Select (recv, _), _) => Some (recv.symbol.name)
1177
- case Select (recv, _) => Some (recv.symbol.name)
1178
- case _ => None
1179
- }
1180
- } catch {
1181
- case npe : NullPointerException =>
1182
- None
1183
- }
1184
- }
1185
-
1186
- private def sourceInfo (): SearchResult = {
1187
- def srcInfo ()(implicit from : List [Symbol ] = List (), to : List [Type ] = List ()): SearchResult = {
1188
- implicit def wrapResult (tree : Tree ): SearchResult =
1189
- if (tree == EmptyTree ) SearchFailure else new SearchResult (tree, new TreeTypeSubstituter (from, to))
1190
-
1191
- val methodName = methodNameOf(tree)
1192
- val receiver = receiverOptOf(tree)
1193
-
1194
- // println("context source info chain:")
1195
- // println(contextInfoChain)
1196
- // println("source info tree:")
1197
- // println(sourceInfoTree(contextInfoChain))
1198
-
1199
- val position = tree.pos.focus
1200
- val fileName = if (position.isDefined) position.source.file.absolute.path
1201
- else " <unknown file>"
1202
- if (receiver.isEmpty)
1203
- sourceInfoFactoryCall(" apply" , Literal (Constant (fileName)), Literal (Constant (methodName.toString)), sourceInfoTree(contextInfoChain))
1204
- else
1205
- sourceInfoFactoryCall(" apply" , Literal (Constant (fileName)), Literal (Constant (methodName.toString)), Literal (Constant (receiver.get.toString)), sourceInfoTree(contextInfoChain))
1206
- }
1207
-
1208
- srcInfo()
1209
- }
1210
-
1211
- private def sourceLocation (): SearchResult = {
1212
- /** Creates a tree that calls the factory method called constructor in object reflect.SourceLocation */
1213
- def sourceLocationFactoryCall (constructor : String , args : Tree * ): Tree =
1214
- if (args contains EmptyTree ) EmptyTree
1215
- else typedPos(tree.pos.focus) {
1216
- Apply (
1217
- Select (gen.mkAttributedRef(SourceLocationModule ), constructor),
1218
- args.toList
1219
- )
1220
- }
1221
-
1222
- def srcLocation ()(implicit from : List [Symbol ] = List (), to : List [Type ] = List ()): SearchResult = {
1223
- implicit def wrapResult (tree : Tree ): SearchResult =
1224
- if (tree == EmptyTree ) SearchFailure else new SearchResult (tree, new TreeTypeSubstituter (from, to))
1225
-
1226
- val position = tree.pos.focus
1227
- val fileName = if (position.isDefined) position.source.file.absolute.path
1228
- else " <unknown file>"
1229
- sourceLocationFactoryCall(" apply" , Literal (Constant (position.line)), Literal (Constant (position.point)), Literal (Constant (fileName)))
1230
- }
1231
-
1232
- srcLocation()
1233
- }
1234
-
1235
1115
/** Creates a tree that calls the relevant factory method in object
1236
1116
* reflect.Manifest for type 'tp'. An EmptyTree is returned if
1237
1117
* no manifest is found. todo: make this instantiate take type params as well?
@@ -1382,7 +1262,7 @@ trait Implicits {
1382
1262
pt.dealias match {
1383
1263
case TypeRef (_, SourceContextClass , _) =>
1384
1264
// construct new SourceContext instance
1385
- result = sourceInfo()
1265
+ result = sourceInfo(this , context0, tree )
1386
1266
// there is no existing SourceContext to chain with
1387
1267
updateSourceContext = false
1388
1268
case _ =>
@@ -1397,29 +1277,7 @@ trait Implicits {
1397
1277
if (result == SearchFailure && settings.debug.value)
1398
1278
log(" no implicits found for " + pt+ " " + pt.typeSymbol.info.baseClasses+ " " + implicitsOfExpectedType)
1399
1279
1400
- val updatedRes = pt/* .dealias*/ match {
1401
- case TypeRef (_, SourceContextClass , _) if updateSourceContext =>
1402
- val position = tree.pos.focus
1403
- val fileName = if (position.isDefined) position.source.file.absolute.path
1404
- else " <unknown file>"
1405
- val methodName = methodNameOf(tree)
1406
- val receiver = receiverOptOf(tree)
1407
- new SearchResult (typedPos(position) {
1408
- // use sourceInfoFactoryCall to construct SourceContext
1409
- val factoryCall = if (receiver.isEmpty)
1410
- sourceInfoFactoryCall(" apply" , Literal (Constant (fileName)), Literal (Constant (methodName.toString)), sourceInfoTree(contextInfoChain))
1411
- else
1412
- sourceInfoFactoryCall(" apply" , Literal (Constant (fileName)), Literal (Constant (methodName.toString)), Literal (Constant (receiver.get.toString)), sourceInfoTree(contextInfoChain))
1413
- Apply (Select (result.tree, " update" ), List (factoryCall))
1414
- }, result.subst)
1415
- case TypeRef (_, SourceLocationClass , _) =>
1416
- val position = tree.pos.focus
1417
- new SearchResult (typedPos(position) {
1418
- sourceLocation().tree
1419
- }, result.subst)
1420
- case _ => result
1421
- }
1422
- updatedRes
1280
+ updatedWithSourceContext(this , tree, pt, context0, result, updateSourceContext)
1423
1281
}
1424
1282
1425
1283
def allImplicits : List [SearchResult ] = {
0 commit comments