Skip to content

Commit ec0b111

Browse files
committed
Added support for file-local global variables, and started working on support for C global variables
1 parent 1d9f4ab commit ec0b111

File tree

11 files changed

+39
-12
lines changed

11 files changed

+39
-12
lines changed

Diff for: src/asg/global.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
use super::{GlobalVarRef, Type};
2-
use crate::{ast::Privacy, name::ResolvedName, source_files::Source};
2+
use crate::{
3+
ast::{Exposure, Privacy},
4+
name::ResolvedName,
5+
source_files::Source,
6+
};
37

48
#[derive(Clone, Debug)]
59
pub struct GlobalVar {
@@ -8,6 +12,7 @@ pub struct GlobalVar {
812
pub source: Source,
913
pub is_foreign: bool,
1014
pub is_thread_local: bool,
15+
pub exposure: Exposure,
1116
}
1217

1318
#[derive(Clone, Debug)]

Diff for: src/ast/exposure.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use derive_more::IsVariant;
2+
3+
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, IsVariant)]
4+
pub enum Exposure {
5+
#[default]
6+
Hidden,
7+
Exposed,
8+
}

Diff for: src/ast/global_variable.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{Privacy, Type};
1+
use super::{Exposure, Privacy, Type};
22
use crate::source_files::Source;
33

44
#[derive(Clone, Debug)]
@@ -9,4 +9,5 @@ pub struct GlobalVar {
99
pub is_foreign: bool,
1010
pub is_thread_local: bool,
1111
pub privacy: Privacy,
12+
pub exposure: Exposure,
1213
}

Diff for: src/ast/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod block;
22
mod conforming;
33
mod datatype;
44
mod enumeration;
5+
mod exposure;
56
mod expr;
67
mod file;
78
mod func;
@@ -21,6 +22,7 @@ pub use block::*;
2122
pub use conforming::*;
2223
pub use datatype::*;
2324
pub use enumeration::*;
25+
pub use exposure::*;
2426
pub use expr::*;
2527
pub use file::*;
2628
pub use func::*;

Diff for: src/c/translation/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ pub fn declare_named_declaration(
7272
is_foreign: true,
7373
is_thread_local,
7474
privacy: Privacy::Public,
75+
exposure: ast::Exposure::Exposed,
7576
});
7677
return Ok(());
7778
}

Diff for: src/ir/global.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use super::Type;
2+
use crate::ast::Exposure;
23

34
#[derive(Clone, Debug)]
45
pub struct Global {
56
pub mangled_name: String,
67
pub ir_type: Type,
78
pub is_foreign: bool,
89
pub is_thread_local: bool,
10+
pub exposure: Exposure,
911
}

Diff for: src/llvm_backend/globals.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub unsafe fn create_globals(ctx: &mut BackendCtx) -> Result<(), BackendError> {
1515
let name = CString::new(global.mangled_name.as_bytes()).unwrap();
1616
let backend_global = LLVMAddGlobal(ctx.backend_module.get(), backend_type, name.as_ptr());
1717

18-
let linkage = if global.is_foreign {
18+
let linkage = if global.exposure.is_exposed() {
1919
LLVMLinkage::LLVMExternalLinkage
2020
} else {
2121
LLVMLinkage::LLVMInternalLinkage

Diff for: src/lower/global.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn lower_global(
1111
global: &asg::GlobalVar,
1212
asg: &Asg,
1313
) -> Result<(), LowerError> {
14-
let mangled_name = if global.is_foreign {
14+
let mangled_name = if global.is_foreign || global.exposure.is_exposed() {
1515
global.name.plain().to_string()
1616
} else {
1717
global.name.display(&asg.workspace.fs).to_string()
@@ -21,13 +21,10 @@ pub fn lower_global(
2121
global_ref,
2222
Global {
2323
mangled_name,
24-
ir_type: lower_type(
25-
ir_module,
26-
&unpoly(&PolyRecipe::default(), &global.ty)?,
27-
asg,
28-
)?,
24+
ir_type: lower_type(ir_module, &unpoly(&PolyRecipe::default(), &global.ty)?, asg)?,
2925
is_foreign: global.is_foreign,
3026
is_thread_local: global.is_thread_local,
27+
exposure: global.exposure,
3128
},
3229
);
3330

Diff for: src/parser/parse_global_variable.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::{
44
Parser,
55
};
66
use crate::{
7-
ast::{GlobalVar, Privacy},
7+
ast::{Exposure, GlobalVar, Privacy},
88
inflow::Inflow,
99
token::{Token, TokenKind},
1010
};
@@ -20,13 +20,15 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
2020
let mut is_foreign = false;
2121
let mut is_thread_local = false;
2222
let mut privacy = Privacy::Protected;
23+
let mut exposure = Exposure::Hidden;
2324

2425
for annotation in annotations {
2526
match annotation.kind {
2627
AnnotationKind::Foreign => is_foreign = true,
2728
AnnotationKind::ThreadLocal => is_thread_local = true,
2829
AnnotationKind::Public => privacy = Privacy::Public,
2930
AnnotationKind::Private => privacy = Privacy::Private,
31+
AnnotationKind::Exposed => exposure = Exposure::Exposed,
3032
_ => {
3133
return Err(self.unexpected_annotation(&annotation, Some("for global variable")))
3234
}
@@ -51,6 +53,7 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
5153
is_foreign,
5254
is_thread_local,
5355
privacy,
56+
exposure,
5457
})
5558
}
5659
}

Diff for: src/resolve/expr/variable.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ pub fn resolve_variable_expr(
5050
if let Some(basename) = name.as_plain_str() {
5151
let maybe_global = ctx
5252
.globals_in_modules
53-
.get(&ctx.module_fs_node_id)
53+
.get(&ctx.physical_fs_node_id)
54+
.or_else(|| ctx.globals_in_modules.get(&ctx.module_fs_node_id))
5455
.and_then(|globals| globals.get(basename));
5556

5657
let maybe_helper_expr = ctx

Diff for: src/resolve/global_variable.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,17 @@ pub fn resolve_global_variables(
3434
source: global.source,
3535
is_foreign: global.is_foreign,
3636
is_thread_local: global.is_thread_local,
37+
exposure: global.exposure,
3738
});
3839

40+
let fs_node_id = if global.privacy.is_private() {
41+
*physical_file_id
42+
} else {
43+
module_folder_id
44+
};
45+
3946
ctx.globals_in_modules
40-
.entry(module_folder_id)
47+
.entry(fs_node_id)
4148
.or_default()
4249
.insert(
4350
global.name.clone(),

0 commit comments

Comments
 (0)