diff options
author | Ben Smith <binji@chromium.org> | 2020-03-16 17:24:37 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-16 17:24:37 -0700 |
commit | 5467faf5cd52ba2da3e331e6c950a3860c559010 (patch) | |
tree | 9531fd3cdce64776c1713fff52782073f4d7934b /src/shared-validator.cc | |
parent | 261dfdd75d8199298c9e0143e351322cc79c996a (diff) | |
download | wabt-5467faf5cd52ba2da3e331e6c950a3860c559010.tar.gz wabt-5467faf5cd52ba2da3e331e6c950a3860c559010.tar.bz2 wabt-5467faf5cd52ba2da3e331e6c950a3860c559010.zip |
Parse struct fields (#1355)
This allows the following field formats:
* `(struct (field $name i32))`
* `(struct (field $name (mut i32)))`
* `(struct (field i32))`
* `(struct (field (mut i32)))`
* `(struct (mut i32))`
* `(struct i32)`
Diffstat (limited to 'src/shared-validator.cc')
-rw-r--r-- | src/shared-validator.cc | 42 |
1 files changed, 33 insertions, 9 deletions
diff --git a/src/shared-validator.cc b/src/shared-validator.cc index 5af98356..a82e3b9f 100644 --- a/src/shared-validator.cc +++ b/src/shared-validator.cc @@ -54,15 +54,24 @@ Result SharedValidator::OnFuncType(const Location& loc, result |= PrintError(loc, "multiple result values not currently supported."); } - types_.push_back(FuncType{ToTypeVector(param_count, param_types), - ToTypeVector(result_count, result_types)}); + func_types_.emplace(num_types_++, + FuncType{ToTypeVector(param_count, param_types), + ToTypeVector(result_count, result_types)}); return result; } +Result SharedValidator::OnStructType(const Location&, + Index field_count, + TypeMut* fields) { + struct_types_.emplace(num_types_++, StructType{TypeMutVector( + &fields[0], &fields[field_count])}); + return Result::Ok; +} + Result SharedValidator::OnFunction(const Location& loc, Var sig_var) { Result result = Result::Ok; FuncType type; - result |= CheckTypeIndex(sig_var, &type); + result |= CheckFuncTypeIndex(sig_var, &type); funcs_.push_back(type); return result; } @@ -219,7 +228,7 @@ Result SharedValidator::OnGlobalInitExpr_Other(const Location& loc) { Result SharedValidator::OnEvent(const Location& loc, Var sig_var) { Result result = Result::Ok; FuncType type; - result |= CheckTypeIndex(sig_var, &type); + result |= CheckFuncTypeIndex(sig_var, &type); if (!type.results.empty()) { result |= PrintError(loc, "Event signature must have 0 results."); } @@ -433,8 +442,23 @@ Result SharedValidator::CheckLocalIndex(Var local_var, Type* out_type) { return Result::Ok; } -Result SharedValidator::CheckTypeIndex(Var sig_var, FuncType* out) { - return CheckIndexWithValue(sig_var, types_, out, "function type"); +Result SharedValidator::CheckFuncTypeIndex(Var sig_var, FuncType* out) { + Result result = CheckIndex(sig_var, num_types_, "function type"); + if (Failed(result)) { + *out = FuncType{}; + return Result::Error; + } + + auto iter = func_types_.find(sig_var.index()); + if (iter == func_types_.end()) { + return PrintError(sig_var.loc, "type %d is not a function", + sig_var.index()); + } + + if (out) { + *out = iter->second; + } + return Result::Ok; } Result SharedValidator::CheckFuncIndex(Var func_var, FuncType* out) { @@ -476,7 +500,7 @@ Result SharedValidator::CheckBlockSignature(const Location& loc, if (sig_type.IsIndex()) { Index sig_index = sig_type.GetIndex(); FuncType func_type; - result |= CheckTypeIndex(Var(sig_index, loc), &func_type); + result |= CheckFuncTypeIndex(Var(sig_index, loc), &func_type); if (!func_type.params.empty() && !options_.features.multi_value_enabled()) { result |= PrintError(loc, "%s params not currently supported.", @@ -709,7 +733,7 @@ Result SharedValidator::OnCallIndirect(const Location& loc, Result result = Result::Ok; expr_loc_ = &loc; FuncType func_type; - result |= CheckTypeIndex(sig_var, &func_type); + result |= CheckFuncTypeIndex(sig_var, &func_type); result |= CheckTableIndex(table_var); result |= typechecker_.OnCallIndirect(func_type.params, func_type.results); return result; @@ -962,7 +986,7 @@ Result SharedValidator::OnReturnCallIndirect(const Location& loc, expr_loc_ = &loc; result |= CheckTableIndex(table_var); FuncType func_type; - result |= CheckTypeIndex(sig_var, &func_type); + result |= CheckFuncTypeIndex(sig_var, &func_type); result |= typechecker_.OnReturnCallIndirect(func_type.params, func_type.results); return result; |