Skip to content

Commit

Permalink
Move type utility classes to type-utils.h (NFC)
Browse files Browse the repository at this point in the history
This renames type-updating.h to type-utils.h and moves TypeSeeker class,
which is currently only used in wasm.cpp, to that file too. I plan to
use this elsewhere in WebAssembly#2451 and I think making this a separate PR makes
things easier to review.
  • Loading branch information
aheejin committed Nov 30, 2019
1 parent 24d2749 commit b46e447
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 66 deletions.
66 changes: 63 additions & 3 deletions src/ir/type-updating.h → src/ir/type-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* limitations under the License.
*/

#ifndef wasm_ir_type_updating_h
#define wasm_ir_type_updating_h
#ifndef wasm_ir_type_utils_h
#define wasm_ir_type_utils_h

#include "wasm-traversal.h"

Expand Down Expand Up @@ -316,6 +316,66 @@ struct TypeUpdater
}
};

// core AST type checking
struct TypeSeeker : public PostWalker<TypeSeeker> {
Expression* target; // look for this one
Name targetName;
std::vector<Type> types;

TypeSeeker(Expression* target, Name targetName)
: target(target), targetName(targetName) {
Expression* temp = target;
walk(temp);
}

void visitBreak(Break* curr) {
if (curr->name == targetName) {
types.push_back(curr->value ? curr->value->type : none);
}
}

void visitSwitch(Switch* curr) {
for (auto name : curr->targets) {
if (name == targetName) {
types.push_back(curr->value ? curr->value->type : none);
}
}
if (curr->default_ == targetName) {
types.push_back(curr->value ? curr->value->type : none);
}
}

void visitBrOnExn(BrOnExn* curr) {
if (curr->name == targetName) {
types.push_back(curr->sent);
}
}

void visitBlock(Block* curr) {
if (curr == target) {
if (curr->list.size() > 0) {
types.push_back(curr->list.back()->type);
} else {
types.push_back(none);
}
} else if (curr->name == targetName) {
// ignore all breaks til now, they were captured by someone with the same
// name
types.clear();
}
}

void visitLoop(Loop* curr) {
if (curr == target) {
types.push_back(curr->body->type);
} else if (curr->name == targetName) {
// ignore all breaks til now, they were captured by someone with the same
// name
types.clear();
}
}
};

} // namespace wasm

#endif // wasm_ir_type_updating_h
#endif // wasm_ir_type_utils_h
2 changes: 1 addition & 1 deletion src/passes/DeadCodeElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

#include <ir/block-utils.h>
#include <ir/branch-utils.h>
#include <ir/type-updating.h>
#include <ir/type-utils.h>
#include <pass.h>
#include <vector>
#include <wasm-builder.h>
Expand Down
2 changes: 1 addition & 1 deletion src/passes/Vacuum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <ir/block-utils.h>
#include <ir/effects.h>
#include <ir/literal-utils.h>
#include <ir/type-updating.h>
#include <ir/type-utils.h>
#include <ir/utils.h>
#include <pass.h>
#include <wasm-builder.h>
Expand Down
62 changes: 1 addition & 61 deletions src/wasm/wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "wasm.h"
#include "ir/branch-utils.h"
#include "ir/type-utils.h"
#include "wasm-traversal.h"

namespace wasm {
Expand Down Expand Up @@ -187,67 +188,6 @@ const char* getExpressionName(Expression* curr) {
WASM_UNREACHABLE();
}

// core AST type checking

struct TypeSeeker : public PostWalker<TypeSeeker> {
Expression* target; // look for this one
Name targetName;
std::vector<Type> types;

TypeSeeker(Expression* target, Name targetName)
: target(target), targetName(targetName) {
Expression* temp = target;
walk(temp);
}

void visitBreak(Break* curr) {
if (curr->name == targetName) {
types.push_back(curr->value ? curr->value->type : none);
}
}

void visitSwitch(Switch* curr) {
for (auto name : curr->targets) {
if (name == targetName) {
types.push_back(curr->value ? curr->value->type : none);
}
}
if (curr->default_ == targetName) {
types.push_back(curr->value ? curr->value->type : none);
}
}

void visitBrOnExn(BrOnExn* curr) {
if (curr->name == targetName) {
types.push_back(curr->sent);
}
}

void visitBlock(Block* curr) {
if (curr == target) {
if (curr->list.size() > 0) {
types.push_back(curr->list.back()->type);
} else {
types.push_back(none);
}
} else if (curr->name == targetName) {
// ignore all breaks til now, they were captured by someone with the same
// name
types.clear();
}
}

void visitLoop(Loop* curr) {
if (curr == target) {
types.push_back(curr->body->type);
} else if (curr->name == targetName) {
// ignore all breaks til now, they were captured by someone with the same
// name
types.clear();
}
}
};

static Type mergeTypes(std::vector<Type>& types) {
Type type = unreachable;
for (auto other : types) {
Expand Down

0 comments on commit b46e447

Please sign in to comment.