R/transpile.R

# Do not edit this file. It is generated from transpiler/R/transpile.R
# by 'make transpiler-sync'. Edit the 'transpiler' package instead.

#' Transpile an R expression
#'
#' @param expr An \R expression, typically a function call to transpile.
#' If FALSE, then the transpiler is disabled, and if TRUE, it is re-enabled.
#' If NA, then TRUE is returned if the transpiler is enabled, otherwise FALSE.
#'
#' @param options (optional) Named options for the transpilation.
#' 
#' @param when If TRUE (default), the expression is transpiled, otherwise not.
#'
#' @param eval If TRUE (default), the transpiled expression is evaluated,
#' otherwise it is returned.
#'
#' @param envir The environment where the expression should be evaluated.
#'
#' @param type Type of the transpiler to use.
#'
#' @param unwrap (optional) A list of functions that should be considered
#' wrapping functions that the transpiler should unwrap ("enter"). This
#' allows us to transpile expressions within `{ ... }` and `local( ... )`.
#'
#' @returns
#' Returns the value of the evaluated expression `expr` if `eval = TRUE`,
#' otherwise the transpiled expression.
#' If `expr` is NA, then TRUE is returned if the transpiler is enabled,
#' otherwise FALSE.
#'
#' @keywords internal
transpile <- local({
  .enabled <- list()
  
  function(expr, options = list(...), ..., when = TRUE, eval = TRUE, envir = parent.frame(), disable = FALSE, type = "built-in", what = "transpile", unwrap = list(base::`{`, base::`(`, base::`!`, base::local, base::with, base::I, base::identity, base::invisible, base::suppressMessages, base::suppressWarnings, base::suppressPackageStartupMessages, base::withCallingHandlers), debug = FALSE) {
    if (debug) {
      mdebug_push("transpile() ...")
      debug_stack <- mdebug_stack()
      on.exit({
        mdebug_stack(debug_stack)
        mdebug_pop()
      })
    }
  
    stopifnot(
      is.logical(when), length(when) == 1L, !is.na(when)
    )
  
    ## Enable or disable transpiler, or query its state?
    enabled <- .enabled[[type]]
    if (is.null(enabled)) {
      enabled <- TRUE
      .enabled[[type]] <<- enabled
    }
    
    ## e.g. transpile(TRUE), transpile(FALSE), or transpile(NA)?
    if (is.logical(expr) && length(expr) == 1L) {
      if (is.na(expr)) return(enabled)
      old_enabled <- enabled
      .enabled[[type]] <<- expr
      return(invisible(old_enabled))
    }
  
    ## Don't transpile, i.e. evaluate as-is?
    if (!enabled || !when || disable) {
      if (eval) {
        if (debug) mdebug("Evaluate call expression")
        return(eval(expr, envir = envir))
      } else {
        if (debug) mdebug("Return call expression")
        return(expr)
      }
    }

    repeat {
      ## 1a. Get a matching transpiler
      transpiler <- get_transpiler(expr, envir = envir, type = type, what = what, unwrap = unwrap, debug = debug)
    
      transpile <- transpiler[["transpiler"]]
  
      ## 1b. If not a nested transpiler function, we're done here
      if (!inherits(transpile, "transpiler")) break
  
      ## 1c. Generate transpiled expression of nested transpiler
      expr <- local({
        if (debug) {
          mdebug_push("Apply nested transpiler ...")
          on.exit({
            mprint(expr)
            mdebug_pop()
          })
          mprint(expr)
        }
        parts <- as.list(expr)
        parts$eval <- FALSE
        expr2 <- as.call(parts)
        expr <- eval(expr2, envir = envir)
        expr
      })
    }
  
  
    ## 2. Transpile
    if (debug) {
      mdebug_push("Transpile call expression ...")
    }
  
    expr_transpiled <- transpile(expr, options = options)
    class(expr_transpiled) <- c("transpiled_call", class(expr_transpiled))
    
    if (debug) {
      mprint(expr_transpiled)
      mdebug_pop()
    }
  
  
    ## 3. Evaluate or return transpiled expression?
    if (eval) {
      if (debug) mdebug("Evaluate transpiled call expression")
      eval(expr_transpiled, envir = envir)
    } else {
      if (debug) mdebug("Return transpiled call expression")
      expr_transpiled
    }
  } ## transpile()
})
class(transpile) <- c("transpiler", class(transpile))


