19
19
20
20
#include " swift/Basic/TaggedUnion.h"
21
21
#include " swift/SIL/SILArgument.h"
22
+ #include " swift/SIL/SILBasicBlock.h"
23
+ #include " swift/SIL/SILFunction.h"
22
24
#include " swift/SIL/SILInstruction.h"
23
25
#include " swift/SIL/SILValue.h"
24
26
#include " llvm/ADT/StringRef.h"
27
+ #include " llvm/Support/FormattedStream.h"
25
28
26
29
namespace llvm {
27
30
template <class T >
@@ -68,10 +71,15 @@ struct Argument {
68
71
69
72
public:
70
73
Kind getKind () const { return kind; }
74
+ void print (llvm::raw_ostream &os);
75
+ #ifndef NDEBUG
76
+ void dump () { print (llvm::errs ()); }
77
+ #endif
71
78
};
72
79
73
- template <typename Stored , Argument::Kind TheKind>
80
+ template <typename _Stored , Argument::Kind TheKind>
74
81
struct ConcreteArgument : Argument {
82
+ using Stored = _Stored;
75
83
using Super = ConcreteArgument<Stored, TheKind>;
76
84
ConcreteArgument (Stored stored) : Argument(Union{stored}, TheKind) {}
77
85
Stored getValue () { return storage.get <Stored>(); }
@@ -142,13 +150,28 @@ struct Arguments {
142
150
Arguments &operator =(Arguments const &) = delete ;
143
151
~Arguments () { assertUsed (); }
144
152
Argument &takeArgument () { return storage[untakenIndex++]; }
145
- StringRef takeString () {
146
- return cast<StringArgument>(takeArgument ()).getValue ();
153
+
154
+ private:
155
+ template <typename Subtype>
156
+ typename Subtype::Stored getInstance (StringRef name, Argument &argument) {
157
+ if (isa<Subtype>(argument)) {
158
+ auto string = cast<Subtype>(argument).getValue ();
159
+ return string;
160
+ }
161
+ llvm::errs () << " Attempting to take a " << name << " argument but have\n " ;
162
+ argument.print (llvm::errs ());
163
+ llvm::report_fatal_error (" Bad unit test" );
147
164
}
148
- bool takeBool () { return cast<BoolArgument>(takeArgument ()).getValue (); }
149
- unsigned long long takeUInt () {
150
- return cast<UIntArgument>(takeArgument ()).getValue ();
165
+ template <typename Subtype>
166
+ typename Subtype::Stored takeInstance (StringRef name) {
167
+ auto argument = takeArgument ();
168
+ return getInstance<Subtype>(name, argument);
151
169
}
170
+
171
+ public:
172
+ StringRef takeString () { return takeInstance<StringArgument>(" string" ); }
173
+ bool takeBool () { return takeInstance<BoolArgument>(" bool" ); }
174
+ unsigned long long takeUInt () { return takeInstance<UIntArgument>(" uint" ); }
152
175
SILValue takeValue () {
153
176
auto argument = takeArgument ();
154
177
if (isa<InstructionArgument>(argument)) {
@@ -159,22 +182,18 @@ struct Arguments {
159
182
auto *arg = cast<BlockArgumentArgument>(argument).getValue ();
160
183
return arg;
161
184
}
162
- return cast<ValueArgument>(argument).getValue ();
163
- }
164
- Operand *takeOperand () {
165
- return cast<OperandArgument>(takeArgument ()).getValue ();
185
+ return getInstance<ValueArgument>(" value" , argument);
166
186
}
187
+ Operand *takeOperand () { return takeInstance<OperandArgument>(" operand" ); }
167
188
SILInstruction *takeInstruction () {
168
- return cast <InstructionArgument>(takeArgument ()). getValue ( );
189
+ return takeInstance <InstructionArgument>(" instruction " );
169
190
}
170
191
SILArgument *takeBlockArgument () {
171
- return cast<BlockArgumentArgument>(takeArgument ()).getValue ();
172
- }
173
- SILBasicBlock *takeBlock () {
174
- return cast<BlockArgument>(takeArgument ()).getValue ();
192
+ return takeInstance<BlockArgumentArgument>(" block argument" );
175
193
}
194
+ SILBasicBlock *takeBlock () { return takeInstance<BlockArgument>(" block" ); }
176
195
SILFunction *takeFunction () {
177
- return cast <FunctionArgument>(takeArgument ()). getValue ( );
196
+ return takeInstance <FunctionArgument>(" block " );
178
197
}
179
198
};
180
199
0 commit comments