13#include "mlir/CAPI/Registration.h"
14#include "mlir/CAPI/Wrap.h"
15#include "mlir/IR/Location.h"
16#include "llvm/ADT/Hashing.h"
17#include "llvm/Support/Casting.h"
46 return wrap(cast<ClassType>(
unwrap(type)).getClassName().getAttr());
51 return isa<FrozenBasePathType>(
unwrap(type));
56 return wrap(FrozenBasePathType::getTypeID());
61 return isa<FrozenPathType>(
unwrap(type));
66 return wrap(FrozenPathType::getTypeID());
77 return wrap(cast<ListType>(
unwrap(type)).getElementType());
82 return isa<StringType>(
unwrap(type));
95 return wrap(cast<MapType>(
unwrap(type)).getKeyType());
133 MlirAttribute className,
134 intptr_t nActualParams,
140 StringAttr cppClassName = cast<StringAttr>(
unwrap(className));
143 SmallVector<std::shared_ptr<evaluator::EvaluatorValue>> cppActualParams;
144 for (
unsigned i = 0; i < nActualParams; i++)
145 cppActualParams.push_back(
unwrap(actualParams[i]));
148 auto result = cppEvaluator->
instantiate(cppClassName, cppActualParams);
156 return wrap(result.value());
177 return wrap(llvm::cast<Object>(
unwrap(
object).get())->getType());
182 return llvm::hash_value(llvm::cast<Object>(
unwrap(
object).get()));
187 return llvm::cast<Object>(
unwrap(
object).get()) ==
188 llvm::cast<Object>(
unwrap(other).get());
193 return wrap(llvm::cast<Object>(
unwrap(
object).get())->getFieldNames());
197 return wrap(llvm::cast<evaluator::MapValue>(
unwrap(value).get())->getType());
202 return wrap(llvm::cast<evaluator::MapValue>(
unwrap(
object).get())->getKeys());
207 MlirAttribute name) {
210 FailureOr<EvaluatorValuePtr> result =
211 llvm::cast<Object>(
unwrap(
object).get())
212 ->getField(cast<StringAttr>(
unwrap(name)));
228 return wrap(
unwrap(evaluatorValue)->getContext());
239 return !evaluatorValue.
ptr;
245 return isa<evaluator::ObjectValue>(
unwrap(evaluatorValue).get());
251 return isa<evaluator::AttributeValue>(
unwrap(evaluatorValue).get());
259 llvm::cast<evaluator::AttributeValue>(
unwrap(evaluatorValue).get())
266 return wrap(std::make_shared<evaluator::AttributeValue>(
unwrap(primitive)));
271 return isa<evaluator::ListValue>(
unwrap(evaluatorValue).get());
279 return evaluatorValue;
284 return cast<evaluator::ListValue>(
unwrap(evaluatorValue).get())
292 return wrap(cast<evaluator::ListValue>(
unwrap(evaluatorValue).get())
293 ->getElements()[pos]);
298 return isa<evaluator::TupleValue>(
unwrap(evaluatorValue).get());
303 return cast<evaluator::TupleValue>(
unwrap(evaluatorValue).get())
311 return wrap(cast<evaluator::TupleValue>(
unwrap(evaluatorValue).get())
312 ->getElements()[pos]);
317 MlirAttribute attr) {
318 const auto &elements =
319 cast<evaluator::MapValue>(
unwrap(evaluatorValue).get())->getElements();
320 const auto &it = elements.find(
unwrap(attr));
321 if (it != elements.end())
322 return wrap(it->second);
328 return isa<evaluator::MapValue>(
unwrap(evaluatorValue).get());
332 return isa<evaluator::BasePathValue>(
unwrap(evaluatorValue).get());
336 return wrap(std::make_shared<evaluator::BasePathValue>(
unwrap(context)));
340 return isa<evaluator::PathValue>(
unwrap(evaluatorValue).get());
344 const auto *path = cast<evaluator::PathValue>(
unwrap(evaluatorValue).get());
345 return wrap((Attribute)path->getAsString());
350 return isa<evaluator::ReferenceValue>(
unwrap(evaluatorValue).get());
362 llvm::cast<evaluator::ReferenceValue>(
unwrap(evaluatorValue).get())
363 ->getStrippedValue();
370 return wrap(result.value());
378 return isa<ReferenceAttr>(
unwrap(attr));
383 (Attribute)cast<ReferenceAttr>(
unwrap(referenceAttr)).getInnerRef());
391 return isa<circt::om::IntegerAttr>(
unwrap(attr));
395 return wrap(cast<circt::om::IntegerAttr>(
unwrap(attr)).getValue());
399 auto integerAttr = cast<mlir::IntegerAttr>(
unwrap(attr));
401 circt::om::IntegerAttr::get(integerAttr.getContext(), integerAttr));
406 mlir::IntegerAttr integerAttr =
407 cast<circt::om::IntegerAttr>(
unwrap(attr)).getValue();
408 SmallVector<char> str;
409 integerAttr.getValue().toString(
410 str, 10, integerAttr.getType().isSignedInteger());
411 return wrap(StringAttr::get(integerAttr.getContext(), str).getValue());
419 return isa<ListAttr>(
unwrap(attr));
423 auto listAttr = llvm::cast<ListAttr>(
unwrap(attr));
424 return static_cast<intptr_t
>(listAttr.getElements().size());
428 auto listAttr = llvm::cast<ListAttr>(
unwrap(attr));
429 return wrap(listAttr.getElements()[pos]);
439 auto mapAttr = llvm::cast<MapAttr>(
unwrap(attr));
440 return static_cast<intptr_t
>(mapAttr.getElements().size());
444 auto mapAttr = llvm::cast<MapAttr>(
unwrap(attr));
445 return wrap(mapAttr.getElements().getValue()[pos].getName());
449 auto mapAttr = llvm::cast<MapAttr>(
unwrap(attr));
450 return wrap(mapAttr.getElements().getValue()[pos].getValue());
assert(baseType &&"element must be base type")
MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(CHIRRTL, chirrtl, circt::chirrtl::CHIRRTLDialect) MlirType chirrtlTypeGetCMemory(MlirContext ctx
static Location getLoc(DefSlot slot)
bool omTypeIsAFrozenBasePathType(MlirType type)
Is the Type a FrozenBasePathType.
bool omEvaluatorValueIsABasePath(OMEvaluatorValue evaluatorValue)
Query if the EvaluatorValue is a BasePath.
MlirAttribute omMapAttrGetElementValue(MlirAttribute attr, intptr_t pos)
bool omEvaluatorValueIsNull(OMEvaluatorValue evaluatorValue)
MlirContext omEvaluatorValueGetContext(OMEvaluatorValue evaluatorValue)
MlirAttribute omReferenceAttrGetInnerRef(MlirAttribute referenceAttr)
bool omTypeIsAStringType(MlirType type)
Is the Type a StringType.
static OMEvaluatorValue wrap(EvaluatorValuePtr object)
Define our own wrap and unwrap instead of using the usual macro.
MlirAttribute omEvaluatorPathGetAsString(OMEvaluatorValue evaluatorValue)
Get a string representation of a Path.
bool omEvaluatorValueIsAPath(OMEvaluatorValue evaluatorValue)
Query if the EvaluatorValue is a Path.
MlirIdentifier omMapAttrGetElementKey(MlirAttribute attr, intptr_t pos)
bool omAttrIsAListAttr(MlirAttribute attr)
OMEvaluatorValue omEvaluatorListGetElement(OMEvaluatorValue evaluatorValue, intptr_t pos)
Get an element of the List.
bool omTypeIsAMapType(MlirType type)
Is the Type a MapType.
OMEvaluatorValue omEvaluatorValueFromPrimitive(MlirAttribute primitive)
Get the Primitive from an EvaluatorValue, which must contain a Primitive.
MlirStringRef omIntegerAttrToString(MlirAttribute attr)
Get a string representation of an om::IntegerAttr.
OMEvaluatorValue omEvaluatorInstantiate(OMEvaluator evaluator, MlirAttribute className, intptr_t nActualParams, OMEvaluatorValue *actualParams)
Use the Evaluator to Instantiate an Object from its class name and actual parameters.
MlirType omEvaluatorMapGetType(OMEvaluatorValue value)
Get the Type from a Map, which will be a MapType.
intptr_t omListAttrGetNumElements(MlirAttribute attr)
MlirAttribute omEvaluatorObjectGetFieldNames(OMEvaluatorValue object)
Get an ArrayAttr with the names of the fields in an Object.
bool omAttrIsAIntegerAttr(MlirAttribute attr)
OMEvaluatorValue omEvaluatorObjectGetField(OMEvaluatorValue object, MlirAttribute name)
Get a field from an Object, which must contain a field of that name.
MlirLocation omEvaluatorValueGetLoc(OMEvaluatorValue evaluatorValue)
bool omEvaluatorValueIsAMap(OMEvaluatorValue evaluatorValue)
Query if the EvaluatorValue is a map.
MlirTypeID omListTypeGetTypeID(void)
Get the TypeID for a ListType.
MlirType omMapTypeGetKeyType(MlirType type)
Return a key type of a map.
MlirTypeID omFrozenBasePathTypeGetTypeID(void)
Get the TypeID for a FrozenBasePathType.
MlirType omStringTypeGet(MlirContext ctx)
Get a StringType.
bool omTypeIsAClassType(MlirType type)
Is the Type a ClassType.
OMEvaluatorValue omEvaluatorTupleGetElement(OMEvaluatorValue evaluatorValue, intptr_t pos)
Get an element of the Tuple.
MlirIdentifier omClassTypeGetName(MlirType type)
Get the name for a ClassType.
OMEvaluatorValue omEvaluatorValueGetReferenceValue(OMEvaluatorValue evaluatorValue)
Dereference a Reference EvaluatorValue.
bool omTypeIsAListType(MlirType type)
Is the Type a ListType.
intptr_t omEvaluatorListGetNumElements(OMEvaluatorValue evaluatorValue)
Get the length of the List.
MlirAttribute omEvaluatorValueGetPrimitive(OMEvaluatorValue evaluatorValue)
Get the Primitive from an EvaluatorValue, which must contain a Primitive.
MlirAttribute omEvaluatorMapGetKeys(OMEvaluatorValue object)
Get an ArrayAttr with the keys in a Map.
MlirAttribute omIntegerAttrGetInt(MlirAttribute attr)
Given an om::IntegerAttr, return the mlir::IntegerAttr.
bool omEvaluatorValueIsAList(OMEvaluatorValue evaluatorValue)
Query if the EvaluatorValue is a List.
bool omTypeIsAFrozenPathType(MlirType type)
Is the Type a FrozenPathType.
bool omAttrIsAMapAttr(MlirAttribute attr)
OMEvaluatorValue omEvaluatorMapGetElement(OMEvaluatorValue evaluatorValue, MlirAttribute attr)
Get an element of the Map.
bool omEvaluatorValueIsAPrimitive(OMEvaluatorValue evaluatorValue)
Query if the EvaluatorValue is a Primitive.
bool omEvaluatorValueIsATuple(OMEvaluatorValue evaluatorValue)
Query if the EvaluatorValue is a Tuple.
bool omEvaluatorObjectIsNull(OMEvaluatorValue object)
Query if the Object is null.
MlirModule omEvaluatorGetModule(OMEvaluator evaluator)
Get the Module the Evaluator is built from.
OMEvaluator omEvaluatorNew(MlirModule mod)
Construct an Evaluator with an IR module.
OMEvaluatorValue omEvaluatorBasePathGetEmpty(MlirContext context)
Create an empty BasePath.
MlirAttribute omListAttrGetElement(MlirAttribute attr, intptr_t pos)
bool omTypeIsAAnyType(MlirType type)
Is the Type an AnyType.
MlirTypeID omAnyTypeGetTypeID(void)
Get the TypeID for an AnyType.
MlirTypeID omFrozenPathTypeGetTypeID(void)
Get the TypeID for a FrozenPathType.
intptr_t omMapAttrGetNumElements(MlirAttribute attr)
MlirType omEvaluatorObjectGetType(OMEvaluatorValue object)
Get the Type from an Object, which will be a ClassType.
intptr_t omEvaluatorTupleGetNumElements(OMEvaluatorValue evaluatorValue)
Get the length of the Tuple.
bool omAttrIsAReferenceAttr(MlirAttribute attr)
static EvaluatorValuePtr unwrap(OMEvaluatorValue c)
MlirAttribute omIntegerAttrGet(MlirAttribute attr)
Get an om::IntegerAttr from mlir::IntegerAttr.
OMEvaluatorValue omEvaluatorValueGetList(OMEvaluatorValue evaluatorValue)
Get the List from an EvaluatorValue, which must contain a List.
bool omEvaluatorValueIsAObject(OMEvaluatorValue evaluatorValue)
Query if the EvaluatorValue is an Object.
MlirType omListTypeGetElementType(MlirType type)
MlirTypeID omClassTypeGetTypeID()
Get the TypeID for a ClassType.
bool omEvaluatorValueIsAReference(OMEvaluatorValue evaluatorValue)
Query if the EvaluatorValue is a Reference.
unsigned omEvaluatorObjectGetHash(OMEvaluatorValue object)
Get the hash for the object.
bool omEvaluatorObjectIsEq(OMEvaluatorValue object, OMEvaluatorValue other)
Check if two objects are same.
evaluator::EvaluatorValuePtr EvaluatorValuePtr
A value type for use in C APIs that just wraps a pointer to an Object.
A value type for use in C APIs that just wraps a pointer to an Evaluator.
An Evaluator, which is constructed with an IR module and can instantiate Objects.
FailureOr< evaluator::EvaluatorValuePtr > instantiate(StringAttr className, ArrayRef< EvaluatorValuePtr > actualParams)
Instantiate an Object with its class name and actual parameters.
Base class for evaluator runtime values.