#' Finds transpiler for S3 method for S3 generic function call and object
#'
#' @inheritParams find_s3_method
#'
#' @param type Type of the transpiler to use.
#'
#' @return
#' A transpiler function, or NULL if none exists.
#'
#' @noRd
find_s3_method_transpiler <- function(fcn, fcn_name, call, envir, type, what = "transpile", debug = FALSE) {
  method <- find_s3_method(fcn, fcn_name = fcn_name, call = call, envir = envir, what = what, debug = debug)
  if (is.null(method)) return(NULL)

  ## Identify method at run time?
  if (isTRUE(method[["deferred"]])) {
    return(make_deferred_dispatch_transpiler(fcn_name, index = method[["index"]]))
  }

  pkg <- method[["package"]]
  name <- method[["name"]]

  ## Look up registered transpiler for the package of the S3 method
  transpiler_sets <- get_transpilers(type)
  transpilers <- transpiler_sets[[pkg]]

  ## If non-existing, retry by first trying to register transpilers for the package
  if (is.null(transpilers)) {
    transpilers <- tryCatch({
      transpilers_for_package(type = type, package = pkg, action = "make", debug = debug)
      transpiler_sets <- get_transpilers(type)
      transpiler_sets[[pkg]]
    }, error = function(e) NULL)
  }

  transpiler <- transpilers[[name]]

  ## Method found, but there is no transpiler for it?
  if (is.null(transpiler)) {
    transpiler <- list(unsupported = c(method, type = "S3"))
  }

  transpiler
} ## find_s3_method_transpiler()


find_s4_method_transpiler <- function(fcn, fcn_name, call, envir, type, what = "transpile", debug = FALSE) {
  method <- find_s4_method(fcn, fcn_name = fcn_name, call = call, envir = envir, what = what, debug = debug)
  if (is.null(method)) return(NULL)

  ## Identify method at run time?
  if (isTRUE(method[["deferred"]])) {
    return(make_deferred_dispatch_transpiler(fcn_name, index = method[["index"]]))
  }

  pkg <- method[["package"]]
  name <- method[["name"]]

  ## Look up registered transpiler for the package of the S4 method
  transpiler_sets <- get_transpilers(type)
  transpilers <- transpiler_sets[[pkg]]

  ## If non-existing, retry by first trying to register transpilers for the package
  if (is.null(transpilers)) {
    transpilers <- tryCatch({
      transpilers_for_package(type = type, package = pkg, action = "make", debug = debug)
      transpiler_sets <- get_transpilers(type)
      transpiler_sets[[pkg]]
    }, error = function(e) NULL)
  }

  transpiler <- transpilers[[name]]

  ## Method found, but there is no transpiler for it?
  if (is.null(transpiler)) {
    transpiler <- list(unsupported = c(method, type = "S4"))
  }

  transpiler
} ## find_s4_method_transpiler()


#' Creates a transpiler that identifies the S3 or S4 method at run time
#'
#' The transpiled expression evaluates the dispatch argument once,
#' assigns it to a variable, and then transpiles the call with the
#' dispatch argument replaced by that variable.
#'
#' @param fcn_name The name of the generic function.
#'
#' @param index The position of the dispatch argument in the call.
#'
#' @return
#' A transpiler, which is a named list with elements `label` and
#' `transpiler`.
#'
#' @noRd
make_deferred_dispatch_transpiler <- function(fcn_name, index) {
  list(
    label = sprintf("%s() ~> evaluate dispatch argument once, then %s()", fcn_name, .packageName),
    transpiler = function(expr, options = NULL) {
      ## e.g. '...futurize.x'
      name <- as.symbol(sprintf("...%s.x", .packageName))
      value <- expr[[index]]
      expr[[index]] <- name
      call <- make_runtime_transpile_call(expr, options = options)
      bquote(local({
        .(name) <- .(value)
        .(call)
      }))
    }
  )
} ## make_deferred_dispatch_transpiler()


