1//===- SMT.cpp - C interface for the SMT dialect --------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "mlir-c/Dialect/SMT.h"
10#include "mlir/CAPI/Registration.h"
11#include "mlir/Dialect/SMT/IR/SMTAttributes.h"
12#include "mlir/Dialect/SMT/IR/SMTDialect.h"
13#include "mlir/Dialect/SMT/IR/SMTTypes.h"
14
15using namespace mlir;
16using namespace smt;
17
18//===----------------------------------------------------------------------===//
19// Dialect API.
20//===----------------------------------------------------------------------===//
21
22MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(SMT, smt, mlir::smt::SMTDialect)
23
24//===----------------------------------------------------------------------===//
25// Type API.
26//===----------------------------------------------------------------------===//
27
28bool mlirSMTTypeIsAnyNonFuncSMTValueType(MlirType type) {
29 return isAnyNonFuncSMTValueType(type: unwrap(c: type));
30}
31
32bool mlirSMTTypeIsAnySMTValueType(MlirType type) {
33 return isAnySMTValueType(type: unwrap(c: type));
34}
35
36bool mlirSMTTypeIsAArray(MlirType type) { return isa<ArrayType>(Val: unwrap(c: type)); }
37
38MlirType mlirSMTTypeGetArray(MlirContext ctx, MlirType domainType,
39 MlirType rangeType) {
40 return wrap(
41 cpp: ArrayType::get(context: unwrap(c: ctx), domainType: unwrap(c: domainType), rangeType: unwrap(c: rangeType)));
42}
43
44bool mlirSMTTypeIsABitVector(MlirType type) {
45 return isa<BitVectorType>(Val: unwrap(c: type));
46}
47
48MlirType mlirSMTTypeGetBitVector(MlirContext ctx, int32_t width) {
49 return wrap(cpp: BitVectorType::get(context: unwrap(c: ctx), width));
50}
51
52MlirStringRef mlirSMTBitVectorTypeGetName(void) {
53 return wrap(ref: BitVectorType::name);
54}
55
56MlirTypeID mlirSMTBitVectorTypeGetTypeID(void) {
57 return wrap(cpp: BitVectorType::getTypeID());
58}
59
60bool mlirSMTTypeIsABool(MlirType type) { return isa<BoolType>(Val: unwrap(c: type)); }
61
62MlirType mlirSMTTypeGetBool(MlirContext ctx) {
63 return wrap(cpp: BoolType::get(ctx: unwrap(c: ctx)));
64}
65
66MlirStringRef mlirSMTBoolTypeGetName(void) { return wrap(ref: BoolType::name); }
67
68MlirTypeID mlirSMTBoolTypeGetTypeID(void) {
69 return wrap(cpp: BoolType::getTypeID());
70}
71
72bool mlirSMTTypeIsAInt(MlirType type) { return isa<IntType>(Val: unwrap(c: type)); }
73
74MlirType mlirSMTTypeGetInt(MlirContext ctx) {
75 return wrap(cpp: IntType::get(ctx: unwrap(c: ctx)));
76}
77
78MlirStringRef mlirSMTIntTypeGetName(void) { return wrap(ref: IntType::name); }
79
80MlirTypeID mlirSMTIntTypeGetTypeID(void) { return wrap(cpp: IntType::getTypeID()); }
81
82bool mlirSMTTypeIsASMTFunc(MlirType type) {
83 return isa<SMTFuncType>(Val: unwrap(c: type));
84}
85
86MlirType mlirSMTTypeGetSMTFunc(MlirContext ctx, size_t numberOfDomainTypes,
87 const MlirType *domainTypes,
88 MlirType rangeType) {
89 SmallVector<Type> domainTypesVec;
90 domainTypesVec.reserve(N: numberOfDomainTypes);
91
92 for (size_t i = 0; i < numberOfDomainTypes; i++)
93 domainTypesVec.push_back(Elt: unwrap(c: domainTypes[i]));
94
95 return wrap(cpp: SMTFuncType::get(context: unwrap(c: ctx), domainTypes: domainTypesVec, rangeType: unwrap(c: rangeType)));
96}
97
98bool mlirSMTTypeIsASort(MlirType type) { return isa<SortType>(Val: unwrap(c: type)); }
99
100MlirType mlirSMTTypeGetSort(MlirContext ctx, MlirIdentifier identifier,
101 size_t numberOfSortParams,
102 const MlirType *sortParams) {
103 SmallVector<Type> sortParamsVec;
104 sortParamsVec.reserve(N: numberOfSortParams);
105
106 for (size_t i = 0; i < numberOfSortParams; i++)
107 sortParamsVec.push_back(Elt: unwrap(c: sortParams[i]));
108
109 return wrap(cpp: SortType::get(context: unwrap(c: ctx), identifier: unwrap(c: identifier), sortParams: sortParamsVec));
110}
111
112//===----------------------------------------------------------------------===//
113// Attribute API.
114//===----------------------------------------------------------------------===//
115
116bool mlirSMTAttrCheckBVCmpPredicate(MlirContext ctx, MlirStringRef str) {
117 return symbolizeBVCmpPredicate(unwrap(ref: str)).has_value();
118}
119
120bool mlirSMTAttrCheckIntPredicate(MlirContext ctx, MlirStringRef str) {
121 return symbolizeIntPredicate(unwrap(ref: str)).has_value();
122}
123
124bool mlirSMTAttrIsASMTAttribute(MlirAttribute attr) {
125 return isa<BitVectorAttr, BVCmpPredicateAttr, IntPredicateAttr>(Val: unwrap(c: attr));
126}
127
128MlirAttribute mlirSMTAttrGetBitVector(MlirContext ctx, uint64_t value,
129 unsigned width) {
130 return wrap(cpp: BitVectorAttr::get(context: unwrap(c: ctx), value, width));
131}
132
133MlirAttribute mlirSMTAttrGetBVCmpPredicate(MlirContext ctx, MlirStringRef str) {
134 auto predicate = symbolizeBVCmpPredicate(unwrap(ref: str));
135 assert(predicate.has_value() && "invalid predicate");
136
137 return wrap(cpp: BVCmpPredicateAttr::get(context: unwrap(c: ctx), val: predicate.value()));
138}
139
140MlirAttribute mlirSMTAttrGetIntPredicate(MlirContext ctx, MlirStringRef str) {
141 auto predicate = symbolizeIntPredicate(unwrap(ref: str));
142 assert(predicate.has_value() && "invalid predicate");
143
144 return wrap(cpp: IntPredicateAttr::get(context: unwrap(c: ctx), val: predicate.value()));
145}
146