[PATCH] D71474: [TableGen] Introduce an if/then/else statement.

Simon Tatham via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 13 09:29:00 PST 2019


simon_tatham created this revision.
simon_tatham added reviewers: nhaehnle, hfinkel.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.
simon_tatham added a comment.

This is a hasty first draft because I'm about to go away for the holidays, and won't have access to this account until the New Year.

It certainly needs documentation, and might also benefit from a refactoring of the mechanism (perhaps the stack of ForeachLoops ought to become a stack of some kind of polymorphic type that can be either a loop or an if).

This patch applies on top of the `defvar` one (D71407 <https://reviews.llvm.org/D71407>), and in this version, I've made it so that unlike a foreach, an if statement //doesn't// begin a new local-variable scope. I think that might be useful because that way you can conditionally define the same local var in both branches of the if, and then use it afterwards.


This allows you to make some of the defs in a multiclass or `foreach`
conditional on an expression computed from the parameters or iteration
variables.

It was already possible to simulate an if statement using a `foreach`
with a dummy iteration variable and a list constructed using `!if` so
that it had length 0 or 1 depending on the condition, e.g.

  foreach unusedIterationVar = !if(condition, [1], []<int>) in { ... }

But this syntax is nicer to read, and also more convenient because it
allows an else clause.

To avoid upheaval in the implementation, I've implemented `if` as pure
syntactic sugar on the `foreach` implementation: internally, `ParseIf`
actually does construct exactly the kind of foreach shown above (and
another reversed one for the else clause if present).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D71474

Files:
  llvm/lib/TableGen/TGLexer.cpp
  llvm/lib/TableGen/TGLexer.h
  llvm/lib/TableGen/TGParser.cpp
  llvm/lib/TableGen/TGParser.h
  llvm/test/TableGen/ifstmt.td

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D71474.233820.patch
Type: text/x-patch
Size: 8683 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191213/29fa8073/attachment.bin>


More information about the llvm-commits mailing list