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));
107 Type visit(
const slang::ast::TypeAliasType &type) {
109 return type.targetType.getType().visit(*
this);
113 Type visit(
const slang::ast::EnumType &type) {
115 return type.baseType.visit(*
this);
120 collectMembers(
const slang::ast::Scope &structType,
121 SmallVectorImpl<moore::StructLikeMember> &members) {
122 for (
auto &field : structType.membersOfType<slang::ast::FieldSymbol>()) {
123 auto name = StringAttr::get(context.
getContext(), field.name);
127 members.push_back({name, cast<moore::UnpackedType>(innerType)});
133 Type visit(
const slang::ast::PackedStructType &type) {
134 SmallVector<moore::StructLikeMember> members;
135 if (failed(collectMembers(type, members)))
137 return moore::StructType::get(context.
getContext(), members);
140 Type visit(
const slang::ast::UnpackedStructType &type) {
141 SmallVector<moore::StructLikeMember> members;
142 if (failed(collectMembers(type, members)))
144 return moore::UnpackedStructType::get(context.
getContext(), members);
147 Type visit(
const slang::ast::PackedUnionType &type) {
148 SmallVector<moore::StructLikeMember> members;
149 if (failed(collectMembers(type, members)))
151 return moore::UnionType::get(context.
getContext(), members);
154 Type visit(
const slang::ast::UnpackedUnionType &type) {
155 SmallVector<moore::StructLikeMember> members;
156 if (failed(collectMembers(type, members)))
158 return moore::UnpackedUnionType::get(context.
getContext(), members);
161 Type visit(
const slang::ast::StringType &type) {
162 return moore::StringType::get(context.
getContext());
166 template <
typename T>
167 Type visit(T &&node) {
168 auto d = mlir::emitError(loc,
"unsupported type: ")
169 << slang::ast::toString(node.kind);
170 d.attachNote() << node.template as<slang::ast::Type>().toString();
178Type Context::convertType(
const slang::ast::Type &type, LocationAttr loc) {
181 return type.visit(TypeVisitor(*
this, loc));
186 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.