/
session.rs
432 lines (395 loc) · 13.9 KB
/
session.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
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use core::prelude::*;
use back::link;
use back::target_strs;
use back;
use driver;
use driver::session;
use metadata::filesearch;
use metadata;
use middle::lint;
use core::cmp;
use core::option;
use syntax::ast::node_id;
use syntax::ast::{int_ty, uint_ty, float_ty};
use syntax::codemap::span;
use syntax::diagnostic;
use syntax::parse::parse_sess;
use syntax::{ast, codemap};
use syntax;
pub enum os { os_win32, os_macos, os_linux, os_android, os_freebsd, }
pub impl os : cmp::Eq {
pure fn eq(&self, other: &os) -> bool {
((*self) as uint) == ((*other) as uint)
}
pure fn ne(&self, other: &os) -> bool { !(*self).eq(other) }
}
pub enum arch { arch_x86, arch_x86_64, arch_arm, }
pub impl arch : cmp::Eq {
pure fn eq(&self, other: &arch) -> bool {
((*self) as uint) == ((*other) as uint)
}
pure fn ne(&self, other: &arch) -> bool { !(*self).eq(other) }
}
pub enum crate_type { bin_crate, lib_crate, unknown_crate, }
pub type config =
{os: os,
arch: arch,
target_strs: target_strs::t,
int_type: int_ty,
uint_type: uint_ty,
float_type: float_ty};
pub const verbose: uint = 1 << 0;
pub const time_passes: uint = 1 << 1;
pub const count_llvm_insns: uint = 1 << 2;
pub const time_llvm_passes: uint = 1 << 3;
pub const trans_stats: uint = 1 << 4;
pub const no_asm_comments: uint = 1 << 5;
pub const no_verify: uint = 1 << 6;
pub const trace: uint = 1 << 7;
pub const coherence: uint = 1 << 8;
pub const borrowck_stats: uint = 1 << 9;
pub const borrowck_note_pure: uint = 1 << 10;
pub const borrowck_note_loan: uint = 1 << 11;
pub const no_landing_pads: uint = 1 << 12;
pub const debug_llvm: uint = 1 << 13;
pub const count_type_sizes: uint = 1 << 14;
pub const meta_stats: uint = 1 << 15;
pub const no_opt: uint = 1 << 16;
pub const no_monomorphic_collapse: uint = 1 << 17;
const gc: uint = 1 << 18;
const jit: uint = 1 << 19;
const debug_info: uint = 1 << 20;
const extra_debug_info: uint = 1 << 21;
const static: uint = 1 << 22;
pub fn debugging_opts_map() -> ~[(~str, ~str, uint)] {
~[(~"verbose", ~"in general, enable more debug printouts", verbose),
(~"time-passes", ~"measure time of each rustc pass", time_passes),
(~"count-llvm-insns", ~"count where LLVM \
instrs originate", count_llvm_insns),
(~"time-llvm-passes", ~"measure time of each LLVM pass",
time_llvm_passes),
(~"trans-stats", ~"gather trans statistics", trans_stats),
(~"no-asm-comments", ~"omit comments when using -S", no_asm_comments),
(~"no-verify", ~"skip LLVM verification", no_verify),
(~"trace", ~"emit trace logs", trace),
(~"coherence", ~"perform coherence checking", coherence),
(~"borrowck-stats", ~"gather borrowck statistics", borrowck_stats),
(~"borrowck-note-pure", ~"note where purity is req'd",
borrowck_note_pure),
(~"borrowck-note-loan", ~"note where loans are req'd",
borrowck_note_loan),
(~"no-landing-pads", ~"omit landing pads for unwinding",
no_landing_pads),
(~"debug-llvm", ~"enable debug output from LLVM", debug_llvm),
(~"count-type-sizes", ~"count the sizes of aggregate types",
count_type_sizes),
(~"meta-stats", ~"gather metadata statistics", meta_stats),
(~"no-opt", ~"do not optimize, even if -O is passed", no_opt),
(~"no-monomorphic-collapse", ~"do not collapse template instantiations",
no_monomorphic_collapse),
(~"gc", ~"Garbage collect shared data (experimental)", gc),
(~"jit", ~"Execute using JIT (experimental)", jit),
(~"extra-debug-info", ~"Extra debugging info (experimental)",
extra_debug_info),
(~"debug-info", ~"Produce debug info (experimental)", debug_info),
(~"static", ~"Use or produce static libraries or binaries " +
"(experimental)", static)
]
}
pub enum OptLevel {
No, // -O0
Less, // -O1
Default, // -O2
Aggressive // -O3
}
pub impl OptLevel : cmp::Eq {
pure fn eq(&self, other: &OptLevel) -> bool {
((*self) as uint) == ((*other) as uint)
}
pure fn ne(&self, other: &OptLevel) -> bool { !(*self).eq(other) }
}
pub type options =
// The crate config requested for the session, which may be combined
// with additional crate configurations during the compile process
{crate_type: crate_type,
static: bool,
gc: bool,
optimize: OptLevel,
debuginfo: bool,
extra_debuginfo: bool,
lint_opts: ~[(lint::lint, lint::level)],
save_temps: bool,
jit: bool,
output_type: back::link::output_type,
addl_lib_search_paths: ~[Path],
maybe_sysroot: Option<Path>,
target_triple: ~str,
// User-specified cfg meta items. The compiler itself will add additional
// items to the crate config, and during parsing the entire crate config
// will be added to the crate AST node. This should not be used for
// anything except building the full crate config prior to parsing.
cfg: ast::crate_cfg,
binary: ~str,
test: bool,
parse_only: bool,
no_trans: bool,
debugging_opts: uint,
};
pub type crate_metadata = {name: ~str, data: ~[u8]};
pub type Session_ = {targ_cfg: @config,
opts: @options,
cstore: metadata::cstore::CStore,
parse_sess: parse_sess,
codemap: @codemap::CodeMap,
// For a library crate, this is always none
mut main_fn: Option<(node_id, codemap::span)>,
span_diagnostic: diagnostic::span_handler,
filesearch: filesearch::FileSearch,
mut building_library: bool,
working_dir: Path,
lint_settings: lint::lint_settings};
pub enum Session {
Session_(@Session_)
}
pub impl Session {
fn span_fatal(sp: span, msg: ~str) -> ! {
self.span_diagnostic.span_fatal(sp, msg)
}
fn fatal(msg: ~str) -> ! {
self.span_diagnostic.handler().fatal(msg)
}
fn span_err(sp: span, msg: ~str) {
self.span_diagnostic.span_err(sp, msg)
}
fn err(msg: ~str) {
self.span_diagnostic.handler().err(msg)
}
fn has_errors() -> bool {
self.span_diagnostic.handler().has_errors()
}
fn abort_if_errors() {
self.span_diagnostic.handler().abort_if_errors()
}
fn span_warn(sp: span, msg: ~str) {
self.span_diagnostic.span_warn(sp, msg)
}
fn warn(msg: ~str) {
self.span_diagnostic.handler().warn(msg)
}
fn span_note(sp: span, msg: ~str) {
self.span_diagnostic.span_note(sp, msg)
}
fn note(msg: ~str) {
self.span_diagnostic.handler().note(msg)
}
fn span_bug(sp: span, msg: ~str) -> ! {
self.span_diagnostic.span_bug(sp, msg)
}
fn bug(msg: ~str) -> ! {
self.span_diagnostic.handler().bug(msg)
}
fn span_unimpl(sp: span, msg: ~str) -> ! {
self.span_diagnostic.span_unimpl(sp, msg)
}
fn unimpl(msg: ~str) -> ! {
self.span_diagnostic.handler().unimpl(msg)
}
fn span_lint_level(level: lint::level, sp: span, +msg: ~str) {
match level {
lint::allow => { },
lint::warn => self.span_warn(sp, msg),
lint::deny | lint::forbid => {
self.span_err(sp, msg);
}
}
}
fn span_lint(lint_mode: lint::lint,
expr_id: ast::node_id,
item_id: ast::node_id,
span: span,
+msg: ~str) {
let level = lint::get_lint_settings_level(
self.lint_settings, lint_mode, expr_id, item_id);
self.span_lint_level(level, span, msg);
}
fn next_node_id() -> ast::node_id {
return syntax::parse::next_node_id(self.parse_sess);
}
fn diagnostic() -> diagnostic::span_handler {
self.span_diagnostic
}
fn debugging_opt(opt: uint) -> bool {
(self.opts.debugging_opts & opt) != 0u
}
// This exists to help with refactoring to eliminate impossible
// cases later on
fn impossible_case(sp: span, msg: &str) -> ! {
self.span_bug(sp, fmt!("Impossible case reached: %s", msg));
}
fn verbose() -> bool { self.debugging_opt(verbose) }
fn time_passes() -> bool { self.debugging_opt(time_passes) }
fn count_llvm_insns() -> bool { self.debugging_opt(count_llvm_insns) }
fn count_type_sizes() -> bool { self.debugging_opt(count_type_sizes) }
fn time_llvm_passes() -> bool { self.debugging_opt(time_llvm_passes) }
fn trans_stats() -> bool { self.debugging_opt(trans_stats) }
fn meta_stats() -> bool { self.debugging_opt(meta_stats) }
fn no_asm_comments() -> bool { self.debugging_opt(no_asm_comments) }
fn no_verify() -> bool { self.debugging_opt(no_verify) }
fn trace() -> bool { self.debugging_opt(trace) }
fn coherence() -> bool { self.debugging_opt(coherence) }
fn borrowck_stats() -> bool { self.debugging_opt(borrowck_stats) }
fn borrowck_note_pure() -> bool { self.debugging_opt(borrowck_note_pure) }
fn borrowck_note_loan() -> bool { self.debugging_opt(borrowck_note_loan) }
fn no_monomorphic_collapse() -> bool {
self.debugging_opt(no_monomorphic_collapse)
}
fn str_of(id: ast::ident) -> ~str {
/*bad*/copy *self.parse_sess.interner.get(id)
}
fn ident_of(+st: ~str) -> ast::ident {
self.parse_sess.interner.intern(@st)
}
fn intr() -> @syntax::parse::token::ident_interner {
self.parse_sess.interner
}
}
/// Some reasonable defaults
pub fn basic_options() -> @options {
@{
crate_type: session::lib_crate,
static: false,
gc: false,
optimize: No,
debuginfo: false,
extra_debuginfo: false,
lint_opts: ~[],
save_temps: false,
jit: false,
output_type: link::output_type_exe,
addl_lib_search_paths: ~[],
maybe_sysroot: None,
target_triple: driver::host_triple(),
cfg: ~[],
binary: ~"rustc",
test: false,
parse_only: false,
no_trans: false,
debugging_opts: 0u
}
}
// Seems out of place, but it uses session, so I'm putting it here
pub fn expect<T: Copy>(sess: Session,
opt: Option<T>,
msg: fn() -> ~str)
-> T {
diagnostic::expect(sess.diagnostic(), opt, msg)
}
pub fn building_library(req_crate_type: crate_type, crate: @ast::crate,
testing: bool) -> bool {
match req_crate_type {
bin_crate => false,
lib_crate => true,
unknown_crate => {
if testing {
false
} else {
match syntax::attr::first_attr_value_str_by_name(
crate.node.attrs,
~"crate_type") {
option::Some(~"lib") => true,
_ => false
}
}
}
}
}
pub fn sess_os_to_meta_os(os: os) -> metadata::loader::os {
use metadata::loader;
match os {
os_win32 => loader::os_win32,
os_linux => loader::os_linux,
os_android => loader::os_android,
os_macos => loader::os_macos,
os_freebsd => loader::os_freebsd
}
}
#[cfg(test)]
pub mod test {
use core::prelude::*;
use driver::session::{bin_crate, building_library, lib_crate};
use driver::session::{unknown_crate};
use syntax::ast;
use syntax::ast_util;
use syntax::codemap;
pub fn make_crate_type_attr(+t: ~str) -> ast::attribute {
codemap::respan(codemap::dummy_sp(), ast::attribute_ {
style: ast::attr_outer,
value: codemap::respan(codemap::dummy_sp(),
ast::meta_name_value(
~"crate_type",
codemap::respan(codemap::dummy_sp(),
ast::lit_str(@t)))),
is_sugared_doc: false
})
}
pub fn make_crate(with_bin: bool, with_lib: bool) -> @ast::crate {
let mut attrs = ~[];
if with_bin { attrs += ~[make_crate_type_attr(~"bin")]; }
if with_lib { attrs += ~[make_crate_type_attr(~"lib")]; }
@codemap::respan(codemap::dummy_sp(), ast::crate_ {
module: ast::_mod { view_items: ~[], items: ~[] },
attrs: attrs,
config: ~[]
})
}
#[test]
pub fn bin_crate_type_attr_results_in_bin_output() {
let crate = make_crate(true, false);
assert !building_library(unknown_crate, crate, false);
}
#[test]
pub fn lib_crate_type_attr_results_in_lib_output() {
let crate = make_crate(false, true);
assert building_library(unknown_crate, crate, false);
}
#[test]
pub fn bin_option_overrides_lib_crate_type() {
let crate = make_crate(false, true);
assert !building_library(bin_crate, crate, false);
}
#[test]
pub fn lib_option_overrides_bin_crate_type() {
let crate = make_crate(true, false);
assert building_library(lib_crate, crate, false);
}
#[test]
pub fn bin_crate_type_is_default() {
let crate = make_crate(false, false);
assert !building_library(unknown_crate, crate, false);
}
#[test]
pub fn test_option_overrides_lib_crate_type() {
let crate = make_crate(false, true);
assert !building_library(unknown_crate, crate, true);
}
#[test]
pub fn test_option_does_not_override_requested_lib_type() {
let crate = make_crate(false, false);
assert building_library(lib_crate, crate, true);
}
}
// Local Variables:
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End: