| 7 | namespace Loader { |
| 8 | |
| 9 | Expect<void> Loader::loadCoreInstance(AST::Component::CoreInstance &Instance) { |
| 10 | auto ReportError = [this](auto E) { |
| 11 | return logLoadError(E, FMgr.getLastOffset(), |
| 12 | ASTNodeAttr::Comp_CoreInstance); |
| 13 | }; |
| 14 | // core:instance ::= ie:<core:instanceexpr> |
| 15 | // => (instance ie) |
| 16 | // core:instanceexpr ::= 0x00 m:<moduleidx> arg*:vec(<core:instantiatearg>) |
| 17 | // => (instantiate m arg*) |
| 18 | // | 0x01 e*:vec(<core:inlineexport>) |
| 19 | // => e* |
| 20 | |
| 21 | auto LoadInstArg = |
| 22 | [this](AST::Component::InstantiateArg<uint32_t> &Arg) -> Expect<void> { |
| 23 | auto ReportArgError = [this](auto E) { |
| 24 | return logLoadError(E, FMgr.getLastOffset(), |
| 25 | ASTNodeAttr::Comp_CoreInstanceArg); |
| 26 | }; |
| 27 | // core:instantiatearg ::= n:<core:name> 0x12 i:<instanceidx> |
| 28 | // => (with n (instance i)) |
| 29 | EXPECTED_TRY(Arg.getName(), FMgr.readName().map_error(ReportArgError)); |
| 30 | EXPECTED_TRY(uint8_t B, FMgr.readByte().map_error(ReportArgError)); |
| 31 | if (B != 0x12U) { |
| 32 | return ReportArgError(ErrCode::Value::MalformedCoreInstance); |
| 33 | } |
| 34 | EXPECTED_TRY(Arg.getIndex(), FMgr.readU32().map_error(ReportArgError)); |
| 35 | return {}; |
| 36 | }; |
| 37 | |
| 38 | auto LoadInlineExp = |
| 39 | [this](AST::Component::InlineExport &Exp) -> Expect<void> { |
| 40 | // core:inlineexport ::= n:<core:name> si:<core:sortidx> => (export n si) |
| 41 | EXPECTED_TRY(Exp.getName(), FMgr.readName().map_error([this](auto E) { |
| 42 | return logLoadError(E, FMgr.getLastOffset(), |
| 43 | ASTNodeAttr::Comp_CoreInlineExport); |
| 44 | })); |
| 45 | return loadSortIndex(Exp.getSortIdx(), true).map_error([](auto E) { |
| 46 | spdlog::error(ErrInfo::InfoAST(ASTNodeAttr::Comp_CoreInlineExport)); |
| 47 | return E; |
| 48 | }); |
| 49 | }; |
| 50 | |
| 51 | EXPECTED_TRY(uint8_t Flag, FMgr.readByte().map_error(ReportError)); |
| 52 | switch (Flag) { |
| 53 | case 0x00: { |
| 54 | EXPECTED_TRY(uint32_t Idx, FMgr.readU32().map_error(ReportError)); |
| 55 | std::vector<AST::Component::InstantiateArg<uint32_t>> Args; |
| 56 | EXPECTED_TRY(loadVec<AST::Component::CoreInstance>(Args, LoadInstArg)); |
| 57 | Instance.setInstantiateArgs(Idx, std::move(Args)); |
| 58 | return {}; |
| 59 | } |
| 60 | case 0x01: { |
| 61 | std::vector<AST::Component::InlineExport> Exports; |
| 62 | EXPECTED_TRY(loadVec<AST::Component::CoreInstance>(Exports, LoadInlineExp)); |
| 63 | Instance.setInlineExports(std::move(Exports)); |
| 64 | return {}; |
| 65 | } |
| 66 | default: |
nothing calls this directly
no test coverage detected