@@ -23,7 +23,7 @@ import util.Statistics._
2323 * @author Martin Odersky
2424 * @version 1.0
2525 */
26- trait Implicits {
26+ trait Implicits extends SourceContextUtils {
2727 self : Analyzer =>
2828
2929 import global ._
@@ -1112,126 +1112,6 @@ trait Implicits {
11121112 implicitInfoss1
11131113 }
11141114
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-
12351115 /** Creates a tree that calls the relevant factory method in object
12361116 * reflect.Manifest for type 'tp'. An EmptyTree is returned if
12371117 * no manifest is found. todo: make this instantiate take type params as well?
@@ -1382,7 +1262,7 @@ trait Implicits {
13821262 pt.dealias match {
13831263 case TypeRef (_, SourceContextClass , _) =>
13841264 // construct new SourceContext instance
1385- result = sourceInfo()
1265+ result = sourceInfo(this , context0, tree )
13861266 // there is no existing SourceContext to chain with
13871267 updateSourceContext = false
13881268 case _ =>
@@ -1407,15 +1287,15 @@ trait Implicits {
14071287 new SearchResult (typedPos(position) {
14081288 // use sourceInfoFactoryCall to construct SourceContext
14091289 val factoryCall = if (receiver.isEmpty)
1410- sourceInfoFactoryCall(" apply" , Literal (Constant (fileName)), Literal (Constant (methodName.toString)), sourceInfoTree(contextInfoChain))
1290+ sourceInfoFactoryCall(this , tree, " apply" , Literal (Constant (fileName)), Literal (Constant (methodName.toString)), sourceInfoTree(contextInfoChain(context0, tree) ))
14111291 else
1412- sourceInfoFactoryCall(" apply" , Literal (Constant (fileName)), Literal (Constant (methodName.toString)), Literal (Constant (receiver.get.toString)), sourceInfoTree(contextInfoChain))
1292+ sourceInfoFactoryCall(this , tree, " apply" , Literal (Constant (fileName)), Literal (Constant (methodName.toString)), Literal (Constant (receiver.get.toString)), sourceInfoTree(contextInfoChain(context0, tree) ))
14131293 Apply (Select (result.tree, " update" ), List (factoryCall))
14141294 }, result.subst)
14151295 case TypeRef (_, SourceLocationClass , _) =>
14161296 val position = tree.pos.focus
14171297 new SearchResult (typedPos(position) {
1418- sourceLocation().tree
1298+ sourceLocation(this , tree ).tree
14191299 }, result.subst)
14201300 case _ => result
14211301 }
0 commit comments