Skip to content

Commit

Permalink
Merge pull request #102 from frwang96/feature-enum-property
Browse files Browse the repository at this point in the history
Feature enum property
  • Loading branch information
frwang96 authored Feb 22, 2022
2 parents 821c7b2 + f03649d commit a40d619
Show file tree
Hide file tree
Showing 92 changed files with 808 additions and 391 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
- Abstract classes compile to regular classes and not virtual classes.
- All classes should inherit from `Class` to get randomization functions.
- Support for randomize annotation `@Rand`.
- Support `toString` function.
- Support for safe access operator.
- Support for enums with values.

## [0.1.13]
### Importer
Expand Down
18 changes: 14 additions & 4 deletions verik-compiler/regression/08-class.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,20 @@ object M : Module() {
val x1 = C1()
val x2 = C2<Int>()
val x3 = C3()
val x4 = C4(false)

@Run
fun f0() {
println(C0.x4)
println(C2.x5)
println(C0.x5)
println(C2.x6)
}
}

open class C0 : Class() {

companion object {

var x4 = false
var x5 = false
}
}

Expand All @@ -49,11 +50,20 @@ class C2<T> : Class() {

companion object {

var x5 = false
var x6 = false
}
}

class C3: C0 {

constructor(): super()
}

class C4(x7: Boolean) : Class() {

var x8 = false

init {
x8 = x7
}
}
21 changes: 17 additions & 4 deletions verik-compiler/regression/08-class.sv
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class C0;
function new();
endfunction : new

static logic x4 = 1'b0;
static logic x5 = 1'b0;

endclass : C0

Expand All @@ -35,7 +35,7 @@ endclass : C1

class C2;

static logic x5 = 1'b0;
static logic x6 = 1'b0;

endclass : C2

Expand All @@ -54,16 +54,29 @@ class C3 extends C0;

endclass : C3

class C4;

logic x8 = 1'b0;

function new(
input logic x7
);
x8 = x7;
endfunction : new

endclass : C4

module M;

