10#include "slang/syntax/AllSyntax.h"
13using namespace ImportVerilog;
20 TypeVisitor(
Context &context, Location loc) : context(context), loc(loc) {}
23 Type getSimpleBitVectorType(
const slang::ast::IntegralType &type) {
24 return moore::IntType::get(context.
getContext(), type.bitWidth,
25 type.isFourState ? Domain::FourValued
30 Type visit(
const slang::ast::VoidType &type) {
31 return moore::VoidType::get(context.
getContext());
34 Type visit(
const slang::ast::ScalarType &type) {
35 return getSimpleBitVectorType(type);
38 Type visit(
const slang::ast::FloatingType &type) {
39 return moore::RealType::get(context.
getContext());
42 Type visit(
const slang::ast::PredefinedIntegerType &type) {
43 return getSimpleBitVectorType(type);
46 Type visit(
const slang::ast::PackedArrayType &type) {
48 if (type.elementType.as_if<slang::ast::ScalarType>())
49 return getSimpleBitVectorType(type);
52 auto innerType = type.elementType.visit(*
this);
56 return moore::ArrayType::get(type.range.width(),
57 cast<moore::PackedType>(innerType));
60 Type visit(
const slang::ast::QueueType &type) {
61 auto innerType = type.elementType.visit(*
this);
64 return moore::QueueType::get(cast<moore::UnpackedType>(innerType),
68 Type visit(
const slang::ast::AssociativeArrayType &type) {
69 auto innerType = type.elementType.visit(*
this);
72 auto indexType = type.indexType->visit(*
this);
75 return moore::AssocArrayType::get(cast<moore::UnpackedType>(innerType),
76 cast<moore::UnpackedType>(indexType));
79 Type visit(
const slang::ast::FixedSizeUnpackedArrayType &type) {
80 auto innerType = type.elementType.visit(*
this);
83 return moore::UnpackedArrayType::get(type.range.width(),
84 cast<moore::UnpackedType>(innerType));
87 Type visit(
const slang::ast::DynamicArrayType &type) {
88 auto innerType = type.elementType.visit(*
this);
91 return moore::OpenUnpackedArrayType::get(
92 cast<moore::UnpackedType>(innerType));
96 Type visit(
const slang::ast::TypeAliasType &type) {
98 return type.targetType.getType().visit(*
this);
102 Type visit(
const slang::ast::EnumType &type) {
104 return type.baseType.visit(*
this);
109 collectMembers(
const slang::ast::Scope &structType,
110 SmallVectorImpl<moore::StructLikeMember> &members) {
111 for (
auto &field : structType.membersOfType<slang::ast::FieldSymbol>()) {
112 auto name = StringAttr::get(context.
getContext(), field.name);
116 members.push_back({name, cast<moore::UnpackedType>(innerType)});
122 Type visit(
const slang::ast::PackedStructType &type) {
123 SmallVector<moore::StructLikeMember> members;
124 if (failed(collectMembers(type, members)))
126 return moore::StructType::get(context.
getContext(), members);
129 Type visit(
const slang::ast::UnpackedStructType &type) {
130 SmallVector<moore::StructLikeMember> members;
131 if (failed(collectMembers(type, members)))
133 return moore::UnpackedStructType::get(context.
getContext(), members);
136 Type visit(
const slang::ast::PackedUnionType &type) {
137 SmallVector<moore::StructLikeMember> members;
138 if (failed(collectMembers(type, members)))
140 return moore::UnionType::get(context.
getContext(), members);
143 Type visit(
const slang::ast::UnpackedUnionType &type) {
144 SmallVector<moore::StructLikeMember> members;
145 if (failed(collectMembers(type, members)))
147 return moore::UnpackedUnionType::get(context.
getContext(), members);
150 Type visit(
const slang::ast::StringType &type) {
151 return moore::StringType::get(context.
getContext());
155 template <
typename T>
156 Type visit(T &&node) {
157 auto d = mlir::emitError(loc,
"unsupported type: ")
158 << slang::ast::toString(node.kind);
159 d.attachNote() << node.template as<slang::ast::Type>().toString();
170 return type.visit(TypeVisitor(*
this, loc));
175 if (
auto *ts = type.getTypeSyntax())
mlir::Type innerType(mlir::Type type)
Domain
The number of values each bit of a type can assume.
@ TwoValued
Two-valued types such as bit or int.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
A helper class to facilitate the conversion from a Slang AST to MLIR operations.
Type convertType(const slang::ast::Type &type, LocationAttr loc={})
Convert a slang type into an MLIR type.
MLIRContext * getContext()
Return the MLIR context.
Location convertLocation(slang::SourceLocation loc)
Convert a slang SourceLocation into an MLIR Location.