LLVM 23.0.0git
CodeGenHelpers.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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// This file defines common utilities for generating C++ code.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_TABLEGEN_CODEGENHELPERS_H
14#define LLVM_TABLEGEN_CODEGENHELPERS_H
15
16#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/StringRef.h"
20#include <string>
21
22namespace llvm {
23
24// Simple RAII helper for emitting ifdef-undef-endif scope. `LateUndef` controls
25// whether the undef is emitted at the start of the scope (false) or at the end
26// of the scope (true).
28public:
29 IfDefEmitter(raw_ostream &OS, StringRef Name, bool LateUndef = false)
30 : Name(Name.str()), OS(OS), LateUndef(LateUndef) {
31 OS << "#ifdef " << Name << "\n";
32 if (!LateUndef)
33 OS << "#undef " << Name << "\n";
34 OS << "\n";
35 }
37 OS << "\n";
38 if (LateUndef)
39 OS << "#undef " << Name << "\n";
40 OS << "#endif // " << Name << "\n\n";
41 }
42
43private:
44 std::string Name;
45 raw_ostream &OS;
46 bool LateUndef;
47};
48
49// Base class for RAII helpers for emitting various if guards.
51protected:
53 : Condition(Condition.str()), OS(OS) {
54 OS << If << " " << Condition << "\n\n";
55 }
56
57 ~IfGuardEmitterBase() { OS << "\n#endif // " << Condition << "\n\n"; }
58
59private:
60 std::string Condition;
61 raw_ostream &OS;
62};
63
64// RAII emitter for:
65// #if <Condition>
66// #endif // <Condition>
68public:
70 : IfGuardEmitterBase(OS, "#if", Condition) {}
71};
72
73// RAII emitter for:
74// #ifdef <Condition>
75// #endif // <Condition>
77public:
79 : IfGuardEmitterBase(OS, "#ifdef", Condition) {}
80};
81
82// RAII emitter for:
83// #ifndef <Condition>
84// #endif // <Condition>
86public:
88 : IfGuardEmitterBase(OS, "#ifndef", Condition) {}
89};
90
91// Simple RAII helper for emitting header include guard (ifndef-define-endif).
93public:
95 : Name(Name.str()), OS(OS) {
96 OS << "#ifndef " << Name << "\n"
97 << "#define " << Name << "\n\n";
98 }
99 ~IncludeGuardEmitter() { OS << "\n#endif // " << Name << "\n\n"; }
100
101private:
102 std::string Name;
103 raw_ostream &OS;
104};
105
106// Simple RAII helper for emitting namespace scope. Name can be a single
107// namespace or nested namespace. If the name is empty, will not generate any
108// namespace scope.
110public:
111 NamespaceEmitter(raw_ostream &OS, const Twine &NameUntrimmed)
112 : Name(trim(NameUntrimmed.str()).str()), OS(OS) {
113 if (!Name.empty())
114 OS << "namespace " << Name << " {\n\n";
115 }
116
118 if (!Name.empty())
119 OS << "\n} // namespace " << Name << "\n";
120 }
121
122private:
123 // Trim "::" prefix. If the namespace specified is ""::mlir::toy", then the
124 // generated namespace scope needs to use
125 //
126 // namespace mlir::toy {
127 // }
128 //
129 // and cannot use "namespace ::mlir::toy".
130 static StringRef trim(StringRef Name) {
131 Name.consume_front("::");
132 return Name;
133 }
134
135 std::string Name;
136 raw_ostream &OS;
137};
138
139// Simple RAII helper for emitting anonymous namespace scope.
141public:
142 AnonNamespaceEmitter(raw_ostream &OS) : OS(OS) { OS << "namespace {\n\n"; }
143 ~AnonNamespaceEmitter() { OS << "} // namespace\n"; }
144
145private:
146 raw_ostream &OS;
147};
148
149} // end namespace llvm
150
151#endif // LLVM_TABLEGEN_CODEGENHELPERS_H
This file contains some templates that are useful if you are working with the STL at all.
This file contains some functions that are useful when dealing with strings.
AnonNamespaceEmitter(raw_ostream &OS)
IfDefEmitter(raw_ostream &OS, StringRef Name, bool LateUndef=false)
IfDefGuardEmitter(raw_ostream &OS, StringRef Condition)
IfGuardEmitterBase(raw_ostream &OS, StringRef If, StringRef Condition)
IfGuardEmitter(raw_ostream &OS, StringRef Condition)
IfNDefGuardEmitter(raw_ostream &OS, StringRef Condition)
IncludeGuardEmitter(raw_ostream &OS, StringRef Name)
NamespaceEmitter(raw_ostream &OS, const Twine &NameUntrimmed)
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26