20
20
#include " ir/manipulation.h"
21
21
#include " parsing.h"
22
22
#include " wasm.h"
23
+ #include < optional>
23
24
24
25
namespace wasm {
25
26
@@ -189,69 +190,47 @@ class Builder {
189
190
bool >;
190
191
191
192
template <typename T, bool_if_not_expr_t <T> = true >
192
- Block* makeBlock (const T& items) {
193
- auto * ret = wasm.allocator .alloc <Block>();
194
- ret->list .set (items);
195
- ret->finalize ();
196
- return ret;
197
- }
198
-
199
- template <typename T, bool_if_not_expr_t <T> = true >
200
- Block* makeBlock (const T& items, Type type) {
193
+ Block* makeBlock (const T& items, std::optional<Type> type = std::nullopt) {
201
194
auto * ret = wasm.allocator .alloc <Block>();
202
195
ret->list .set (items);
203
196
ret->finalize (type);
204
197
return ret;
205
198
}
206
199
207
200
template <typename T, bool_if_not_expr_t <T> = true >
208
- Block* makeBlock (Name name, const T& items, Type type) {
201
+ Block* makeBlock (Name name,
202
+ const T& items,
203
+ std::optional<Type> type = std::nullopt) {
209
204
auto * ret = wasm.allocator .alloc <Block>();
210
205
ret->name = name;
211
206
ret->list .set (items);
212
207
ret->finalize (type);
213
208
return ret;
214
209
}
215
- Block* makeBlock (std::initializer_list<Expression*>&& items) {
216
- return makeBlock (items);
217
- }
218
- Block* makeBlock (std::initializer_list<Expression*>&& items, Type type) {
210
+ Block* makeBlock (std::initializer_list<Expression*>&& items,
211
+ std::optional<Type> type = std::nullopt) {
219
212
return makeBlock (items, type);
220
213
}
221
- Block*
222
- makeBlock (Name name, std::initializer_list<Expression*>&& items, Type type) {
214
+ Block* makeBlock (Name name,
215
+ std::initializer_list<Expression*>&& items,
216
+ std::optional<Type> type = std::nullopt) {
223
217
return makeBlock (name, items, type);
224
218
}
225
219
226
220
If* makeIf (Expression* condition,
227
221
Expression* ifTrue,
228
- Expression* ifFalse = nullptr ) {
229
- auto * ret = wasm.allocator .alloc <If>();
230
- ret->condition = condition;
231
- ret->ifTrue = ifTrue;
232
- ret->ifFalse = ifFalse;
233
- ret->finalize ();
234
- return ret;
235
- }
236
- If* makeIf (Expression* condition,
237
- Expression* ifTrue,
238
- Expression* ifFalse,
239
- Type type) {
222
+ Expression* ifFalse = nullptr ,
223
+ std::optional<Type> type = std::nullopt) {
240
224
auto * ret = wasm.allocator .alloc <If>();
241
225
ret->condition = condition;
242
226
ret->ifTrue = ifTrue;
243
227
ret->ifFalse = ifFalse;
244
228
ret->finalize (type);
245
229
return ret;
246
230
}
247
- Loop* makeLoop (Name name, Expression* body) {
248
- auto * ret = wasm.allocator .alloc <Loop>();
249
- ret->name = name;
250
- ret->body = body;
251
- ret->finalize ();
252
- return ret;
253
- }
254
- Loop* makeLoop (Name name, Expression* body, Type type) {
231
+ Loop* makeLoop (Name name,
232
+ Expression* body,
233
+ std::optional<Type> type = std::nullopt) {
255
234
auto * ret = wasm.allocator .alloc <Loop>();
256
235
ret->name = name;
257
236
ret->body = body;
@@ -792,77 +771,47 @@ class Builder {
792
771
const std::vector<Name>& catchTags,
793
772
const std::vector<Expression*>& catchBodies,
794
773
Name delegateTarget,
795
- Type type,
796
- bool hasType) { // differentiate whether a type was passed in
774
+ std::optional<Type> type = std::nullopt) {
797
775
auto * ret = wasm.allocator .alloc <Try>();
798
776
ret->name = name;
799
777
ret->body = body;
800
778
ret->catchTags .set (catchTags);
801
779
ret->catchBodies .set (catchBodies);
802
- if (hasType) {
803
- ret->finalize (type);
804
- } else {
805
- ret->finalize ();
806
- }
780
+ ret->finalize (type);
807
781
return ret;
808
782
}
809
783
810
784
public:
811
- Try* makeTry (Expression* body,
812
- const std::vector<Name>& catchTags,
813
- const std::vector<Expression*>& catchBodies) {
814
- return makeTry (
815
- Name (), body, catchTags, catchBodies, Name (), Type::none, false );
816
- }
785
+ // TODO delete?
817
786
Try* makeTry (Expression* body,
818
787
const std::vector<Name>& catchTags,
819
788
const std::vector<Expression*>& catchBodies,
820
- Type type) {
821
- return makeTry (Name (), body, catchTags, catchBodies, Name (), type, true );
822
- }
823
- Try* makeTry (Name name,
824
- Expression* body,
825
- const std::vector<Name>& catchTags,
826
- const std::vector<Expression*>& catchBodies) {
827
- return makeTry (
828
- name, body, catchTags, catchBodies, Name (), Type::none, false );
789
+ std::optional<Type> type = std::nullopt) {
790
+ return makeTry (Name (), body, catchTags, catchBodies, Name (), type);
829
791
}
830
792
Try* makeTry (Name name,
831
793
Expression* body,
832
794
const std::vector<Name>& catchTags,
833
795
const std::vector<Expression*>& catchBodies,
834
- Type type) {
835
- return makeTry (name, body, catchTags, catchBodies, Name (), type, true );
836
- }
837
- Try* makeTry (Expression* body, Name delegateTarget) {
838
- return makeTry (Name (), body, {}, {}, delegateTarget, Type::none, false );
839
- }
840
- Try* makeTry (Expression* body, Name delegateTarget, Type type) {
841
- return makeTry (Name (), body, {}, {}, delegateTarget, type, true );
796
+ std::optional<Type> type = std::nullopt) {
797
+ return makeTry (name, body, catchTags, catchBodies, Name (), type);
842
798
}
843
- Try* makeTry (Name name, Expression* body, Name delegateTarget) {
844
- return makeTry (name, body, {}, {}, delegateTarget, Type::none, false );
845
- }
846
- Try* makeTry (Name name, Expression* body, Name delegateTarget, Type type) {
847
- return makeTry (name, body, {}, {}, delegateTarget, type, true );
799
+ Try* makeTry (Expression* body,
800
+ Name delegateTarget,
801
+ std::optional<Type> type = std::nullopt) {
802
+ return makeTry (Name (), body, {}, {}, delegateTarget, type);
848
803
}
849
- TryTable* makeTryTable (Expression* body,
850
- const std::vector<Name>& catchTags,
851
- const std::vector<Name>& catchDests,
852
- const std::vector<bool >& catchRefs) {
853
- auto * ret = wasm.allocator .alloc <TryTable>();
854
- ret->body = body;
855
- ret->catchTags .set (catchTags);
856
- ret->catchDests .set (catchDests);
857
- ret->catchRefs .set (catchRefs);
858
- ret->finalize (&wasm);
859
- return ret;
804
+ Try* makeTry (Name name,
805
+ Expression* body,
806
+ Name delegateTarget,
807
+ std::optional<Type> type = std::nullopt) {
808
+ return makeTry (name, body, {}, {}, delegateTarget, type);
860
809
}
861
810
TryTable* makeTryTable (Expression* body,
862
811
const std::vector<Name>& catchTags,
863
812
const std::vector<Name>& catchDests,
864
813
const std::vector<bool >& catchRefs,
865
- Type type) {
814
+ std::optional< Type> type = std::nullopt ) {
866
815
auto * ret = wasm.allocator .alloc <TryTable>();
867
816
ret->body = body;
868
817
ret->catchTags .set (catchTags);
@@ -1359,8 +1308,10 @@ class Builder {
1359
1308
// ensure a node is a block, if it isn't already, and optionally append to the
1360
1309
// block this variant sets a name for the block, so it will not reuse a block
1361
1310
// already named
1362
- Block*
1363
- blockifyWithName (Expression* any, Name name, Expression* append = nullptr ) {
1311
+ Block* blockifyWithName (Expression* any,
1312
+ Name name,
1313
+ Expression* append = nullptr ,
1314
+ std::optional<Type> type = std::nullopt) {
1364
1315
Block* block = nullptr ;
1365
1316
if (any) {
1366
1317
block = any->dynCast <Block>();
@@ -1371,21 +1322,16 @@ class Builder {
1371
1322
block->name = name;
1372
1323
if (append) {
1373
1324
block->list .push_back (append);
1374
- block->finalize ();
1325
+ block->finalize (type );
1375
1326
}
1376
1327
return block;
1377
1328
}
1378
1329
1379
1330
// a helper for the common pattern of a sequence of two expressions. Similar
1380
1331
// to blockify, but does *not* reuse a block if the first is one.
1381
- Block* makeSequence (Expression* left, Expression* right) {
1382
- auto * block = makeBlock (left);
1383
- block->list .push_back (right);
1384
- block->finalize ();
1385
- return block;
1386
- }
1387
-
1388
- Block* makeSequence (Expression* left, Expression* right, Type type) {
1332
+ Block* makeSequence (Expression* left,
1333
+ Expression* right,
1334
+ std::optional<Type> type = std::nullopt) {
1389
1335
auto * block = makeBlock (left);
1390
1336
block->list .push_back (right);
1391
1337
block->finalize (type);
0 commit comments