]> git.lizzy.rs Git - rust.git/blob - src/libgreen/macros.rs
Convert most code to new inner attribute syntax.
[rust.git] / src / libgreen / macros.rs
1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // FIXME: this file probably shouldn't exist
12
13 #![macro_escape]
14
15 use std::fmt;
16
17 // Indicates whether we should perform expensive sanity checks, including rtassert!
18 // FIXME: Once the runtime matures remove the `true` below to turn off rtassert, etc.
19 pub static ENFORCE_SANITY: bool = true || !cfg!(rtopt) || cfg!(rtdebug) || cfg!(rtassert);
20
21 macro_rules! rterrln (
22     ($($arg:tt)*) => ( {
23         format_args!(::macros::dumb_println, $($arg)*)
24     } )
25 )
26
27 // Some basic logging. Enabled by passing `--cfg rtdebug` to the libstd build.
28 macro_rules! rtdebug (
29     ($($arg:tt)*) => ( {
30         if cfg!(rtdebug) {
31             rterrln!($($arg)*)
32         }
33     })
34 )
35
36 macro_rules! rtassert (
37     ( $arg:expr ) => ( {
38         if ::macros::ENFORCE_SANITY {
39             if !$arg {
40                 rtabort!(" assertion failed: {}", stringify!($arg));
41             }
42         }
43     } )
44 )
45
46
47 macro_rules! rtabort (
48     ($($arg:tt)*) => ( {
49         ::macros::abort(format!($($arg)*));
50     } )
51 )
52
53 pub fn dumb_println(args: &fmt::Arguments) {
54     use std::io;
55     use std::libc;
56
57     struct Stderr;
58     impl io::Writer for Stderr {
59         fn write(&mut self, data: &[u8]) -> io::IoResult<()> {
60             unsafe {
61                 libc::write(libc::STDERR_FILENO,
62                             data.as_ptr() as *libc::c_void,
63                             data.len() as libc::size_t);
64             }
65             Ok(()) // just ignore the result
66         }
67     }
68     let mut w = Stderr;
69     let _ = fmt::writeln(&mut w as &mut io::Writer, args);
70 }
71
72 pub fn abort(msg: &str) -> ! {
73     let msg = if !msg.is_empty() { msg } else { "aborted" };
74     let hash = msg.chars().fold(0, |accum, val| accum + (val as uint) );
75     let quote = match hash % 10 {
76         0 => "
77 It was from the artists and poets that the pertinent answers came, and I
78 know that panic would have broken loose had they been able to compare notes.
79 As it was, lacking their original letters, I half suspected the compiler of
80 having asked leading questions, or of having edited the correspondence in
81 corroboration of what he had latently resolved to see.",
82         1 => "
83 There are not many persons who know what wonders are opened to them in the
84 stories and visions of their youth; for when as children we listen and dream,
85 we think but half-formed thoughts, and when as men we try to remember, we are
86 dulled and prosaic with the poison of life. But some of us awake in the night
87 with strange phantasms of enchanted hills and gardens, of fountains that sing
88 in the sun, of golden cliffs overhanging murmuring seas, of plains that stretch
89 down to sleeping cities of bronze and stone, and of shadowy companies of heroes
90 that ride caparisoned white horses along the edges of thick forests; and then
91 we know that we have looked back through the ivory gates into that world of
92 wonder which was ours before we were wise and unhappy.",
93         2 => "
94 Instead of the poems I had hoped for, there came only a shuddering blackness
95 and ineffable loneliness; and I saw at last a fearful truth which no one had
96 ever dared to breathe before — the unwhisperable secret of secrets — The fact
97 that this city of stone and stridor is not a sentient perpetuation of Old New
98 York as London is of Old London and Paris of Old Paris, but that it is in fact
99 quite dead, its sprawling body imperfectly embalmed and infested with queer
100 animate things which have nothing to do with it as it was in life.",
101         3 => "
102 The ocean ate the last of the land and poured into the smoking gulf, thereby
103 giving up all it had ever conquered. From the new-flooded lands it flowed
104 again, uncovering death and decay; and from its ancient and immemorial bed it
105 trickled loathsomely, uncovering nighted secrets of the years when Time was
106 young and the gods unborn. Above the waves rose weedy remembered spires. The
107 moon laid pale lilies of light on dead London, and Paris stood up from its damp
108 grave to be sanctified with star-dust. Then rose spires and monoliths that were
109 weedy but not remembered; terrible spires and monoliths of lands that men never
110 knew were lands...",
111         4 => "
112 There was a night when winds from unknown spaces whirled us irresistibly into
113 limitless vacuum beyond all thought and entity. Perceptions of the most
114 maddeningly untransmissible sort thronged upon us; perceptions of infinity
115 which at the time convulsed us with joy, yet which are now partly lost to my
116 memory and partly incapable of presentation to others.",
117         _ => "You've met with a terrible fate, haven't you?"
118     };
119     rterrln!("{}", "");
120     rterrln!("{}", quote);
121     rterrln!("{}", "");
122     rterrln!("fatal runtime error: {}", msg);
123
124     abort();
125
126     fn abort() -> ! {
127         use std::intrinsics;
128         unsafe { intrinsics::abort() }
129     }
130 }