rune/compile/options.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
use core::fmt;
use ::rust_alloc::boxed::Box;
/// Error raised when trying to parse an invalid option.
#[derive(Debug, Clone)]
pub struct ParseOptionError {
env: Option<&'static str>,
option: Box<str>,
}
impl fmt::Display for ParseOptionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Unsupported compile option `{}`", self.option)?;
if let Some(env) = self.env {
write!(f, " (environment `{env}`)")?;
}
Ok(())
}
}
impl core::error::Error for ParseOptionError {}
/// Options specific to formatting.
#[derive(Debug, Clone)]
pub(crate) struct FmtOptions {
/// Attempt to format even when faced with syntax errors.
pub(crate) error_recovery: bool,
/// Force newline at end of document.
pub(crate) force_newline: bool,
}
impl FmtOptions {
/// The default format option.
pub(crate) const DEFAULT: Self = Self {
error_recovery: false,
force_newline: true,
};
/// Parse an option with the extra diagnostics metadata.
fn parse_option_with(
&mut self,
option: &str,
env: Option<&'static str>,
) -> Result<(), ParseOptionError> {
let (head, tail) = if let Some((head, tail)) = option.trim().split_once('=') {
(head.trim(), Some(tail.trim()))
} else {
(option.trim(), None)
};
match head {
"error-recovery" => {
self.error_recovery = tail.map_or(true, |s| s == "true");
}
"force-newline" => {
self.force_newline = tail.map_or(true, |s| s == "true");
}
_ => {
return Err(ParseOptionError {
env,
option: option.into(),
});
}
}
Ok(())
}
}
impl Default for FmtOptions {
#[inline]
fn default() -> Self {
FmtOptions::DEFAULT
}
}
/// Documentation for a single compiler option.
#[non_exhaustive]
pub struct OptionMeta {
/// The key.
pub key: &'static str,
/// Whether the option is unstable or not.
pub unstable: bool,
/// The documentation for the option.
pub doc: &'static [&'static str],
/// The default value for the option.
pub default: &'static str,
/// Available options.
pub options: &'static str,
}
/// Options that can be provided to the compiler.
///
/// See [Build::with_options][crate::Build::with_options].
#[derive(Debug, Clone)]
pub struct Options {
/// Perform link-time checks.
pub(crate) link_checks: bool,
/// Memoize the instance function in a loop.
pub(crate) memoize_instance_fn: bool,
/// Include debug information when compiling.
pub(crate) debug_info: bool,
/// Support macros.
pub(crate) macros: bool,
/// Support bytecode caching.
pub(crate) bytecode: bool,
/// Build sources as function bodies.
///
/// The function to run will be named 0, which can be constructed with
/// `Hash::EMPTY`.
pub(crate) function_body: bool,
/// When running tests, include std tests.
pub(crate) test_std: bool,
/// Enable lowering optimizations.
pub(crate) lowering: u8,
/// Print source tree.
pub(crate) print_tree: bool,
/// Use the v2 compiler.
pub(crate) v2: bool,
/// Maximum macro depth.
pub(crate) max_macro_depth: usize,
/// Rune format options.
pub(crate) fmt: FmtOptions,
}
impl Options {
/// The default options.
pub(crate) const DEFAULT: Options = Options {
link_checks: true,
memoize_instance_fn: true,
debug_info: true,
macros: true,
bytecode: false,
function_body: false,
test_std: false,
lowering: 0,
print_tree: false,
v2: false,
max_macro_depth: 64,
fmt: FmtOptions::DEFAULT,
};
/// Construct lossy rune options from the `RUNEFLAGS` environment variable.
pub fn from_default_env() -> Result<Self, ParseOptionError> {
#[allow(unused_mut)]
let mut options = Self::DEFAULT;
#[cfg(feature = "std")]
{
/// The environment variable where runeflags are loaded from.
static ENV: &str = "RUNEFLAGS";
if let Some(value) = std::env::var_os(ENV) {
let value = value.to_string_lossy();
options.parse_option_with(&value, Some(ENV))?;
}
}
Ok(options)
}
/// Get a list and documentation for all available compiler options.
pub fn available() -> &'static [OptionMeta] {
static BOOL: &str = "true, false";
static VALUES: &[OptionMeta] = &[
OptionMeta {
key: "link-checks",
unstable: false,
doc: &docstring! {
/// Perform link-time checks to ensure that
/// function hashes which are referenced during
/// compilation exist.
},
default: "true",
options: BOOL,
},
OptionMeta {
key: "memoize-instance-fn",
unstable: false,
doc: &docstring! {
/// Memoize the instance function in a loop.
},
default: "true",
options: BOOL,
},
OptionMeta {
key: "debug-info",
unstable: false,
doc: &docstring! {
/// Include debug information when compiling.
///
/// This provides better diagnostics, but also
/// increases memory usage.
},
default: "true",
options: BOOL,
},
OptionMeta {
key: "macros",
unstable: false,
doc: &docstring! {
/// Support macro expansion.
},
default: "true",
options: BOOL,
},
OptionMeta {
key: "bytecode",
unstable: true,
doc: &docstring! {
/// Make use of bytecode, which might make
/// compilation units smaller.
},
default: "false",
options: BOOL,
},
OptionMeta {
key: "function-body",
unstable: true,
doc: &docstring! {
/// Causes sources to be treated as-if they were
/// function bodies, rather than modules.
},
default: "false",
options: BOOL,
},
OptionMeta {
key: "test-std",
unstable: true,
doc: &docstring! {
/// When running tests, includes tests found in the
/// standard library.
},
default: "false",
options: BOOL,
},
OptionMeta {
key: "lowering",
unstable: true,
doc: &docstring! {
/// Enable lowering optimizations.
///
/// Supports a value of 0-3 with increasingly higher
/// levels of optimizations applied.
///
/// Enabling a higher level results in better code
/// generation, but contributes to compilation times.
},
default: "0",
options: "0-3",
},
OptionMeta {
key: "print-tree",
unstable: false,
doc: &docstring! {
/// Print the parsed source tree when formatting to
/// standard output.
///
/// Only avialable when the `std` feature is enabled.
},
default: "false",
options: BOOL,
},
OptionMeta {
key: "v2",
unstable: true,
doc: &docstring! {
/// Use the v2 compiler.
},
default: "false",
options: BOOL,
},
OptionMeta {
key: "max-macro-depth",
unstable: true,
doc: &docstring! {
/// Maximum supported macro depth.
},
default: "64",
options: "<number>",
},
OptionMeta {
key: "fmt.error-recovery",
unstable: true,
doc: &docstring! {
/// Perform error recovery when formatting.
///
/// This allows code to be formatted even if it
/// contains invalid syntax.
},
default: "false",
options: BOOL,
},
OptionMeta {
key: "fmt.force-newline",
unstable: true,
doc: &docstring! {
/// Force newline at end of document.
},
default: "true",
options: BOOL,
},
];
VALUES
}
/// Parse a compiler option. This is the function which parses the
/// `<option>[=<value>]` syntax, which is used by among other things the
/// Rune CLI with the `-O <option>[=<value>]` option.
///
/// It can be used to consistenly parse a collection of options by other
/// programs as well.
pub fn parse_option(&mut self, option: &str) -> Result<(), ParseOptionError> {
self.parse_option_with(option, None)
}
fn parse_option_with(
&mut self,
option: &str,
env: Option<&'static str>,
) -> Result<(), ParseOptionError> {
for option in option.split(',') {
let option = option.trim();
let (head, tail) = if let Some((head, tail)) = option.trim().split_once('=') {
(head.trim(), Some(tail.trim()))
} else {
(option.trim(), None)
};
match head {
"memoize-instance-fn" => {
self.memoize_instance_fn = tail.map_or(true, |s| s == "true");
}
"debug-info" => {
self.debug_info = tail.map_or(true, |s| s == "true");
}
"link-checks" => {
self.link_checks = tail.map_or(true, |s| s == "true");
}
"macros" => {
self.macros = tail.map_or(true, |s| s == "true");
}
"bytecode" => {
self.bytecode = tail.map_or(true, |s| s == "true");
}
"function-body" => {
self.function_body = tail.map_or(true, |s| s == "true");
}
"test-std" => {
self.test_std = tail.map_or(true, |s| s == "true");
}
"lowering" => {
self.lowering = match tail {
Some("0") | None => 0,
Some("1") => 1,
_ => {
return Err(ParseOptionError {
env,
option: option.into(),
})
}
};
}
"print-tree" if cfg!(feature = "std") => {
self.print_tree = tail.map_or(true, |s| s == "true");
}
"v2" => {
self.v2 = tail.map_or(true, |s| s == "true");
}
"max-macro-depth" => {
let Some(Ok(number)) = tail.map(str::parse) else {
return Err(ParseOptionError {
env,
option: option.into(),
});
};
self.max_macro_depth = number;
}
other => {
let Some((head, tail)) = other.split_once('.') else {
return Err(ParseOptionError {
env,
option: option.into(),
});
};
let head = head.trim();
let tail = tail.trim();
match head {
"fmt" => {
self.fmt.parse_option_with(tail, env)?;
}
_ => {
return Err(ParseOptionError {
env,
option: option.into(),
});
}
}
}
}
}
Ok(())
}
/// Enable the test configuration flag.
pub fn test(&mut self, _enabled: bool) {
// ignored
}
/// Set if debug info is enabled or not. Defaults to `true`.
pub fn debug_info(&mut self, enabled: bool) {
self.debug_info = enabled;
}
/// Set if link checks are enabled or not. Defaults to `true`. This will
/// cause compilation to fail if an instruction references a function which
/// does not exist.
pub fn link_checks(&mut self, enabled: bool) {
self.link_checks = enabled;
}
/// Set if macros are enabled or not. Defaults to `false`.
pub fn macros(&mut self, enabled: bool) {
self.macros = enabled;
}
/// Set if bytecode caching is enabled or not. Defaults to `false`.
pub fn bytecode(&mut self, enabled: bool) {
self.bytecode = enabled;
}
/// Memoize the instance function in a loop. Defaults to `false`.
pub fn memoize_instance_fn(&mut self, enabled: bool) {
self.memoize_instance_fn = enabled;
}
/// Whether to build sources as scripts where the source is executed like a
/// function body.
pub fn script(&mut self, enabled: bool) {
self.function_body = enabled;
}
}
impl Default for Options {
#[inline]
fn default() -> Self {
Options::DEFAULT
}
}