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 return moore::RealType::get(context.
getContext());
44 Type visit(
const slang::ast::PredefinedIntegerType &type) {
45 if (type.integerKind == slang::ast::PredefinedIntegerType::Kind::Time)
46 return moore::TimeType::get(context.
getContext());
47 return getSimpleBitVectorType(type);
50 Type visit(
const slang::ast::PackedArrayType &type) {
52 if (type.elementType.as_if<slang::ast::ScalarType>())
53 return getSimpleBitVectorType(type);
56 auto innerType = type.elementType.visit(*
this);
60 return moore::ArrayType::get(type.range.width(),
61 cast<moore::PackedType>(innerType));
64 Type visit(
const slang::ast::QueueType &type) {
65 auto innerType = type.elementType.visit(*
this);
68 return moore::QueueType::get(cast<moore::UnpackedType>(innerType),
72 Type visit(
const slang::ast::AssociativeArrayType &type) {
73 auto innerType = type.elementType.visit(*
this);
76 if (!type.indexType) {
78 loc,
"unsupported type: associative arrays with wildcard index");
81 auto indexType = type.indexType->visit(*
this);
84 return moore::AssocArrayType::get(cast<moore::UnpackedType>(innerType),
85 cast<moore::UnpackedType>(indexType));
88 Type visit(
const slang::ast::FixedSizeUnpackedArrayType &type) {
89 auto innerType = type.elementType.visit(*
this);
92 return moore::UnpackedArrayType::get(type.range.width(),
93 cast<moore::UnpackedType>(innerType));
96 Type visit(
const slang::ast::DynamicArrayType &type) {
97 auto innerType = type.elementType.visit(*
this);
100 return moore::OpenUnpackedArrayType::get(
101 cast<moore::UnpackedType>(innerType));
105 Type visit(
const slang::ast::TypeAliasType &type) {
107 return type.targetType.getType().visit(*
this);
111 Type visit(
const slang::ast::EnumType &type) {
113 return type.baseType.visit(*
this);
118 collectMembers(
const slang::ast::Scope &structType,
119 SmallVectorImpl<moore::StructLikeMember> &members) {
120 for (
auto &field : structType.membersOfType<slang::ast::FieldSymbol>()) {
121 auto name = StringAttr::get(context.
getContext(), field.name);
125 members.push_back({name, cast<moore::UnpackedType>(innerType)});
131 Type visit(
const slang::ast::PackedStructType &type) {
132 SmallVector<moore::StructLikeMember> members;
133 if (failed(collectMembers(type, members)))
135 return moore::StructType::get(context.
getContext(), members);
138 Type visit(
const slang::ast::UnpackedStructType &type) {
139 SmallVector<moore::StructLikeMember> members;
140 if (failed(collectMembers(type, members)))
142 return moore::UnpackedStructType::get(context.
getContext(), members);
145 Type visit(
const slang::ast::PackedUnionType &type) {
146 SmallVector<moore::StructLikeMember> members;
147 if (failed(collectMembers(type, members)))
149 return moore::UnionType::get(context.
getContext(), members);
152 Type visit(
const slang::ast::UnpackedUnionType &type) {
153 SmallVector<moore::StructLikeMember> members;
154 if (failed(collectMembers(type, members)))
156 return moore::UnpackedUnionType::get(context.
getContext(), members);
159 Type visit(
const slang::ast::StringType &type) {
160 return moore::StringType::get(context.
getContext());
164 template <
typename T>
165 Type visit(T &&node) {
166 auto d = mlir::emitError(loc,
"unsupported type: ")
167 << slang::ast::toString(node.kind);
168 d.attachNote() << node.template as<slang::ast::Type>().toString();
176Type Context::convertType(
const slang::ast::Type &type, LocationAttr loc) {
179 return type.visit(TypeVisitor(*
this, loc));
184 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.