#' Signals an error that an S3 or S4 method is not supported
#'
#' @param method A named list with elements `package`, `name`, `class`,
#' and `type` (`"S3"` or `"S4"`) of the method dispatched to.
#'
#' @param fcn_name,ns_name The name of the generic function and the
#' namespace where it lives.
#'
#' @param what A character string describing what type of transpiler
#' is used.
#'
#' @return
#' Nothing; produces an error.
#'
#' @noRd
stop_unsupported_method <- function(method, fcn_name, ns_name, what) {
  type <- method[["type"]]
  if (type == "S3") {
    info <- sprintf("S3 method %s()", method[["name"]])
  } else {
    info <- "S4 method"
  }
  msg <- sprintf("Do not know how to %s %s(), because it dispatches to the %s of package %s for class %s, which is not supported", what, fcn_name, info, sQuote(method[["package"]]), sQuote(method[["class"]]))

  ## A generic masking a base function, e.g. BiocGenerics::lapply()?
  if (ns_name != "base" && exists(fcn_name, envir = baseenv(), mode = "function", inherits = FALSE)) {
    msg <- sprintf("%s. If you meant base::%s(), call it explicitly, e.g. 'base::%s(...) |> %s()', possibly after coercing the first argument to a basic R object", msg, fcn_name, fcn_name, what)
  }

  stop_with_version(msg)
} ## stop_unsupported_method()


