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 }
36
37 IfDefEmitter(const IfDefEmitter &) = delete;
39
41 OS << "\n";
42 if (LateUndef)
43 OS << "#undef " << Name << "\n";
44 OS << "#endif // " << Name << "\n\n";
45 }
46
47private:
48 std::string Name;
49 raw_ostream &OS;
50 bool LateUndef;
51};
52
53// Base class for RAII helpers for emitting various if guards.
55protected:
57 : Condition(Condition.str()), OS(OS) {
58 OS << If << " " << Condition << "\n\n";
59 }
60
63
64 ~IfGuardEmitterBase() { OS << "\n#endif // " << Condition << "\n\n"; }
65
66private:
67 std::string Condition;
68 raw_ostream &OS;
69};
70
71// RAII emitter for:
72// #if <Condition>
73// #endif // <Condition>
75public:
77 : IfGuardEmitterBase(OS, "#if", Condition) {}
78};
79
80// RAII emitter for:
81// #ifdef <Condition>
82// #endif // <Condition>
84public:
86 : IfGuardEmitterBase(OS, "#ifdef", Condition) {}
87};
88
89// RAII emitter for:
90// #ifndef <Condition>
91// #endif // <Condition>
93public:
95 : IfGuardEmitterBase(OS, "#ifndef", Condition) {}
96};
97
98// Simple RAII helper for emitting header include guard (ifndef-define-endif).
100public:
102 : Name(Name.str()), OS(OS) {
103 OS << "#ifndef " << Name << "\n"
104 << "#define " << Name << "\n\n";
105 }
106
109
110 ~IncludeGuardEmitter() { OS << "\n#endif // " << Name << "\n\n"; }
111
112private:
113 std::string Name;
114 raw_ostream &OS;
115};
116
117// Simple RAII helper for emitting namespace scope. Name can be a single
118// namespace or nested namespace. If the name is empty, will not generate any
119// namespace scope.
121public:
122 NamespaceEmitter(raw_ostream &OS, const Twine &NameUntrimmed)
123 : Name(trim(NameUntrimmed.str()).str()), OS(OS) {
124 if (!Name.empty())
125 OS << "namespace " << Name << " {\n\n";
126 }
127
130
132 if (!Name.empty())
133 OS << "\n} // namespace " << Name << "\n";
134 }
135
136private:
137 // Trim "::" prefix. If the namespace specified is ""::mlir::toy", then the
138 // generated namespace scope needs to use
139 //
140 // namespace mlir::toy {
141 // }
142 //
143 // and cannot use "namespace ::mlir::toy".
144 static StringRef trim(StringRef Name) {
145 Name.consume_front("::");
146 return Name;
147 }
148
149 std::string Name;
150 raw_ostream &OS;
151};
152
153// Simple RAII helper for emitting anonymous namespace scope.
155public:
156 AnonNamespaceEmitter(raw_ostream &OS) : OS(OS) { OS << "namespace {\n\n"; }
159 ~AnonNamespaceEmitter() { OS << "} // namespace\n"; }
160
161private:
162 raw_ostream &OS;
163};
164
165} // end namespace llvm
166
167#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(const AnonNamespaceEmitter &)=delete
AnonNamespaceEmitter(raw_ostream &OS)
AnonNamespaceEmitter & operator=(const AnonNamespaceEmitter &)=delete
IfDefEmitter & operator=(const IfDefEmitter &)=delete
IfDefEmitter(raw_ostream &OS, StringRef Name, bool LateUndef=false)
IfDefEmitter(const IfDefEmitter &)=delete
IfDefGuardEmitter(raw_ostream &OS, StringRef Condition)
IfGuardEmitterBase & operator=(const IfGuardEmitterBase &)=delete
IfGuardEmitterBase(raw_ostream &OS, StringRef If, StringRef Condition)
IfGuardEmitterBase(const IfGuardEmitterBase &)=delete
IfGuardEmitter(raw_ostream &OS, StringRef Condition)
IfNDefGuardEmitter(raw_ostream &OS, StringRef Condition)
IncludeGuardEmitter & operator=(const IncludeGuardEmitter &)=delete
IncludeGuardEmitter(const IncludeGuardEmitter &)=delete
IncludeGuardEmitter(raw_ostream &OS, StringRef Name)
NamespaceEmitter(raw_ostream &OS, const Twine &NameUntrimmed)
NamespaceEmitter(const NamespaceEmitter &)=delete
NamespaceEmitter & operator=(const NamespaceEmitter &)=delete
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