xref: /llvm-project/llvm/include/llvm/TableGen/TableGenBackend.h (revision b91f0def6f261064cedfe0e62395c0097130b8f1)
1 //===- llvm/TableGen/TableGenBackend.h - Backend utilities ------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Useful utilities for TableGen backends.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_TABLEGEN_TABLEGENBACKEND_H
14 #define LLVM_TABLEGEN_TABLEGENBACKEND_H
15 
16 #include "llvm/ADT/STLFunctionalExtras.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/TableGen/Record.h"
19 
20 namespace llvm {
21 
22 class RecordKeeper;
23 class raw_ostream;
24 
25 namespace TableGen::Emitter {
26 using FnT = function_ref<void(const RecordKeeper &Records, raw_ostream &OS)>;
27 
28 /// Creating an `Opt` object registers the command line option \p Name with
29 /// TableGen backend and associates the callback \p CB with that option. If
30 /// \p ByDefault is true, then that callback is applied by default if no
31 /// command line option was specified.
32 struct Opt {
33   Opt(StringRef Name, FnT CB, StringRef Desc, bool ByDefault = false);
34 };
35 
36 /// Convienence wrapper around `Opt` that registers `EmitterClass::run` as the
37 /// callback.
38 template <class EmitterC> class OptClass : Opt {
39   static void run(const RecordKeeper &RK, raw_ostream &OS) {
40     EmitterC(RK).run(OS);
41   }
42 
43 public:
44   OptClass(StringRef Name, StringRef Desc) : Opt(Name, run, Desc) {}
45 };
46 
47 /// Apply callback for any command line option registered above. Returns false
48 /// is no callback was applied.
49 bool ApplyCallback(const RecordKeeper &Records, raw_ostream &OS);
50 
51 } // namespace TableGen::Emitter
52 
53 /// emitSourceFileHeader - Output an LLVM style file header to the specified
54 /// raw_ostream.
55 void emitSourceFileHeader(StringRef Desc, raw_ostream &OS,
56                           const RecordKeeper &Record = RecordKeeper());
57 
58 } // namespace llvm
59 
60 #endif // LLVM_TABLEGEN_TABLEGENBACKEND_H
61