Skip to content

Commit be02d3f

Browse files
authored
Update to C++17 and use std::optional for getSuperType (#4203)
This sets the C++ standard variable in the build to C++17, and makes use of std::optional (a C++17 library feature) in one place, to test that it's working.
1 parent f0a8de3 commit be02d3f

File tree

10 files changed

+32
-37
lines changed

10 files changed

+32
-37
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ include(GNUInstallDirs)
66

77
# The C++ standard whose features are required to build Binaryen.
88
# Keep in sync with scripts/test/shared.py cxx_standard
9-
set(CXX_STANDARD 14)
9+
set(CXX_STANDARD 17)
1010

1111
if(NOT CMAKE_BUILD_TYPE)
1212
message(STATUS "No build type selected, default to Release")

scripts/test/shared.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
# The C++ standard whose features are required to build Binaryen.
2727
# Keep in sync with CMakeLists.txt CXX_STANDARD
28-
cxx_standard = 14
28+
cxx_standard = 17
2929

3030

3131
def parse_args(args):

src/ir/module-utils.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -597,15 +597,15 @@ inline void collectHeapTypes(Module& wasm,
597597
counts.note(child);
598598
}
599599
}
600-
HeapType super;
601-
if (ht.getSuperType(super)) {
602-
if (!counts.count(super)) {
603-
newTypes.insert(super);
600+
601+
if (auto super = ht.getSuperType()) {
602+
if (!counts.count(*super)) {
603+
newTypes.insert(*super);
604604
// We should unconditionally count supertypes, but while the type system
605605
// is in flux, skip counting them to keep the type orderings in nominal
606606
// test outputs more similar to the orderings in the equirecursive
607607
// outputs. FIXME
608-
counts.note(super);
608+
counts.note(*super);
609609
}
610610
}
611611
}

src/ir/struct-utils.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,12 @@ template<typename T> class TypeHierarchyPropagator {
222222
auto& infos = combinedInfos[type];
223223

224224
// Propagate shared fields to the supertype.
225-
HeapType superType;
226-
if (type.getSuperType(superType)) {
227-
auto& superInfos = combinedInfos[superType];
228-
auto& superFields = superType.getStruct().fields;
225+
if (auto superType = type.getSuperType()) {
226+
auto& superInfos = combinedInfos[*superType];
227+
auto& superFields = superType->getStruct().fields;
229228
for (Index i = 0; i < superFields.size(); i++) {
230229
if (superInfos[i].combine(infos[i])) {
231-
work.push(superType);
230+
work.push(*superType);
232231
}
233232
}
234233
}

src/ir/subtypes.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ struct SubTypes {
5757
std::vector<HeapType> getAllSuperTypes(HeapType type) {
5858
std::vector<HeapType> ret;
5959
while (1) {
60-
HeapType super;
61-
if (!type.getSuperType(super)) {
60+
auto super = type.getSuperType();
61+
if (!super) {
6262
return ret;
6363
}
64-
ret.push_back(super);
65-
type = super;
64+
ret.push_back(*super);
65+
type = *super;
6666
}
6767
}
6868

@@ -71,9 +71,8 @@ struct SubTypes {
7171
private:
7272
// Add a type to the graph.
7373
void note(HeapType type) {
74-
HeapType super;
75-
if (type.getSuperType(super)) {
76-
typeSubTypes[super].push_back(type);
74+
if (auto super = type.getSuperType()) {
75+
typeSubTypes[*super].push_back(type);
7776
}
7877
}
7978

src/ir/type-updating.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,8 @@ void GlobalTypeRewriter::update() {
6565
}
6666

6767
// Apply a super, if there is one
68-
HeapType super;
69-
if (type.getSuperType(super)) {
70-
typeBuilder.setSubType(i, typeIndices[super]);
68+
if (auto super = type.getSuperType()) {
69+
typeBuilder.setSubType(i, typeIndices[*super]);
7170
}
7271
}
7372

src/passes/Print.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2515,9 +2515,8 @@ struct PrintSExpression : public UnifiedExpressionVisitor<PrintSExpression> {
25152515
}
25162516
// Module-level visitors
25172517
void printSupertypeOr(HeapType curr, std::string noSuper) {
2518-
HeapType super;
2519-
if (curr.getSuperType(super)) {
2520-
TypeNamePrinter(o, currModule).print(super);
2518+
if (auto super = curr.getSuperType()) {
2519+
TypeNamePrinter(o, currModule).print(*super);
25212520
} else {
25222521
o << noSuper;
25232522
}

src/wasm-type.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "support/name.h"
2121
#include "wasm-features.h"
22+
#include <optional>
2223
#include <ostream>
2324
#include <vector>
2425

@@ -382,12 +383,12 @@ class HeapType {
382383
const Struct& getStruct() const;
383384
Array getArray() const;
384385

385-
// Return whether there is a nontrivial (i.e. non-basic) nominal supertype and
386-
// store it in `out` if there is. Nominal types (in the sense of isNominal,
386+
// If there is a nontrivial (i.e. non-basic) nominal supertype, return it,
387+
// else an empty optional. Nominal types (in the sense of isNominal,
387388
// i.e. Milestone 4 nominal types) may always have supertypes and other types
388389
// may have supertypes in `TypeSystem::Nominal` mode but not in
389390
// `TypeSystem::Equirecursive` mode.
390-
bool getSuperType(HeapType& out) const;
391+
std::optional<HeapType> getSuperType() const;
391392

392393
// Whether this is a nominal type in the sense of being a GC Milestone 4
393394
// nominal type. Although all non-basic HeapTypes are nominal in

src/wasm/wasm-binary.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,11 @@ void WasmBinaryWriter::writeTypes() {
251251
WASM_UNREACHABLE("TODO GC type writing");
252252
}
253253
if (nominal) {
254-
HeapType super;
255-
bool hasSuper = type.getSuperType(super);
256-
if (!hasSuper) {
254+
auto super = type.getSuperType();
255+
if (!super) {
257256
super = type.isFunction() ? HeapType::func : HeapType::data;
258257
}
259-
writeHeapType(super);
258+
writeHeapType(*super);
260259
}
261260
}
262261
finishSection(start);

src/wasm/wasm-type.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,16 +1206,15 @@ Array HeapType::getArray() const {
12061206
return getHeapTypeInfo(*this)->array;
12071207
}
12081208

1209-
bool HeapType::getSuperType(HeapType& out) const {
1209+
std::optional<HeapType> HeapType::getSuperType() const {
12101210
if (isBasic()) {
1211-
return false;
1211+
return {};
12121212
}
12131213
HeapTypeInfo* super = getHeapTypeInfo(*this)->supertype;
12141214
if (super != nullptr) {
1215-
out = HeapType(uintptr_t(super));
1216-
return true;
1215+
return HeapType(uintptr_t(super));
12171216
}
1218-
return false;
1217+
return {};
12191218
}
12201219

12211220
bool HeapType::isNominal() const {

0 commit comments

Comments
 (0)