-
Notifications
You must be signed in to change notification settings - Fork 12.2k
/
type_of.rs
442 lines (387 loc) · 15.2 KB
/
type_of.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
// Copyright 2012-2013 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.
#![allow(non_camel_case_types)]
use middle::subst;
use middle::trans::adt;
use middle::trans::common::*;
use middle::trans::foreign;
use middle::trans::machine;
use middle::ty;
use util::ppaux;
use util::ppaux::Repr;
use middle::trans::type_::Type;
use syntax::abi;
use syntax::ast;
pub fn arg_is_indirect(ccx: &CrateContext, arg_ty: ty::t) -> bool {
!type_is_immediate(ccx, arg_ty)
}
pub fn return_uses_outptr(ccx: &CrateContext, ty: ty::t) -> bool {
!type_is_immediate(ccx, ty)
}
pub fn type_of_explicit_arg(ccx: &CrateContext, arg_ty: ty::t) -> Type {
let llty = arg_type_of(ccx, arg_ty);
if arg_is_indirect(ccx, arg_ty) {
llty.ptr_to()
} else {
llty
}
}
/// Yields the types of the "real" arguments for this function. For most
/// functions, these are simply the types of the arguments. For functions with
/// the `RustCall` ABI, however, this untuples the arguments of the function.
fn untuple_arguments_if_necessary(ccx: &CrateContext,
inputs: &[ty::t],
abi: abi::Abi)
-> Vec<ty::t> {
if abi != abi::RustCall {
return inputs.iter().map(|x| (*x).clone()).collect()
}
if inputs.len() == 0 {
return Vec::new()
}
let mut result = Vec::new();
for (i, &arg_prior_to_tuple) in inputs.iter().enumerate() {
if i < inputs.len() - 1 {
result.push(arg_prior_to_tuple);
}
}
match ty::get(inputs[inputs.len() - 1]).sty {
ty::ty_tup(ref tupled_arguments) => {
debug!("untuple_arguments_if_necessary(): untupling arguments");
for &tupled_argument in tupled_arguments.iter() {
result.push(tupled_argument);
}
}
ty::ty_nil => {}
_ => {
ccx.tcx().sess.bug("argument to function with \"rust-call\" ABI \
is neither a tuple nor unit")
}
}
result
}
pub fn type_of_rust_fn(cx: &CrateContext,
llenvironment_type: Option<Type>,
inputs: &[ty::t],
output: ty::t,
abi: abi::Abi)
-> Type {
let mut atys: Vec<Type> = Vec::new();
// First, munge the inputs, if this has the `rust-call` ABI.
let inputs = untuple_arguments_if_necessary(cx, inputs, abi);
// Arg 0: Output pointer.
// (if the output type is non-immediate)
let use_out_pointer = return_uses_outptr(cx, output);
let lloutputtype = arg_type_of(cx, output);
if use_out_pointer {
atys.push(lloutputtype.ptr_to());
}
// Arg 1: Environment
match llenvironment_type {
None => {}
Some(llenvironment_type) => atys.push(llenvironment_type),
}
// ... then explicit args.
let input_tys = inputs.iter().map(|&arg_ty| type_of_explicit_arg(cx, arg_ty));
atys.extend(input_tys);
// Use the output as the actual return value if it's immediate.
if use_out_pointer || return_type_is_void(cx, output) {
Type::func(atys.as_slice(), &Type::void(cx))
} else {
Type::func(atys.as_slice(), &lloutputtype)
}
}
// Given a function type and a count of ty params, construct an llvm type
pub fn type_of_fn_from_ty(cx: &CrateContext, fty: ty::t) -> Type {
match ty::get(fty).sty {
ty::ty_closure(ref f) => {
type_of_rust_fn(cx,
Some(Type::i8p(cx)),
f.sig.inputs.as_slice(),
f.sig.output,
f.abi)
}
ty::ty_bare_fn(ref f) => {
if f.abi == abi::Rust || f.abi == abi::RustCall {
type_of_rust_fn(cx,
None,
f.sig.inputs.as_slice(),
f.sig.output,
f.abi)
} else {
foreign::lltype_for_foreign_fn(cx, fty)
}
}
_ => {
cx.sess().bug("type_of_fn_from_ty given non-closure, non-bare-fn")
}
}
}
// A "sizing type" is an LLVM type, the size and alignment of which are
// guaranteed to be equivalent to what you would get out of `type_of()`. It's
// useful because:
//
// (1) It may be cheaper to compute the sizing type than the full type if all
// you're interested in is the size and/or alignment;
//
// (2) It won't make any recursive calls to determine the structure of the
// type behind pointers. This can help prevent infinite loops for
// recursive types. For example, enum types rely on this behavior.
pub fn sizing_type_of(cx: &CrateContext, t: ty::t) -> Type {
match cx.llsizingtypes.borrow().find_copy(&t) {
Some(t) => return t,
None => ()
}
let llsizingty = match ty::get(t).sty {
_ if !ty::lltype_is_sized(cx.tcx(), t) => {
cx.sess().bug(format!("trying to take the sizing type of {}, an unsized type",
ppaux::ty_to_string(cx.tcx(), t)).as_slice())
}
ty::ty_nil | ty::ty_bot => Type::nil(cx),
ty::ty_bool => Type::bool(cx),
ty::ty_char => Type::char(cx),
ty::ty_int(t) => Type::int_from_ty(cx, t),
ty::ty_uint(t) => Type::uint_from_ty(cx, t),
ty::ty_float(t) => Type::float_from_ty(cx, t),
ty::ty_box(..) => Type::i8p(cx),
ty::ty_uniq(ty) | ty::ty_rptr(_, ty::mt{ty, ..}) | ty::ty_ptr(ty::mt{ty, ..}) => {
if ty::type_is_sized(cx.tcx(), ty) {
Type::i8p(cx)
} else {
Type::struct_(cx, [Type::i8p(cx), Type::i8p(cx)], false)
}
}
ty::ty_bare_fn(..) => Type::i8p(cx),
ty::ty_closure(..) => Type::struct_(cx, [Type::i8p(cx), Type::i8p(cx)], false),
ty::ty_vec(ty, Some(size)) => {
Type::array(&sizing_type_of(cx, ty), size as u64)
}
ty::ty_tup(..) | ty::ty_enum(..) | ty::ty_unboxed_closure(..) => {
let repr = adt::represent_type(cx, t);
adt::sizing_type_of(cx, &*repr, false)
}
ty::ty_struct(..) => {
if ty::type_is_simd(cx.tcx(), t) {
let et = ty::simd_type(cx.tcx(), t);
let n = ty::simd_size(cx.tcx(), t);
Type::vector(&type_of(cx, et), n as u64)
} else {
let repr = adt::represent_type(cx, t);
adt::sizing_type_of(cx, &*repr, false)
}
}
ty::ty_open(_) => {
Type::struct_(cx, [Type::i8p(cx), Type::i8p(cx)], false)
}
ty::ty_infer(..) | ty::ty_param(..) | ty::ty_err(..) => {
cx.sess().bug(format!("fictitious type {} in sizing_type_of()",
ppaux::ty_to_string(cx.tcx(), t)).as_slice())
}
ty::ty_vec(_, None) | ty::ty_trait(..) | ty::ty_str => fail!("unreachable")
};
cx.llsizingtypes.borrow_mut().insert(t, llsizingty);
llsizingty
}
pub fn arg_type_of(cx: &CrateContext, t: ty::t) -> Type {
if ty::type_is_bool(t) {
Type::i1(cx)
} else {
type_of(cx, t)
}
}
// NB: If you update this, be sure to update `sizing_type_of()` as well.
pub fn type_of(cx: &CrateContext, t: ty::t) -> Type {
fn type_of_unsize_info(cx: &CrateContext, t: ty::t) -> Type {
// It is possible to end up here with a sized type. This happens with a
// struct which might be unsized, but is monomorphised to a sized type.
// In this case we'll fake a fat pointer with no unsize info (we use 0).
// However, its still a fat pointer, so we need some type use.
if ty::type_is_sized(cx.tcx(), t) {
return Type::i8p(cx);
}
match ty::get(ty::unsized_part_of_type(cx.tcx(), t)).sty {
ty::ty_str | ty::ty_vec(..) => Type::uint_from_ty(cx, ast::TyU),
ty::ty_trait(_) => Type::vtable_ptr(cx),
_ => fail!("Unexpected type returned from unsized_part_of_type : {}",
t.repr(cx.tcx()))
}
}
// Check the cache.
match cx.lltypes.borrow().find(&t) {
Some(&llty) => return llty,
None => ()
}
debug!("type_of {} {:?}", t.repr(cx.tcx()), ty::get(t).sty);
// Replace any typedef'd types with their equivalent non-typedef
// type. This ensures that all LLVM nominal types that contain
// Rust types are defined as the same LLVM types. If we don't do
// this then, e.g. `Option<{myfield: bool}>` would be a different
// type than `Option<myrec>`.
let t_norm = ty::normalize_ty(cx.tcx(), t);
if t != t_norm {
let llty = type_of(cx, t_norm);
debug!("--> normalized {} {:?} to {} {:?} llty={}",
t.repr(cx.tcx()),
t,
t_norm.repr(cx.tcx()),
t_norm,
cx.tn.type_to_string(llty));
cx.lltypes.borrow_mut().insert(t, llty);
return llty;
}
let mut llty = match ty::get(t).sty {
ty::ty_nil | ty::ty_bot => Type::nil(cx),
ty::ty_bool => Type::bool(cx),
ty::ty_char => Type::char(cx),
ty::ty_int(t) => Type::int_from_ty(cx, t),
ty::ty_uint(t) => Type::uint_from_ty(cx, t),
ty::ty_float(t) => Type::float_from_ty(cx, t),
ty::ty_enum(did, ref substs) => {
// Only create the named struct, but don't fill it in. We
// fill it in *after* placing it into the type cache. This
// avoids creating more than one copy of the enum when one
// of the enum's variants refers to the enum itself.
let repr = adt::represent_type(cx, t);
let tps = substs.types.get_slice(subst::TypeSpace);
let name = llvm_type_name(cx, an_enum, did, tps);
adt::incomplete_type_of(cx, &*repr, name.as_slice())
}
ty::ty_unboxed_closure(did, _) => {
// Only create the named struct, but don't fill it in. We
// fill it in *after* placing it into the type cache.
let repr = adt::represent_type(cx, t);
let name = llvm_type_name(cx, an_unboxed_closure, did, []);
adt::incomplete_type_of(cx, &*repr, name.as_slice())
}
ty::ty_box(typ) => {
Type::at_box(cx, type_of(cx, typ)).ptr_to()
}
ty::ty_uniq(ty) | ty::ty_rptr(_, ty::mt{ty, ..}) | ty::ty_ptr(ty::mt{ty, ..}) => {
match ty::get(ty).sty {
ty::ty_str => {
// This means we get a nicer name in the output (str is always
// unsized).
cx.tn.find_type("str_slice").unwrap()
}
ty::ty_trait(..) => Type::opaque_trait(cx),
_ if !ty::type_is_sized(cx.tcx(), ty) => {
let p_ty = type_of(cx, ty).ptr_to();
Type::struct_(cx, [p_ty, type_of_unsize_info(cx, ty)], false)
}
_ => type_of(cx, ty).ptr_to(),
}
}
ty::ty_vec(ty, Some(n)) => {
Type::array(&type_of(cx, ty), n as u64)
}
ty::ty_vec(ty, None) => {
type_of(cx, ty)
}
ty::ty_trait(..) => {
Type::opaque_trait_data(cx)
}
ty::ty_str => Type::i8(cx),
ty::ty_bare_fn(_) => {
type_of_fn_from_ty(cx, t).ptr_to()
}
ty::ty_closure(_) => {
let fn_ty = type_of_fn_from_ty(cx, t).ptr_to();
Type::struct_(cx, [fn_ty, Type::i8p(cx)], false)
}
ty::ty_tup(..) => {
let repr = adt::represent_type(cx, t);
adt::type_of(cx, &*repr)
}
ty::ty_struct(did, ref substs) => {
if ty::type_is_simd(cx.tcx(), t) {
let et = ty::simd_type(cx.tcx(), t);
let n = ty::simd_size(cx.tcx(), t);
Type::vector(&type_of(cx, et), n as u64)
} else {
// Only create the named struct, but don't fill it in. We fill it
// in *after* placing it into the type cache. This prevents
// infinite recursion with recursive struct types.
let repr = adt::represent_type(cx, t);
let tps = substs.types.get_slice(subst::TypeSpace);
let name = llvm_type_name(cx, a_struct, did, tps);
adt::incomplete_type_of(cx, &*repr, name.as_slice())
}
}
ty::ty_open(t) => match ty::get(t).sty {
ty::ty_struct(..) => {
let p_ty = type_of(cx, t).ptr_to();
Type::struct_(cx, [p_ty, type_of_unsize_info(cx, t)], false)
}
ty::ty_vec(ty, None) => {
let p_ty = type_of(cx, ty).ptr_to();
Type::struct_(cx, [p_ty, type_of_unsize_info(cx, t)], false)
}
ty::ty_str => {
let p_ty = Type::i8p(cx);
Type::struct_(cx, [p_ty, type_of_unsize_info(cx, t)], false)
}
ty::ty_trait(..) => Type::opaque_trait(cx),
_ => cx.sess().bug(format!("ty_open with sized type: {}",
ppaux::ty_to_string(cx.tcx(), t)).as_slice())
},
ty::ty_infer(..) => cx.sess().bug("type_of with ty_infer"),
ty::ty_param(..) => cx.sess().bug("type_of with ty_param"),
ty::ty_err(..) => cx.sess().bug("type_of with ty_err"),
};
debug!("--> mapped t={} {:?} to llty={}",
t.repr(cx.tcx()),
t,
cx.tn.type_to_string(llty));
cx.lltypes.borrow_mut().insert(t, llty);
// If this was an enum or struct, fill in the type now.
match ty::get(t).sty {
ty::ty_enum(..) | ty::ty_struct(..) | ty::ty_unboxed_closure(..)
if !ty::type_is_simd(cx.tcx(), t) => {
let repr = adt::represent_type(cx, t);
adt::finish_type_of(cx, &*repr, &mut llty);
}
_ => ()
}
return llty;
}
pub fn align_of(cx: &CrateContext, t: ty::t) -> u64 {
let llty = sizing_type_of(cx, t);
machine::llalign_of_min(cx, llty)
}
// Want refinements! (Or case classes, I guess
pub enum named_ty {
a_struct,
an_enum,
an_unboxed_closure,
}
pub fn llvm_type_name(cx: &CrateContext,
what: named_ty,
did: ast::DefId,
tps: &[ty::t])
-> String
{
let name = match what {
a_struct => "struct",
an_enum => "enum",
an_unboxed_closure => return "closure".to_string(),
};
let base = ty::item_path_str(cx.tcx(), did);
let strings: Vec<String> = tps.iter().map(|t| t.repr(cx.tcx())).collect();
let tstr = format!("{}<{}>", base, strings);
if did.krate == 0 {
format!("{}.{}", name, tstr)
} else {
format!("{}.{}[{}{}]", name, tstr, "#", did.krate)
}
}
pub fn type_of_dtor(ccx: &CrateContext, self_ty: ty::t) -> Type {
let self_ty = type_of(ccx, self_ty).ptr_to();
Type::func([self_ty], &Type::void(ccx))
}