/
var.rs
171 lines (147 loc) · 5.28 KB
/
var.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
use super::item::{Item, ItemId};
use super::context::BindgenContext;
use super::ty::TypeKind;
use super::int::IntKind;
use super::function::cursor_mangling;
use parse::{ClangItemParser, ClangSubItemParser, ParseResult, ParseError};
use clang;
#[derive(Debug)]
pub struct Var {
/// The name of the variable.
name: String,
/// The mangled name of the variable.
mangled_name: Option<String>,
/// The type of the variable.
ty: ItemId,
/// TODO: support non-integer constants?
/// The integer value of the variable.
val: Option<i64>,
/// Whether this variable is const.
is_const: bool,
}
impl Var {
pub fn new(name: String,
mangled: Option<String>,
ty: ItemId,
val: Option<i64>,
is_const: bool) -> Var {
assert!(!name.is_empty());
Var {
name: name,
mangled_name: mangled,
ty: ty,
val: val,
is_const: is_const,
}
}
pub fn is_const(&self) -> bool {
self.is_const
}
pub fn val(&self) -> Option<i64> {
self.val
}
pub fn ty(&self) -> ItemId {
self.ty
}
pub fn name(&self) -> &str {
&self.name
}
pub fn mangled_name(&self) -> Option<&str> {
self.mangled_name.as_ref().map(|n| &**n)
}
}
impl ClangSubItemParser for Var {
fn parse(cursor: clang::Cursor,
context: &mut BindgenContext) -> Result<ParseResult<Self>, ParseError> {
use clangll::*;
match cursor.kind() {
CXCursor_MacroDefinition => {
let value = match parse_int_literal_tokens(&cursor, context.translation_unit()) {
None => return Err(ParseError::Continue),
Some(v) => v,
};
let name = cursor.spelling();
if name.is_empty() {
warn!("Empty macro name?");
return Err(ParseError::Continue);
}
if context.parsed_macro(&name) {
warn!("Duplicated macro definition: {}", name);
return Err(ParseError::Continue);
}
context.note_parsed_macro(name.clone());
let ty = if value < 0 {
Item::builtin_type(TypeKind::Int(IntKind::Int), true, context)
} else if value.abs() > u32::max_value() as i64 {
Item::builtin_type(TypeKind::Int(IntKind::ULongLong), true, context)
} else {
Item::builtin_type(TypeKind::Int(IntKind::UInt), true, context)
};
Ok(ParseResult::New(Var::new(name, None, ty, Some(value), true), Some(cursor)))
}
CXCursor_VarDecl => {
let name = cursor.spelling();
if name.is_empty() {
warn!("Empty constant name?");
return Err(ParseError::Continue);
}
let ty = cursor.cur_type();
// XXX this is redundant, remove!
let is_const = ty.is_const();
let ty = Item::from_ty(&ty, Some(cursor), None, context)
.expect("Unable to resolve constant type?");
let mut value = None;
// Note: Ty might not be totally resolved yet, see
// tests/headers/inner_const.hpp
//
// That's fine because in that case we know it's not a literal.
if context.safe_resolve_type(ty).map_or(false, |t| t.is_integer_literal()) {
// Try to parse a literal token value
cursor.visit(|c, _| {
if c.kind() == CXCursor_IntegerLiteral {
value = parse_int_literal_tokens(&c, context.translation_unit());
}
CXChildVisit_Continue
});
}
let mangling = cursor_mangling(&cursor);
let var = Var::new(name, mangling, ty, value, is_const);
Ok(ParseResult::New(var, Some(cursor)))
}
_ => {
/* TODO */
Err(ParseError::Continue)
}
}
}
}
/// Try and parse the immediately found tokens from an unit (if any) to integers
fn parse_int_literal_tokens(cursor: &clang::Cursor,
unit: &clang::TranslationUnit) -> Option<i64> {
use clangll::{CXToken_Literal, CXToken_Punctuation};
let mut lit = String::new();
let tokens = match unit.tokens(cursor) {
None => return None,
Some(tokens) => tokens,
};
for token in tokens {
match token.kind {
CXToken_Punctuation if token.spelling == "-" => {
// If there's ever any punctuation, we only need to worry about
// unary minus '-' (for now)
lit.push_str(&token.spelling);
},
CXToken_Literal => {
lit.push_str(&token.spelling);
break
},
_ => (),
}
}
// TODO: try to preserve hex literals?
if lit.starts_with("0x") {
i64::from_str_radix(&lit[2..], 16).ok()
} else {
lit.parse().ok()
}
}