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
use serde_bytes::ByteBuf;
use serde_cbor::error;
use serde_cbor::{from_value, Value};
use std;
use std::collections::HashMap;
use std::path::{Path, PathBuf};

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum LRValue {
    LValue,
    RValue,
}

impl LRValue {
    pub fn is_lvalue(&self) -> bool {
        *self == LRValue::LValue
    }
    pub fn is_rvalue(&self) -> bool {
        *self == LRValue::RValue
    }
}

#[derive(Debug, Clone)]
pub struct AstNode {
    pub tag: ASTEntryTag,
    pub children: Vec<Option<u64>>,
    pub fileid: u64,
    pub line: u64,
    pub column: u64,
    pub file_path: Option<PathBuf>,
    pub type_id: Option<u64>,
    pub rvalue: LRValue,
    pub extras: Vec<Value>,
}

#[derive(Debug, Clone)]
pub struct TypeNode {
    pub tag: TypeTag,
    pub extras: Vec<Value>,
}

#[derive(Debug, Clone)]
pub struct CommentNode {
    pub fileid: u64,
    pub line: u64,
    pub column: u64,
    pub string: String,
}

impl TypeNode {
    // Masks used to decode the IDs given to type nodes
    pub const ID_MASK: u64 = !0b111;
    pub const CONST_MASK: u64 = 0b001;
    pub const RESTRICT_MASK: u64 = 0b010;
    pub const VOLATILE_MASK: u64 = 0b100;
}

#[derive(Debug, Clone)]
pub struct AstContext {
    pub ast_nodes: HashMap<u64, AstNode>,
    pub type_nodes: HashMap<u64, TypeNode>,
    pub top_nodes: Vec<u64>,
    pub comments: Vec<CommentNode>,
}

pub fn expect_opt_str(val: &Value) -> Option<Option<&str>> {
    match *val {
        Value::Null => Some(None),
        Value::String(ref s) => Some(Some(s)),
        _ => None,
    }
}

pub fn expect_opt_u64(val: &Value) -> Option<Option<u64>> {
    match *val {
        Value::Null => Some(None),
        Value::U64(n) => Some(Some(n)),
        _ => None,
    }
}

fn import_ast_tag(tag: u64) -> ASTEntryTag {
    unsafe {
        return std::mem::transmute::<u32, ASTEntryTag>(tag as u32);
    }
}

fn import_type_tag(tag: u64) -> TypeTag {
    unsafe {
        return std::mem::transmute::<u32, TypeTag>(tag as u32);
    }
}

pub fn process(items: Value) -> error::Result<AstContext> {
    let mut asts: HashMap<u64, AstNode> = HashMap::new();
    let mut types: HashMap<u64, TypeNode> = HashMap::new();
    let mut comments: Vec<CommentNode> = vec![];

    let (all_nodes, top_nodes, file_paths, raw_comments): (
        Vec<Vec<Value>>,
        Vec<u64>,
        Vec<String>,
        Vec<(u64, u64, u64, ByteBuf)>,
    ) = from_value(items)?;

    for (fileid, line, column, bytes) in raw_comments {
        comments.push(CommentNode {
            fileid,
            line,
            column,
            string: String::from_utf8_lossy(&bytes).to_string(),
        })
    }

    for entry in all_nodes {
        let entry_id = entry[0].as_u64().unwrap();
        let tag = entry[1].as_u64().unwrap();

        if tag < 400 {
            let children = entry[2]
                .as_array()
                .unwrap()
                .iter()
                .map(|x| expect_opt_u64(x).unwrap())
                .collect::<Vec<Option<u64>>>();

            let type_id: Option<u64> = expect_opt_u64(&entry[6]).unwrap();
            let fileid = entry[3].as_u64().unwrap();
            let file_path = match file_paths[fileid as usize].as_str() {
                "" => None,
                "?" => None,
                path => Some(Path::new(path).to_path_buf()),
            };

            let node = AstNode {
                tag: import_ast_tag(tag),
                children,
                fileid,
                line: entry[4].as_u64().unwrap(),
                column: entry[5].as_u64().unwrap(),
                type_id,
                file_path,
                rvalue: if entry[7].as_boolean().unwrap() {
                    LRValue::RValue
                } else {
                    LRValue::LValue
                },
                extras: entry[8..].to_vec(),
            };

            asts.insert(entry_id, node);
        } else {
            let node = TypeNode {
                tag: import_type_tag(tag),
                extras: entry[2..].to_vec(),
            };

            types.insert(entry_id, node);
        }
    }
    Ok(AstContext {
        top_nodes,
        ast_nodes: asts,
        type_nodes: types,
        comments,
    })
}