Skip to content

Commit

Permalink
Add others
Browse files Browse the repository at this point in the history
  • Loading branch information
noti0na1 committed Jan 20, 2022
1 parent 5b58e47 commit 147b2c5
Show file tree
Hide file tree
Showing 14 changed files with 100 additions and 75 deletions.
6 changes: 4 additions & 2 deletions compiler/src/dotty/tools/dotc/core/Phases.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package dotty.tools
package dotc
package core

import scala.language.{unsafeNulls => _}

import Periods._
import Contexts._
import dotty.tools.backend.jvm.GenBCode
Expand All @@ -13,7 +15,7 @@ import scala.collection.mutable.ListBuffer
import dotty.tools.dotc.transform.MegaPhase._
import dotty.tools.dotc.transform._
import Periods._
import parsing.{ Parser}
import parsing.Parser
import typer.{TyperPhase, RefChecks}
import typer.ImportInfo.withRootImports
import ast.tpd
Expand Down Expand Up @@ -341,7 +343,7 @@ object Phases {
def initContext(ctx: FreshContext): Unit = ()

private var myPeriod: Period = Periods.InvalidPeriod
private var myBase: ContextBase = null
private var myBase: ContextBase = _
private var myErasedTypes = false
private var myFlatClasses = false
private var myRefChecked = false
Expand Down
116 changes: 59 additions & 57 deletions compiler/src/dotty/tools/dotc/core/Scopes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package dotty.tools
package dotc
package core

import scala.language.{unsafeNulls => _}

import Symbols._
import Types.{TermRef, NoPrefix}
import Flags._
Expand Down Expand Up @@ -50,11 +52,11 @@ object Scopes {

/** the next entry in the hash bucket
*/
var tail: ScopeEntry = null
var tail: ScopeEntry | Null = null

/** the preceding entry in this scope
*/
var prev: ScopeEntry = null
var prev: ScopeEntry | Null = null

override def toString: String = sym.toString
}
Expand Down Expand Up @@ -88,7 +90,7 @@ object Scopes {
def iterator(using Context): Iterator[Symbol] = toList.iterator

/** Is the scope empty? */
def isEmpty: Boolean = lastEntry eq null
def isEmpty: Boolean = lastEntry == null

/** Applies a function f to all Symbols of this Scope. */
def foreach[U](f: Symbol => U)(using Context): Unit = toList.foreach(f)
Expand All @@ -98,7 +100,7 @@ object Scopes {
ensureComplete()
var syms: List[Symbol] = Nil
var e = lastEntry
while ((e ne null) && e.owner == this) {
while ((e != null) && e.owner == this) {
val sym = e.sym
if (p(sym)) syms = sym :: syms
e = e.prev
Expand All @@ -122,20 +124,20 @@ object Scopes {
def lookupEntry(name: Name)(using Context): ScopeEntry | Null

/** Lookup next entry with same name as this one */
def lookupNextEntry(entry: ScopeEntry)(using Context): ScopeEntry
def lookupNextEntry(entry: ScopeEntry)(using Context): ScopeEntry | Null

/** Lookup a symbol */
final def lookup(name: Name)(using Context): Symbol = {
val e = lookupEntry(name)
if (e eq null) NoSymbol else e.sym
if (e == null) NoSymbol else e.sym
}

/** Returns an iterator yielding every symbol with given name in this scope.
*/
final def lookupAll(name: Name)(using Context): Iterator[Symbol] = new Iterator[Symbol] {
var e = lookupEntry(name)
def hasNext: Boolean = e ne null
def next(): Symbol = { val r = e.sym; e = lookupNextEntry(e); r }
def hasNext: Boolean = e != null
def next(): Symbol = { val r = e.uncheckedNN.sym; e = lookupNextEntry(e.uncheckedNN); r }
}

/** Does this scope contain a reference to `sym` when looking up `name`? */
Expand Down Expand Up @@ -163,13 +165,13 @@ object Scopes {
* a copy with the matching symbols.
*/
final def filteredScope(p: Symbol => Boolean)(using Context): Scope = {
var result: MutableScope = null
var result: MutableScope | Null = null
for (sym <- iterator)
if (!p(sym)) {
if (result == null) result = cloneScope
result.unlink(sym)
result.nn.unlink(sym)
}
if (result == null) this else result
if (result == null) this else result.nn
}

def implicitDecls(using Context): List[TermRef] = Nil
Expand All @@ -193,7 +195,7 @@ object Scopes {
* This is necessary because when run from reflection every scope needs to have a
* SynchronizedScope as mixin.
*/
class MutableScope protected[Scopes](initElems: ScopeEntry, initSize: Int, val nestingLevel: Int)
class MutableScope protected[Scopes](initElems: ScopeEntry | Null, initSize: Int, val nestingLevel: Int)
extends Scope {

/** Scope shares elements with `base` */
Expand All @@ -213,14 +215,14 @@ object Scopes {

/** the hash table
*/
private var hashTable: Array[ScopeEntry] = null
private var hashTable: Array[ScopeEntry | Null] | Null = null

/** a cache for all elements, to be used by symbol iterator.
*/
private var elemsCache: List[Symbol] = null
private var elemsCache: List[Symbol] | Null = null

/** The synthesizer to be used, or `null` if no synthesis is done on this scope */
private var synthesize: SymbolSynthesizer = null
private var synthesize: SymbolSynthesizer | Null = null

/** Use specified synthesize for this scope */
def useSynthesizer(s: SymbolSynthesizer): Unit = synthesize = s
Expand All @@ -232,7 +234,7 @@ object Scopes {
def cloneScope(using Context): MutableScope = {
val entries = new mutable.ArrayBuffer[ScopeEntry]
var e = lastEntry
while ((e ne null) && e.owner == this) {
while ((e != null) && e.owner == this) {
entries += e
e = e.prev
}
Expand All @@ -247,21 +249,21 @@ object Scopes {

/** create and enter a scope entry with given name and symbol */
protected def newScopeEntry(name: Name, sym: Symbol)(using Context): ScopeEntry = {
ensureCapacity(if (hashTable ne null) hashTable.length else MinHashedScopeSize)
ensureCapacity(if (hashTable != null) hashTable.uncheckedNN.length else MinHashedScopeSize)
val e = new ScopeEntry(name, sym, this)
e.prev = lastEntry
lastEntry = e
if (hashTable ne null) enterInHash(e)
if (hashTable != null) enterInHash(e)
size += 1
elemsCache = null
e
}

private def enterInHash(e: ScopeEntry)(using Context): Unit = {
val idx = e.name.hashCode & (hashTable.length - 1)
e.tail = hashTable(idx)
val idx = e.name.hashCode & (hashTable.nn.length - 1)
e.tail = hashTable.nn(idx)
assert(e.tail != e)
hashTable(idx) = e
hashTable.nn(idx) = e
}

/** enter a symbol in this scope. */
Expand Down Expand Up @@ -289,21 +291,21 @@ object Scopes {
private def createHash(tableSize: Int)(using Context): Unit =
if (size > tableSize * FillFactor) createHash(tableSize * 2)
else {
hashTable = new Array[ScopeEntry](tableSize)
hashTable = new Array[ScopeEntry | Null](tableSize)
enterAllInHash(lastEntry)
// checkConsistent() // DEBUG
}

private def enterAllInHash(e: ScopeEntry, n: Int = 0)(using Context): Unit =
if (e ne null)
private def enterAllInHash(e: ScopeEntry | Null, n: Int = 0)(using Context): Unit =
if (e != null)
if (n < MaxRecursions) {
enterAllInHash(e.prev, n + 1)
enterInHash(e)
}
else {
var entries: List[ScopeEntry] = List()
var ee = e
while (ee ne null) {
var ee: ScopeEntry | Null = e
while (ee != null) {
entries = ee :: entries
ee = ee.prev
}
Expand All @@ -315,18 +317,18 @@ object Scopes {
if (lastEntry == e)
lastEntry = e.prev
else {
var e1 = lastEntry
while (e1.prev != e) e1 = e1.prev
var e1 = lastEntry.nn
while (e1.prev != e) e1 = e1.prev.nn
e1.prev = e.prev
}
if (hashTable ne null) {
val index = e.name.hashCode & (hashTable.length - 1)
var e1 = hashTable(index)
if (hashTable != null) {
val index = e.name.hashCode & (hashTable.nn.length - 1)
var e1 = hashTable.nn(index)
if (e1 == e)
hashTable(index) = e.tail
hashTable.nn(index) = e.tail
else {
while (e1.tail != e) e1 = e1.tail
e1.tail = e.tail
while (e1.nn.tail != e) e1 = e1.nn.tail
e1.nn.tail = e.tail
}
}
elemsCache = null
Expand All @@ -340,7 +342,7 @@ object Scopes {
/** remove symbol from this scope if it is present under the given name */
final def unlink(sym: Symbol, name: Name)(using Context): Unit = {
var e = lookupEntry(name)
while (e ne null) {
while (e != null) {
if (e.sym == sym) unlink(e)
e = lookupNextEntry(e)
}
Expand All @@ -352,7 +354,7 @@ object Scopes {
final def replace(prev: Symbol, replacement: Symbol)(using Context): Unit = {
require(prev.name == replacement.name)
var e = lookupEntry(prev.name)
while (e ne null) {
while (e != null) {
if (e.sym == prev) e.sym = replacement
e = lookupNextEntry(e)
}
Expand All @@ -362,55 +364,55 @@ object Scopes {
/** Lookup a symbol entry matching given name.
*/
override def lookupEntry(name: Name)(using Context): ScopeEntry | Null = {
var e: ScopeEntry = null
if (hashTable ne null) {
e = hashTable(name.hashCode & (hashTable.length - 1))
while ((e ne null) && e.name != name)
var e: ScopeEntry | Null = null
if (hashTable != null) {
e = hashTable.nn(name.hashCode & (hashTable.nn.length - 1))
while ((e != null) && e.name != name)
e = e.tail
}
else {
e = lastEntry
while ((e ne null) && e.name != name)
while ((e != null) && e.name != name)
e = e.prev
}
if ((e eq null) && (synthesize != null)) {
val sym = synthesize(name)
if ((e == null) && (synthesize != null)) {
val sym = synthesize.uncheckedNN(name)
if (sym.exists) newScopeEntry(sym.name, sym) else e
}
else e
}

/** lookup next entry with same name as this one */
override final def lookupNextEntry(entry: ScopeEntry)(using Context): ScopeEntry = {
var e = entry
if (hashTable ne null)
while ({ e = e.tail ; (e ne null) && e.name != entry.name }) ()
override final def lookupNextEntry(entry: ScopeEntry)(using Context): ScopeEntry | Null = {
var e: ScopeEntry | Null = entry
if (hashTable != null)
while ({ e = e.nn.tail ; (e != null) && e.uncheckedNN.name != entry.name }) ()
else
while ({ e = e.prev ; (e ne null) && e.name != entry.name }) ()
while ({ e = e.nn.prev ; (e != null) && e.uncheckedNN.name != entry.name }) ()
e
}

/** Returns all symbols as a list in the order they were entered in this scope.
* Does _not_ include the elements of inherited scopes.
*/
override final def toList(using Context): List[Symbol] = {
if (elemsCache eq null) {
if (elemsCache == null) {
ensureComplete()
elemsCache = Nil
var e = lastEntry
while ((e ne null) && e.owner == this) {
elemsCache = e.sym :: elemsCache
while ((e != null) && e.owner == this) {
elemsCache = e.sym :: elemsCache.nn
e = e.prev
}
}
elemsCache
elemsCache.nn
}

override def implicitDecls(using Context): List[TermRef] = {
ensureComplete()
var irefs = new mutable.ListBuffer[TermRef]
var e = lastEntry
while (e ne null) {
while (e != null) {
if (e.sym.isOneOf(GivenOrImplicitVal)) {
val d = e.sym.denot
irefs += TermRef(NoPrefix, d.symbol.asTerm).withDenot(d)
Expand All @@ -433,7 +435,7 @@ object Scopes {
while (e != null) {
var e1 = lookupEntry(e.name)
while (e1 != e && e1 != null) e1 = lookupNextEntry(e1)
assert(e1 == e, s"PANIC: Entry ${e.name} is badly linked")
assert(e1 == e, s"PANIC: Entry ${e.nn.name} is badly linked")
e = e.prev
}
}
Expand Down Expand Up @@ -463,12 +465,12 @@ object Scopes {
/** The empty scope (immutable).
*/
object EmptyScope extends Scope {
override private[dotc] def lastEntry: ScopeEntry = null
override private[dotc] def lastEntry: ScopeEntry | Null = null
override def size: Int = 0
override def nestingLevel: Int = 0
override def toList(using Context): List[Symbol] = Nil
override def cloneScope(using Context): MutableScope = unsupported("cloneScope")
override def lookupEntry(name: Name)(using Context): ScopeEntry = null
override def lookupNextEntry(entry: ScopeEntry)(using Context): ScopeEntry = null
override def lookupEntry(name: Name)(using Context): ScopeEntry | Null = null
override def lookupNextEntry(entry: ScopeEntry)(using Context): ScopeEntry | Null = null
}
}
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/rewrites/Rewrites.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dotty.tools.dotc
package rewrites

import scala.language.{unsafeNulls => _}

import util.{SourceFile, Spans}
import Spans.Span
import core.Contexts._
Expand Down
Loading

0 comments on commit 147b2c5

Please sign in to comment.