#' Get a registered transpiler for an R expression
#' 
#' @inheritParams transpile
#' @inheritParams parse_call
#' 
#' @param expr The R expression, which contains an R symbol or an R call
#' to be transpiled.
#'
#' @return
#' A transpiler, which is a named list with elements:
#'
#'  * `label` - a character string describing the transpiler
#'
#'  * `transpiler` - a function that takes an R expression and
#'                   an optional argument `options`
#'
#' @keywords internal
#' @noRd
get_transpiler <- function(expr, envir = parent.frame(), unwrap = list(), type, what, debug = FALSE) {
  if (debug) {
    mdebug_push("get_transpiler() ...")
    debug_stack <- mdebug_stack()
    on.exit({
      mdebug_stack(debug_stack)
      mdebug_pop()
    })
    mdebug_push("Finding call to be transpiled ...")
  }
  
  call_pos <- descend_wrappers(expr, envir = envir, unwrap = unwrap, what = what, debug = debug)

  ## Assert that the (unwrapped) expression is a function call
  target <- if (length(call_pos) == 1L) expr else expr[[call_pos[-length(call_pos)]]]
  is_empty_braces <- is.call(target) && length(target) == 1L && identical(target[[1]], as.symbol("{"))
  if (!is.call(target) || is_empty_braces) {
    stop_with_version(sprintf("Do not know how to %s %s, because it is not a function call", what, sQuote(paste(deparse(target), collapse = " "))))
  }

  call <- expr[[call_pos]]
  call_info <- parse_call(call, envir = envir, what = what, debug = debug)
  fcn <- call_info[["fcn"]]
  fcn_name <- call_info[["fcn_name"]]
  ns_name <- call_info[["ns_name"]]

  if (debug) {
    mdebugf("Position of call to be transpiled in expression: c(%s)", comma(call_pos))
    mprint(call)
    mdebug_pop()   
    mdebugf_push("Locating %s transpiler for %s::%s() of class %s ...", sQuote(type), ns_name, fcn_name, sQuote(class(fcn)[1]))
  }

  ## Special case: A nested transpiler function?
  if (inherits(fcn, "transpiler")) {
    if (debug) {
      mdebugf("Detected a nested transpiler function: %s::%s()", ns_name, fcn_name)
    }
    transpiler <- list(
      label      = fcn_name,
      transpiler = fcn
    )

    stopifnot(call_pos == 1L)
    return(transpiler)
  }

  transpiler_sets <- get_transpilers(type)
  transpilers <- transpiler_sets[[ns_name]]
  if (is.null(transpilers)) {
    ## Trying to transpile a non-supported function not part of a package?
    if (!isNamespaceLoaded(ns_name)) {
      info <- if (grepl("^%.*%$", fcn_name)) {
        sprintf("`%s`", fcn_name)
      } else {
        sprintf("%s()", fcn_name)
      }
      stop_with_version(sprintf("Do not know how to %s %s, because it is not part of a package (it lives in environment %s)", what, info, sQuote(ns_name)))
    }

    if (!requireNamespace(ns_name, quietly = TRUE)) {
      info <- if (grepl("^%.*%$", fcn_name)) {
        sprintf("%s::`%s`", ns_name, fcn_name)
      } else {
        sprintf("%s::%s()", ns_name, fcn_name)
      }
      stop(sprintf("Please install %s in order to %s %s",
           sQuote(ns_name), what, info))
    }

    ## Get transpiler package addons
    ## tryCatch() is needed for cases where a package re-exports a generic
    ## from another package, e.g. scater::runPCA() is a re-export of
    ## BiocSingular::runPCA()
    req_pkgs <- tryCatch(
      transpilers_for_package(type = type, package = ns_name, action = "make", debug = debug),
      error = function(e) NULL
    )
    if (!is.null(req_pkgs)) {
      if (debug) {
        mdebugf("Required packages: [n=%d] %s", length(req_pkgs), commaq(req_pkgs))
      }

      okay <- vapply(req_pkgs, FUN.VALUE = NA, FUN = requireNamespace, quietly = TRUE)
      if (!all(okay)) {
        pkgs <- req_pkgs[!okay]
        info <- if (grepl("^%.*%$", fcn_name)) {
          sprintf("%s::`%s`", ns_name, fcn_name)
        } else {
          sprintf("%s::%s()", ns_name, fcn_name)
        }
        stop(sprintf("Please install %s in order to %s %s",
             commaq(pkgs), what, info))
      }
      transpiler_sets <- get_transpilers(type)
      transpilers <- transpiler_sets[[ns_name]]
    }
  }

  if (debug) {
    mdebugf("Namespaces registered with %s(): %s", what, commaq(names(transpiler_sets)))
  }
  
  ## Is there a registered transpiler for the function?
  if (is.null(transpilers) || ! fcn_name %in% names(transpilers)) {
    ## Fallback: S3 generic dispatching to a method in another package
    ## Note: 'call' is expr[[call_pos]] = the function head symbol, not the
    ## full call. The full call expression including arguments is needed for
    ## match.call(), so we reconstruct it here.
    full_call <- if (length(call_pos) == 1L) expr else expr[[call_pos[-length(call_pos)]]]
    if (is_s3_generic(fcn)) {
      transpiler <- find_s3_method_transpiler(fcn, fcn_name, full_call, type, envir = envir, what = what, debug = debug)
    } else if (inherits(fcn, "genericFunction")) {
      transpiler <- find_s4_method_transpiler(fcn, fcn_name, full_call, type, envir = envir, what = what, debug = debug)
    } else {
      transpiler <- NULL
    }
    unsupported <- transpiler[["unsupported"]]
    if (!is.null(unsupported)) {
      stop_unsupported_method(unsupported, fcn_name = fcn_name, ns_name = ns_name, what = what)
    }
    if (is.null(transpiler)) {
      if (is.null(transpilers)) {
        stop_with_version(sprintf("Function %s::%s() is not in one of the registered %s namespaces: %s", ns_name, fcn_name, what, commaq(names(transpiler_sets))))
      }
      stop_with_version(sprintf("Do not know how to %s function: %s()", what, deparse(call)))
    }
  } else {
    transpiler <- transpilers[[fcn_name]]
  }
  if (debug) {
    stopifnot(is.list(transpiler), "label" %in% names(transpiler), "transpiler" %in% names(transpiler))
    mdebugf("Transpiler description: %s", transpiler[["label"]])
    mdebug("Transpiler function:")
    mprint(transpiler[["transpiler"]])
  }

  if (length(call_pos) > 1L) {
    if (debug) {
      mdebug_push("Creating wrapper transpiler ...")
    }
    transpiler_inner <- transpiler[["transpiler"]]
    transpiler <- list(
      label      = sprintf("Apply transpiler to inner expression at c(%s)", comma(call_pos)),
      transpiler = function(expr, ...) {
        inner_pos <- call_pos[-length(call_pos)]         
        expr_inner <- expr[[inner_pos]]
        expr_inner <- transpiler_inner(expr_inner, ...)
        expr[[inner_pos]] <- expr_inner
        expr
      }
    )
    if (debug) {
      mprint(transpiler)
      mdebug_pop()
    }
  }

  if (debug) mdebugf_pop()

  transpiler
} ## get_transpiler()


