diff options
Diffstat (limited to 'src/wasm-type.h')
-rw-r--r-- | src/wasm-type.h | 55 |
1 files changed, 52 insertions, 3 deletions
diff --git a/src/wasm-type.h b/src/wasm-type.h index 8c8d85ebf..29e5114b7 100644 --- a/src/wasm-type.h +++ b/src/wasm-type.h @@ -17,14 +17,18 @@ #ifndef wasm_wasm_type_h #define wasm_wasm_type_h -#include "support/name.h" -#include "support/parent_index_iterator.h" -#include "wasm-features.h" +#include <functional> #include <optional> #include <ostream> +#include <string> #include <variant> #include <vector> +#include "support/index.h" +#include "support/name.h" +#include "support/parent_index_iterator.h" +#include "wasm-features.h" + // TODO: At various code locations we were assuming that single types are basic // types, but this is going to change with the introduction of the compound // Signature, Struct and Array types that will be single but not basic. To @@ -68,6 +72,17 @@ struct Rtt; enum Nullability { NonNullable, Nullable }; enum Mutability { Immutable, Mutable }; +// HeapType name information used for printing. +struct TypeNames { + // The name of the type. + Name name; + // For a Struct, names of fields. + std::unordered_map<Index, Name> fieldNames; +}; + +// Used to generate HeapType names. +using HeapTypeNameGenerator = std::function<TypeNames(HeapType)>; + // The type used for interning IDs in the public interfaces of Type and // HeapType. using TypeID = uint64_t; @@ -271,6 +286,22 @@ public: return lub; } + // Helper allowing the value of `print(...)` to be sent to an ostream. Stores + // a `TypeID` because `Type` is incomplete at this point and using a reference + // makes it less convenient to use. + struct Printed { + TypeID typeID; + HeapTypeNameGenerator generateName; + }; + + // Given a function for generating non-basic HeapType names, print this Type + // to `os`.`generateName` should return the same name each time it is called + // with the same HeapType and it should return different names for different + // types. + Printed print(HeapTypeNameGenerator generateName) { + return Printed{getID(), generateName}; + } + std::string toString() const; size_t size() const; @@ -385,6 +416,22 @@ public: // Return the LUB of two HeapTypes. The LUB always exists. static HeapType getLeastUpperBound(HeapType a, HeapType b); + // Helper allowing the value of `print(...)` to be sent to an ostream. Stores + // a `TypeID` because `Type` is incomplete at this point and using a reference + // makes it less convenient to use. + struct Printed { + TypeID typeID; + HeapTypeNameGenerator generateName; + }; + + // Given a function for generating HeapType names, print the definition of + // this HeapType to `os`. `generateName` should return the same + // name each time it is called with the same HeapType and it should return + // different names for different types. + Printed print(HeapTypeNameGenerator generateName) { + return Printed{getID(), generateName}; + } + std::string toString() const; }; @@ -667,7 +714,9 @@ struct TypeBuilder { }; std::ostream& operator<<(std::ostream&, Type); +std::ostream& operator<<(std::ostream&, Type::Printed); std::ostream& operator<<(std::ostream&, HeapType); +std::ostream& operator<<(std::ostream&, HeapType::Printed); std::ostream& operator<<(std::ostream&, Tuple); std::ostream& operator<<(std::ostream&, Signature); std::ostream& operator<<(std::ostream&, Field); |