|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package chisel3.internal.plugin |
| 4 | + |
| 5 | +import dotty.tools.dotc.* |
| 6 | +import dotty.tools.dotc.ast.tpd |
| 7 | +import dotty.tools.dotc.ast.tpd.* |
| 8 | +import dotty.tools.dotc.ast.Trees |
| 9 | +import dotty.tools.dotc.core.Contexts.* |
| 10 | +import dotty.tools.dotc.core.Symbols.* |
| 11 | +import dotty.tools.dotc.core.Names.TermName |
| 12 | +import dotty.tools.dotc.core.StdNames.* |
| 13 | +import dotty.tools.dotc.core.Constants.Constant |
| 14 | +import dotty.tools.dotc.typer.TyperPhase |
| 15 | +import dotty.tools.dotc.plugins.{PluginPhase, StandardPlugin} |
| 16 | +import dotty.tools.dotc.transform.{Erasure, Pickler, PostTyper} |
| 17 | +import dotty.tools.dotc.core.Types.* |
| 18 | +import dotty.tools.dotc.core.Flags |
| 19 | +import dotty.tools.dotc.util.SourcePosition |
| 20 | + |
| 21 | +import scala.collection.mutable |
| 22 | + |
| 23 | +class ChiselComponent extends StandardPlugin { |
| 24 | + val name: String = "ChiselComponent" |
| 25 | + override val description: String = "Chisel's type-specific naming" |
| 26 | + |
| 27 | + override def init(options: List[String]): List[PluginPhase] = { |
| 28 | + (new ChiselComponentPhase) :: Nil |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +class ChiselComponentPhase extends PluginPhase { |
| 33 | + val phaseName: String = "chiselComponentPhase" |
| 34 | + override val runsAfter = Set(TyperPhase.name) |
| 35 | + |
| 36 | + private var dataTpe: TypeRef = _ |
| 37 | + private var memBaseTpe: TypeRef = _ |
| 38 | + private var verifTpe: TypeRef = _ |
| 39 | + private var dynObjTpe: TypeRef = _ |
| 40 | + private var affectsTpe: TypeRef = _ |
| 41 | + private var moduleTpe: TypeRef = _ |
| 42 | + private var instTpe: TypeRef = _ |
| 43 | + private var prefixTpe: TypeRef = _ |
| 44 | + private var bundleTpe: TypeRef = _ |
| 45 | + |
| 46 | + private var pluginModule: TermSymbol = _ |
| 47 | + private var autoNameMethod: TermSymbol = _ |
| 48 | + private var withNames: TermSymbol = _ |
| 49 | + private var prefixModule: TermSymbol = _ |
| 50 | + private var prefixApplyMethod: TermSymbol = _ |
| 51 | + |
| 52 | + override def prepareForUnit(tree: Tree)(using ctx: Context): Context = { |
| 53 | + dataTpe = requiredClassRef("chisel3.Data") |
| 54 | + memBaseTpe = requiredClassRef("chisel3.MemBase") |
| 55 | + verifTpe = requiredClassRef("chisel3.VerificationStatement") |
| 56 | + dynObjTpe = requiredClassRef("chisel3.Disable") |
| 57 | + affectsTpe = requiredClassRef("chisel3.experimental.AffectsChiselName") |
| 58 | + moduleTpe = requiredClassRef("chisel3.experimental.BaseModule") |
| 59 | + instTpe = requiredClassRef("chisel3.experimental.hierarchy.Instance") |
| 60 | + prefixTpe = requiredClassRef("chisel3.experimental.AffectsChiselPrefix") |
| 61 | + bundleTpe = requiredClassRef("chisel3.Bundle") |
| 62 | + |
| 63 | + pluginModule = requiredModule("chisel3") |
| 64 | + autoNameMethod = pluginModule.requiredMethod("withName") |
| 65 | + withNames = pluginModule.requiredMethod("withNames") |
| 66 | + prefixModule = requiredModule("chisel3.experimental.prefix") |
| 67 | + prefixApplyMethod = prefixModule.requiredMethod("applyString") |
| 68 | + ctx |
| 69 | + } |
| 70 | + |
| 71 | + override def transformValDef(tree: tpd.ValDef)(using Context): tpd.Tree = { |
| 72 | + val sym = tree.symbol |
| 73 | + val tpt = tree.tpt.tpe |
| 74 | + val name = sym.name |
| 75 | + val rhs = tree.rhs |
| 76 | + |
| 77 | + val valName: String = tree.name.show |
| 78 | + val nameLiteral = Literal(Constant(valName)) |
| 79 | + val prefixLiteral = |
| 80 | + if (valName.head == '_') |
| 81 | + Literal(Constant(valName.tail)) |
| 82 | + else Literal(Constant(valName)) |
| 83 | + |
| 84 | + val isData = ChiselTypeHelpers.isData(tpt) |
| 85 | + val isBoxedData = ChiselTypeHelpers.isBoxedData(tpt) |
| 86 | + val isNamedComp = isData || isBoxedData || ChiselTypeHelpers.isNamed(tpt) |
| 87 | + val isPrefixed = isNamedComp || ChiselTypeHelpers.isPrefixed(tpt) |
| 88 | + |
| 89 | + if (!ChiselTypeHelpers.okVal(tree)) tree // Cannot name this, so skip |
| 90 | + else if (isData && ChiselTypeHelpers.inBundle(tree)) { // Data in a bundle |
| 91 | + val newRHS = transformFollowing(rhs) |
| 92 | + val named = |
| 93 | + tpd |
| 94 | + .ref(pluginModule) |
| 95 | + .select(autoNameMethod) |
| 96 | + .appliedToType(tpt) |
| 97 | + .appliedTo(nameLiteral) |
| 98 | + .appliedTo(newRHS) |
| 99 | + cpy.ValDef(tree)(rhs = named) |
| 100 | + } else if (isData || isBoxedData || isPrefixed) { // All Data subtype instances |
| 101 | + val newRHS = transformFollowing(rhs) |
| 102 | + val prefixed = |
| 103 | + tpd |
| 104 | + .ref(prefixModule) |
| 105 | + .select(prefixApplyMethod) |
| 106 | + .appliedToType(tpt) |
| 107 | + .appliedTo(prefixLiteral) |
| 108 | + .appliedTo(newRHS) |
| 109 | + val named = if (isNamedComp) { |
| 110 | + tpd |
| 111 | + .ref(pluginModule) |
| 112 | + .select(autoNameMethod) |
| 113 | + .appliedToType(tpt) |
| 114 | + .appliedTo(nameLiteral) |
| 115 | + .appliedTo(prefixed) |
| 116 | + } else prefixed |
| 117 | + cpy.ValDef(tree)(rhs = named) |
| 118 | + } else if ( // Modules or instances |
| 119 | + ChiselTypeHelpers.isModule(tpt) || |
| 120 | + ChiselTypeHelpers.isInstance(tpt) |
| 121 | + ) { |
| 122 | + val newRHS = transformFollowing(rhs) |
| 123 | + val named = |
| 124 | + tpd |
| 125 | + .ref(pluginModule) |
| 126 | + .select(autoNameMethod) |
| 127 | + .appliedToType(tpt) |
| 128 | + .appliedTo(nameLiteral) |
| 129 | + .appliedTo(newRHS) |
| 130 | + cpy.ValDef(tree)(rhs = named) |
| 131 | + } else { |
| 132 | + super.transformValDef(tree) |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments