koopa 0.0.10

Library for generating/parsing/optimizing Koopa IR.
Documentation
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
//! Span ([`Span`]) and error ([`Error`]) related implementations.

use std::cell::RefCell;
use std::fmt::{self, Arguments};
use std::path::PathBuf;

#[cfg(not(feature = "no-front-logger"))]
use colored::*;
#[cfg(not(feature = "no-front-logger"))]
use std::{fs::File, io::BufRead, io::BufReader, io::Result as IoResult};

/// The type of error returned by logger methods of [`Span`].
#[cfg(feature = "no-front-logger")]
#[derive(Debug)]
pub enum Error {
  /// Normal error.
  Normal(String),
  /// Fatal error.
  Fatal(String),
}

/// The type of error returned by logger methods of [`Span`].
#[cfg(not(feature = "no-front-logger"))]
#[derive(Debug)]
pub enum Error {
  /// Normal error.
  Normal,
  /// Fatal error.
  Fatal,
}

impl Error {
  /// Returns `true` if the current error is fatal.
  #[cfg(feature = "no-front-logger")]
  pub fn is_fatal(&self) -> bool {
    matches!(self, Error::Fatal(..))
  }

  /// Returns `true` if the current error is fatal.
  #[cfg(not(feature = "no-front-logger"))]
  pub fn is_fatal(&self) -> bool {
    matches!(self, Error::Fatal)
  }
}

impl Default for Error {
  /// Creates a normal error.
  #[cfg(feature = "no-front-logger")]
  fn default() -> Self {
    Error::Normal(String::default())
  }

  /// Creates a normal error.
  #[cfg(not(feature = "no-front-logger"))]
  fn default() -> Self {
    Error::Normal
  }
}

impl<T> From<Error> for Result<T, Error> {
  /// Creates a result from the given error,
  /// which the value is always [`Err`].
  fn from(error: Error) -> Self {
    Err(error)
  }
}

/// A span that records source code locations.
///
/// Used to print error messages.
#[derive(Clone, Copy)]
pub struct Span {
  start: Pos,
  end: Pos,
}

/// Gets the string of the given line.
#[cfg(not(feature = "no-front-logger"))]
macro_rules! get_line_str {
  ($line:expr) => {
    $line
      .map_or("".into(), |r| r.unwrap())
      .replace('\t', &format!("{:w$}", "", w = Span::TAB_WIDTH))
  };
  ($line:expr, $col:expr) => {{
    let line = $line.map_or("".into(), |r| r.unwrap());
    let col = $col as usize;
    let tabs = (&line[..col]).matches('\t').count();
    (
      line.replace('\t', &format!("{:w$}", "", w = Span::TAB_WIDTH)),
      col + tabs * (Span::TAB_WIDTH - 1),
    )
  }};
  ($line:expr, $col1:expr, $col2:expr) => {{
    let line = $line.map_or("".into(), |r| r.unwrap());
    let col1 = $col1 as usize;
    let col2 = $col2 as usize;
    let tabs1 = (&line[..col1]).matches('\t').count();
    let tabs2 = tabs1 + (&line[col1..col2]).matches('\t').count();
    (
      line.replace('\t', &format!("{:w$}", "", w = Span::TAB_WIDTH)),
      col1 + tabs1 * (Span::TAB_WIDTH - 1),
      col2 + tabs2 * (Span::TAB_WIDTH - 1),
    )
  }};
}

impl Span {
  /// The column width occupied by the tab character.
  #[cfg(not(feature = "no-front-logger"))]
  const TAB_WIDTH: usize = 2;

  thread_local! {
    static STATE: RefCell<GlobalState> = const {
      RefCell::new(GlobalState {
        file: FileType::Buffer,
        err_num: 0,
        warn_num: 0,
      })
    };
  }

  /// Creates a new span from [`Pos`].
  pub fn new(start: Pos) -> Self {
    Self { start, end: start }
  }

  /// Resets the global state in all spans.
  pub fn reset(file: FileType) {
    Self::STATE.with(|gs| {
      *gs.borrow_mut() = GlobalState {
        file,
        err_num: 0,
        warn_num: 0,
      }
    });
  }

  /// Logs normal error with no span provided.
  #[cfg(feature = "no-front-logger")]
  pub fn log_raw_error(args: Arguments) -> Error {
    // update error number
    Self::STATE.with(|gs| gs.borrow_mut().err_num += 1);
    Error::Normal(format!("{}", args))
  }

  /// Logs normal error with no span provided.
  #[cfg(not(feature = "no-front-logger"))]
  pub fn log_raw_error(args: Arguments) -> Error {
    Self::STATE.with(|gs| {
      // update error number
      gs.borrow_mut().err_num += 1;
      // print message to stderr
      eprintln!("{}: {}", "error".bright_red(), args);
    });
    Error::Normal
  }

  /// Logs fatal error with no span provided.
  #[cfg(feature = "no-front-logger")]
  pub fn log_raw_fatal_error(args: Arguments) -> Error {
    // update error number
    Self::STATE.with(|gs| gs.borrow_mut().err_num += 1);
    Error::Fatal(format!("{}", args))
  }

  /// Logs fatal error with no span provided.
  #[cfg(not(feature = "no-front-logger"))]
  pub fn log_raw_fatal_error(args: Arguments) -> Error {
    Self::STATE.with(|gs| {
      // update error number
      gs.borrow_mut().err_num += 1;
      // print message to stderr
      eprintln!("{}: {}", "error".bright_red(), args);
    });
    Error::Fatal
  }

  /// Logs warning with no span provided.
  #[cfg(feature = "no-front-logger")]
  pub fn log_raw_warning(_: Arguments) {
    // update warning number
    Self::STATE.with(|gs| gs.borrow_mut().warn_num += 1);
  }

  /// Logs warning with no span provided.
  #[cfg(not(feature = "no-front-logger"))]
  pub fn log_raw_warning(args: Arguments) {
    Self::STATE.with(|gs| {
      // update warning number
      gs.borrow_mut().warn_num += 1;
      // print message to stderr
      eprintln!("{}: {}", "warning".yellow(), args);
    });
  }

  /// Logs global information (total error/warning number).
  #[cfg(feature = "no-front-logger")]
  pub fn log_global() {}

  /// Logs global information (total error/warning number).
  #[cfg(not(feature = "no-front-logger"))]
  pub fn log_global() {
    Self::STATE.with(|gs| {
      let gs = gs.borrow();
      let mut msg = String::new();
      // error info
      if gs.err_num != 0 {
        msg += &format!("{} error", gs.err_num);
        if gs.err_num > 1 {
          msg += "s";
        }
      }
      // separator
      if gs.err_num != 0 && gs.warn_num != 0 {
        msg += " and ";
      }
      // warning info
      if gs.warn_num != 0 {
        msg += &format!("{} warning", gs.warn_num);
        if gs.warn_num > 1 {
          msg += "s";
        }
      }
      // ending
      msg += " emitted";
      eprintln!("{}", msg.bold());
    });
  }

  /// Gets the number of errors.
  pub fn error_num() -> usize {
    Self::STATE.with(|gs| gs.borrow().err_num)
  }

  /// Gets the number of warnings.
  pub fn warning_num() -> usize {
    Self::STATE.with(|gs| gs.borrow().warn_num)
  }

  /// Logs normal error message.
  #[cfg(feature = "no-front-logger")]
  pub fn log_error(&self, args: Arguments) -> Error {
    Self::log_raw_error(args);
    Error::Normal(self.error_message(args))
  }

  /// Logs normal error message.
  #[cfg(not(feature = "no-front-logger"))]
  pub fn log_error(&self, args: Arguments) -> Error {
    Self::log_raw_error(args);
    Self::STATE.with(|gs| self.print_file_info(&gs.borrow().file, Color::BrightRed));
    Error::Normal
  }

  /// Logs fatal error message.
  #[cfg(feature = "no-front-logger")]
  pub fn log_fatal_error(&self, args: Arguments) -> Error {
    Self::log_raw_error(args);
    Error::Fatal(self.error_message(args))
  }

  /// Logs fatal error message.
  #[cfg(not(feature = "no-front-logger"))]
  pub fn log_fatal_error(&self, args: Arguments) -> Error {
    Self::log_raw_error(args);
    Self::STATE.with(|gs| self.print_file_info(&gs.borrow().file, Color::BrightRed));
    Error::Fatal
  }

  /// Logs warning message.
  #[cfg(feature = "no-front-logger")]
  pub fn log_warning(&self, args: Arguments) {
    Self::log_raw_warning(args);
  }

  /// Logs warning message.
  #[cfg(not(feature = "no-front-logger"))]
  pub fn log_warning(&self, args: Arguments) {
    Self::log_raw_warning(args);
    Self::STATE.with(|gs| self.print_file_info(&gs.borrow().file, Color::Yellow));
  }

  /// Converts the current span into a new one
  /// where the end position has been updated.
  pub fn into_updated(self, end: Pos) -> Self {
    Self {
      start: self.start,
      end,
    }
  }

  /// Updates the end position.
  pub fn update(&mut self, end: Pos) {
    self.end = end;
  }

