summaryrefslogtreecommitdiff
path: root/src/validator.cc
diff options
context:
space:
mode:
authorBen Smith <binji@chromium.org>2020-02-25 14:57:12 -0800
committerGitHub <noreply@github.com>2020-02-25 14:57:12 -0800
commit5ce1a556f55779d3d59930b16a9d328ad6a7c1f2 (patch)
tree7fd0f4c37f298e24c028eafabe664801d1dd8e5e /src/validator.cc
parentd125db7274f17f743379ea7b34f9aae9563b3bc2 (diff)
downloadwabt-5ce1a556f55779d3d59930b16a9d328ad6a7c1f2.tar.gz
wabt-5ce1a556f55779d3d59930b16a9d328ad6a7c1f2.tar.bz2
wabt-5ce1a556f55779d3d59930b16a9d328ad6a7c1f2.zip
Move validation of func signatures into the parser (#1341)
The previous PR called the validator function to do this, but now the code is moved entirely into the parser. This is preferable, since a mismatching function signature is considered malformed text, not a validation error.
Diffstat (limited to 'src/validator.cc')
-rw-r--r--src/validator.cc112
1 files changed, 2 insertions, 110 deletions
diff --git a/src/validator.cc b/src/validator.cc
index c23aeae4..c2c2a01c 100644
--- a/src/validator.cc
+++ b/src/validator.cc
@@ -40,7 +40,6 @@ class Validator : public ExprVisitor::Delegate {
Result CheckModule(const Module* module);
Result CheckScript(const Script* script);
- Result CheckAllFuncSignatures(const Module* module);
// Implements ExprVisitor::Delegate.
Result OnBinaryExpr(BinaryExpr*) override;
@@ -158,11 +157,6 @@ class Validator : public ExprVisitor::Delegate {
const char* desc,
Index index,
const char* index_kind);
- void CheckTypes(const Location* loc,
- const TypeVector& actual,
- const TypeVector& expected,
- const char* desc,
- const char* index_kind);
void CheckResultTypes(const Location* loc,
const TypeVector& actual,
const TypeVector& expected,
@@ -177,7 +171,6 @@ class Validator : public ExprVisitor::Delegate {
template <typename T>
void CheckAtomicExpr(const T* expr, Result (TypeChecker::*func)(Opcode));
void CheckFuncSignature(const Location* loc, const FuncDeclaration& decl);
- class CheckFuncSignatureExprVisitorDelegate;
void CheckFunc(const Location* loc, const Func* func);
void PrintConstExprError(const Location* loc, const char* desc);
@@ -443,21 +436,6 @@ void Validator::CheckTypeIndex(const Location* loc,
}
}
-void Validator::CheckTypes(const Location* loc,
- const TypeVector& actual,
- const TypeVector& expected,
- const char* desc,
- const char* index_kind) {
- if (actual.size() == expected.size()) {
- for (size_t i = 0; i < actual.size(); ++i) {
- CheckTypeIndex(loc, actual[i], expected[i], desc, i, index_kind);
- }
- } else {
- PrintError(loc, "expected %" PRIzd " %ss, got %" PRIzd, expected.size(),
- index_kind, actual.size());
- }
-}
-
void Validator::CheckResultTypes(const Location* loc,
const TypeVector& actual,
const TypeVector& expected,
@@ -520,13 +498,7 @@ void Validator::CheckBlockDeclaration(const Location* loc,
opcode.GetName());
}
if (decl->has_func_type) {
- const FuncType* func_type;
- if (Succeeded(CheckFuncTypeVar(&decl->type_var, &func_type))) {
- CheckTypes(loc, decl->sig.result_types, func_type->sig.result_types,
- opcode.GetName(), "result");
- CheckTypes(loc, decl->sig.param_types, func_type->sig.param_types,
- opcode.GetName(), "argument");
- }
+ CheckFuncTypeVar(&decl->type_var, nullptr);
}
}
@@ -1014,13 +986,7 @@ Result Validator::OnLoadSplatExpr(LoadSplatExpr* expr) {
void Validator::CheckFuncSignature(const Location* loc,
const FuncDeclaration& decl) {
if (decl.has_func_type) {
- const FuncType* func_type;
- if (Succeeded(CheckFuncTypeVar(&decl.type_var, &func_type))) {
- CheckTypes(loc, decl.sig.result_types, func_type->sig.result_types,
- "function", "result");
- CheckTypes(loc, decl.sig.param_types, func_type->sig.param_types,
- "function", "argument");
- }
+ CheckFuncTypeVar(&decl.type_var, nullptr);
}
}
@@ -1564,72 +1530,6 @@ Result Validator::CheckScript(const Script* script) {
return result_;
}
-class Validator::CheckFuncSignatureExprVisitorDelegate
- : public ExprVisitor::DelegateNop {
- public:
- explicit CheckFuncSignatureExprVisitorDelegate(Validator* validator)
- : validator_(validator) {}
-
- Result BeginBlockExpr(BlockExpr* expr) override {
- validator_->CheckBlockDeclaration(&expr->loc, Opcode::Block,
- &expr->block.decl);
- return Result::Ok;
- }
-
- Result OnCallIndirectExpr(CallIndirectExpr* expr) override {
- validator_->CheckFuncSignature(&expr->loc, expr->decl);
- return Result::Ok;
- }
-
- Result OnReturnCallIndirectExpr(ReturnCallIndirectExpr* expr) override {
- validator_->CheckFuncSignature(&expr->loc, expr->decl);
- return Result::Ok;
- }
-
- Result BeginIfExpr(IfExpr* expr) override {
- validator_->CheckBlockDeclaration(&expr->loc, Opcode::If,
- &expr->true_.decl);
- return Result::Ok;
- }
-
- Result BeginLoopExpr(LoopExpr* expr) override {
- validator_->CheckBlockDeclaration(&expr->loc, Opcode::Loop,
- &expr->block.decl);
- return Result::Ok;
- }
-
- Result BeginTryExpr(TryExpr* expr) override {
- validator_->CheckBlockDeclaration(&expr->loc, Opcode::Try,
- &expr->block.decl);
- return Result::Ok;
- }
-
- private:
- Validator* validator_;
-};
-
-Result Validator::CheckAllFuncSignatures(const Module* module) {
- current_module_ = module;
- for (const ModuleField& field : module->fields) {
- switch (field.type()) {
- case ModuleFieldType::Func: {
- auto func_field = cast<FuncModuleField>(&field);
- CheckFuncSignature(&field.loc, func_field->func.decl);
- CheckFuncSignatureExprVisitorDelegate delegate(this);
- ExprVisitor visitor(&delegate);
- // TODO(binji): would rather not do a const_cast here, but the visitor
- // is non-const only.
- visitor.VisitFunc(const_cast<Func*>(&func_field->func));
- break;
- }
-
- default:
- break;
- }
- }
- return result_;
-}
-
} // end anonymous namespace
Result ValidateScript(const Script* script,
@@ -1648,12 +1548,4 @@ Result ValidateModule(const Module* module,
return validator.CheckModule(module);
}
-Result ValidateFuncSignatures(const Module* module,
- Errors* errors,
- const ValidateOptions& options) {
- Validator validator(errors, nullptr, options);
-
- return validator.CheckAllFuncSignatures(module);
-}
-
} // namespace wabt