Skip to content

Commit

Permalink
Fix type names for function references (#1787)
Browse files Browse the repository at this point in the history
This requires `Type::GetName` to return to be dynamicllay created and
return `std::string` rather then a `const char*`

As this diff shows this type name is only used in textual output and
error messages so should this change should not have a effect of binary
parse time or the interpreter.
  • Loading branch information
sbc100 authored Dec 13, 2021
1 parent 134bafd commit 8538a88
Show file tree
Hide file tree
Showing 19 changed files with 93 additions and 88 deletions.
32 changes: 17 additions & 15 deletions src/binary-reader-logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void BinaryReaderLogging::LogType(Type type) {
if (type.IsIndex()) {
LOGF_NOINDENT("typeidx[%d]", type.GetIndex());
} else {
LOGF_NOINDENT("%s", type.GetName());
LOGF_NOINDENT("%s", type.GetName().c_str());
}
}

Expand Down Expand Up @@ -208,7 +208,7 @@ Result BinaryReaderLogging::OnImportTable(Index import_index,
SPrintLimits(buf, sizeof(buf), elem_limits);
LOGF("OnImportTable(import_index: %" PRIindex ", table_index: %" PRIindex
", elem_type: %s, %s)\n",
import_index, table_index, elem_type.GetName(), buf);
import_index, table_index, elem_type.GetName().c_str(), buf);
return reader_->OnImportTable(import_index, module_name, field_name,
table_index, elem_type, elem_limits);
}
Expand Down Expand Up @@ -236,7 +236,8 @@ Result BinaryReaderLogging::OnImportGlobal(Index import_index,
LOGF("OnImportGlobal(import_index: %" PRIindex ", global_index: %" PRIindex
", type: %s, mutable: "
"%s)\n",
import_index, global_index, type.GetName(), mutable_ ? "true" : "false");
import_index, global_index, type.GetName().c_str(),
mutable_ ? "true" : "false");
return reader_->OnImportGlobal(import_index, module_name, field_name,
global_index, type, mutable_);
}
Expand All @@ -259,7 +260,7 @@ Result BinaryReaderLogging::OnTable(Index index,
char buf[100];
SPrintLimits(buf, sizeof(buf), elem_limits);
LOGF("OnTable(index: %" PRIindex ", elem_type: %s, %s)\n", index,
elem_type.GetName(), buf);
elem_type.GetName().c_str(), buf);
return reader_->OnTable(index, elem_type, elem_limits);
}

Expand All @@ -272,7 +273,7 @@ Result BinaryReaderLogging::OnMemory(Index index, const Limits* page_limits) {

Result BinaryReaderLogging::BeginGlobal(Index index, Type type, bool mutable_) {
LOGF("BeginGlobal(index: %" PRIindex ", type: %s, mutable: %s)\n", index,
type.GetName(), mutable_ ? "true" : "false");
type.GetName().c_str(), mutable_ ? "true" : "false");
return reader_->BeginGlobal(index, type, mutable_);
}

Expand All @@ -295,7 +296,7 @@ Result BinaryReaderLogging::OnLocalDecl(Index decl_index,
Index count,
Type type) {
LOGF("OnLocalDecl(index: %" PRIindex ", count: %" PRIindex ", type: %s)\n",
decl_index, count, type.GetName());
decl_index, count, type.GetName().c_str());
return reader_->OnLocalDecl(decl_index, count, type);
}

Expand Down Expand Up @@ -412,7 +413,7 @@ Result BinaryReaderLogging::BeginElemSegment(Index index,

Result BinaryReaderLogging::OnElemSegmentElemType(Index index, Type elem_type) {
LOGF("OnElemSegmentElemType(index: %" PRIindex ", type: %s)\n", index,
elem_type.GetName());
elem_type.GetName().c_str());
return reader_->OnElemSegmentElemType(index, elem_type);
}

Expand Down Expand Up @@ -657,10 +658,10 @@ Result BinaryReaderLogging::OnComdatEntry(ComdatType kind, Index index) {
return reader_->name(value); \
}

