Skip to content

Commit

Permalink
Define ExprList as vector<unique_ptr<Expr>>
Browse files Browse the repository at this point in the history
  • Loading branch information
keithw committed Nov 20, 2023
1 parent 32a110d commit 435af28
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 29 deletions.
16 changes: 3 additions & 13 deletions include/wabt/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -433,19 +433,7 @@ const char* GetExprTypeName(ExprType type);

class Expr;

// Workaround for MSVC where std::list move constructor isn't noexcept,
// which prevents use of vector<list<unique_ptr<T>>> (e.g. in ExprListVector)
// (https://stackoverflow.com/questions/57299324/why-is-stdmaps-move-constructor-not-noexcept)
template <typename T>
class move_only_list : public std::list<T> {
public:
using std::list<T>::list;
WABT_DISALLOW_COPY_AND_ASSIGN(move_only_list);
move_only_list(move_only_list&&) = default;
move_only_list& operator=(move_only_list&&) = default;
};

using ExprList = move_only_list<std::unique_ptr<Expr>>;
using ExprList = std::vector<std::unique_ptr<Expr>>;

using BlockDeclaration = FuncDeclaration;

Expand All @@ -469,6 +457,8 @@ struct Catch {
bool IsCatchAll() const {
return var.is_index() && var.index() == kInvalidIndex;
}
Catch(Catch&&) = default;
Catch& operator=(Catch&&) = default;
};
using CatchVector = std::vector<Catch>;

Expand Down
9 changes: 7 additions & 2 deletions src/c-writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3869,7 +3869,10 @@ void CWriter::Write(const ExprList& exprs) {

if (!IsImport(func.name) && !func.features_used.tailcall) {
// make normal call, then return
Write(ExprList{std::make_unique<CallExpr>(inst->var, inst->loc)});
auto expr = std::make_unique<CallExpr>(inst->var, inst->loc);
ExprList expr_list;
expr_list.push_back(std::move(expr));
Write(expr_list);
Write("goto ", LabelName(kImplicitFuncLabel), ";", Newline());
return;
}
Expand Down Expand Up @@ -3919,7 +3922,9 @@ void CWriter::Write(const ExprList& exprs) {
".data[", StackVar(0), "].func_tailcallee.fn) ", OpenBrace());
auto ci = std::make_unique<CallIndirectExpr>(inst->loc);
std::tie(ci->decl, ci->table) = std::make_pair(inst->decl, inst->table);
Write(ExprList{std::move(ci)});
ExprList expr_list;
expr_list.push_back(std::move(ci));
Write(expr_list);
if (in_tail_callee_) {
Write("next->fn = NULL;", Newline());
}
Expand Down
18 changes: 4 additions & 14 deletions src/wast-parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1970,19 +1970,14 @@ Result WastParser::ParseResultList(

Result WastParser::ParseInstrList(ExprList* exprs) {
WABT_TRACE(ParseInstrList);
ExprList new_exprs;
while (true) {
auto pair = PeekPair();
if (IsInstr(pair)) {
if (Succeeded(ParseInstr(&new_exprs))) {
exprs->splice(exprs->end(), new_exprs);
} else {
if (Failed(ParseInstr(exprs))) {
CHECK_RESULT(Synchronize(IsInstr));
}
} else if (IsLparAnn(pair)) {
if (Succeeded(ParseCodeMetadataAnnotation(&new_exprs))) {
exprs->splice(exprs->end(), new_exprs);
} else {
if (Failed(ParseCodeMetadataAnnotation(exprs))) {
CHECK_RESULT(Synchronize(IsLparAnn));
}
} else {
Expand Down Expand Up @@ -3036,11 +3031,8 @@ Result WastParser::ParseBlock(Block* block) {

Result WastParser::ParseExprList(ExprList* exprs) {
WABT_TRACE(ParseExprList);
ExprList new_exprs;
while (PeekMatchExpr()) {
if (Succeeded(ParseExpr(&new_exprs))) {
exprs->splice(exprs->end(), new_exprs);
} else {
if (Failed(ParseExpr(exprs))) {
CHECK_RESULT(Synchronize(IsExpr));
}
}
Expand Down Expand Up @@ -3093,9 +3085,7 @@ Result WastParser::ParseExpr(ExprList* exprs) {
CHECK_RESULT(ParseBlockDeclaration(&expr->true_.decl));

if (PeekMatchExpr()) {
ExprList cond;
CHECK_RESULT(ParseExpr(&cond));
exprs->splice(exprs->end(), cond);
CHECK_RESULT(ParseExpr(exprs));
}

if (MatchLpar(TokenType::Then)) {
Expand Down

0 comments on commit 435af28

Please sign in to comment.