Skip to content

Commit 02dc2bf

Browse files
jrmuizelemilio
authored andcommitted
Store function linkage in the ir
This lets us capture 'static inline' functions in the ir and filter them later down the pipeline. This is the first step on the way to handling these functions better.
1 parent 8582a90 commit 02dc2bf

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

src/codegen/mod.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use ir::derive::{CanDeriveCopy, CanDeriveDebug, CanDeriveDefault,
2020
CanDerivePartialEq, CanDeriveEq, CannotDeriveReason};
2121
use ir::dot;
2222
use ir::enum_ty::{Enum, EnumVariant, EnumVariantValue};
23-
use ir::function::{Abi, Function, FunctionSig};
23+
use ir::function::{Abi, Function, FunctionSig, Linkage};
2424
use ir::int::IntKind;
2525
use ir::item::{IsOpaque, Item, ItemCanonicalName, ItemCanonicalPath};
2626
use ir::item_kind::ItemKind;
@@ -3127,6 +3127,13 @@ impl CodeGenerator for Function {
31273127
debug!("<Function as CodeGenerator>::codegen: item = {:?}", item);
31283128
debug_assert!(item.is_enabled_for_codegen(ctx));
31293129

3130+
// We can't currently do anything with Internal functions so just
3131+
// avoid generating anything for them.
3132+
match self.linkage() {
3133+
Linkage::Internal => return,
3134+
Linkage::External => {}
3135+
}
3136+
31303137
// Similar to static member variables in a class template, we can't
31313138
// generate bindings to template functions, because the set of
31323139
// instantiations is open ended and we have no way of knowing which

src/ir/function.rs

+26-6
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ impl FunctionKind {
4949
}
5050
}
5151

52+
/// The style of linkage
53+
#[derive(Debug, Clone, Copy)]
54+
pub enum Linkage {
55+
/// Externally visible and can be linked against
56+
External,
57+
/// Not exposed externally. 'static inline' functions will have this kind of linkage
58+
Internal
59+
}
60+
5261
/// A function declaration, with a signature, arguments, and argument names.
5362
///
5463
/// The argument names vector must be the same length as the ones in the
@@ -69,6 +78,9 @@ pub struct Function {
6978

7079
/// The kind of function this is.
7180
kind: FunctionKind,
81+
82+
/// The linkage of the function.
83+
linkage: Linkage,
7284
}
7385

7486
impl Function {
@@ -79,13 +91,15 @@ impl Function {
7991
sig: TypeId,
8092
comment: Option<String>,
8193
kind: FunctionKind,
94+
linkage: Linkage
8295
) -> Self {
8396
Function {
8497
name: name,
8598
mangled_name: mangled_name,
8699
signature: sig,
87100
comment: comment,
88101
kind: kind,
102+
linkage: linkage
89103
}
90104
}
91105

@@ -108,6 +122,12 @@ impl Function {
108122
pub fn kind(&self) -> FunctionKind {
109123
self.kind
110124
}
125+
126+
/// Get this function's linkage.
127+
pub fn linkage(&self) -> Linkage {
128+
self.linkage
129+
}
130+
111131
}
112132

113133
impl DotAttributes for Function {
@@ -477,11 +497,11 @@ impl ClangSubItemParser for Function {
477497
}
478498

479499
let linkage = cursor.linkage();
480-
if linkage != CXLinkage_External &&
481-
linkage != CXLinkage_UniqueExternal
482-
{
483-
return Err(ParseError::Continue);
484-
}
500+
let linkage = match linkage {
501+
CXLinkage_External | CXLinkage_UniqueExternal => Linkage::External,
502+
CXLinkage_Internal => Linkage::Internal,
503+
_ => return Err(ParseError::Continue)
504+
};
485505

486506
// Grab the signature using Item::from_ty.
487507
let sig =
@@ -511,7 +531,7 @@ impl ClangSubItemParser for Function {
511531

512532
let comment = cursor.raw_comment();
513533

514-
let function = Self::new(name, mangled_name, sig, comment, kind);
534+
let function = Self::new(name, mangled_name, sig, comment, kind, linkage);
515535
Ok(ParseResult::New(function, Some(cursor)))
516536
}
517537
}

0 commit comments

Comments
 (0)