ptr.rs 4.08 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright 2017 The Australian National University
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

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
// Copyright 2014 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.

//! The AST pointer
//!
//! Provides `P<T>`, a frozen owned smart pointer, as a replacement for `@T` in
//! the AST.
//!
//! # Motivations and benefits
//!
//! * **Identity**: sharing AST nodes is problematic for the various analysis
//!   passes (e.g. one may be able to bypass the borrow checker with a shared
//!   `ExprAddrOf` node taking a mutable borrow). The only reason `@T` in the
//!   AST hasn't caused issues is because of inefficient folding passes which
//!   would always deduplicate any such shared nodes. Even if the AST were to
//!   switch to an arena, this would still hold, i.e. it couldn't use `&'a T`,
//!   but rather a wrapper like `P<'a, T>`.
//!
//! * **Immutability**: `P<T>` disallows mutating its inner `T`, unlike `Box<T>`
//!   (unless it contains an `Unsafe` interior, but that may be denied later).
//!   This mainly prevents mistakes, but can also enforces a kind of "purity".
//!
//! * **Efficiency**: folding can reuse allocation space for `P<T>` and `Vec<T>`,
//!   the latter even when the input and output types differ (as it would be the
//!   case with arenas or a GADT AST using type parameters to toggle features).
//!
//! * **Maintainability**: `P<T>` provides a fixed interface - `Deref`,
//!   `and_then` and `map` - which can remain fully functional even if the
//!   implementation changes (using a special thread-local heap, for example).
//!   Moreover, a switch to, e.g. `P<'a, T>` would be easy and mostly automated.

qinsoon's avatar
qinsoon committed
53 54 55 56
//use std::fmt::{self, Display, Debug};
//use std::hash::{Hash, Hasher};
//use std::ops::Deref;
//use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
57

58 59
use std::sync::Arc;

qinsoon's avatar
qinsoon committed
60 61 62
pub type P<T> = Arc<T>;
//pub struct P<T: MuEntity> {
//    ptr: Arc<T>
63
//}
64 65 66

#[allow(non_snake_case)]
/// Construct a `P<T>` from a `T` value.
qinsoon's avatar
qinsoon committed
67
pub fn P<T>(value: T) -> P<T> {
qinsoon's avatar
qinsoon committed
68
//    P {ptr: Arc::new(value)}
69
    Arc::new(value)
70 71
}

qinsoon's avatar
qinsoon committed
72
//impl<T: MuEntity> Deref for P<T> {
73 74 75 76 77 78 79
//    type Target = T;
//
//    fn deref<'a>(&'a self) -> &'a T {
//        &*self.ptr
//    }
//}
//
qinsoon's avatar
qinsoon committed
80
//impl<T: MuEntity> Clone for P<T> {
81
//    fn clone(&self) -> P<T> {
qinsoon's avatar
qinsoon committed
82
//        P {ptr: self.ptr.clone()}
83 84 85
//    }
//}
//
qinsoon's avatar
qinsoon committed
86
//impl<T: MuEntity + PartialEq> PartialEq for P<T> {
87 88 89 90 91
//    fn eq(&self, other: &P<T>) -> bool {
//        **self == **other
//    }
//}
//
qinsoon's avatar
qinsoon committed
92
//impl<T: MuEntity + Eq> Eq for P<T> {}
93
//
qinsoon's avatar
qinsoon committed
94
//impl<T: MuEntity + Debug> Debug for P<T> {
95 96 97 98
//    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
//        Debug::fmt(&**self, f)
//    }
//}
qinsoon's avatar
qinsoon committed
99
//impl<T: MuEntity + Display> Display for P<T> {
100 101 102 103 104
//    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
//        Display::fmt(&**self, f)
//    }
//}
//
qinsoon's avatar
qinsoon committed
105
//impl<T: MuEntity> fmt::Pointer for P<T> {
106 107 108 109 110
//    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
//        fmt::Pointer::fmt(&self.ptr, f)
//    }
//}
//
qinsoon's avatar
qinsoon committed
111
//impl<T: MuEntity + Hash> Hash for P<T> {
112 113 114 115
//    fn hash<H: Hasher>(&self, state: &mut H) {
//        (**self).hash(state);
//    }
//}
qinsoon's avatar
qinsoon committed
116 117 118 119 120

//impl<T: MuEntity> Encodable for P<T> {
//    fn encode<S: Encoder> (&self, s: &mut S) -> Result<(), S::Error> {
//        s.emit_usize(self.id())
//    }
121
//}