.env <- new.env()
.env[["transpiler_db"]] <- list()

get_transpilers <- function(type) {
  transpiler_db <- .env[["transpiler_db"]]
  transpiler_db[[type]]
}

append_transpilers <- function(type, ...) {
  transpiler_db <- .env[["transpiler_db"]]
  transpilers <- transpiler_db[[type]]
  if (is.null(transpilers)) transpilers <- list()

  ## Add new, or replace existing, transpiler sets per package
  sets <- c(...)
  for (name in names(sets)) transpilers[name] <- sets[name]

  transpiler_db[[type]] <- transpilers
  .env[["transpiler_db"]] <- transpiler_db
}


list_transpilers <- function(pattern = NULL, class) {
  data <- list()
  transpiler_db <- .env[["transpiler_db"]]
  db <- transpiler_db[[class]]
  if (is.null(db)) db <- list()
  types <- names(db)
  if (!is.null(pattern)) {
    types <- grep(pattern, types, value = TRUE)
  }
  for (type in types) {
    transpilers <- db[[type]]
    fcns <- unique(names(transpilers))
    for (fcn in fcns) {
      idxs <- which(fcn == names(transpilers))
      if (length(idxs) == 1) {
        transpilers_fcn <- transpilers[idxs]
      } else {
        ## length(idxs) > 1 should not happen, but in case ...
        transpilers_fcn <- list()
        for (idx in idxs) {
          transpilers_fcn <- c(transpilers_fcn, transpilers[idx])
        }
        drop <- duplicated(names(transpilers_fcn), fromLast = TRUE)
        transpilers_fcn <- transpilers_fcn[!drop]
      }
      transpilers_fcn <- transpilers_fcn[order(names(transpilers_fcn))]
      names <- names(transpilers_fcn)
      labels <- vapply(transpilers_fcn, FUN = function(t) t$label, FUN.VALUE = "")
      dd <- data.frame(type = type, fcn = names, description = labels)
      data <- c(data, list(dd))
    }
  }
  data <- Reduce(rbind, data)
  rownames(data) <- NULL
  data
}


