Skip to content

Commit 9d01442

Browse files
Move ostream operators to wasm namespace (#8189)
Extending std with ostream operators is UB: https://en.cppreference.com/w/cpp/language/extending_std.html. Including these definitions in wasm is just as good because argument-dependent lookup will look it up using the argument's namespace which is always wasm anyway. Also move MemoryOrder ostream operator from wasm.cpp to Print.cpp to match others.
1 parent 7c6eb7c commit 9d01442

File tree

3 files changed

+25
-33
lines changed

3 files changed

+25
-33
lines changed

src/passes/Print.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3894,10 +3894,6 @@ printStackIR(std::ostream& o, Module* module, const PassOptions& options) {
38943894
return o;
38953895
}
38963896

3897-
} // namespace wasm
3898-
3899-
namespace std {
3900-
39013897
std::ostream& operator<<(std::ostream& o, wasm::Module& module) {
39023898
wasm::PassRunner runner(&module);
39033899
wasm::Printer printer(&o);
@@ -3958,4 +3954,19 @@ std::ostream& operator<<(std::ostream& o,
39583954
return o << importNames.module << "." << importNames.name;
39593955
}
39603956

3961-
} // namespace std
3957+
std::ostream& operator<<(std::ostream& os, wasm::MemoryOrder mo) {
3958+
switch (mo) {
3959+
case wasm::MemoryOrder::Unordered:
3960+
os << "Unordered";
3961+
break;
3962+
case wasm::MemoryOrder::SeqCst:
3963+
os << "SeqCst";
3964+
break;
3965+
case wasm::MemoryOrder::AcqRel:
3966+
os << "AcqRel";
3967+
break;
3968+
}
3969+
return os;
3970+
}
3971+
3972+
} // namespace wasm

src/wasm.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2654,15 +2654,6 @@ struct ShallowExpression {
26542654
Module* module = nullptr;
26552655
};
26562656

2657-
} // namespace wasm
2658-
2659-
namespace std {
2660-
template<> struct hash<wasm::Address> {
2661-
size_t operator()(const wasm::Address a) const {
2662-
return std::hash<wasm::Address::address64_t>()(a.addr);
2663-
}
2664-
};
2665-
26662657
std::ostream& operator<<(std::ostream& o, wasm::Module& module);
26672658
std::ostream& operator<<(std::ostream& o, wasm::Function& func);
26682659
std::ostream& operator<<(std::ostream& o, wasm::Expression& expression);
@@ -2673,6 +2664,15 @@ std::ostream& operator<<(std::ostream& o, wasm::ModuleHeapType pair);
26732664
std::ostream& operator<<(std::ostream& os, wasm::MemoryOrder mo);
26742665
std::ostream& operator<<(std::ostream& o, const wasm::ImportNames& importNames);
26752666

2667+
} // namespace wasm
2668+
2669+
namespace std {
2670+
template<> struct hash<wasm::Address> {
2671+
size_t operator()(const wasm::Address a) const {
2672+
return std::hash<wasm::Address::address64_t>()(a.addr);
2673+
}
2674+
};
2675+
26762676
} // namespace std
26772677

26782678
#endif // wasm_wasm_h

src/wasm/wasm.cpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,22 +2015,3 @@ void Module::clearDebugInfo() {
20152015
}
20162016

20172017
} // namespace wasm
2018-
2019-
namespace std {
2020-
2021-
std::ostream& operator<<(std::ostream& os, wasm::MemoryOrder mo) {
2022-
switch (mo) {
2023-
case wasm::MemoryOrder::Unordered:
2024-
os << "Unordered";
2025-
break;
2026-
case wasm::MemoryOrder::SeqCst:
2027-
os << "SeqCst";
2028-
break;
2029-
case wasm::MemoryOrder::AcqRel:
2030-
os << "AcqRel";
2031-
break;
2032-
}
2033-
return os;
2034-
}
2035-
2036-
} // namespace std

0 commit comments

Comments
 (0)