2020#include " ir/manipulation.h"
2121#include " parsing.h"
2222#include " wasm.h"
23+ #include < optional>
2324
2425namespace wasm {
2526
@@ -189,69 +190,47 @@ class Builder {
189190 bool >;
190191
191192 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 ) {
201194 auto * ret = wasm.allocator .alloc <Block>();
202195 ret->list .set (items);
203196 ret->finalize (type);
204197 return ret;
205198 }
206199
207200 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 ) {
209204 auto * ret = wasm.allocator .alloc <Block>();
210205 ret->name = name;
211206 ret->list .set (items);
212207 ret->finalize (type);
213208 return ret;
214209 }
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 ) {
219212 return makeBlock (items, type);
220213 }
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 ) {
223217 return makeBlock (name, items, type);
224218 }
225219
226220 If* makeIf (Expression* condition,
227221 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 ) {
240224 auto * ret = wasm.allocator .alloc <If>();
241225 ret->condition = condition;
242226 ret->ifTrue = ifTrue;
243227 ret->ifFalse = ifFalse;
244228 ret->finalize (type);
245229 return ret;
246230 }
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 ) {
255234 auto * ret = wasm.allocator .alloc <Loop>();
256235 ret->name = name;
257236 ret->body = body;
@@ -792,77 +771,47 @@ class Builder {
792771 const std::vector<Name>& catchTags,
793772 const std::vector<Expression*>& catchBodies,
794773 Name delegateTarget,
795- Type type,
796- bool hasType) { // differentiate whether a type was passed in
774+ std::optional<Type> type = std::nullopt ) {
797775 auto * ret = wasm.allocator .alloc <Try>();
798776 ret->name = name;
799777 ret->body = body;
800778 ret->catchTags .set (catchTags);
801779 ret->catchBodies .set (catchBodies);
802- if (hasType) {
803- ret->finalize (type);
804- } else {
805- ret->finalize ();
806- }
780+ ret->finalize (type);
807781 return ret;
808782 }
809783
810784public:
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?
817786 Try* makeTry (Expression* body,
818787 const std::vector<Name>& catchTags,
819788 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);
829791 }
830792 Try* makeTry (Name name,
831793 Expression* body,
832794 const std::vector<Name>& catchTags,
833795 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);
842798 }
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);
848803 }
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);
860809 }
861810 TryTable* makeTryTable (Expression* body,
862811 const std::vector<Name>& catchTags,
863812 const std::vector<Name>& catchDests,
864813 const std::vector<bool >& catchRefs,
865- Type type) {
814+ std::optional< Type> type = std:: nullopt ) {
866815 auto * ret = wasm.allocator .alloc <TryTable>();
867816 ret->body = body;
868817 ret->catchTags .set (catchTags);
@@ -1359,8 +1308,10 @@ class Builder {
13591308 // ensure a node is a block, if it isn't already, and optionally append to the
13601309 // block this variant sets a name for the block, so it will not reuse a block
13611310 // 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 ) {
13641315 Block* block = nullptr ;
13651316 if (any) {
13661317 block = any->dynCast <Block>();
@@ -1371,21 +1322,16 @@ class Builder {
13711322 block->name = name;
13721323 if (append) {
13731324 block->list .push_back (append);
1374- block->finalize ();
1325+ block->finalize (type );
13751326 }
13761327 return block;
13771328 }
13781329
13791330 // a helper for the common pattern of a sequence of two expressions. Similar
13801331 // 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 ) {
13891335 auto * block = makeBlock (left);
13901336 block->list .push_back (right);
13911337 block->finalize (type);
0 commit comments