Skip to content

Commit

Permalink
Merge pull request #43 from frwang96/feature/source-sets
Browse files Browse the repository at this point in the history
Source sets
  • Loading branch information
frwang96 authored Nov 4, 2021
2 parents 8ece43c + fe8b69a commit 947feae
Show file tree
Hide file tree
Showing 23 changed files with 137 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class EModule(
override val ports: List<EPort>,
override var declarations: ArrayList<EDeclaration>,
val isSynthesisTop: Boolean,
val isSimulationTop: Boolean
val isSimulationTop: Boolean,
val isExtern: Boolean
) : EAbstractContainerComponent() {

init {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ object CasterStage : ProjectStage() {
override fun process(projectContext: ProjectContext) {
val files = HashMap<String, ArrayList<EFile>>()

projectContext.ktFiles.forEach { file ->
projectContext.getKtFiles().forEach { file ->
val location = file.location()
val packageName = file.packageFqName.asString()
val inputPath = Platform.getPathFromString(file.virtualFilePath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ object DeclarationCastIndexerStage : ProjectStage() {
override fun process(projectContext: ProjectContext) {
val castContext = CastContext(projectContext.bindingContext)
val declarationCastIndexerVisitor = DeclarationCastIndexerVisitor(castContext)
projectContext.ktFiles.forEach { it.accept(declarationCastIndexerVisitor) }
projectContext.getKtFiles().forEach { it.accept(declarationCastIndexerVisitor) }
projectContext.castContext = castContext
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ object ImportDirectiveCheckerStage : ProjectStage() {
override fun process(projectContext: ProjectContext) {
val packageNames = HashSet<String>()
packageNames.add(CorePackage.VK.name)
projectContext.ktFiles.forEach {
projectContext.getKtFiles().forEach {
val packageName = it.packageFqName.asString()
if (packageName == "")
Messages.PACKAGE_NAME_ROOT.on(it)
Expand All @@ -40,7 +40,7 @@ object ImportDirectiveCheckerStage : ProjectStage() {
}

val importDirectiveCheckerVisitor = ImportDirectiveCheckerVisitor(packageNames)
projectContext.ktFiles.forEach { it.accept(importDirectiveCheckerVisitor) }
projectContext.getKtFiles().forEach { it.accept(importDirectiveCheckerVisitor) }
}

class ImportDirectiveCheckerVisitor(private val packageNames: Set<String>) : KtTreeVisitorVoid() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ object UnsupportedElementCheckerStage : ProjectStage() {
override val checkNormalization = false

override fun process(projectContext: ProjectContext) {
projectContext.ktFiles.forEach { it.accept(UnsupportedElementCheckerVisitor) }
projectContext.getKtFiles().forEach { it.accept(UnsupportedElementCheckerVisitor) }
}

object UnsupportedElementCheckerVisitor : KtTreeVisitorVoid() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ object UnsupportedModifierCheckerStage : ProjectStage() {
override val checkNormalization = false

override fun process(projectContext: ProjectContext) {
projectContext.ktFiles.forEach { it.accept(UnsupportedModifierCheckerVisitor) }
projectContext.getKtFiles().forEach { it.accept(UnsupportedModifierCheckerVisitor) }
}

object UnsupportedModifierCheckerVisitor : KtTreeVisitorVoid() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ object KotlinCompilerAnalyzerStage : ProjectStage() {
override fun process(projectContext: ProjectContext) {
val environment = projectContext.kotlinCoreEnvironment
val analyzer = AnalyzerWithCompilerReport(environment.configuration)
analyzer.analyzeAndReport(projectContext.ktFiles) {
val ktFiles = projectContext.getKtFiles()
analyzer.analyzeAndReport(ktFiles) {
TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(
environment.project,
projectContext.ktFiles,
ktFiles,
NoScopeRecordCliBindingTrace(),
environment.configuration,
environment::createPackagePartProvider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ object KotlinCompilerParserStage : ProjectStage() {

override fun process(projectContext: ProjectContext) {
val psiFileFactory = KtPsiFactory(projectContext.kotlinCoreEnvironment.project, false)
projectContext.ktFiles = projectContext.inputTextFiles.map {
psiFileFactory.createPhysicalFile(it.path.toString(), it.content)
projectContext.sourceSetContexts.forEach { sourceSetContext ->
sourceSetContext.ktFiles = sourceSetContext.textFiles.map {
psiFileFactory.createPhysicalFile(it.path.toString(), it.content)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@ package io.verik.compiler.core.common

object Annotations {

const val MAKE = "io.verik.core.Make"

const val SYNTHESIS_TOP = "io.verik.core.SynthTop"
const val SIMULATION_TOP = "io.verik.core.SimTop"
const val RELABEL = "io.verik.core.Relabel"

const val IN = "io.verik.core.In"
const val OUT = "io.verik.core.Out"

const val COM = "io.verik.core.Com"
const val SEQ = "io.verik.core.Seq"
const val RUN = "io.verik.core.Run"
const val TASK = "io.verik.core.Task"

const val MAKE = "io.verik.core.Make"

const val IN = "io.verik.core.In"
const val OUT = "io.verik.core.Out"
const val EXTERN = "io.verik.core.Extern"
const val RELABEL = "io.verik.core.Relabel"
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ object ComponentInterpreter {
val ports = interpretPorts(basicClass.primaryConstructor?.valueParameters, referenceUpdater)
val isSynthesisTop = basicClass.hasAnnotation(Annotations.SYNTHESIS_TOP)
val isSimulationTop = basicClass.hasAnnotation(Annotations.SIMULATION_TOP)
val isExtern = basicClass.hasAnnotation(Annotations.EXTERN)
val module = EModule(
basicClass.location,
basicClass.name,
basicClass.superType,
ports,
basicClass.declarations,
isSynthesisTop,
isSimulationTop
isSimulationTop,
isExtern
)
referenceUpdater.replace(basicClass, module)
basicClass.primaryConstructor?.let { referenceUpdater.update(it, module) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ data class Config(
val projectName: String,
val projectDir: Path,
val buildDir: Path,
val projectFiles: List<Path>,
val sourceSetConfigs: List<SourceSetConfig>,
val debug: Boolean,
val suppressedWarnings: List<String>,
val promotedWarnings: List<String>,
Expand Down
9 changes: 6 additions & 3 deletions verik-compiler/src/main/kotlin/io/verik/compiler/main/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ object Main {
}

private fun readFiles(projectContext: ProjectContext) {
projectContext.inputTextFiles = projectContext.config.projectFiles.map {
val lines = Files.readAllLines(it)
TextFile(it, lines.joinToString(separator = "\n", postfix = "\n"))
projectContext.sourceSetContexts = projectContext.config.sourceSetConfigs.map { sourceSetConfig ->
val textFiles = sourceSetConfig.files.map {
val lines = Files.readAllLines(it)
TextFile(it, lines.joinToString(separator = "\n", postfix = "\n"))
}
SourceSetContext(sourceSetConfig.name, textFiles)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ object Platform {
}

fun getStringFromPath(path: Path): String {
if (path.isAbsolute)
throw IllegalArgumentException("Unexpected absolute path: $path")
val names = (0 until path.nameCount).map { path.getName(it).toString() }
return names.joinToString(separator = "/")
val namesString = names.joinToString(separator = "/")
return if (path.isAbsolute) "/$namesString"
else namesString
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ class ProjectContext(
val config: Config
) {

lateinit var inputTextFiles: List<TextFile>
lateinit var sourceSetContexts: List<SourceSetContext>
lateinit var kotlinCoreEnvironment: KotlinCoreEnvironment
lateinit var ktFiles: List<KtFile>
lateinit var bindingContext: BindingContext
lateinit var castContext: CastContext
lateinit var project: EProject
val outputContext = OutputContext()

fun getKtFiles(): List<KtFile> {
return sourceSetContexts.flatMap { it.ktFiles }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2021 Francis Wang
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.verik.compiler.main

import java.nio.file.Path

data class SourceSetConfig(val name: String, val files: List<Path>)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2021 Francis Wang
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.verik.compiler.main

import org.jetbrains.kotlin.psi.KtFile

class SourceSetContext(val name: String, val textFiles: List<TextFile>) {

lateinit var ktFiles: List<KtFile>
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,10 @@ object StageSequencer {
// Compile
stageSequence.add(KotlinEnvironmentBuilderStage)
stageSequence.add(KotlinCompilerParserStage)
stageSequence.add(KotlinCompilerAnalyzerStage)

// PreCheck
stageSequence.add(UnsupportedElementCheckerStage)
stageSequence.add(UnsupportedModifierCheckerStage)
stageSequence.add(ImportDirectiveCheckerStage)
stageSequence.add(KotlinCompilerAnalyzerStage)

// Cast
stageSequence.add(DeclarationCastIndexerStage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,19 @@ object ConfigFileSerializerStage : ProjectStage() {

val synthesisTopNames = ArrayList<String>()
val simulationTopNames = ArrayList<String>()
val topVisitor = object : TreeVisitor() {
val externNames = ArrayList<String>()
val moduleVisitor = object : TreeVisitor() {
override fun visitModule(module: EModule) {
super.visitModule(module)
if (module.isSynthesisTop)
synthesisTopNames.add(module.name)
if (module.isSimulationTop)
simulationTopNames.add(module.name)
if (module.isExtern)
externNames.add(module.name)
}
}
projectContext.project.accept(topVisitor)
projectContext.project.accept(moduleVisitor)

val builder = StringBuilder()
builder.append(fileHeader)
Expand All @@ -63,6 +66,12 @@ object ConfigFileSerializerStage : ProjectStage() {
builder.appendLine(" - $it")
}
}
if (externNames.isNotEmpty()) {
builder.appendLine("extern:")
externNames.forEach {
builder.appendLine(" - $it")
}
}
projectContext.outputContext.configTextFile = TextFile(outputPath, builder.toString())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ object FileHeaderBuilder {
val lines = ArrayList<String>()

val inputPathString = inputPath
?.let { Platform.getStringFromPath(projectContext.config.projectDir.relativize(it)) }
val outputPathString = Platform.getStringFromPath(projectContext.config.projectDir.relativize(outputPath))
?.let { Platform.getStringFromPath(it.toAbsolutePath()) }
val outputPathString = Platform.getStringFromPath(outputPath.toAbsolutePath())

lines.add("Project: ${projectContext.config.projectName}")
if (inputPathString != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,10 @@ class SourceSerializerVisitor(private val serializerContext: SerializerContext)
companion object {

fun declarationIsHidden(element: EElement): Boolean {
return element is EModulePort || element is EClockingBlock || element is ESvEnumEntry
return (element is EModule && element.isExtern) ||
element is EModulePort ||
element is EClockingBlock ||
element is ESvEnumEntry
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import io.verik.compiler.common.ProjectStage
import io.verik.compiler.main.Config
import io.verik.compiler.main.Platform
import io.verik.compiler.main.ProjectContext
import io.verik.compiler.main.SourceSetConfig
import io.verik.compiler.main.SourceSetContext
import io.verik.compiler.main.StageSequencer
import io.verik.compiler.main.TextFile
import io.verik.compiler.message.MessageCollector
Expand All @@ -40,9 +42,10 @@ abstract class BaseTest {
import io.verik.core.*
$content
""".trimIndent()
val textFile = TextFile(config.projectFiles[0], contentWithPackageHeader)
val textFile = TextFile(config.sourceSetConfigs[0].files[0], contentWithPackageHeader)
val projectContext = ProjectContext(config)
projectContext.inputTextFiles = listOf(textFile)
val sourceSetContext = SourceSetContext(config.sourceSetConfigs[0].name, listOf(textFile))
projectContext.sourceSetContexts = listOf(sourceSetContext)

val stageSequence = StageSequencer.getStageSequence()
assert(stageSequence.contains(stageClass))
Expand Down Expand Up @@ -153,13 +156,14 @@ abstract class BaseTest {
} else {
"/src/main/kotlin/test/Test.kt"
}
val sourceSetConfig = SourceSetConfig("test", listOf(Paths.get(projectFile)))
return Config(
version = "local-SNAPSHOT",
timestamp = "",
projectName = "test",
projectDir = Paths.get(projectDir),
buildDir = Paths.get(buildDir),
projectFiles = listOf(Paths.get(projectFile)),
sourceSetConfigs = listOf(sourceSetConfig),
debug = true,
suppressedWarnings = listOf("KOTLIN_COMPILE_WARNING"),
promotedWarnings = listOf(),
Expand Down
Loading

0 comments on commit 947feae

Please sign in to comment.