  /// Converts the current span into a new one where the end position
  /// has been updated according to another span.
  pub fn into_updated_span(self, span: Span) -> Self {
    Self {
      start: self.start,
      end: span.end,
    }
  }

  /// Updates the end position according to another span.
  pub fn update_span(&mut self, span: Span) {
    self.end = span.end;
  }

  /// Checks if the current span is in the same line as the given span.
  pub fn is_in_same_line_as(&self, span: &Span) -> bool {
    self.end.line == span.start.line
  }

  /// Returns the error message.
  #[cfg(feature = "no-front-logger")]
  fn error_message(&self, args: Arguments) -> String {
    Self::STATE.with(|gs| format!("{}:{}: {}", gs.borrow().file, self.start, args))
  }

  /// Prints the file information.
  #[cfg(not(feature = "no-front-logger"))]
  fn print_file_info(&self, file: &FileType, color: Color) {
    eprintln!("  {} {}:{}", "at".blue(), file, self.start);
    if self.start.col > 0
      && self.end.col > 0
      && let FileType::File(path) = file
    {
      // open file and get lines
      let mut lines = BufReader::new(File::open(path).unwrap()).lines();
      if self.start.line == self.end.line {
        self.print_single_line_info(&mut lines, color);
      } else {
        self.print_multi_line_info(&mut lines, color);
      }
    }
    eprintln!();
  }

  /// Prints the single line information.
  ///
  /// Used by method `print_file_info`.
  #[cfg(not(feature = "no-front-logger"))]
  fn print_single_line_info<T>(&self, lines: &mut T, color: Color)
  where
    T: Iterator<Item = IoResult<String>>,
  {
    // get some parameters
    let line_num = self.start.line as usize;
    let (line, c1, c2) = get_line_str!(lines.nth(line_num - 1), self.start.col, self.end.col);
    let width = ((line_num + 1) as f32).log10().ceil() as usize;
    let leading = c1 - 1;
    let len = c2 - c1 + 1;
    // print the current line to stderr
    eprintln!("{:w$} {}", "", "|".blue(), w = width);
    eprint!("{} ", format!("{:w$}", line_num, w = width).blue());
    eprintln!("{} {}", "|".blue(), line);
    eprint!("{:w$} {} {:l$}", "", "|".blue(), "", w = width, l = leading);
    eprintln!("{}", format!("{:^>w$}", "", w = len).color(color));
  }

  /// Prints the multi-line information.
  ///
  /// Used by method `print_file_info`.
  #[cfg(not(feature = "no-front-logger"))]
  fn print_multi_line_info<T>(&self, lines: &mut T, color: Color)
  where
    T: Iterator<Item = IoResult<String>>,
  {
    // get some parameters
    let width = ((self.end.line + 1) as f32).log10().ceil() as usize;
    // print the first line to stderr
    let line_num = self.start.line as usize;
    let mut lines = lines.skip(line_num - 1);
    let (line, start) = get_line_str!(lines.next(), self.start.col);
    eprintln!("{:w$} {}", "", "|".blue(), w = width);
    eprint!("{} ", format!("{:w$}", line_num, w = width).blue());
    eprintln!("{}   {}", "|".blue(), line);
    eprint!("{:w$} {}  ", "", "|".blue(), w = width);
    eprintln!("{}", format!("{:_>w$}^", "", w = start).color(color));
    // print the middle lines to stderr
    let mid_lines = (self.end.line - self.start.line) as usize - 1;
    if mid_lines <= 4 {
      for i in 0..mid_lines {
        let line = get_line_str!(lines.next());
        eprint!("{} ", format!("{:w$}", line_num + i + 1, w = width).blue());
        eprintln!("{} {} {}", "|".blue(), "|".color(color), line);
      }
    } else {
      for i in 0..2usize {
        let line = get_line_str!(lines.next());
        eprint!("{} ", format!("{:w$}", line_num + i + 1, w = width).blue());
        eprintln!("{} {} {}", "|".blue(), "|".color(color), line);
      }
      eprint!("{:.>w$} {} {}", "", "|".blue(), "|".color(color), w = width);
      let line = get_line_str!(lines.nth(mid_lines - 3));
      eprint!("{} ", format!("{:w$}", self.end.line - 1, w = width).blue());
      eprintln!("{} {} {}", "|".blue(), "|".color(color), line);
    }
    // print the last line to stderr
    let line_num = self.end.line as usize;
    let (line, end) = get_line_str!(lines.next(), self.end.col);
    eprint!("{} ", format!("{:w$}", line_num, w = width).blue());
    eprintln!("{} {} {}", "|".blue(), "|".color(color), line);
    eprint!("{:w$} {} {}", "", "|".blue(), "|".color(color), w = width);
    eprintln!("{}", format!("{:_>w$}^", "", w = end).color(color));
  }
}

