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 if (type.floatKind == slang::ast::FloatingType::Kind::RealTime)
40 return moore::TimeType::get(context.
getContext());
41 if (type.floatKind == slang::ast::FloatingType::Kind::Real)
42 return moore::RealType::get(context.
getContext(), moore::RealWidth::f64);
43 return moore::RealType::get(context.
getContext(), moore::RealWidth::f32);
46 Type visit(
const slang::ast::PredefinedIntegerType &type) {
47 if (type.integerKind == slang::ast::PredefinedIntegerType::Kind::Time)
48 return moore::TimeType::get(context.
getContext());
49 return getSimpleBitVectorType(type);
52 Type visit(
const slang::ast::PackedArrayType &type) {
54 if (type.elementType.as_if<slang::ast::ScalarType>())
55 return getSimpleBitVectorType(type);
58 auto innerType = type.elementType.visit(*
this);
62 return moore::ArrayType::get(type.range.width(),
63 cast<moore::PackedType>(innerType));
66 Type visit(
const slang::ast::QueueType &type) {
67 auto innerType = type.elementType.visit(*
this);
70 return moore::QueueType::get(cast<moore::UnpackedType>(innerType),
74 Type visit(
const slang::ast::AssociativeArrayType &type) {
75 auto innerType = type.elementType.visit(*
this);
78 if (!type.indexType) {
80 loc,
"unsupported type: associative arrays with wildcard index");
83 auto indexType = type.indexType->visit(*
this);
86 return moore::AssocArrayType::get(cast<moore::UnpackedType>(innerType),
87 cast<moore::UnpackedType>(indexType));
90 Type visit(
const slang::ast::FixedSizeUnpackedArrayType &type) {
91 auto innerType = type.elementType.visit(*
this);
94 return moore::UnpackedArrayType::get(type.range.width(),
95 cast<moore::UnpackedType>(innerType));
98 Type visit(
const slang::ast::DynamicArrayType &type) {
99 auto innerType = type.elementType.visit(*
this);
102 return moore::OpenUnpackedArrayType::get(
103 cast<moore::UnpackedType>(innerType));
106 Type visit(
const slang::ast::DPIOpenArrayType &type) {
107 auto innerType = type.elementType.visit(*
this);
111 return moore::OpenArrayType::get(cast<moore::PackedType>(innerType));
112 return moore::OpenUnpackedArrayType::get(
113 cast<moore::UnpackedType>(innerType));
117 Type visit(
const slang::ast::TypeAliasType &type) {
119 return type.targetType.getType().visit(*
this);
123 Type visit(
const slang::ast::EnumType &type) {
125 return type.baseType.visit(*
this);
130 collectMembers(
const slang::ast::Scope &structType,
131 SmallVectorImpl<moore::StructLikeMember> &members) {
132 for (
auto &field : structType.membersOfType<slang::ast::FieldSymbol>()) {
133 auto name = StringAttr::get(context.
getContext(), field.name);
137 members.push_back({name, cast<moore::UnpackedType>(innerType)});
143 Type visit(
const slang::ast::PackedStructType &type) {
144 SmallVector<moore::StructLikeMember> members;
145 if (failed(collectMembers(type, members)))
147 return moore::StructType::get(context.
getContext(), members);
150 Type visit(
const slang::ast::UnpackedStructType &type) {
151 SmallVector<moore::StructLikeMember> members;
152 if (failed(collectMembers(type, members)))
154 return moore::UnpackedStructType::get(context.
getContext(), members);
157 Type visit(
const slang::ast::PackedUnionType &type) {
158 SmallVector<moore::StructLikeMember> members;
159 if (failed(collectMembers(type, members)))
161 return moore::UnionType::get(context.
getContext(), members);
164 Type visit(
const slang::ast::UnpackedUnionType &type) {
165 SmallVector<moore::StructLikeMember> members;
166 if (failed(collectMembers(type, members)))
168 return moore::UnpackedUnionType::get(context.
getContext(), members);
171 Type visit(
const slang::ast::StringType &type) {
172 return moore::StringType::get(context.
getContext());
175 Type visit(
const slang::ast::CHandleType &type) {
176 return moore::ChandleType::get(context.
getContext());
179 Type visit(
const slang::ast::ClassType &type) {
184 mlir::emitError(loc) <<
"no lowering generated for class type `"
185 << type.toString() <<
"`";
188 mlir::StringAttr symName = lowering->
op.getSymNameAttr();
189 mlir::FlatSymbolRefAttr symRef = mlir::FlatSymbolRefAttr::get(symName);
190 return moore::ClassHandleType::get(context.
getContext(), symRef);
193 Type visit(
const slang::ast::NullType &type) {
194 return moore::NullType::get(context.
getContext());
197 Type visit(
const slang::ast::EventType &type) {
200 return moore::IntType::getInt(context.
getContext(), 1);
204 template <
typename T>
205 Type visit(T &&node) {
206 auto d = mlir::emitError(loc,
"unsupported type: ")
207 << slang::ast::toString(node.kind);
208 d.attachNote() << node.template as<slang::ast::Type>().toString();
216Type Context::convertType(
const slang::ast::Type &type, LocationAttr loc) {
219 return type.visit(TypeVisitor(*
this, loc));
224 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.
circt::moore::ClassDeclOp op
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.
ClassLowering * declareClass(const slang::ast::ClassType &cls)
LogicalResult buildClassProperties(const slang::ast::ClassType &classdecl)
MLIRContext * getContext()
Return the MLIR context.
Location convertLocation(slang::SourceLocation loc)
Convert a slang SourceLocation into an MLIR Location.