Serialize sub type. See "include/loader/serialize.h".
| 164 | |
| 165 | // Serialize sub type. See "include/loader/serialize.h". |
| 166 | Expect<void> |
| 167 | Serializer::serializeType(const AST::SubType &SType, |
| 168 | std::vector<uint8_t> &OutVec) const noexcept { |
| 169 | // Sub type: vec(typeidx) |
| 170 | if (SType.getSuperTypeIndices().size() > 0) { |
| 171 | if (!Conf.hasProposal(Proposal::GC)) { |
| 172 | return logNeedProposal(ErrCode::Value::MalformedValType, Proposal::GC, |
| 173 | ASTNodeAttr::Type_Rec); |
| 174 | } |
| 175 | if (SType.isFinal()) { |
| 176 | OutVec.push_back(static_cast<uint8_t>(TypeCode::SubFinal)); |
| 177 | } else { |
| 178 | OutVec.push_back(static_cast<uint8_t>(TypeCode::Sub)); |
| 179 | } |
| 180 | serializeU32(static_cast<uint32_t>(SType.getSuperTypeIndices().size()), |
| 181 | OutVec); |
| 182 | for (const auto &Idx : SType.getSuperTypeIndices()) { |
| 183 | serializeU32(Idx, OutVec); |
| 184 | } |
| 185 | } |
| 186 | // Composite type: array | struct | func |
| 187 | TypeCode CTypeCode = SType.getCompositeType().getContentTypeCode(); |
| 188 | OutVec.push_back(static_cast<uint8_t>(CTypeCode)); |
| 189 | switch (CTypeCode) { |
| 190 | case TypeCode::Func: |
| 191 | EXPECTED_TRY(serializeType(SType.getCompositeType().getFuncType(), OutVec)); |
| 192 | break; |
| 193 | case TypeCode::Array: |
| 194 | if (!Conf.hasProposal(Proposal::GC)) { |
| 195 | return logNeedProposal(ErrCode::Value::MalformedValType, Proposal::GC, |
| 196 | ASTNodeAttr::Type_Rec); |
| 197 | } |
| 198 | EXPECTED_TRY(serializeType(SType.getCompositeType().getFieldTypes().front(), |
| 199 | OutVec)); |
| 200 | break; |
| 201 | case TypeCode::Struct: |
| 202 | // Struct type: vec(fieldtype) |
| 203 | if (!Conf.hasProposal(Proposal::GC)) { |
| 204 | return logNeedProposal(ErrCode::Value::MalformedValType, Proposal::GC, |
| 205 | ASTNodeAttr::Type_Rec); |
| 206 | } |
| 207 | serializeU32( |
| 208 | static_cast<uint32_t>(SType.getCompositeType().getFieldTypes().size()), |
| 209 | OutVec); |
| 210 | for (auto FType : SType.getCompositeType().getFieldTypes()) { |
| 211 | EXPECTED_TRY(serializeType(FType, OutVec)); |
| 212 | } |
| 213 | break; |
| 214 | default: |
| 215 | return logSerializeError(ErrCode::Value::MalformedValType, |
| 216 | ASTNodeAttr::Type_Rec); |
| 217 | } |
| 218 | return {}; |
| 219 | } |
| 220 | |
| 221 | // Serialize function type. See "include/loader/serialize.h". |
| 222 | Expect<void> |
nothing calls this directly
no test coverage detected