Confusion in !cast bang operator

In following class is defined:

class RISCVVIntrinsic {
  // These intrinsics may accept illegal integer values in their llvm_any_ty
  // operand, so they have to be extended.
  Intrinsic IntrinsicID = !cast<Intrinsic>(NAME);
  bits<4> ScalarOperand = NoScalarOperand;
  bits<5> VLOperand = NoVLOperand;

What is !cast doing in this class and what is NAME ?

There is an “implicit template argument named NAME”.

Printing the records of the following shows string h = "MyRecord" in the def record:

class P<string n> {
  string name = n;
  string h = NAME;

def MyRecord : P<"Test"> {

The string literal "NAME" is handled specially in llvm/lib/TableGen/TGParser.cpp.

For what !cast is, you can read its description in 1   TableGen Programmer’s Reference — LLVM 16.0.0git documentation