@@ -18,6 +18,7 @@ import config.Config
18
18
import reporting .diagnostic .Message
19
19
import reporting .diagnostic .messages .BadSymbolicReference
20
20
import reporting .trace
21
+ import collection .mutable
21
22
22
23
import scala .annotation .internal .sharable
23
24
@@ -518,12 +519,8 @@ object SymDenotations {
518
519
name == tpnme.REFINE_CLASS
519
520
520
521
/** Is this symbol a package object or its module class? */
521
- def isPackageObject (implicit ctx : Context ): Boolean = {
522
- val nameMatches =
523
- if (isType) name == tpnme.PACKAGE .moduleClassName
524
- else name == nme.PACKAGE
525
- nameMatches && (owner is Package ) && (this is Module )
526
- }
522
+ def isPackageObject (implicit ctx : Context ): Boolean =
523
+ name.isPackageObjectName && (owner is Package ) && (this is Module )
527
524
528
525
/** Is this symbol an abstract type? */
529
526
final def isAbstractType (implicit ctx : Context ): Boolean = this is DeferredType
@@ -1680,9 +1677,9 @@ object SymDenotations {
1680
1677
val denots1 = collect(denots, ps)
1681
1678
p.classSymbol.denot match {
1682
1679
case parentd : ClassDenotation =>
1683
- denots1 union
1680
+ denots1. union(
1684
1681
parentd.nonPrivateMembersNamed(name)
1685
- .mapInherited(ownDenots, denots1, thisType)
1682
+ .mapInherited(ownDenots, denots1, thisType) )
1686
1683
case _ =>
1687
1684
denots1
1688
1685
}
@@ -1940,17 +1937,39 @@ object SymDenotations {
1940
1937
initPrivateWithin : Symbol )
1941
1938
extends ClassDenotation (symbol, ownerIfExists, name, initFlags, initInfo, initPrivateWithin) {
1942
1939
1943
- private [this ] var packageObjCache : SymDenotation = _
1944
- private [this ] var packageObjRunId : RunId = NoRunId
1945
-
1946
- /** The package object in this class, of one exists */
1947
- def packageObj (implicit ctx : Context ): SymDenotation = {
1948
- if (packageObjRunId != ctx.runId) {
1949
- packageObjRunId = ctx.runId
1950
- packageObjCache = NoDenotation // break cycle in case we are looking for package object itself
1951
- packageObjCache = findMember(nme.PACKAGE , thisType, EmptyFlagConjunction , EmptyFlags ).asSymDenotation
1940
+ private [this ] var packageObjsCache : List [ClassDenotation ] = _
1941
+ private [this ] var packageObjsRunId : RunId = NoRunId
1942
+
1943
+ /** The package objects in this class */
1944
+ def packageObjs (implicit ctx : Context ): List [ClassDenotation ] = {
1945
+ if (packageObjsRunId != ctx.runId) {
1946
+ packageObjsRunId = ctx.runId
1947
+ packageObjsCache = Nil // break cycle in case we are looking for package object itself
1948
+ packageObjsCache = {
1949
+ val pkgObjBuf = new mutable.ListBuffer [ClassDenotation ]
1950
+ for (sym <- info.decls) { // don't use filter, since that loads classes with `$`s in their name
1951
+ val denot = sym.lastKnownDenotation // don't use `sym.denot`, as this brings forward classes too early
1952
+ if (denot.isType && denot.name.isPackageObjectName)
1953
+ pkgObjBuf += sym.asClass.classDenot
1954
+ }
1955
+ pkgObjBuf.toList
1956
+ }
1952
1957
}
1953
- packageObjCache
1958
+ packageObjsCache
1959
+ }
1960
+
1961
+ /** The package object (as a term symbol) in this package that might contain
1962
+ * `sym` as a member.
1963
+ */
1964
+ def packageObjFor (sym : Symbol )(implicit ctx : Context ): Symbol = {
1965
+ val owner = sym.maybeOwner
1966
+ if (owner.is(Package )) NoSymbol
1967
+ else if (owner.isPackageObject) owner.sourceModule
1968
+ else // owner could be class inherited by package object (until package object inheritance is removed)
1969
+ packageObjs.find(_.name == packageTypeName) match {
1970
+ case Some (pobj) => pobj.sourceModule
1971
+ case _ => NoSymbol
1972
+ }
1954
1973
}
1955
1974
1956
1975
/** Looks in both the package object and the package for members. The precise algorithm
@@ -1966,28 +1985,31 @@ object SymDenotations {
1966
1985
* object that hides a class or object in the scala package of the same name, because
1967
1986
* the behavior would then be unintuitive for such members.
1968
1987
*/
1969
- override def computeNPMembersNamed (name : Name )(implicit ctx : Context ): PreDenotation =
1970
- packageObj.moduleClass.denot match {
1971
- case pcls : ClassDenotation if ! pcls.isCompleting =>
1972
- if (symbol eq defn.ScalaPackageClass ) {
1973
- val denots = super .computeNPMembersNamed(name)
1974
- if (denots.exists) denots else pcls.computeNPMembersNamed(name)
1975
- }
1976
- else {
1977
- val denots = pcls.computeNPMembersNamed(name)
1978
- if (denots.exists) denots else super .computeNPMembersNamed(name)
1979
- }
1980
- case _ =>
1981
- super .computeNPMembersNamed(name)
1988
+ override def computeNPMembersNamed (name : Name )(implicit ctx : Context ): PreDenotation = {
1989
+ def recur (pobjs : List [ClassDenotation ], acc : PreDenotation ): PreDenotation = pobjs match {
1990
+ case pcls :: pobjs1 =>
1991
+ if (pcls.isCompleting) recur(pobjs1, acc)
1992
+ else recur(pobjs1, acc.union(pcls.computeNPMembersNamed(name)))
1993
+ case nil =>
1994
+ if (acc.exists) acc else super .computeNPMembersNamed(name)
1982
1995
}
1996
+ if (symbol `eq` defn.ScalaPackageClass ) {
1997
+ val denots = super .computeNPMembersNamed(name)
1998
+ if (denots.exists) denots
1999
+ else recur(packageObjs, NoDenotation )
2000
+ }
2001
+ else recur(packageObjs, NoDenotation )
2002
+ }
1983
2003
1984
2004
/** The union of the member names of the package and the package object */
1985
2005
override def memberNames (keepOnly : NameFilter )(implicit onBehalf : MemberNames , ctx : Context ): Set [Name ] = {
1986
- val ownNames = super .memberNames(keepOnly)
1987
- packageObj.moduleClass.denot match {
1988
- case pcls : ClassDenotation => ownNames union pcls.memberNames(keepOnly)
1989
- case _ => ownNames
2006
+ def recur (pobjs : List [ClassDenotation ], acc : Set [Name ]): Set [Name ] = pobjs match {
2007
+ case pcls :: pobjs1 =>
2008
+ recur(pobjs1, acc.union(pcls.memberNames(keepOnly)))
2009
+ case nil =>
2010
+ acc
1990
2011
}
2012
+ recur(packageObjs, super .memberNames(keepOnly))
1991
2013
}
1992
2014
1993
2015
/** If another symbol with the same name is entered, unlink it,
@@ -1999,7 +2021,7 @@ object SymDenotations {
1999
2021
if (entry != null ) {
2000
2022
if (entry.sym == sym) return false
2001
2023
mscope.unlink(entry)
2002
- if (sym.name == nme. PACKAGE ) packageObjRunId = NoRunId
2024
+ if (sym.name.isPackageObjectName) packageObjsRunId = NoRunId
2003
2025
}
2004
2026
true
2005
2027
}
@@ -2349,5 +2371,7 @@ object SymDenotations {
2349
2371
def baseClasses : List [ClassSymbol ] = classes
2350
2372
}
2351
2373
2374
+ private val packageTypeName = ModuleClassName (nme.PACKAGE ).toTypeName
2375
+
2352
2376
@ sharable private [this ] var indent = 0 // for completions printing
2353
2377
}
0 commit comments