Where Online Learning is simpler!
The C and C++ Include Header Files
cat -n /usr/include/wabt/type.h
1 /* 2 * Copyright 2020 WebAssembly Community Group participants 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef WABT_TYPE_H_ 18 #define WABT_TYPE_H_ 19 20 #include <cassert> 21 #include <cstdint> 22 #include <vector> 23 24 #include "wabt/config.h" 25 #include "wabt/base-types.h" 26 #include "wabt/string-format.h" 27 28 namespace wabt { 29 30 class Type; 31 32 using TypeVector = std::vector<Type>; 33 34 class Type { 35 public: 36 // Matches binary format, do not change. 37 enum Enum : int32_t { 38 I32 = -0x01, // 0x7f 39 I64 = -0x02, // 0x7e 40 F32 = -0x03, // 0x7d 41 F64 = -0x04, // 0x7c 42 V128 = -0x05, // 0x7b 43 I8 = -0x06, // 0x7a : packed-type only, used in gc and as v128 lane 44 I16 = -0x07, // 0x79 : packed-type only, used in gc and as v128 lane 45 FuncRef = -0x10, // 0x70 46 ExternRef = -0x11, // 0x6f 47 Reference = -0x15, // 0x6b 48 Func = -0x20, // 0x60 49 Struct = -0x21, // 0x5f 50 Array = -0x22, // 0x5e 51 Void = -0x40, // 0x40 52 ___ = Void, // Convenient for the opcode table in opcode.h 53 54 Any = 0, // Not actually specified, but useful for type-checking 55 I8U = 4, // Not actually specified, but used internally with load/store 56 I16U = 6, // Not actually specified, but used internally with load/store 57 I32U = 7, // Not actually specified, but used internally with load/store 58 }; 59 60 Type() = default; // Provided so Type can be member of a union. 61 Type(int32_t code) 62 : enum_(static_cast<Enum>(code)), type_index_(kInvalidIndex) {} 63 Type(Enum e) : enum_(e), type_index_(kInvalidIndex) {} 64 Type(Enum e, Index type_index) : enum_(e), type_index_(type_index) { 65 assert(e == Enum::Reference); 66 } 67 constexpr operator Enum() const { return enum_; } 68 69 bool IsRef() const { 70 return enum_ == Type::ExternRef || enum_ == Type::FuncRef || 71 enum_ == Type::Reference; 72 } 73 74 bool IsReferenceWithIndex() const { return enum_ == Type::Reference; } 75 76 bool IsNullableRef() const { 77 // Currently all reftypes are nullable 78 return IsRef(); 79 } 80 81 std::string GetName() const { 82 switch (enum_) { 83 case Type::I32: return "i32"; 84 case Type::I64: return "i64"; 85 case Type::F32: return "f32"; 86 case Type::F64: return "f64"; 87 case Type::V128: return "v128"; 88 case Type::I8: return "i8"; 89 case Type::I16: return "i16"; 90 case Type::FuncRef: return "funcref"; 91 case Type::Func: return "func"; 92 case Type::Void: return "void"; 93 case Type::Any: return "any"; 94 case Type::ExternRef: return "externref"; 95 case Type::Reference: 96 return StringPrintf("(ref %d)", type_index_); 97 default: 98 return StringPrintf("<type_index[%d]>", enum_); 99 } 100 } 101 102 const char* GetRefKindName() const { 103 switch (enum_) { 104 case Type::FuncRef: return "func"; 105 case Type::ExternRef: return "extern"; 106 case Type::Struct: return "struct"; 107 case Type::Array: return "array"; 108 default: return "<invalid>"; 109 } 110 } 111 112 // Functions for handling types that are an index into the type section. 113 // These are always positive integers. They occur in the binary format in 114 // block signatures, e.g. 115 // 116 // (block (result i32 i64) ...) 117 // 118 // is encoded as 119 // 120 // (type $T (func (result i32 i64))) 121 // ... 122 // (block (type $T) ...) 123 // 124 bool IsIndex() const { return static_cast<int32_t>(enum_) >= 0; } 125 126 Index GetIndex() const { 127 assert(IsIndex()); 128 return static_cast<Index>(enum_); 129 } 130 131 Index GetReferenceIndex() const { 132 assert(enum_ == Enum::Reference); 133 return type_index_; 134 } 135 136 TypeVector GetInlineVector() const { 137 assert(!IsIndex()); 138 switch (enum_) { 139 case Type::Void: 140 return TypeVector(); 141 142 case Type::I32: 143 case Type::I64: 144 case Type::F32: 145 case Type::F64: 146 case Type::V128: 147 case Type::FuncRef: 148 case Type::ExternRef: 149 case Type::Reference: 150 return TypeVector(this, this + 1); 151 152 default: 153 WABT_UNREACHABLE; 154 } 155 } 156 157 private: 158 Enum enum_; 159 Index type_index_; // Only used for for Type::Reference 160 }; 161 162 } // namespace wabt 163 164 #endif // WABT_TYPE_H_