Skip to content

Commit

Permalink
Create Scala 2 veneer for SourceInfo
Browse files Browse the repository at this point in the history
The SourceInfo hierarchy is sealed, and very basic, so most of the code
is just moved to src/main/scala-2.
  • Loading branch information
jackkoenig committed Sep 18, 2024
1 parent 34cb0ea commit 3fda4e5
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 50 deletions.
59 changes: 59 additions & 0 deletions core/src/main/scala-2/chisel3/experimental/SourceInfo.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// SPDX-License-Identifier: Apache-2.0

// This file contains macros for adding source locators at the point of invocation.
//
// This is not part of coreMacros to disallow this macro from being implicitly invoked in Chisel
// frontend (and generating source locators in Chisel core), which is almost certainly a bug.
//
// Note: While these functions and definitions are not private (macros can't be
// private), these are NOT meant to be part of the public API (yet) and no
// forward compatibility guarantees are made.
// A future revision may stabilize the source locator API to allow library
// writers to append source locator information at the point of a library
// function invocation.

package chisel3.experimental

import scala.language.experimental.macros
import scala.reflect.macros.blackbox.Context
import chisel3.internal.sourceinfo.SourceInfoMacro

/** Abstract base class for generalized source information.
*/
sealed trait SourceInfo {

/** A prettier toString
*
* Make a useful message if SourceInfo is available, nothing otherwise
*/
def makeMessage(f: String => String = x => x): String

/** The filename for the originating source file, if known */
def filenameOption: Option[String]
}

sealed trait NoSourceInfo extends SourceInfo {
def makeMessage(f: String => String = x => x): String = ""
def filenameOption: Option[String] = None
}

/** For when source info can't be generated because of a technical limitation, like for Reg because
* Scala macros don't support named or default arguments.
*/
case object UnlocatableSourceInfo extends NoSourceInfo

/** For when source info isn't generated because the function is deprecated and we're lazy.
*/
case object DeprecatedSourceInfo extends NoSourceInfo

/** For FIRRTL lines from a Scala source line.
*
* @note A column == 0 indicates no column
*/
case class SourceLine(filename: String, line: Int, col: Int) extends SourceInfo with SourceLineImpl {
def makeMessage(f: String => String = x => x): String = _makeMessageImpl(f)
}

object SourceInfo extends ObjectSourceInfoImpl {
implicit def materialize: SourceInfo = macro SourceInfoMacro.generate_source_info
}
56 changes: 6 additions & 50 deletions core/src/main/scala/chisel3/experimental/SourceInfoImpl.scala
Original file line number Diff line number Diff line change
@@ -1,57 +1,14 @@
// SPDX-License-Identifier: Apache-2.0

// This file contains macros for adding source locators at the point of invocation.
//
// This is not part of coreMacros to disallow this macro from being implicitly invoked in Chisel
// frontend (and generating source locators in Chisel core), which is almost certainly a bug.
//
// Note: While these functions and definitions are not private (macros can't be
// private), these are NOT meant to be part of the public API (yet) and no
// forward compatibility guarantees are made.
// A future revision may stabilize the source locator API to allow library
// writers to append source locator information at the point of a library
// function invocation.

package chisel3.experimental

import scala.language.experimental.macros
import scala.reflect.macros.blackbox.Context
import chisel3.internal.sourceinfo.SourceInfoMacro

/** Abstract base class for generalized source information.
*/
sealed trait SourceInfo {

/** A prettier toString
*
* Make a useful message if SourceInfo is available, nothing otherwise
*/
def makeMessage(f: String => String = x => x): String

/** The filename for the originating source file, if known */
def filenameOption: Option[String]
}

sealed trait NoSourceInfo extends SourceInfo {
def makeMessage(f: String => String = x => x): String = ""
def filenameOption: Option[String] = None
}

/** For when source info can't be generated because of a technical limitation, like for Reg because
* Scala macros don't support named or default arguments.
*/
case object UnlocatableSourceInfo extends NoSourceInfo
private[chisel3] trait SourceLineImpl {
def filename: String
def line: Int
def col: Int

/** For when source info isn't generated because the function is deprecated and we're lazy.
*/
case object DeprecatedSourceInfo extends NoSourceInfo
protected def _makeMessageImpl(f: String => String = x => x): String = f(s"@[${this.prettyPrint}]")

/** For FIRRTL lines from a Scala source line.
*
* @note A column == 0 indicates no column
*/
case class SourceLine(filename: String, line: Int, col: Int) extends SourceInfo {
def makeMessage(f: String => String = x => x): String = f(s"@[${this.prettyPrint}]")
def filenameOption: Option[String] = Some(filename)

private def prettyPrint: String = {
Expand All @@ -64,8 +21,7 @@ case class SourceLine(filename: String, line: Int, col: Int) extends SourceInfo
}
}

object SourceInfo {
implicit def materialize: SourceInfo = macro SourceInfoMacro.generate_source_info
private[chisel3] trait ObjectSourceInfoImpl {

/** Returns the best guess at the first stack frame that belongs to user code.
*/
Expand Down

0 comments on commit 3fda4e5

Please sign in to comment.