lib/loader/ast/description.cpp
Expect<void> Loader::loadDesc(AST::ImportDesc &ImpDesc) {
  // Read the module name.
  if (auto Res = FMgr.readName()) {
    ImpDesc.setModuleName(*Res);
  } else {
    return logLoadError(Res.error(), FMgr.getLastOffset(),
                        ASTNodeAttr::Desc_Import);
  }

  // Read the external name.
  if (auto Res = FMgr.readName()) {
    ImpDesc.setExternalName(*Res);
  } else {
    return logLoadError(Res.error(), FMgr.getLastOffset(),
                        ASTNodeAttr::Desc_Import);
  }

  // Read the external type.
  if (auto Res = FMgr.readByte()) {
    ImpDesc.setExternalType(static_cast<ExternalType>(*Res));
  } else {
    return logLoadError(Res.error(), FMgr.getLastOffset(),
                        ASTNodeAttr::Desc_Import);
  }

  // Make content node according to external type.
  switch (ImpDesc.getExternalType()) {
  case ExternalType::Function: {
    // Read the function type index.
    if (auto Res = FMgr.readU32()) {
      ImpDesc.setExternalFuncTypeIdx(*Res);
    } else {
      return logLoadError(Res.error(), FMgr.getLastOffset(),
                          ASTNodeAttr::Desc_Import);
    }
    break;
  }
  case ExternalType::Table: {
    // Read the table type node.
    return loadType(ImpDesc.getExternalTableType());
  }
  case ExternalType::Memory: {
    // Read the memory type node.
    return loadType(ImpDesc.getExternalMemoryType());
  }
  case ExternalType::Global: {
    // Read the global type node.
    if (auto Res = loadType(ImpDesc.getExternalGlobalType()); !Res) {
      return Unexpect(Res.error());
    }
    // Import the mutable globals are for ImportExportMutGlobals proposal.
    if (ImpDesc.getExternalGlobalType().getValMut() == ValMut::Var &&
        unlikely(!Conf.hasProposal(Proposal::ImportExportMutGlobals))) {
      return logNeedProposal(ErrCode::InvalidMut,
                             Proposal::ImportExportMutGlobals,
                             FMgr.getLastOffset(), ASTNodeAttr::Desc_Import);
    }
    return {};
  }
  default:
    return logLoadError(ErrCode::MalformedImportKind, FMgr.getLastOffset(),
                        ASTNodeAttr::Desc_Import);
  }
  return {};
}
// Load binary of Import description. See "include/loader/loader.h".
Load binary of Import description.
Load binary of Import description .
Expect<void> Loader::loadDesc(AST::ImportDesc &ImpDesc) {
  if (auto Res = FMgr.readName()) {
    ImpDesc.setModuleName(*Res);
  } else {
    return logLoadError(Res.error(), FMgr.getLastOffset(),
                        ASTNodeAttr::Desc_Import);
  }
  if (auto Res = FMgr.readName()) {
    ImpDesc.setExternalName(*Res);
  } else {
    return logLoadError(Res.error(), FMgr.getLastOffset(),
                        ASTNodeAttr::Desc_Import);
  }
  if (auto Res = FMgr.readByte()) {
    ImpDesc.setExternalType(static_cast<ExternalType>(*Res));
  } else {
    return logLoadError(Res.error(), FMgr.getLastOffset(),
                        ASTNodeAttr::Desc_Import);
  }
  switch (ImpDesc.getExternalType()) {
  case ExternalType::Function: {
    if (auto Res = FMgr.readU32()) {
      ImpDesc.setExternalFuncTypeIdx(*Res);
    } else {
      return logLoadError(Res.error(), FMgr.getLastOffset(),
                          ASTNodeAttr::Desc_Import);
    }
    break;
  }
  case ExternalType::Table: {
    return loadType(ImpDesc.getExternalTableType());
  }
  case ExternalType::Memory: {
    return loadType(ImpDesc.getExternalMemoryType());
  }
  case ExternalType::Global: {
    if (auto Res = loadType(ImpDesc.getExternalGlobalType()); !Res) {
      return Unexpect(Res.error());
    }
    if (ImpDesc.getExternalGlobalType().getValMut() == ValMut::Var &&
        unlikely(!Conf.hasProposal(Proposal::ImportExportMutGlobals))) {
      return logNeedProposal(ErrCode::InvalidMut,
                             Proposal::ImportExportMutGlobals,
                             FMgr.getLastOffset(), ASTNodeAttr::Desc_Import);
    }
    return {};
  }
  default:
    return logLoadError(ErrCode::MalformedImportKind, FMgr.getLastOffset(),
                        ASTNodeAttr::Desc_Import);
  }
  return {};
}
Expect < void > Loader :: loadDesc ( AST :: ImportDesc & ImpDesc ) { if ( auto Res = FMgr . readName ( ) ) { ImpDesc . setModuleName ( * Res ) ; } else { return logLoadError ( Res . error ( ) , FMgr . getLastOffset ( ) , ASTNodeAttr :: Desc_Import ) ; } if ( auto Res = FMgr . readName ( ) ) { ImpDesc . setExternalName ( * Res ) ; } else { return logLoadError ( Res . error ( ) , FMgr . getLastOffset ( ) , ASTNodeAttr :: Desc_Import ) ; } if ( auto Res = FMgr . readByte ( ) ) { ImpDesc . setExternalType ( static_cast < ExternalType > ( * Res ) ) ; } else { return logLoadError ( Res . error ( ) , FMgr . getLastOffset ( ) , ASTNodeAttr :: Desc_Import ) ; } switch ( ImpDesc . getExternalType ( ) ) { case ExternalType :: Function : { if ( auto Res = FMgr . readU32 ( ) ) { ImpDesc . setExternalFuncTypeIdx ( * Res ) ; } else { return logLoadError ( Res . error ( ) , FMgr . getLastOffset ( ) , ASTNodeAttr :: Desc_Import ) ; } break ; } case ExternalType :: Table : { return loadType ( ImpDesc . getExternalTableType ( ) ) ; } case ExternalType :: Memory : { return loadType ( ImpDesc . getExternalMemoryType ( ) ) ; } case ExternalType :: Global : { if ( auto Res = loadType ( ImpDesc . getExternalGlobalType ( ) ) ; ! Res ) { return Unexpect ( Res . error ( ) ) ; } if ( ImpDesc . getExternalGlobalType ( ) . getValMut ( ) == ValMut :: Var && unlikely ( ! Conf . hasProposal ( Proposal :: ImportExportMutGlobals ) ) ) { return logNeedProposal ( ErrCode :: InvalidMut , Proposal :: ImportExportMutGlobals , FMgr . getLastOffset ( ) , ASTNodeAttr :: Desc_Import ) ; } return { } ; } default : return logLoadError ( ErrCode :: MalformedImportKind , FMgr . getLastOffset ( ) , ASTNodeAttr :: Desc_Import ) ; } return { } ; }
Load binary of Import description.
Load binary of Import description .
// Read the module name. // Read the external name. // Read the external type. // Make content node according to external type. // Read the function type index. // Read the table type node. // Read the memory type node. // Read the global type node. // Import the mutable globals are for ImportExportMutGlobals proposal.