Skip to content

Commit

Permalink
improvement: Eliminate empty positions in generated NIR (#3775)
Browse files Browse the repository at this point in the history
  • Loading branch information
WojciechMazur committed Feb 16, 2024
1 parent e24a9dc commit e40b901
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 120 deletions.
6 changes: 1 addition & 5 deletions nir/src/main/scala/scala/scalanative/nir/Defns.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,14 @@ import scala.scalanative.nir.Defn.Define
* their semantics (e.g., whether a method may be inlined). Attributes are also
* used to mark special-purpose definitions, such as stubs, proxies and FFIs.
*/
sealed abstract class Defn {
sealed abstract class Defn extends Positioned {

/** Returns the name of the definition. */
def name: Global

/** Returns the attributes of the definition. */
def attrs: Attrs

/** Returns the site in the program sources corresponding to the definition.
*/
def pos: SourcePosition

/** Returns a textual representation of `this`. */
final def show: String =
nir.Show(this)
Expand Down
3 changes: 1 addition & 2 deletions nir/src/main/scala/scala/scalanative/nir/Insts.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package scala.scalanative
package nir

sealed abstract class Inst {
sealed abstract class Inst extends Positioned {
final def show: String = nir.Show(this)
def pos: SourcePosition
}

object Inst {
Expand Down
21 changes: 21 additions & 0 deletions nir/src/main/scala/scala/scalanative/nir/Positioned.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package scala.scalanative.nir

trait Positioned {

/** Returns the site in the program sources corresponding to the definition.
*/
def pos: SourcePosition

if (Positioned.debugEmptyPositions && pos.isEmpty) {
System.err.println(s"\nFound empty position in $this, backtrace:")
new RuntimeException()
.getStackTrace()
.take(10)
.foreach(println)
}
}

object Positioned {
private final val debugEmptyPositions =
sys.props.contains("scalanative.debug.nir.positions")
}
4 changes: 4 additions & 0 deletions nir/src/main/scala/scala/scalanative/nir/SourcePosition.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ sealed case class NIRSource(directory: Path, path: Path) {
object NIRSource {
object None extends NIRSource(null, null) {
override def debugName: String = "<no-source>"
override def toString(): String = s"NIRSource($debugName)"
}
}

Expand All @@ -36,6 +37,9 @@ final case class SourcePosition(

def isEmpty: Boolean = this eq SourcePosition.NoPosition
def isDefined: Boolean = !isEmpty
def orElse(other: => SourcePosition): SourcePosition =
if (isEmpty) other
else this
}

object SourcePosition {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ trait NirGenExports[G <: nsc.Global with Singleton] {
val boxedParams = paramTypes
.zip(entryParams)
.map((buf.fromExtern _).tupled(_))
val argsp = boxedParams.map(ValTree(_))
val argsp = boxedParams.map(ValTree(_)(member.pos))
val res = buf.genApplyModuleMethod(owner, member, argsp)
val unboxedRes = buf.toExtern(externRetType, res)
buf.ret(unboxedRes)
Expand Down

0 comments on commit e40b901

Please sign in to comment.