|
| 1 | +quickr_error_msg_name <- function() "quickr_err_msg" |
| 2 | + |
| 3 | +quickr_error_msg_len <- function() 256L |
| 4 | + |
| 5 | +quickr_error_setter_name <- function() "quickr_set_error_msg" |
| 6 | + |
| 7 | +quickr_error_arg_names <- function() { |
| 8 | + c(quickr_error_msg_name()) |
| 9 | +} |
| 10 | + |
| 11 | +is_quickr_error_msg <- function(name) { |
| 12 | + identical(name, quickr_error_msg_name()) |
| 13 | +} |
| 14 | + |
| 15 | +scope_root_for_errors <- function(scope) { |
| 16 | + if (!inherits(scope, "quickr_scope")) { |
| 17 | + return(scope) |
| 18 | + } |
| 19 | + while ( |
| 20 | + !identical(attr(scope, "kind", exact = TRUE), "subroutine") && |
| 21 | + inherits(parent.env(scope), "quickr_scope") |
| 22 | + ) { |
| 23 | + scope <- parent.env(scope) |
| 24 | + } |
| 25 | + scope |
| 26 | +} |
| 27 | + |
| 28 | +mark_scope_uses_errors <- function(scope) { |
| 29 | + root <- scope_root_for_errors(scope) |
| 30 | + if (inherits(root, "quickr_scope")) { |
| 31 | + attr(root, "uses_errors") <- TRUE |
| 32 | + } |
| 33 | + invisible(TRUE) |
| 34 | +} |
| 35 | + |
| 36 | +scope_uses_errors <- function(scope) { |
| 37 | + root <- scope_root_for_errors(scope) |
| 38 | + isTRUE(attr(root, "uses_errors", TRUE)) |
| 39 | +} |
| 40 | + |
| 41 | +fortran_string_literal <- function(x) { |
| 42 | + stopifnot(is_string(x)) |
| 43 | + escaped <- gsub("\r\n|\r|\n", "\\\\n", x) |
| 44 | + escaped <- gsub("\"", "\"\"", escaped, fixed = TRUE) |
| 45 | + paste0("\"", escaped, "\"") |
| 46 | +} |
| 47 | + |
| 48 | +check_quickr_error_message_continuable <- function(msg) { |
| 49 | + stopifnot(is_string(msg)) |
| 50 | + if (grepl("[ \t]", msg)) { |
| 51 | + return(invisible(TRUE)) |
| 52 | + } |
| 53 | + msg_literal <- fortran_string_literal(msg) |
| 54 | + line_len <- nchar(glue( |
| 55 | + "&{quickr_error_setter_name()}( {msg_literal} )" |
| 56 | + )) |
| 57 | + if (line_len > 132L) { |
| 58 | + stop( |
| 59 | + "Error message is too long to fit in a single Fortran line without spaces.", |
| 60 | + " Add spaces to allow line continuations.", |
| 61 | + call. = FALSE |
| 62 | + ) |
| 63 | + } |
| 64 | + invisible(TRUE) |
| 65 | +} |
| 66 | + |
| 67 | +quickr_error_manifest_lines <- function() { |
| 68 | + msg_name <- quickr_error_msg_name() |
| 69 | + len_val <- quickr_error_msg_len() |
| 70 | + |
| 71 | + glue("character(kind=c_char), intent(inout) :: {msg_name}({len_val})") |
| 72 | +} |
| 73 | + |
| 74 | +quickr_error_helper_fortran <- function(openmp = FALSE) { |
| 75 | + msg_name <- quickr_error_msg_name() |
| 76 | + setter <- quickr_error_setter_name() |
| 77 | + len_val <- quickr_error_msg_len() |
| 78 | + |
| 79 | + glue::trim(str_flatten_lines( |
| 80 | + glue("subroutine {setter}(msg)"), |
| 81 | + " character(len=*), intent(in) :: msg", |
| 82 | + " integer :: i", |
| 83 | + " integer :: n", |
| 84 | + if (isTRUE(openmp)) " !$omp critical (quickr_error)", |
| 85 | + glue(" if ({msg_name}(1) == c_null_char) then"), |
| 86 | + glue(" n = min(len(msg), {len_val} - 1)"), |
| 87 | + glue(" {msg_name}(1:n) = [(msg(i:i), i = 1, n)]"), |
| 88 | + glue(" {msg_name}(n + 1) = c_null_char"), |
| 89 | + " end if", |
| 90 | + if (isTRUE(openmp)) " !$omp end critical (quickr_error)", |
| 91 | + glue("end subroutine {setter}") |
| 92 | + )) |
| 93 | +} |
| 94 | + |
| 95 | +quickr_error_fortran_lines <- function(message = NULL, scope = NULL) { |
| 96 | + msg <- message %||% "quickr error" |
| 97 | + stopifnot(is_string(msg)) |
| 98 | + if (!nzchar(msg)) { |
| 99 | + msg <- "quickr error" |
| 100 | + } |
| 101 | + check_quickr_error_message_continuable(msg) |
| 102 | + msg_literal <- fortran_string_literal(msg) |
| 103 | + lines <- glue("call {quickr_error_setter_name()}({msg_literal})") |
| 104 | + if (isTRUE(scope_in_openmp(scope))) { |
| 105 | + lines <- c(lines, "!$omp cancel do") |
| 106 | + } else { |
| 107 | + lines <- c(lines, "return") |
| 108 | + } |
| 109 | + lines |
| 110 | +} |
| 111 | + |
| 112 | +quickr_error_return_if_set <- function( |
| 113 | + scope, |
| 114 | + openmp_depth = scope_openmp_depth(scope) |
| 115 | +) { |
| 116 | + if (!isTRUE(scope_uses_errors(scope))) { |
| 117 | + return("") |
| 118 | + } |
| 119 | + if (is.null(openmp_depth)) { |
| 120 | + openmp_depth <- 0L |
| 121 | + } |
| 122 | + openmp_depth <- max(as.integer(openmp_depth), 0L) |
| 123 | + if (openmp_depth > 0L) { |
| 124 | + return(str_flatten_lines( |
| 125 | + glue("if ({quickr_error_msg_name()}(1) /= c_null_char) then"), |
| 126 | + " !$omp cancel do", |
| 127 | + "end if" |
| 128 | + )) |
| 129 | + } |
| 130 | + glue("if ({quickr_error_msg_name()}(1) /= c_null_char) return") |
| 131 | +} |
0 commit comments