]> git.lizzy.rs Git - rust.git/blob - example/mini_core.rs
[WIP] Implement dylib loading for the JIT
[rust.git] / example / mini_core.rs
1 #![feature(
2     no_core, lang_items, intrinsics, unboxed_closures, type_ascription, extern_types,
3     untagged_unions, decl_macro, rustc_attrs
4 )]
5 #![no_core]
6 #![allow(dead_code)]
7
8 #[lang = "sized"]
9 pub trait Sized {}
10
11 #[lang = "unsize"]
12 pub trait Unsize<T: ?Sized> {}
13
14 #[lang = "coerce_unsized"]
15 pub trait CoerceUnsized<T> {}
16
17 impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {}
18 impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a mut U> for &'a mut T {}
19 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *const T {}
20 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {}
21
22 #[lang = "dispatch_from_dyn"]
23 pub trait DispatchFromDyn<T> {}
24
25 // &T -> &U
26 impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<&'a U> for &'a T {}
27 // &mut T -> &mut U
28 impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<&'a mut U> for &'a mut T {}
29 // *const T -> *const U
30 impl<T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<*const U> for *const T {}
31 // *mut T -> *mut U
32 impl<T: ?Sized+Unsize<U>, U: ?Sized> DispatchFromDyn<*mut U> for *mut T {}
33 impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Box<U>> for Box<T> {}
34
35 #[lang = "receiver"]
36 pub trait Receiver {}
37
38 impl<T: ?Sized> Receiver for &T {}
39 impl<T: ?Sized> Receiver for &mut T {}
40 impl<T: ?Sized> Receiver for Box<T> {}
41
42 #[lang = "copy"]
43 pub unsafe trait Copy {}
44
45 unsafe impl Copy for bool {}
46 unsafe impl Copy for u8 {}
47 unsafe impl Copy for u16 {}
48 unsafe impl Copy for u32 {}
49 unsafe impl Copy for u64 {}
50 unsafe impl Copy for usize {}
51 unsafe impl Copy for i8 {}
52 unsafe impl Copy for i16 {}
53 unsafe impl Copy for i32 {}
54 unsafe impl Copy for isize {}
55 unsafe impl Copy for char {}
56 unsafe impl<'a, T: ?Sized> Copy for &'a T {}
57 unsafe impl<T: ?Sized> Copy for *const T {}
58 unsafe impl<T: ?Sized> Copy for *mut T {}
59
60 #[lang = "sync"]
61 pub unsafe trait Sync {}
62
63 unsafe impl Sync for bool {}
64 unsafe impl Sync for u8 {}
65 unsafe impl Sync for u16 {}
66 unsafe impl Sync for u32 {}
67 unsafe impl Sync for u64 {}
68 unsafe impl Sync for usize {}
69 unsafe impl Sync for i8 {}
70 unsafe impl Sync for i16 {}
71 unsafe impl Sync for i32 {}
72 unsafe impl Sync for isize {}
73 unsafe impl Sync for char {}
74 unsafe impl<'a, T: ?Sized> Sync for &'a T {}
75 unsafe impl Sync for [u8; 16] {}
76
77 #[lang = "freeze"]
78 trait Freeze {}
79
80 #[lang = "not"]
81 pub trait Not {
82     type Output;
83
84     fn not(self) -> Self::Output;
85 }
86
87 impl Not for bool {
88     type Output = bool;
89
90     fn not(self) -> bool {
91         !self
92     }
93 }
94
95 #[lang = "mul"]
96 pub trait Mul<RHS = Self> {
97     type Output;
98
99     #[must_use]
100     fn mul(self, rhs: RHS) -> Self::Output;
101 }
102
103 impl Mul for u8 {
104     type Output = Self;
105
106     fn mul(self, rhs: Self) -> Self::Output {
107         self * rhs
108     }
109 }
110
111 impl Mul for usize {
112     type Output = Self;
113
114     fn mul(self, rhs: Self) -> Self::Output {
115         self * rhs
116     }
117 }
118
119 #[lang = "add"]
120 pub trait Add<RHS = Self> {
121     type Output;
122
123     fn add(self, rhs: RHS) -> Self::Output;
124 }
125
126 impl Add for u8 {
127     type Output = Self;
128
129     fn add(self, rhs: Self) -> Self {
130         self + rhs
131     }
132 }
133
134 impl Add for i8 {
135     type Output = Self;
136
137     fn add(self, rhs: Self) -> Self {
138         self + rhs
139     }
140 }
141
142 impl Add for usize {
143     type Output = Self;
144
145     fn add(self, rhs: Self) -> Self {
146         self + rhs
147     }
148 }
149
150 #[lang = "sub"]
151 pub trait Sub<RHS = Self> {
152     type Output;
153
154     fn sub(self, rhs: RHS) -> Self::Output;
155 }
156
157 impl Sub for usize {
158     type Output = Self;
159
160     fn sub(self, rhs: Self) -> Self {
161         self - rhs
162     }
163 }
164
165 impl Sub for u8 {
166     type Output = Self;
167
168     fn sub(self, rhs: Self) -> Self {
169         self - rhs
170     }
171 }
172
173 impl Sub for i8 {
174     type Output = Self;
175
176     fn sub(self, rhs: Self) -> Self {
177         self - rhs
178     }
179 }
180
181 impl Sub for i16 {
182     type Output = Self;
183
184     fn sub(self, rhs: Self) -> Self {
185         self - rhs
186     }
187 }
188
189 #[lang = "rem"]
190 pub trait Rem<RHS = Self> {
191     type Output;
192
193     fn rem(self, rhs: RHS) -> Self::Output;
194 }
195
196 impl Rem for usize {
197     type Output = Self;
198
199     fn rem(self, rhs: Self) -> Self {
200         self % rhs
201     }
202 }
203
204 #[lang = "bitor"]
205 pub trait BitOr<RHS = Self> {
206     type Output;
207
208     #[must_use]
209     fn bitor(self, rhs: RHS) -> Self::Output;
210 }
211
212 impl BitOr for bool {
213     type Output = bool;
214
215     fn bitor(self, rhs: bool) -> bool {
216         self | rhs
217     }
218 }
219
220 impl<'a> BitOr<bool> for &'a bool {
221     type Output = bool;
222
223     fn bitor(self, rhs: bool) -> bool {
224         *self | rhs
225     }
226 }
227
228 #[lang = "eq"]
229 pub trait PartialEq<Rhs: ?Sized = Self> {
230     fn eq(&self, other: &Rhs) -> bool;
231     fn ne(&self, other: &Rhs) -> bool;
232 }
233
234 impl PartialEq for u8 {
235     fn eq(&self, other: &u8) -> bool {
236         (*self) == (*other)
237     }
238     fn ne(&self, other: &u8) -> bool {
239         (*self) != (*other)
240     }
241 }
242
243 impl PartialEq for u16 {
244     fn eq(&self, other: &u16) -> bool {
245         (*self) == (*other)
246     }
247     fn ne(&self, other: &u16) -> bool {
248         (*self) != (*other)
249     }
250 }
251
252 impl PartialEq for u32 {
253     fn eq(&self, other: &u32) -> bool {
254         (*self) == (*other)
255     }
256     fn ne(&self, other: &u32) -> bool {
257         (*self) != (*other)
258     }
259 }
260
261
262 impl PartialEq for u64 {
263     fn eq(&self, other: &u64) -> bool {
264         (*self) == (*other)
265     }
266     fn ne(&self, other: &u64) -> bool {
267         (*self) != (*other)
268     }
269 }
270
271 impl PartialEq for usize {
272     fn eq(&self, other: &usize) -> bool {
273         (*self) == (*other)
274     }
275     fn ne(&self, other: &usize) -> bool {
276         (*self) != (*other)
277     }
278 }
279
280 impl PartialEq for i32 {
281     fn eq(&self, other: &i32) -> bool {
282         (*self) == (*other)
283     }
284     fn ne(&self, other: &i32) -> bool {
285         (*self) != (*other)
286     }
287 }
288
289 impl PartialEq for isize {
290     fn eq(&self, other: &isize) -> bool {
291         (*self) == (*other)
292     }
293     fn ne(&self, other: &isize) -> bool {
294         (*self) != (*other)
295     }
296 }
297
298 impl PartialEq for char {
299     fn eq(&self, other: &char) -> bool {
300         (*self) == (*other)
301     }
302     fn ne(&self, other: &char) -> bool {
303         (*self) != (*other)
304     }
305 }
306
307 impl<T: ?Sized> PartialEq for *const T {
308     fn eq(&self, other: &*const T) -> bool {
309         *self == *other
310     }
311     fn ne(&self, other: &*const T) -> bool {
312         *self != *other
313     }
314 }
315
316 #[lang = "neg"]
317 pub trait Neg {
318     type Output;
319
320     fn neg(self) -> Self::Output;
321 }
322
323 impl Neg for i8 {
324     type Output = i8;
325
326     fn neg(self) -> i8 {
327         -self
328     }
329 }
330
331 impl Neg for i16 {
332     type Output = i16;
333
334     fn neg(self) -> i16 {
335         self
336     }
337 }
338
339 impl Neg for isize {
340     type Output = isize;
341
342     fn neg(self) -> isize {
343         -self
344     }
345 }
346
347 pub enum Option<T> {
348     Some(T),
349     None,
350 }
351
352 pub use Option::*;
353
354 #[lang = "phantom_data"]
355 pub struct PhantomData<T: ?Sized>;
356
357 #[lang = "fn_once"]
358 #[rustc_paren_sugar]
359 pub trait FnOnce<Args> {
360     type Output;
361
362     extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
363 }
364
365 #[lang = "fn_mut"]
366 #[rustc_paren_sugar]
367 pub trait FnMut<Args>: FnOnce<Args> {
368     extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output;
369 }
370
371 #[lang = "panic"]
372 pub fn panic(&(_msg, _file, _line, _col): &(&'static str, &'static str, u32, u32)) -> ! {
373     unsafe {
374         libc::puts("Panicking\0" as *const str as *const u8);
375         intrinsics::abort();
376     }
377 }
378
379 #[lang = "eh_personality"]
380 fn eh_personality() -> ! {
381     loop {}
382 }
383
384 #[lang = "drop_in_place"]
385 #[allow(unconditional_recursion)]
386 pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
387     // Code here does not matter - this is replaced by the
388     // real drop glue by the compiler.
389     drop_in_place(to_drop);
390 }
391
392 #[lang = "deref"]
393 pub trait Deref {
394     type Target: ?Sized;
395
396     fn deref(&self) -> &Self::Target;
397 }
398
399 #[lang = "owned_box"]
400 pub struct Box<T: ?Sized>(*mut T);
401
402 impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Box<U>> for Box<T> {}
403
404 impl<T: ?Sized> Drop for Box<T> {
405     fn drop(&mut self) {
406         // drop is currently performed by compiler.
407     }
408 }
409
410 impl<T> Deref for Box<T> {
411     type Target = T;
412
413     fn deref(&self) -> &Self::Target {
414         &**self
415     }
416 }
417
418 #[lang = "exchange_malloc"]
419 unsafe fn allocate(size: usize, _align: usize) -> *mut u8 {
420     libc::malloc(size)
421 }
422
423 #[lang = "box_free"]
424 unsafe fn box_free<T: ?Sized>(ptr: *mut T) {
425     libc::free(ptr as *mut u8);
426 }
427
428 #[lang = "drop"]
429 pub trait Drop {
430     fn drop(&mut self);
431 }
432
433 pub union MaybeUninit<T> {
434     pub uninit: (),
435     pub value: T,
436 }
437
438 pub mod intrinsics {
439     extern "rust-intrinsic" {
440         pub fn abort() -> !;
441         pub fn size_of<T>() -> usize;
442         pub fn size_of_val<T: ?::Sized>(val: &T) -> usize;
443         pub fn min_align_of<T>() -> usize;
444         pub fn min_align_of_val<T: ?::Sized>(val: &T) -> usize;
445         pub fn copy<T>(src: *const T, dst: *mut T, count: usize);
446         pub fn transmute<T, U>(e: T) -> U;
447         pub fn init<T>() -> T;
448         pub fn ctlz_nonzero<T>(x: T) -> T;
449         pub fn needs_drop<T>() -> bool;
450         pub fn bitreverse<T>(x: T) -> T;
451         pub fn bswap<T>(x: T) -> T;
452     }
453 }
454
455 pub mod libc {
456     #[link(name = "c")]
457     extern "C" {
458         pub fn puts(s: *const u8);
459         pub fn printf(format: *const i8, ...) -> i32;
460         pub fn malloc(size: usize) -> *mut u8;
461         pub fn free(ptr: *mut u8);
462         pub fn memcpy(dst: *mut u8, src: *const u8, size: usize);
463         pub fn memmove(dst: *mut u8, src: *const u8, size: usize);
464         pub fn strncpy(dst: *mut u8, src: *const u8, size: usize);
465     }
466 }
467
468 #[lang = "index"]
469 pub trait Index<Idx: ?Sized> {
470     type Output: ?Sized;
471     fn index(&self, index: Idx) -> &Self::Output;
472 }
473
474 impl<T> Index<usize> for [T; 3] {
475     type Output = T;
476
477     fn index(&self, index: usize) -> &Self::Output {
478         &self[index]
479     }
480 }
481
482 impl<T> Index<usize> for [T] {
483     type Output = T;
484
485     fn index(&self, index: usize) -> &Self::Output {
486         &self[index]
487     }
488 }
489
490 extern {
491     type VaListImpl;
492 }
493
494 #[lang = "va_list"]
495 #[repr(transparent)]
496 pub struct VaList<'a>(&'a mut VaListImpl);
497
498 #[rustc_builtin_macro]
499 #[rustc_macro_transparency = "semitransparent"]
500 pub macro stringify($($t:tt)*) { /* compiler built-in */ }
501
502 #[rustc_builtin_macro]
503 #[rustc_macro_transparency = "semitransparent"]
504 pub macro file() { /* compiler built-in */ }
505
506 #[rustc_builtin_macro]
507 #[rustc_macro_transparency = "semitransparent"]
508 pub macro line() { /* compiler built-in */ }