@@ -18,6 +18,7 @@ import ast.Trees._
1818import parsing .JavaParsers .OutlineJavaParser
1919import parsing .Parsers .OutlineParser
2020import reporting .trace
21+ import ast .desugar .{ packageObjectName , hasTopLevelDef }
2122
2223object SymbolLoaders {
2324 import ast .untpd ._
@@ -31,7 +32,7 @@ object SymbolLoaders {
3132 owner : Symbol , member : Symbol ,
3233 completer : SymbolLoader , scope : Scope = EmptyScope )(implicit ctx : Context ): Symbol = {
3334 val comesFromScan =
34- completer.isInstanceOf [SourcefileLoader ] && ctx.settings.scansource.value
35+ completer.isInstanceOf [SourcefileLoader ]
3536 assert(comesFromScan || scope.lookup(member.name) == NoSymbol ,
3637 s " ${owner.fullName}. ${member.name} already has a symbol " )
3738 owner.asClass.enter(member, scope)
@@ -103,69 +104,62 @@ object SymbolLoaders {
103104 scope = scope)
104105 }
105106
106- /** If setting -scansource is set:
107- * Enter all toplevel classes and objects in file `src` into package `owner`, provided
108- * they are in the right package. Issue a warning if a class or object is in the wrong
109- * package, i.e. if the file path differs from the declared package clause.
110- * If -scansource is not set:
111- * Enter class and module with given `name` into scope of `owner`.
107+ /** Enter all toplevel classes and objects in file `src` into package `owner`, provided
108+ * they are in the right package. Issue a warning if a class or object is in the wrong
109+ * package, i.e. if the file path differs from the declared package clause.
112110 *
113111 * All entered symbols are given a source completer of `src` as info.
114112 */
115113 def enterToplevelsFromSource (
116114 owner : Symbol , name : PreName , src : AbstractFile ,
117- scope : Scope = EmptyScope )(implicit ctx : Context ): Unit = {
115+ scope : Scope = EmptyScope )(implicit ctx : Context ): Unit =
116+ if src.exists && ! src.isDirectory
117+ val completer = new SourcefileLoader (src)
118+ val filePath = owner.ownersIterator.takeWhile(! _.isRoot).map(_.name.toTermName).toList
119+
120+ def addPrefix (pid : RefTree , path : List [TermName ]): List [TermName ] = pid match {
121+ case Ident (name : TermName ) => name :: path
122+ case Select (qual : RefTree , name : TermName ) => name :: addPrefix(qual, path)
123+ case _ => path
124+ }
118125
119- val completer = new SourcefileLoader (src)
120- if (ctx.settings.scansource.value && ctx.run != null ) {
121- if (src.exists && ! src.isDirectory) {
122- val filePath = owner.ownersIterator.takeWhile(! _.isRoot).map(_.name.toTermName).toList
126+ def enterScanned (unit : CompilationUnit )(implicit ctx : Context ) = {
123127
124- def addPrefix (pid : RefTree , path : List [TermName ]): List [TermName ] = pid match {
125- case Ident (name : TermName ) => name :: path
126- case Select (qual : RefTree , name : TermName ) => name :: addPrefix(qual, path)
127- case _ => path
128+ def checkPathMatches (path : List [TermName ], what : String , tree : NameTree ): Boolean = {
129+ val ok = filePath == path
130+ if (! ok)
131+ ctx.warning(i """ $what ${tree.name} is in the wrong directory.
132+ |It was declared to be in package ${path.reverse.mkString(" ." )}
133+ |But it is found in directory ${filePath.reverse.mkString(File .separator)}""" ,
134+ tree.sourcePos.focus)
135+ ok
128136 }
129137
130- def enterScanned (unit : CompilationUnit )(implicit ctx : Context ) = {
131-
132- def checkPathMatches (path : List [TermName ], what : String , tree : MemberDef ): Boolean = {
133- val ok = filePath == path
134- if (! ok)
135- ctx.warning(i """ $what ${tree.name} is in the wrong directory.
136- |It was declared to be in package ${path.reverse.mkString(" ." )}
137- |But it is found in directory ${filePath.reverse.mkString(File .separator)}""" ,
138- tree.sourcePos)
139- ok
140- }
141-
142- def traverse (tree : Tree , path : List [TermName ]): Unit = tree match {
143- case PackageDef (pid, body) =>
144- val path1 = addPrefix(pid, path)
145- for (stat <- body) traverse(stat, path1)
146- case tree : TypeDef if tree.isClassDef =>
147- if (checkPathMatches(path, " class" , tree))
148- enterClassAndModule(owner, tree.name, completer, scope = scope)
149- // It might be a case class or implicit class,
150- // so enter class and module to be on the safe side
151- case tree : ModuleDef =>
152- if (checkPathMatches(path, " object" , tree))
153- enterModule(owner, tree.name, completer, scope = scope)
154- case _ =>
155- }
156-
157- traverse(
158- if (unit.isJava) new OutlineJavaParser (unit.source).parse()
159- else new OutlineParser (unit.source).parse(),
160- Nil )
138+ def traverse (tree : Tree , path : List [TermName ]): Unit = tree match {
139+ case tree @ PackageDef (pid, body) =>
140+ val path1 = addPrefix(pid, path)
141+ if hasTopLevelDef(tree) && checkPathMatches(path1, " package" , pid)
142+ enterModule(owner, packageObjectName(unit.source), completer, scope = scope)
143+ for (stat <- body) traverse(stat, path1)
144+ case tree : TypeDef if tree.isClassDef =>
145+ if (checkPathMatches(path, " class" , tree))
146+ // It might be a case class or implicit class,
147+ // so enter class and module to be on the safe side
148+ enterClassAndModule(owner, tree.name, completer, scope = scope)
149+ case tree : ModuleDef =>
150+ if (checkPathMatches(path, " object" , tree))
151+ enterModule(owner, tree.name, completer, scope = scope)
152+ case _ =>
161153 }
162154
163- val unit = CompilationUnit (ctx.getSource(src.path))
164- enterScanned(unit)(ctx.run.runContext.fresh.setCompilationUnit(unit))
155+ traverse(
156+ if (unit.isJava) new OutlineJavaParser (unit.source).parse()
157+ else new OutlineParser (unit.source).parse(),
158+ Nil )
165159 }
166- }
167- else enterClassAndModule(owner, name, completer, scope = scope )
168- }
160+
161+ val unit = CompilationUnit (ctx.getSource(src.path) )
162+ enterScanned(unit)(ctx.fresh.setCompilationUnit(unit))
169163
170164 /** The package objects of scala and scala.reflect should always
171165 * be loaded in binary if classfiles are available, even if sourcefiles
0 commit comments