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));
98 return wrap(cast<MapType>(
unwrap(type)).getKeyType());
136 MlirAttribute className,
137 intptr_t nActualParams,
143 StringAttr cppClassName = cast<StringAttr>(
unwrap(className));
146 SmallVector<std::shared_ptr<evaluator::EvaluatorValue>> cppActualParams;
147 for (
unsigned i = 0; i < nActualParams; i++)
148 cppActualParams.push_back(
unwrap(actualParams[i]));
151 auto result = cppEvaluator->
instantiate(cppClassName, cppActualParams);
159 return wrap(result.value());
180 return wrap(llvm::cast<Object>(
unwrap(
object).get())->getType());
185 return llvm::hash_value(llvm::cast<Object>(
unwrap(
object).get()));
190 return llvm::cast<Object>(
unwrap(
object).get()) ==
191 llvm::cast<Object>(
unwrap(other).get());
196 return wrap(llvm::cast<Object>(
unwrap(
object).get())->getFieldNames());
200 return wrap(llvm::cast<evaluator::MapValue>(
unwrap(value).get())->getType());
205 return wrap(llvm::cast<evaluator::MapValue>(
unwrap(
object).get())->getKeys());
210 MlirAttribute name) {
213 FailureOr<EvaluatorValuePtr> result =
214 llvm::cast<Object>(
unwrap(
object).get())
215 ->getField(cast<StringAttr>(
unwrap(name)));
231 return wrap(
unwrap(evaluatorValue)->getContext());
242 return !evaluatorValue.
ptr;
248 return isa<evaluator::ObjectValue>(
unwrap(evaluatorValue).get());
254 return isa<evaluator::AttributeValue>(
unwrap(evaluatorValue).get());
262 llvm::cast<evaluator::AttributeValue>(
unwrap(evaluatorValue).get())
274 return isa<evaluator::ListValue>(
unwrap(evaluatorValue).get());
282 return evaluatorValue;
287 return cast<evaluator::ListValue>(
unwrap(evaluatorValue).get())
295 return wrap(cast<evaluator::ListValue>(
unwrap(evaluatorValue).get())
296 ->getElements()[pos]);
301 return isa<evaluator::TupleValue>(
unwrap(evaluatorValue).get());
306 return cast<evaluator::TupleValue>(
unwrap(evaluatorValue).get())
314 return wrap(cast<evaluator::TupleValue>(
unwrap(evaluatorValue).get())
315 ->getElements()[pos]);
320 MlirAttribute attr) {
321 const auto &elements =
322 cast<evaluator::MapValue>(
unwrap(evaluatorValue).get())->getElements();
323 const auto &it = elements.find(
unwrap(attr));
324 if (it != elements.end())
325 return wrap(it->second);
331 return isa<evaluator::MapValue>(
unwrap(evaluatorValue).get());
335 return isa<evaluator::BasePathValue>(
unwrap(evaluatorValue).get());
339 return wrap(std::make_shared<evaluator::BasePathValue>(
unwrap(context)));
343 return isa<evaluator::PathValue>(
unwrap(evaluatorValue).get());
347 const auto *path = cast<evaluator::PathValue>(
unwrap(evaluatorValue).get());
348 return wrap((Attribute)path->getAsString());
353 return isa<evaluator::ReferenceValue>(
unwrap(evaluatorValue).get());
365 llvm::cast<evaluator::ReferenceValue>(
unwrap(evaluatorValue).get())
366 ->getStrippedValue();
373 return wrap(result.value());
381 return isa<ReferenceAttr>(
unwrap(attr));
386 (Attribute)cast<ReferenceAttr>(
unwrap(referenceAttr)).getInnerRef());
394 return isa<circt::om::IntegerAttr>(
unwrap(attr));
398 return wrap(cast<circt::om::IntegerAttr>(
unwrap(attr)).getValue());
402 auto integerAttr = cast<mlir::IntegerAttr>(
unwrap(attr));
404 circt::om::IntegerAttr::get(integerAttr.getContext(), integerAttr));
409 mlir::IntegerAttr integerAttr =
410 cast<circt::om::IntegerAttr>(
unwrap(attr)).getValue();
411 SmallVector<char> str;
412 integerAttr.getValue().toString(
413 str, 10, integerAttr.getType().isSignedInteger());
414 return wrap(StringAttr::get(integerAttr.getContext(), str).getValue());
422 return isa<ListAttr>(
unwrap(attr));
426 auto listAttr = llvm::cast<ListAttr>(
unwrap(attr));
427 return static_cast<intptr_t
>(listAttr.getElements().size());
431 auto listAttr = llvm::cast<ListAttr>(
unwrap(attr));
432 return wrap(listAttr.getElements()[pos]);
436 const MlirAttribute *elements) {
437 SmallVector<Attribute, 8> attrs;
438 (void)unwrapList(
static_cast<size_t>(
numElements), elements, attrs);
440 auto *ctx = type.getContext();
441 return wrap(ListAttr::get(ctx, type, ArrayAttr::get(ctx, attrs)));
451 auto mapAttr = llvm::cast<MapAttr>(
unwrap(attr));
452 return static_cast<intptr_t
>(mapAttr.getElements().size());
456 auto mapAttr = llvm::cast<MapAttr>(
unwrap(attr));
457 return wrap(mapAttr.getElements().getValue()[pos].getName());
461 auto mapAttr = llvm::cast<MapAttr>(
unwrap(attr));
462 return wrap(mapAttr.getElements().getValue()[pos].getValue());
assert(baseType &&"element must be base type")
MlirType uint64_t numElements
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.
MlirTypeID omStringTypeGetTypeID(void)
Get the TypeID for a StringType.
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 omListAttrGet(MlirType elementType, intptr_t numElements, const MlirAttribute *elements)
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.
friend std::shared_ptr< EvaluatorValue > get(Attribute attr, LocationAttr loc)
Base class for evaluator runtime values.