Skip to content

Commit 815516b

Browse files
committed
Implement getClassIfDefined.
1 parent 5f31ce4 commit 815516b

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/dotty/tools/dotc/core/Denotations.scala

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,15 @@ object Denotations {
177177
}
178178

179179
/** Return symbol in this denotation that satisfies the given predicate.
180-
* Return a stubsymbol if denotation is a missing ref.
180+
* if generateStubs is specified, return a stubsymbol if denotation is a missing ref.
181181
* Throw a `TypeError` if predicate fails to disambiguate symbol or no alternative matches.
182182
*/
183-
def requiredSymbol(p: Symbol => Boolean, source: AbstractFile = null)(implicit ctx: Context): Symbol =
183+
def requiredSymbol(p: Symbol => Boolean, source: AbstractFile = null, generateStubs: Boolean = true)(implicit ctx: Context): Symbol =
184184
disambiguate(p) match {
185185
case MissingRef(ownerd, name) =>
186-
ctx.newStubSymbol(ownerd.symbol, name, source)
186+
if (generateStubs)
187+
ctx.newStubSymbol(ownerd.symbol, name, source)
188+
else NoSymbol
187189
case NoDenotation | _: NoQualifyingRef =>
188190
throw new TypeError(s"None of the alternatives of $this satisfies required predicate")
189191
case denot =>
@@ -874,8 +876,9 @@ object Denotations {
874876

875877
/** The current denotation of the static reference given by path,
876878
* or a MissingRef or NoQualifyingRef instance, if it does not exist.
879+
* if generateStubs is set, generates stubs for missing top-level symbols
877880
*/
878-
def staticRef(path: Name)(implicit ctx: Context): Denotation = {
881+
def staticRef(path: Name, generateStubs: Boolean = true)(implicit ctx: Context): Denotation = {
879882
def recur(path: Name, len: Int): Denotation = {
880883
val point = path.lastIndexOf('.', len - 1)
881884
val owner =
@@ -887,7 +890,9 @@ object Denotations {
887890
val result = owner.info.member(name)
888891
if (result ne NoDenotation) result
889892
else {
890-
val alt = missingHook(owner.symbol.moduleClass, name)
893+
val alt =
894+
if (generateStubs) missingHook(owner.symbol.moduleClass, name)
895+
else NoSymbol
891896
if (alt.exists) alt.denot
892897
else MissingRef(owner, name)
893898
}

src/dotty/tools/dotc/core/Symbols.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,12 @@ trait Symbols { this: Context =>
325325
def requiredClass(path: PreName): ClassSymbol =
326326
base.staticRef(path.toTypeName).requiredSymbol(_.isClass).asClass
327327

328+
/** Get ClassSymbol if class is either defined in current compilation run
329+
* or present on classpath.
330+
* Returns NoSymbol otherwise. */
331+
def getClassIfDefined(path: PreName): Symbol =
332+
base.staticRef(path.toTypeName, generateStubs = false).requiredSymbol(_.isClass, generateStubs = false)
333+
328334
def requiredModule(path: PreName): TermSymbol =
329335
base.staticRef(path.toTermName).requiredSymbol(_ is Module).asTerm
330336

0 commit comments

Comments
 (0)