diff --git a/parser/src/main/kotlin/tools/samt/parser/Nodes.kt b/parser/src/main/kotlin/tools/samt/parser/Nodes.kt index e8c99791..92a197d0 100644 --- a/parser/src/main/kotlin/tools/samt/parser/Nodes.kt +++ b/parser/src/main/kotlin/tools/samt/parser/Nodes.kt @@ -58,7 +58,7 @@ class RecordFieldNode( val name: IdentifierNode, val type: ExpressionNode, override val annotations: List, -) : Node, AnnotatedNode +) : AnnotatedNode class EnumDeclarationNode( override val location: Location, @@ -81,7 +81,7 @@ class ServiceDeclarationNode( override val annotations: List, ) : NamedDeclarationNode, AnnotatedNode -sealed interface OperationNode : Node, AnnotatedNode { +sealed interface OperationNode : AnnotatedNode { val name: IdentifierNode val parameters: List } @@ -91,7 +91,7 @@ class OperationParameterNode( val name: IdentifierNode, val type: ExpressionNode, override val annotations: List, -) : Node, AnnotatedNode +) : AnnotatedNode class RequestResponseOperationNode( override val location: Location, diff --git a/semantic/src/main/kotlin/tools/samt/semantic/ConstraintBuilder.kt b/semantic/src/main/kotlin/tools/samt/semantic/ConstraintBuilder.kt index 87e3c366..92877ecd 100644 --- a/semantic/src/main/kotlin/tools/samt/semantic/ConstraintBuilder.kt +++ b/semantic/src/main/kotlin/tools/samt/semantic/ConstraintBuilder.kt @@ -230,11 +230,11 @@ internal class ConstraintBuilder(private val controller: DiagnosticController) { private fun validateConstraintMatches(constraint: ResolvedTypeReference.Constraint, baseType: Type): Boolean { return when (constraint) { is ResolvedTypeReference.Constraint.Pattern -> baseType is StringType - is ResolvedTypeReference.Constraint.Range -> baseType is NumberType + is ResolvedTypeReference.Constraint.Range -> baseType is LiteralNumberType is ResolvedTypeReference.Constraint.Size -> baseType is StringType || baseType is ListType || baseType is MapType is ResolvedTypeReference.Constraint.Value -> when (constraint.value) { is String -> baseType is StringType - is Number -> baseType is NumberType + is Number -> baseType is LiteralNumberType is Boolean -> baseType is BooleanType else -> false } @@ -260,7 +260,7 @@ internal class ConstraintBuilder(private val controller: DiagnosticController) { val applicableConstraints = when (baseType) { is StringType -> "pattern, size or value" - is NumberType -> "range or value" + is LiteralNumberType -> "range or value" is BooleanType -> "value" is ListType -> "size" is MapType -> "size" diff --git a/semantic/src/main/kotlin/tools/samt/semantic/SemanticModelAnnotationProcessor.kt b/semantic/src/main/kotlin/tools/samt/semantic/SemanticModelAnnotationProcessor.kt index 92e256e5..7a25fca5 100644 --- a/semantic/src/main/kotlin/tools/samt/semantic/SemanticModelAnnotationProcessor.kt +++ b/semantic/src/main/kotlin/tools/samt/semantic/SemanticModelAnnotationProcessor.kt @@ -46,7 +46,7 @@ internal class SemanticModelAnnotationProcessor( return UserMetadata(descriptions, deprecations) } - private fun Package.getAnnotatedElements(): List = buildList { + private fun Package.getAnnotatedElements(): List = buildList { this@getAnnotatedElements.allSubPackages.forEach { addAll(it.records) it.records.flatMapTo(this, RecordType::fields) diff --git a/semantic/src/main/kotlin/tools/samt/semantic/SemanticModelPreProcessor.kt b/semantic/src/main/kotlin/tools/samt/semantic/SemanticModelPreProcessor.kt index 4cfeb153..b9710b76 100644 --- a/semantic/src/main/kotlin/tools/samt/semantic/SemanticModelPreProcessor.kt +++ b/semantic/src/main/kotlin/tools/samt/semantic/SemanticModelPreProcessor.kt @@ -72,7 +72,6 @@ internal class SemanticModelPreProcessor(private val controller: DiagnosticContr ) } parentPackage += RecordType( - name = statement.name.name, fields = fields, declaration = statement ) @@ -82,7 +81,7 @@ internal class SemanticModelPreProcessor(private val controller: DiagnosticContr reportDuplicateDeclaration(parentPackage, statement) reportDuplicates(statement.values, "Enum value") { it } val values = statement.values.map { it.name } - parentPackage += EnumType(statement.name.name, values, statement) + parentPackage += EnumType(values, statement) } is ServiceDeclarationNode -> { @@ -124,7 +123,7 @@ internal class SemanticModelPreProcessor(private val controller: DiagnosticContr } } } - parentPackage += ServiceType(statement.name.name, operations, statement) + parentPackage += ServiceType(operations, statement) } is ProviderDeclarationNode -> { @@ -140,7 +139,7 @@ internal class SemanticModelPreProcessor(private val controller: DiagnosticContr name = statement.transport.protocolName.name, configuration = statement.transport.configuration ) - parentPackage += ProviderType(statement.name.name, implements, transport, statement) + parentPackage += ProviderType(implements, transport, statement) } is ConsumerDeclarationNode -> { @@ -160,7 +159,6 @@ internal class SemanticModelPreProcessor(private val controller: DiagnosticContr is TypeAliasNode -> { reportDuplicateDeclaration(parentPackage, statement) parentPackage += AliasType( - name = statement.name.name, aliasedType = UnresolvedTypeReference(statement.type), declaration = statement ) diff --git a/semantic/src/main/kotlin/tools/samt/semantic/Types.kt b/semantic/src/main/kotlin/tools/samt/semantic/Types.kt index 806683d3..50ed4d7b 100644 --- a/semantic/src/main/kotlin/tools/samt/semantic/Types.kt +++ b/semantic/src/main/kotlin/tools/samt/semantic/Types.kt @@ -20,12 +20,12 @@ object UnknownType : Type { sealed interface LiteralType : Type -sealed interface NumberType : Type +sealed interface LiteralNumberType : LiteralType /** * 32-bit whole number, signed */ -object IntType : LiteralType, NumberType { +object IntType : LiteralNumberType { override val humanReadableName: String get() = "Int" } @@ -33,7 +33,7 @@ object IntType : LiteralType, NumberType { /** * 64-bit whole number, signed */ -object LongType : LiteralType, NumberType { +object LongType : LiteralNumberType { override val humanReadableName: String get() = "Long" } @@ -41,7 +41,7 @@ object LongType : LiteralType, NumberType { /** * 32-bit floating point number, signed */ -object FloatType : LiteralType, NumberType { +object FloatType : LiteralNumberType { override val humanReadableName: String get() = "Float" } @@ -49,7 +49,7 @@ object FloatType : LiteralType, NumberType { /** * 64-bit floating point number, signed */ -object DoubleType : LiteralType, NumberType { +object DoubleType : LiteralNumberType { override val humanReadableName: String get() = "Double" } @@ -57,7 +57,7 @@ object DoubleType : LiteralType, NumberType { /** * Arbitrary precision number, fixed amount of digits before and after decimal point */ -object DecimalType : LiteralType, NumberType { +object DecimalType : LiteralNumberType { override val humanReadableName: String get() = "Decimal" } @@ -110,13 +110,17 @@ object DurationType : LiteralType { get() = "Duration" } -sealed interface CompoundType : Type +sealed interface UserDeclaredNamedType : UserDeclared, Type { + override val humanReadableName: String get() = name + override val declaration: NamedDeclarationNode + val name: String get() = declaration.name.name +} sealed interface UserDeclared { val declaration: Node } -sealed interface Annotated : UserDeclared { +sealed interface UserAnnotated : UserDeclared { override val declaration: AnnotatedNode val annotations: List get() = declaration.annotations } @@ -124,7 +128,7 @@ sealed interface Annotated : UserDeclared { data class ListType( val elementType: TypeReference, val node: GenericSpecializationNode, -) : CompoundType { +) : Type { override val humanReadableName: String = "List<${elementType.humanReadableName}>" } @@ -132,57 +136,48 @@ data class MapType( val keyType: TypeReference, val valueType: TypeReference, val node: GenericSpecializationNode, -) : CompoundType { +) : Type { override val humanReadableName: String = "Map<${keyType.humanReadableName}, ${valueType.humanReadableName}>" } class AliasType( - val name: String, /** The type this alias stands for, could be another alias */ var aliasedType: TypeReference, /** The fully resolved type, will not contain any type aliases anymore, just the underlying merged type */ var fullyResolvedType: ResolvedTypeReference? = null, override val declaration: TypeAliasNode, -) : CompoundType, Annotated { - override val humanReadableName: String = name -} +) : UserDeclaredNamedType, UserAnnotated class RecordType( - val name: String, val fields: List, override val declaration: RecordDeclarationNode, -) : CompoundType, Annotated { +) : UserDeclaredNamedType, UserAnnotated { class Field( val name: String, var type: TypeReference, override val declaration: RecordFieldNode, - ) : Annotated - - override val humanReadableName: String = name + ) : UserAnnotated } class EnumType( - val name: String, val values: List, override val declaration: EnumDeclarationNode, -) : CompoundType, Annotated { - override val humanReadableName: String = name -} +) : UserDeclaredNamedType, UserAnnotated class ServiceType( - val name: String, val operations: List, override val declaration: ServiceDeclarationNode, -) : CompoundType, Annotated { - sealed interface Operation : Annotated { +) : UserDeclaredNamedType, UserAnnotated { + sealed interface Operation : UserAnnotated { val name: String val parameters: List override val declaration: OperationNode + class Parameter( val name: String, var type: TypeReference, override val declaration: OperationParameterNode, - ): UserDeclared, Annotated + ) : UserDeclared, UserAnnotated } class RequestResponseOperation( @@ -199,16 +194,13 @@ class ServiceType( override val parameters: List, override val declaration: OnewayOperationNode, ) : Operation - - override val humanReadableName: String = name } class ProviderType( - val name: String, val implements: List, @Suppress("unused") val transport: Transport, override val declaration: ProviderDeclarationNode, -) : CompoundType, UserDeclared { +) : UserDeclaredNamedType { class Implements( var service: TypeReference, var operations: List, @@ -219,15 +211,13 @@ class ProviderType( val name: String, @Suppress("unused") val configuration: Any?, ) - - override val humanReadableName: String = name } class ConsumerType( var provider: TypeReference, var uses: List, override val declaration: ConsumerDeclarationNode, -) : CompoundType, UserDeclared { +) : Type, UserDeclared { class Uses( var service: TypeReference, var operations: List,