impl Default for Span {
  /// Creates a span with the default source code locations.
  fn default() -> Self {
    Self::new(Pos::default())
  }
}

impl fmt::Debug for Span {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    write!(f, "{}-{}", self.start, self.end)
  }
}

/// A line-column mark.
#[derive(Clone, Copy)]
pub struct Pos {
  line: u32,
  col: u32,
}

impl Pos {
  /// Creates a new mark.
  pub fn new() -> Self {
    Self { line: 1, col: 0 }
  }

  /// Updates the line number ans column number based on the given character.
  pub fn update(&mut self, c: char) {
    match c {
      '\n' => {
        self.col = 0;
        self.line += 1;
      }
      _ => self.col += 1,
    }
  }
}

impl Default for Pos {
  fn default() -> Self {
    Self::new()
  }
}

impl fmt::Display for Pos {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    write!(f, "{}:{}", self.line, self.col)
  }
}

/// Global state for `Span`.
struct GlobalState {
  file: FileType,
  err_num: usize,
  warn_num: usize,
}

/// Type of input file.
pub enum FileType {
  /// File with a path.
  File(PathBuf),
  /// Standard input file.
  Stdin,
  /// A buffer in the memory.
  Buffer,
}

impl fmt::Display for FileType {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    match self {
      FileType::File(path) => write!(f, "{}", path.display()),
      FileType::Stdin => f.write_str("<stdin>"),
      FileType::Buffer => f.write_str("<buffer>"),
    }
  }
}

/// Logs normal error with no span provided.
#[macro_export]
macro_rules! log_raw_error {
  ($($arg:tt)+) => {
    Span::log_raw_error(format_args!($($arg)+))
  };
}

/// Logs fatal error with no span provided.
#[macro_export]
macro_rules! log_raw_fatal_error {
  ($($arg:tt)+) => {
    Span::log_raw_fatal_error(format_args!($($arg)+))
  };
}

/// Logs warning with no span provided.
#[macro_export]
macro_rules! log_raw_warning {
  ($($arg:tt)+) => {
    Span::log_raw_warning(format_args!($($arg)+))
  };
}

/// Logs normal error message.
#[macro_export]
macro_rules! log_error {
  ($span:expr, $($arg:tt)+) => {
    $span.log_error(format_args!($($arg)+))
  };
}

/// Logs fatal error message.
#[macro_export]
macro_rules! log_fatal_error {
  ($span:expr, $($arg:tt)+) => {
    $span.log_fatal_error(format_args!($($arg)+))
  };
}

/// Logs warning message.
#[macro_export]
macro_rules! log_warning {
  ($span:expr, $($arg:tt)+) => {
    $span.log_warning(format_args!($($arg)+))
  };
}

/// Logs error message and returns a result.
#[macro_export]
macro_rules! return_error {
  ($span:expr, $($arg:tt)+) => {
    return $span.log_error(format_args!($($arg)+)).into()
  };
}

#[cfg(test)]
mod test {
  use super::*;

  #[test]
  fn pos_update() {
    let mut pos = Pos::new();
    assert_eq!(format!("{}", pos), "1:0");
    pos.update(' ');
    pos.update(' ');
    assert_eq!(format!("{}", pos), "1:2");
    pos.update('\n');
    assert_eq!(format!("{}", pos), "2:0");
    pos.update('\n');
    pos.update('\n');
    assert_eq!(format!("{}", pos), "4:0");
  }

  #[test]
  fn span_update() {
    let mut pos = Pos::new();
    pos.update(' ');
    let sp1 = Span::new(pos);
    pos.update(' ');
    pos.update(' ');
    let sp2 = sp1.into_updated(pos);
    assert!(sp1.is_in_same_line_as(&sp2));
    log_error!(sp2, "test error");
    log_warning!(sp2, "test warning");
    log_warning!(sp2, "test warning 2");
    Span::log_global();
    assert_eq!(format!("{}", sp2.start), "1:1");
    assert_eq!(format!("{}", sp2.end), "1:3");
    let mut sp = Span::new(Pos { line: 10, col: 10 });
    sp.update(Pos { line: 10, col: 15 });
    assert!(!sp2.is_in_same_line_as(&sp));
    let sp3 = sp2.into_updated_span(sp);
    assert!(sp2.is_in_same_line_as(&sp3));
    assert_eq!(format!("{}", sp3.start), "1:1");
    assert_eq!(format!("{}", sp3.end), "10:15");
  }
}