@@ -18,6 +18,7 @@ import ast.Trees._
18
18
import parsing .JavaParsers .OutlineJavaParser
19
19
import parsing .Parsers .OutlineParser
20
20
import reporting .trace
21
+ import ast .desugar .{ packageObjectName , hasTopLevelDef }
21
22
22
23
object SymbolLoaders {
23
24
import ast .untpd ._
@@ -31,7 +32,7 @@ object SymbolLoaders {
31
32
owner : Symbol , member : Symbol ,
32
33
completer : SymbolLoader , scope : Scope = EmptyScope )(implicit ctx : Context ): Symbol = {
33
34
val comesFromScan =
34
- completer.isInstanceOf [SourcefileLoader ] && ctx.settings.scansource.value
35
+ completer.isInstanceOf [SourcefileLoader ]
35
36
assert(comesFromScan || scope.lookup(member.name) == NoSymbol ,
36
37
s " ${owner.fullName}. ${member.name} already has a symbol " )
37
38
owner.asClass.enter(member, scope)
@@ -103,69 +104,62 @@ object SymbolLoaders {
103
104
scope = scope)
104
105
}
105
106
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.
112
110
*
113
111
* All entered symbols are given a source completer of `src` as info.
114
112
*/
115
113
def enterToplevelsFromSource (
116
114
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
+ }
118
125
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 ) = {
123
127
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
128
136
}
129
137
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 _ =>
161
153
}
162
154
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 )
165
159
}
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))
169
163
170
164
/** The package objects of scala and scala.reflect should always
171
165
* be loaded in binary if classfiles are available, even if sourcefiles
0 commit comments