C0 x0 = C0::new();
C1 x1 = C1::new();
C2_T_Int x2 = C2_T_Int::new();
C3 x3 = C3::new();
C4 x4 = C4::new(.x7(1'b0));

initial begin : f0
$display($sformatf("%b", C0::x4));
$display($sformatf("%b", C2::x5));
$display($sformatf("%b", C0::x5));
$display($sformatf("%b", C2::x6));
end : f0

endmodule : M
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

package io.verik.compiler.ast.element.declaration.common

import io.verik.compiler.ast.common.ExpressionContainer
import io.verik.compiler.ast.common.Type
import io.verik.compiler.ast.element.expression.common.EExpression
import io.verik.compiler.ast.property.AnnotationEntry
import io.verik.compiler.common.TreeVisitor
import io.verik.compiler.common.Visitor
Expand All @@ -27,22 +29,40 @@ class EEnumEntry(
override var name: String,
override var type: Type,
override var annotationEntries: List<AnnotationEntry>,
override var documentationLines: List<String>?
) : EAbstractProperty() {
override var documentationLines: List<String>?,
var expression: EExpression?
) : EAbstractProperty(), ExpressionContainer {

init {
expression?.parent = this
}

fun fill(
type: Type,
annotationEntries: List<AnnotationEntry>,
documentationLines: List<String>?
documentationLines: List<String>?,
expression: EExpression?
) {
expression?.parent = this
this.type = type
this.annotationEntries = annotationEntries
this.documentationLines = documentationLines
this.expression = expression
}

override fun accept(visitor: Visitor) {
visitor.visitEnumEntry(this)
}

override fun acceptChildren(visitor: TreeVisitor) {}
override fun acceptChildren(visitor: TreeVisitor) {
expression?.accept(visitor)
}

override fun replaceChild(oldExpression: EExpression, newExpression: EExpression): Boolean {
newExpression.parent = this
return if (expression == oldExpression) {
expression = newExpression
true
} else false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package io.verik.compiler.ast.element.declaration.sv
import io.verik.compiler.ast.common.Type
import io.verik.compiler.ast.element.declaration.common.EAbstractClass
import io.verik.compiler.ast.element.declaration.common.EEnumEntry
import io.verik.compiler.ast.element.declaration.common.EProperty
import io.verik.compiler.ast.property.AnnotationEntry
import io.verik.compiler.common.TreeVisitor
import io.verik.compiler.common.Visitor
Expand All @@ -33,14 +34,21 @@ class EEnum(
override var type: Type,
override var annotationEntries: List<AnnotationEntry>,
override var documentationLines: List<String>?,
val property: EProperty?,
val enumEntries: List<EEnumEntry>
) : EAbstractClass() {

override var superType = Target.C_Void.toType()

init {
property?.parent = this
}

override fun accept(visitor: Visitor) {
visitor.visitEnum(this)
}

override fun acceptChildren(visitor: TreeVisitor) {}
override fun acceptChildren(visitor: TreeVisitor) {
property?.accept(visitor)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class ECallExpression(
override var type: Type,
override var reference: Declaration,
override var receiver: EExpression?,
override var isSafeAccess: Boolean,
val valueArguments: ArrayList<EExpression>,
var typeArguments: ArrayList<Type>
) : EReceiverExpression() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ abstract class EReceiverExpression : EExpression(), ExpressionContainer {

abstract var reference: Declaration
abstract var receiver: EExpression?
abstract var isSafeAccess: Boolean

override fun acceptChildren(visitor: TreeVisitor) {
receiver?.accept(visitor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package io.verik.compiler.ast.element.expression.common

import io.verik.compiler.ast.common.Declaration
import io.verik.compiler.ast.common.Type
import io.verik.compiler.ast.element.declaration.common.EAbstractProperty
import io.verik.compiler.ast.property.SerializationType
import io.verik.compiler.common.Visitor
import io.verik.compiler.message.SourceLocation
Expand All @@ -26,7 +27,8 @@ class EReferenceExpression(
override val location: SourceLocation,
override var type: Type,
override var reference: Declaration,
override var receiver: EExpression?
override var receiver: EExpression?,
override var isSafeAccess: Boolean
) : EReceiverExpression() {

override val serializationType = SerializationType.EXPRESSION
Expand All @@ -38,4 +40,17 @@ class EReferenceExpression(
override fun accept(visitor: Visitor) {
visitor.visitReferenceExpression(this)
}

companion object {

fun of(property: EAbstractProperty): EReferenceExpression {
return EReferenceExpression(
property.location,
property.type.copy(),
property,
null,
false
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@
package io.verik.compiler.ast.element.expression.sv

import io.verik.compiler.ast.common.ExpressionContainer
import io.verik.compiler.ast.common.Type
import io.verik.compiler.ast.element.expression.common.EBlockExpression
import io.verik.compiler.ast.element.expression.common.EExpression
import io.verik.compiler.ast.property.SerializationType
import io.verik.compiler.common.TreeVisitor
import io.verik.compiler.common.Visitor
import io.verik.compiler.message.SourceLocation
import io.verik.compiler.target.common.Target

class EImmediateAssertStatement(
override val location: SourceLocation,
override var type: Type,
var condition: EExpression,
var elseExpression: EBlockExpression?
) : EExpression(), ExpressionContainer {

override var type = Target.C_Void.toType()
override val serializationType = SerializationType.STATEMENT

init {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ object CastIndexerStage : ProjectStage() {
type = NullDeclaration.toType(),
annotationEntries = listOf(),
documentationLines = null,
expression = null
)
castContext.registerDeclaration(descriptor, indexedEnumEntry)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import org.jetbrains.kotlin.psi.KtClassInitializer
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.KtConstantExpression
import org.jetbrains.kotlin.psi.KtDoWhileExpression
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtEnumEntry
import org.jetbrains.kotlin.psi.KtForExpression
Expand All @@ -43,6 +42,7 @@ import org.jetbrains.kotlin.psi.KtPostfixExpression
import org.jetbrains.kotlin.psi.KtPrefixExpression
import org.jetbrains.kotlin.psi.KtPrimaryConstructor
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.KtQualifiedExpression
import org.jetbrains.kotlin.psi.KtReturnExpression
import org.jetbrains.kotlin.psi.KtSecondaryConstructor
import org.jetbrains.kotlin.psi.KtSimpleNameExpression
Expand Down Expand Up @@ -145,7 +145,7 @@ class CasterVisitor(private val castContext: CastContext) : KtVisitor<EElement,
return ExpressionCaster.castCallExpression(expression, castContext)
}

override fun visitDotQualifiedExpression(expression: KtDotQualifiedExpression, data: Unit?): EElement {
override fun visitQualifiedExpression(expression: KtQualifiedExpression, data: Unit?): EElement {
return ExpressionCaster.castReferenceExpressionOrCallExpression(expression, castContext)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,18 @@ object DeclarationCaster {
val type = castContext.castType(descriptor.classValueType!!, enumEntry)
val annotationEntries = castAnnotationEntries(enumEntry.annotationEntries, castContext)
val documentationLines = castDocumentationLines(enumEntry.docComment)
val initializerList = enumEntry.initializerList
val expression = if (initializerList != null && initializerList.initializers.isNotEmpty()) {
val initializer = initializerList.initializers[0]
if (initializer is KtSuperTypeCallEntry) {
val callExpression = castSuperTypeCallExpression(initializer, castContext)
if (callExpression.valueArguments.isNotEmpty()) {
callExpression.valueArguments[0]
} else null
} else null
} else null

castedEnumEntry.fill(type, annotationEntries, documentationLines)
castedEnumEntry.fill(type, annotationEntries, documentationLines, expression)
return castedEnumEntry
}

Expand Down Expand Up @@ -373,6 +383,7 @@ object DeclarationCaster {
type,
declaration,
null,
false,
valueArguments,
ArrayList(typeArguments)
)
Expand Down Expand Up @@ -400,6 +411,7 @@ object DeclarationCaster {
type,
declaration,
null,
false,
valueArguments,
typeArguments
)
Expand Down
Loading

0 comments on commit a40d619

Please sign in to comment.