Skip to content

Commit 6088272

Browse files
committed
Added support for C global variable definitions
1 parent 0d534d8 commit 6088272

File tree

7 files changed

+62
-12
lines changed

7 files changed

+62
-12
lines changed

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::{
2020
ast::{AstFile, Param, Type, TypeKind},
2121
diagnostics::{Diagnostics, WarningDiagnostic},
2222
source_files::source::Source,
23+
workspace::compile::header::CFileType,
2324
};
2425
use derive_more::{From, IsVariant};
2526
use itertools::Itertools;
@@ -30,6 +31,7 @@ pub struct Parser<'a> {
3031
typedefs: HashMap<String, CTypedef>,
3132
enum_constants: HashMap<String, Integer>,
3233
diagnostics: &'a Diagnostics<'a>,
34+
c_file_type: CFileType,
3335
}
3436

3537
impl Parser<'_> {
@@ -476,7 +478,7 @@ pub struct EnumerationNamed {
476478
}
477479

478480
impl<'a> Parser<'a> {
479-
pub fn new(input: Input<'a>, diagnostics: &'a Diagnostics<'a>) -> Self {
481+
pub fn new(input: Input<'a>, diagnostics: &'a Diagnostics<'a>, c_file_type: CFileType) -> Self {
480482
let mut typedefs = HashMap::default();
481483

482484
diagnostics.push(WarningDiagnostic::new(
@@ -499,6 +501,7 @@ impl<'a> Parser<'a> {
499501
typedefs,
500502
enum_constants: HashMap::default(),
501503
diagnostics,
504+
c_file_type,
502505
}
503506
}
504507

@@ -526,6 +529,7 @@ impl<'a> Parser<'a> {
526529
&declaration.declaration_specifiers,
527530
&mut self.typedefs,
528531
self.diagnostics,
532+
self.c_file_type,
529533
)?,
530534
DeclaratorKind::Function(declarator, parameter_type_list) => {
531535
declare_function(
@@ -536,6 +540,7 @@ impl<'a> Parser<'a> {
536540
declarator,
537541
parameter_type_list,
538542
self.diagnostics,
543+
self.c_file_type,
539544
)?;
540545
}
541546
}

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use super::{parameters::has_parameters, types::get_name_and_type};
22
use crate::{
33
asg::TypeParams,
4-
ast::{self, AstFile, Func, FuncHead, Param, Params, Privacy},
4+
ast::{self, AstFile, Func, FuncHead, Param, Params},
55
c::parser::{
66
error::ParseErrorKind, CTypedef, DeclarationSpecifiers, Declarator,
77
ParameterDeclarationCore, ParameterTypeList, ParseError, StorageClassSpecifier,
88
},
99
diagnostics::Diagnostics,
10+
workspace::compile::header::CFileType,
1011
};
1112
use std::collections::HashMap;
1213

@@ -18,6 +19,7 @@ pub fn declare_function(
1819
declarator: &Declarator,
1920
parameter_type_list: &ParameterTypeList,
2021
diagnostics: &Diagnostics,
22+
c_file_type: CFileType,
2123
) -> Result<(), ParseError> {
2224
let source = declarator.source;
2325
let (name, return_type, storage_class, function_specifier, _is_thread_local) =
@@ -100,7 +102,7 @@ pub fn declare_function(
100102
source,
101103
abide_abi: true,
102104
tag: None,
103-
privacy: Privacy::Public,
105+
privacy: c_file_type.privacy(),
104106
};
105107

106108
ast_file.funcs.push(Func {

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

+9-5
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ use self::types::get_name_and_type;
88
pub use self::{expr::translate_expr, function::declare_function};
99
use crate::{
1010
asg::TypeParams,
11-
ast::{self, AstFile, Privacy},
11+
ast::{self, AstFile},
1212
c::parser::{CTypedef, DeclarationSpecifiers, Declarator, ParseError, StorageClassSpecifier},
1313
diagnostics::{Diagnostics, WarningDiagnostic},
14+
workspace::compile::header::CFileType,
1415
};
1516
use std::collections::HashMap;
1617

@@ -21,6 +22,7 @@ pub fn declare_named_declaration(
2122
declaration_specifiers: &DeclarationSpecifiers,
2223
typedefs: &mut HashMap<String, CTypedef>,
2324
diagnostics: &Diagnostics,
25+
c_file_type: CFileType,
2426
) -> Result<(), ParseError> {
2527
let (name, ast_type, storage_class, function_specifier, is_thread_local) = get_name_and_type(
2628
ast_file,
@@ -47,14 +49,16 @@ pub fn declare_named_declaration(
4749
params: TypeParams::default(),
4850
value: ast_type.clone(),
4951
source: declarator.source,
50-
privacy: Privacy::Public,
52+
privacy: c_file_type.privacy(),
5153
});
5254

5355
typedefs.insert(name, CTypedef { ast_type });
5456
return Ok(());
5557
}
5658

57-
if let Some(StorageClassSpecifier::Extern) = storage_class {
59+
if let None | Some(StorageClassSpecifier::Extern) = storage_class {
60+
let is_foreign = storage_class.is_some();
61+
5862
if let Some(function_specifier) = function_specifier {
5963
diagnostics.push(WarningDiagnostic::new(
6064
format!(
@@ -69,9 +73,9 @@ pub fn declare_named_declaration(
6973
name,
7074
ast_type,
7175
source: declarator.source,
72-
is_foreign: true,
76+
is_foreign,
7377
is_thread_local,
74-
privacy: Privacy::Public,
78+
privacy: c_file_type.privacy(),
7579
exposure: ast::Exposure::Exposed,
7680
});
7781
return Ok(());

Diff for: src/workspace/compile/header.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,26 @@ use crate::{
1212
text::Text,
1313
};
1414

15-
pub fn header(
15+
#[derive(Copy, Clone, Debug)]
16+
pub enum CFileType {
17+
Header,
18+
Source,
19+
}
20+
21+
impl CFileType {
22+
pub fn privacy(&self) -> Privacy {
23+
match self {
24+
CFileType::Header => Privacy::Protected,
25+
CFileType::Source => Privacy::Private,
26+
}
27+
}
28+
}
29+
30+
pub fn c_code(
1631
compiler: &Compiler,
1732
text: impl Text,
1833
key: SourceFileKey,
34+
c_file_type: CFileType,
1935
) -> Result<AstFile, Box<(dyn Show + 'static)>> {
2036
let Preprocessed {
2137
document,
@@ -28,6 +44,7 @@ pub fn header(
2844
let mut parser = c::parser::Parser::new(
2945
c::parser::Input::new(lexed, compiler.source_files, key),
3046
compiler.diagnostics,
47+
c_file_type,
3148
);
3249

3350
let mut ast_file = parser.parse().map_err(into_show)?;

Diff for: src/workspace/compile/normal.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::header::header;
1+
use super::header::{c_code, CFileType};
22
use crate::{
33
ast::AstFile,
44
compiler::Compiler,
@@ -47,10 +47,16 @@ pub fn compile_normal_file(
4747
Source::new(key, Location { line: 1, column: 1 }),
4848
));
4949

50-
out_ast_files.push((normal_file.fs_node_id, header(compiler, text, key)?));
50+
out_ast_files.push((
51+
normal_file.fs_node_id,
52+
c_code(compiler, text, key, CFileType::Source)?,
53+
));
5154
}
5255
NormalFileKind::CHeader => {
53-
out_ast_files.push((normal_file.fs_node_id, header(compiler, text, key)?));
56+
out_ast_files.push((
57+
normal_file.fs_node_id,
58+
c_code(compiler, text, key, CFileType::Header)?,
59+
));
5460
}
5561
}
5662

Diff for: tests/success/c_global_variable/_.adept

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
pragma => adept("3.0")
3+
4+
#[foreign]
5+
func printf(format ptr#char, ...) int
6+
7+
#[foreign]
8+
special_value int
9+
10+
func main {
11+
special_value = 1234
12+
printf(c"special_value = %d\n", special_value)
13+
}
14+

Diff for: tests/success/c_global_variable/special_value.c

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
int special_value;

0 commit comments

Comments
 (0)