transpilers_for_package <- local({
  .db <- list()
  
  function(type = "default", package, fcn, action = c("add", "make", "get", "list", "reset"), debug = FALSE) {
    stopifnot(is.character(type), length(type) == 1L, !is.na(type))
    action <- match.arg(action, several.ok = FALSE)
    
    if (debug) {
      mdebugf_push("transpilers_for_package(action = %s, type = %s) ...", sQuote(action), sQuote(type))
      on.exit(mdebug_pop())
    }

    db <- .db[[type]]
    if (is.null(db)) db <- list()
    
    if (action == "add") {
      stopifnot(
        is.character(package), length(package) == 1L,
        is.function(fcn)
      )
      if (debug) {
        mdebugf(" - package: %s", sQuote(package))
      }
      fcns <- old_fcns <- db[[package]]
      fcns <- if (length(fcns) == 0) list(fcn) else c(fcns, list(fcn))
      db[[package]] <- fcns
      .db[[type]] <<- db
      invisible(old_fcns)
    } else if (action == "get") {
      if (debug) {
        mdebugf(" - package: %s", sQuote(package))
      }
      stopifnot(
        is.character(package), length(package) == 1L
      )
      if (debug) mdebugf(" - package: %s", sQuote(package))
      db[[package]]
    } else if (action == "make") {
      stopifnot(
        is.character(package), length(package) == 1L
      )
      if (debug) {
        mdebugf(" - package: %s", sQuote(package))
      }
      fcns <- db[[package]]
      if (debug) mprint(list(fcns = fcns))
      if (length(fcns) == 0L) {
        stop_with_version(sprintf("There are no factory functions for creating %s transpilers for package %s", sQuote(type), sQuote(package)))
      }
      req_pkgs <- lapply(fcns, FUN = function(fcn) fcn())
      req_pkgs <- unlist(req_pkgs, use.names = FALSE)
      req_pkgs <- sort(unique(req_pkgs))
      req_pkgs
    } else if (action == "list") {
      .db
    } else if (action == "reset") {
      old_db <- db
      db <- list()
      .db[[type]] <<- db
      invisible(old_db)
    }
  }
})


transpiler_packages <- function(classes = NULL) {
  db <- transpilers_for_package(action = "list")
  if (!is.null(classes)) {
    db <- db[names(db) %in% classes]
  }
  transpilers <- data.frame(class = character(0L), package = character(0L))
  for (class in names(db)) {
    set <- db[[class]]
    pkgs <- names(set)
    transpilers <- rbind(transpilers, data.frame(class = class, package = pkgs))
  }
  transpilers
}



#' @param package (character string) Package name.
#'
#' @param FUN A functions taking arguments `fcn` (a function),
#' `package` (character string), and `name` (character string).
#'
#' @param export (logical) If TRUE, exported functions are considered.
#'
#' @param s3methods (logical) If TRUE, registered S3 methods are considered.
#'
#' @return
#' A named list of lists of transpilers, where the names correspond
#' to function names of the package `package` and transpilers are lists.
#'
#' @noRd
make_package_transpilers <- function(package, FUN, exports = TRUE, s3methods = TRUE) {
  transpilers <- list()
  ns <- getNamespace(package)

  names <- character(0L)
  if (exports) {
    exports <- names(getNamespaceInfo(ns, "exports"))
    names <- c(names, exports)
  }
  if (s3methods) {
    s3methods <- getNamespaceInfo(ns, "S3methods")[,3]
    if (!is.character(s3methods)) {
      ## Note, although 's3methods' is typically a character vector, it
      ## might also be a list. For example, loadNamespace("strucchange")
      ## results in a character vector, but if we then load
      ## loadNamespace("partykit"), the registered S3 methods for
      ## 'strucchange' becomes a list with two function objects appended
      ## at the very end.
      s3methods <- s3methods[vapply(s3methods, FUN.VALUE = FALSE, FUN = is.character)]
      s3methods <- unlist(s3methods, use.names = FALSE)
    }
    names <- c(names, s3methods)
  }
  
  for (name in names) {
    if (exists(name, mode = "function", envir = ns, inherits = FALSE)) {
      fcn <- get(name, mode = "function", envir = ns, inherits = FALSE)
      transpilers[[name]] <- FUN(fcn, name = name)
    }
  }

  transpilers <- list(transpilers)
  names(transpilers) <- package

  transpilers
} ## make_package_transpilers()


#' @importFrom utils packageName
stop_with_version <- function(msg, ...) {
  msg <- sprintf("[%s %s] %s", packageName(), getNamespaceVersion(packageName()), msg)
  stop(msg, ...)
}

Try the progressify package in your browser

Any scripts or data that you put into this service are public.

progressify documentation built on Nov. 23, 2026, 9:08 a.m.