# {{ dialect_name }} dialect implementation

load("@heir//lib/Dialect:dialect.bzl", "add_heir_dialect_library")
load("@llvm-project//mlir:tblgen.bzl", "td_library")
load("@rules_cc//cc:cc_library.bzl", "cc_library")

package(
    default_applicable_licenses = ["@heir//:license"],
    default_visibility = ["//visibility:public"],
)

cc_library(
    name = "Dialect",
    srcs = [
        "{{ dialect_name }}Dialect.cpp",
    ],
    hdrs = [
        {% if enable_attributes %}
        "{{ dialect_name }}Attributes.h",
        {% endif %}
        "{{ dialect_name }}Dialect.h",
        {% if enable_ops %}
        "{{ dialect_name }}Ops.h",
        {% endif %}
        {% if enable_types %}
        "{{ dialect_name }}Types.h",
        {% endif %}
    ],
    deps = [
        {% if enable_attributes %}
        ":{{ dialect_name }}Attributes",
        {% endif %}
        {% if enable_ops %}
        ":{{ dialect_name }}Ops",
        {% endif %}
        {% if enable_types %}
        ":{{ dialect_name }}Types",
        {% endif %}
        {% if enable_attributes %}
        "attributes_inc_gen",
        {% endif %}
        "dialect_inc_gen",
        {% if enable_ops %}
        "ops_inc_gen",
        {% endif %}
        {% if enable_types %}
        "types_inc_gen",
        {% endif %}
        "@llvm-project//llvm:Support",
        "@llvm-project//mlir:IR",
    ],
)

{% if enable_attributes %}
cc_library(
    name = "{{ dialect_name }}Attributes",
    srcs = [
        "{{ dialect_name }}Attributes.cpp",
    ],
    hdrs = [
        "{{ dialect_name }}Attributes.h",
        "{{ dialect_name }}Dialect.h",
    ],
    deps = [
        ":attributes_inc_gen",
        ":dialect_inc_gen",
        "@llvm-project//mlir:IR",
    ],
)
{% endif %}

{% if enable_types %}
cc_library(
    name = "{{ dialect_name }}Types",
    srcs = [
        "{{ dialect_name }}Types.cpp",
    ],
    hdrs = [
        "{{ dialect_name }}Dialect.h",
        "{{ dialect_name }}Types.h",
        {% if enable_attributes %}
        "{{ dialect_name }}Attributes.h",
        {% endif %}
    ],
    deps = [
        {% if enable_attributes %}
        ":{{ dialect_name }}Attributes",
        {% endif %}
        ":dialect_inc_gen",
        {% if enable_ops %}
        ":ops_inc_gen",
        {% endif %}
        ":types_inc_gen",
        "@llvm-project//mlir:IR",
    ],
)
{% endif %}

{% if enable_ops %}
cc_library(
    name = "{{ dialect_name }}Ops",
    srcs = [
        "{{ dialect_name }}Ops.cpp",
    ],
    hdrs = [
        "{{ dialect_name }}Dialect.h",
        "{{ dialect_name }}Ops.h",
        {% if enable_types %}
        "{{ dialect_name }}Types.h",
        {% endif %}
    ],
    deps = [
        {% if enable_attributes %}
        ":{{ dialect_name }}Attributes",
        {% endif %}
        {% if enable_types %}
        ":{{ dialect_name }}Types",
        {% endif %}
        ":dialect_inc_gen",
        ":ops_inc_gen",
        {% if enable_types %}
        ":types_inc_gen",
        {% endif %}
        "@llvm-project//mlir:IR",
    ],
)
{% endif %}

td_library(
    name = "td_files",
    srcs = [
        {% if enable_attributes %}
        "{{ dialect_name }}Attributes.td",
        {% endif %}
        "{{ dialect_name }}Dialect.td",
        {% if enable_ops %}
        "{{ dialect_name }}Ops.td",
        {% endif %}
        {% if enable_types %}
        "{{ dialect_name }}Types.td",
        {% endif %}
    ],
    # include from the heir-root to enable fully-qualified include-paths
    includes = ["../../../.."],
    deps = [
        "@llvm-project//mlir:BuiltinDialectTdFiles",
        "@llvm-project//mlir:OpBaseTdFiles",
    ],
)


add_heir_dialect_library(
    name = "dialect_inc_gen",
    dialect = "{{ dialect_name }}",
    kind = "dialect",
    td_file = "{{ dialect_name }}Dialect.td",
    deps = [
        ":td_files",
    ],
)

{% if enable_attributes %}
add_heir_dialect_library(
    name = "attributes_inc_gen",
    dialect = "{{ dialect_name }}",
    kind = "attribute",
    td_file = "{{ dialect_name }}Attributes.td",
    deps = [
        ":td_files",
    ],
)
{% endif %}

{% if enable_types %}
add_heir_dialect_library(
    name = "types_inc_gen",
    dialect = "{{ dialect_name }}",
    kind = "type",
    td_file = "{{ dialect_name }}Types.td",
    deps = [
        ":td_files",
    ],
)
{% endif %}

{% if enable_ops %}
add_heir_dialect_library(
    name = "ops_inc_gen",
    dialect = "{{ dialect_name }}",
    kind = "op",
    td_file = "{{ dialect_name }}Ops.td",
    deps = [
        ":td_files",
    ],
)
{% endif %}
