[PATCH] D127948: [TableGen] Add new operator !instanceof

Wang Pengcheng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 16 02:43:53 PDT 2022


pcwang-thead created this revision.
pcwang-thead added reviewers: tra, nhaehnle, craig.topper, jdoerfert.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
pcwang-thead requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

We can cast a string to a record via !cast, but we have no mechanism
to check if it is valid and TableGen will raise an error if failed to
cast. Besides, we have no semantic null in TableGen (we have `?` but
different backends handle uninitialized value differently), so operator
like `dyn_cast<>` is hard to implement.

In this patch, we add a new operator `!instance<T>(s)` to check whether
a record with type `T` and name `s` exists. Self-references are allowed
just like `!cast`.

By doing these, we can write code like:

  class dyn_cast_to_record<string r> {
    R value = !if(!instanceof<R>(r), !cast<R>(r), default_value);
  }
  defvar v = dyn_cast_to_record<"R0">.value; // R0 or default_value.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D127948

Files:
  llvm/docs/TableGen/ProgRef.rst
  llvm/include/llvm/TableGen/Record.h
  llvm/lib/TableGen/Record.cpp
  llvm/lib/TableGen/TGLexer.cpp
  llvm/lib/TableGen/TGLexer.h
  llvm/lib/TableGen/TGParser.cpp
  llvm/test/TableGen/instanceof-error.td
  llvm/test/TableGen/instanceof.td

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D127948.437483.patch
Type: text/x-patch
Size: 10094 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220616/2206acb6/attachment.bin>


More information about the llvm-commits mailing list