#define DEFINE_TYPE(name) \
Result BinaryReaderLogging::name(Type type) { \
LOGF(#name "(%s)\n", type.GetName()); \
return reader_->name(type); \
#define DEFINE_TYPE(name) \
Result BinaryReaderLogging::name(Type type) { \
LOGF(#name "(%s)\n", type.GetName().c_str()); \
return reader_->name(type); \
}

#define DEFINE_INDEX_DESC(name, desc) \
Expand All @@ -669,10 +670,11 @@ Result BinaryReaderLogging::OnComdatEntry(ComdatType kind, Index index) {
return reader_->name(value); \
}

#define DEFINE_INDEX_TYPE(name) \
Result BinaryReaderLogging::name(Index value, Type type) { \
LOGF(#name "(index: %" PRIindex ", type: %s)\n", value, type.GetName()); \
return reader_->name(value, type); \
#define DEFINE_INDEX_TYPE(name) \
Result BinaryReaderLogging::name(Index value, Type type) { \
LOGF(#name "(index: %" PRIindex ", type: %s)\n", value, \
type.GetName().c_str()); \
return reader_->name(value, type); \
}

#define DEFINE_INDEX_INDEX(name, desc0, desc1) \
Expand Down
26 changes: 13 additions & 13 deletions src/binary-reader-objdump.cc
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ Result BinaryReaderObjdumpDisassemble::OnLocalDecl(Index decl_index,
}
local_index_ += count;

printf("] type=%s\n", type.GetName());
printf("] type=%s\n", type.GetName().c_str());

last_opcode_end = current_opcode_offset + data_size;
current_opcode_offset = last_opcode_end;
Expand Down Expand Up @@ -783,7 +783,7 @@ Result BinaryReaderObjdumpDisassemble::OnOpcodeType(Type type) {
return Result::Ok;
}
if (current_opcode == Opcode::SelectT) {
LogOpcode(type.GetName());
LogOpcode(type.GetName().c_str());
} else {
LogOpcode(type.GetRefKindName());
}
Expand Down Expand Up @@ -1257,23 +1257,23 @@ Result BinaryReaderObjdump::OnFuncType(Index index,
if (i != 0) {
printf(", ");
}
printf("%s", param_types[i].GetName());
printf("%s", param_types[i].GetName().c_str());
}
printf(") -> ");
switch (result_count) {
case 0:
printf("nil");
break;
case 1:
printf("%s", result_types[0].GetName());
printf("%s", result_types[0].GetName().c_str());
break;
default:
printf("(");
for (Index i = 0; i < result_count; i++) {
if (i != 0) {
printf(", ");
}
printf("%s", result_types[i].GetName());
printf("%s", result_types[i].GetName().c_str());
}
printf(")");
break;
Expand All @@ -1293,7 +1293,7 @@ Result BinaryReaderObjdump::OnStructType(Index index,
if (fields[i].mutable_) {
printf(" (mut");
}
printf(" %s", fields[i].type.GetName());
printf(" %s", fields[i].type.GetName().c_str());
if (fields[i].mutable_) {
printf(")");
}
Expand All @@ -1310,7 +1310,7 @@ Result BinaryReaderObjdump::OnArrayType(Index index, TypeMut field) {
if (field.mutable_) {
printf(" (mut");
}
printf(" %s", field.type.GetName());
printf(" %s", field.type.GetName().c_str());
if (field.mutable_) {
printf(")");
}
Expand Down Expand Up @@ -1396,7 +1396,7 @@ Result BinaryReaderObjdump::OnImportTable(Index import_index,
Type elem_type,
const Limits* elem_limits) {
PrintDetails(" - table[%" PRIindex "] type=%s initial=%" PRId64, table_index,
elem_type.GetName(), elem_limits->initial);
elem_type.GetName().c_str(), elem_limits->initial);
if (elem_limits->has_max) {
PrintDetails(" max=%" PRId64, elem_limits->max);
}
Expand Down Expand Up @@ -1435,7 +1435,7 @@ Result BinaryReaderObjdump::OnImportGlobal(Index import_index,
Type type,
bool mutable_) {
PrintDetails(" - global[%" PRIindex "] %s mutable=%d", global_index,
type.GetName(), mutable_);
type.GetName().c_str(), mutable_);
PrintDetails(" <- " PRIstringview "." PRIstringview "\n",
WABT_PRINTF_STRING_VIEW_ARG(module_name),
WABT_PRINTF_STRING_VIEW_ARG(field_name));
Expand Down Expand Up @@ -1486,7 +1486,7 @@ Result BinaryReaderObjdump::OnTable(Index index,
Type elem_type,
const Limits* elem_limits) {
PrintDetails(" - table[%" PRIindex "] type=%s initial=%" PRId64, index,
elem_type.GetName(), elem_limits->initial);
elem_type.GetName().c_str(), elem_limits->initial);
if (elem_limits->has_max) {
PrintDetails(" max=%" PRId64, elem_limits->max);
}
Expand Down Expand Up @@ -1522,7 +1522,7 @@ Result BinaryReaderObjdump::OnExport(Index index,
Result BinaryReaderObjdump::OnElemSegmentElemExpr_RefNull(Index segment_index,
Type type) {
PrintDetails(" - elem[%" PRIzd "] = ref.null %s\n",
elem_offset_ + elem_index_, type.GetName());
elem_offset_ + elem_index_, type.GetName().c_str());
elem_index_++;
return Result::Ok;
}
Expand Down Expand Up @@ -1576,8 +1576,8 @@ Result BinaryReaderObjdump::OnGlobalCount(Index count) {
}

Result BinaryReaderObjdump::BeginGlobal(Index index, Type type, bool mutable_) {
PrintDetails(" - global[%" PRIindex "] %s mutable=%d", index, type.GetName(),
mutable_);
PrintDetails(" - global[%" PRIindex "] %s mutable=%d", index,
type.GetName().c_str(), mutable_);
string_view name = GetGlobalName(index);
if (!name.empty()) {
PrintDetails(" <" PRIstringview ">", WABT_PRINTF_STRING_VIEW_ARG(name));
Expand Down
2 changes: 1 addition & 1 deletion src/binary-reader-opcnt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ void OpcodeInfo::Write(Stream& stream) {
if (type.IsIndex()) {
stream.Writef(" type:%d", type.GetIndex());
} else if (type != Type::Void) {
stream.Writef(" %s", type.GetName());
stream.Writef(" %s", type.GetName().c_str());
}
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/binary-reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2569,7 +2569,7 @@ Result BinaryReader::ReadElemSection(Offset section_size) {
CHECK_RESULT(ReadExternalKind(&kind, "export kind"));
ERROR_UNLESS(kind == ExternalKind::Func,
"segment elem type must be func (%s)",
elem_type.GetName());
elem_type.GetName().c_str());
elem_type = Type::FuncRef;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/binary-writer-spec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ void BinaryWriterSpec::WriteVar(const Var& var) {
void BinaryWriterSpec::WriteTypeObject(Type type) {
json_stream_->Writef("{");
WriteKey("type");
WriteString(type.GetName());
WriteString(type.GetName().c_str());
json_stream_->Writef("}");
}

Expand Down Expand Up @@ -259,7 +259,7 @@ void BinaryWriterSpec::WriteConst(const Const& const_) {
WriteString("v128");
WriteSeparator();
WriteKey("lane_type");
WriteString(const_.lane_type().GetName());
WriteString(const_.lane_type().GetName().c_str());
WriteSeparator();
WriteKey("value");
json_stream_->Writef("[");
Expand Down
4 changes: 2 additions & 2 deletions src/binary-writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ void WriteOpcode(Stream* stream, Opcode opcode) {
}

void WriteType(Stream* stream, Type type, const char* desc) {
WriteS32Leb128(stream, type, desc ? desc : type.GetName());
WriteS32Leb128(stream, type, desc ? desc : type.GetName().c_str());
if (type.IsReferenceWithIndex()) {
WriteS32Leb128(stream, type.GetReferenceIndex(),
desc ? desc : type.GetName());
desc ? desc : type.GetName().c_str());
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/interp/interp-wasm-c-api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -537,21 +537,21 @@ static void print_sig(const FuncType& sig) {
#ifndef NDEBUG
fprintf(stderr, "(");
bool first = true;
for (auto Type : sig.params) {
for (auto type : sig.params) {
if (!first) {
fprintf(stderr, ", ");
}
first = false;
fprintf(stderr, "%s", Type.GetName());
fprintf(stderr, "%s", type.GetName().c_str());
}
fprintf(stderr, ") -> (");
first = true;
for (auto Type : sig.results) {
for (auto type : sig.results) {
if (!first) {
fprintf(stderr, ", ");
}
first = false;
fprintf(stderr, "%s", Type.GetName());
fprintf(stderr, "%s", type.GetName().c_str());
}
fprintf(stderr, ")\n");
#endif
Expand Down
10 changes: 5 additions & 5 deletions src/interp/interp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const char* GetName(Mutability mut) {
return kNames[int(mut)];
}

const char* GetName(ValueType type) {
const std::string GetName(ValueType type) {
return type.GetName();
}

Expand Down Expand Up @@ -105,9 +105,9 @@ Result Match(const TableType& expected,
const TableType& actual,
std::string* out_msg) {
if (expected.element != actual.element) {
*out_msg =
StringPrintf("type mismatch in imported table, expected %s but got %s.",
GetName(expected.element), GetName(actual.element));
*out_msg = StringPrintf(
"type mismatch in imported table, expected %s but got %s.",
GetName(expected.element).c_str(), GetName(actual.element).c_str());
return Result::Error;
}

Expand Down Expand Up @@ -149,7 +149,7 @@ Result Match(const GlobalType& expected,
!TypesMatch(expected.type, actual.type))) {
*out_msg = StringPrintf(
"type mismatch in imported global, expected %s but got %s.",
GetName(expected.type), GetName(actual.type));
GetName(expected.type).c_str(), GetName(actual.type).c_str());
return Result::Error;
}

Expand Down
2 changes: 1 addition & 1 deletion src/interp/interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ enum class ObjectKind {
};

const char* GetName(Mutability);
const char* GetName(ValueType);
const std::string GetName(ValueType);
const char* GetName(ExternKind);
const char* GetName(ObjectKind);

Expand Down
2 changes: 1 addition & 1 deletion src/shared-validator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ Result SharedValidator::CheckType(const Location& loc,
const char* desc) {
if (Failed(TypeChecker::CheckType(actual, expected))) {
PrintError(loc, "type mismatch at %s. got %s, expected %s", desc,
actual.GetName(), expected.GetName());
actual.GetName().c_str(), expected.GetName().c_str());
return Result::Error;
}
return Result::Ok;
Expand Down
10 changes: 6 additions & 4 deletions src/tools/spectest-interp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ wabt::Result JSONParser::ParseLaneConstValue(Type lane_type,
}

default:
PrintError("unknown concrete type: \"%s\"", lane_type.GetName());
PrintError("unknown concrete type: \"%s\"", lane_type.GetName().c_str());
return wabt::Result::Error;
}

Expand Down Expand Up @@ -834,7 +834,7 @@ wabt::Result JSONParser::ParseConstValue(Type type,
break;

default:
PrintError("unknown concrete type: \"%s\"", type.GetName());
PrintError("unknown concrete type: \"%s\"", type.GetName().c_str());
return wabt::Result::Error;
}

Expand Down Expand Up @@ -1657,10 +1657,12 @@ static std::string ExpectedValueToString(const ExpectedValue& ev) {
return TypedValueToString(ev.value);

case ExpectedNan::Arithmetic:
return StringPrintf("%s:nan:arithmetic", ev.value.type.GetName());
return StringPrintf("%s:nan:arithmetic",
ev.value.type.GetName().c_str());

case ExpectedNan::Canonical:
return StringPrintf("%s:nan:canonical", ev.value.type.GetName());
return StringPrintf("%s:nan:canonical",
ev.value.type.GetName().c_str());
}
break;

Expand Down
Loading

0 comments on commit 8538a88

Please sign in to comment.