Skip to content

Commit b7d6aba

Browse files
committed
Added support for private file-local types
1 parent cf96890 commit b7d6aba

File tree

10 files changed

+93
-10
lines changed

10 files changed

+93
-10
lines changed

Diff for: src/asg/type_decl.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ use super::{Asg, TypeKind};
22
use crate::{
33
ast::{self, Privacy},
44
source_files::Source,
5+
workspace::fs::FsNodeId,
56
};
67

78
#[derive(Clone, Debug)]
89
pub struct TypeDecl {
910
pub kind: TypeKind,
1011
pub source: Source,
1112
pub privacy: Privacy,
13+
pub file_fs_node_id: FsNodeId,
1214
}
1315

1416
impl TypeDecl {

Diff for: src/parser/parse_enum.rs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
2020
for annotation in annotations {
2121
match annotation.kind {
2222
AnnotationKind::Public => privacy = Privacy::Public,
23+
AnnotationKind::Private => privacy = Privacy::Private,
2324
_ => return Err(self.unexpected_annotation(&annotation, Some("for enum"))),
2425
}
2526
}

Diff for: src/parser/parse_structure.rs

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
2525
match annotation.kind {
2626
AnnotationKind::Packed => is_packed = true,
2727
AnnotationKind::Public => privacy = Privacy::Public,
28+
AnnotationKind::Private => privacy = Privacy::Private,
2829
_ => return Err(self.unexpected_annotation(&annotation, Some("for struct"))),
2930
}
3031
}

Diff for: src/parser/parse_trait.rs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
2222
for annotation in annotations {
2323
match annotation.kind {
2424
AnnotationKind::Public => privacy = Privacy::Public,
25+
AnnotationKind::Private => privacy = Privacy::Private,
2526
_ => return Err(self.unexpected_annotation(&annotation, Some("for trait"))),
2627
}
2728
}

Diff for: src/resolve/type_ctx/find.rs

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ impl<'a> ResolveTypeCtx<'a> {
3131
self.types_in_modules
3232
.get(&self.module_fs_node_id)
3333
.and_then(|types_in_module| types_in_module.get(name))
34+
.filter(|ty_decl| {
35+
!ty_decl.privacy.is_private()
36+
|| self.file_fs_node_id == ty_decl.file_fs_node_id
37+
})
3438
})
3539
.filter(|local| local.num_parameters(self.asg) == type_args.len())
3640
.map(Ok)

Diff for: src/resolve/type_definition/prepare.rs

+41-10
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,55 @@ pub fn prepare_type_jobs(
2121
let mut type_jobs = Vec::with_capacity(ast_workspace.files.len());
2222

2323
for (physical_file_id, file) in ast_workspace.files.iter() {
24-
let module_fs_node_id = ast_workspace.get_owning_module_or_self(*physical_file_id);
24+
let physical_file_id = *physical_file_id;
25+
let module_fs_node_id = ast_workspace.get_owning_module_or_self(physical_file_id);
2526

2627
let mut job = TypeJob {
27-
physical_file_id: *physical_file_id,
28+
physical_file_id,
2829
type_aliases: Vec::with_capacity(file.type_aliases.len()),
2930
traits: Vec::with_capacity(file.traits.len()),
3031
structs: Vec::with_capacity(file.structs.len()),
3132
enums: Vec::with_capacity(file.enums.len()),
3233
};
3334

3435
for user_trait in file.traits.iter() {
35-
job.traits
36-
.push(prepare_trait(ctx, asg, module_fs_node_id, user_trait)?);
36+
job.traits.push(prepare_trait(
37+
ctx,
38+
asg,
39+
module_fs_node_id,
40+
physical_file_id,
41+
user_trait,
42+
)?);
3743
}
3844

3945
for structure in file.structs.iter() {
40-
job.structs
41-
.push(prepare_structure(ctx, asg, module_fs_node_id, structure)?);
46+
job.structs.push(prepare_structure(
47+
ctx,
48+
asg,
49+
module_fs_node_id,
50+
physical_file_id,
51+
structure,
52+
)?);
4253
}
4354

4455
for definition in file.enums.iter() {
45-
job.enums
46-
.push(prepare_enum(ctx, asg, module_fs_node_id, definition)?);
56+
job.enums.push(prepare_enum(
57+
ctx,
58+
asg,
59+
module_fs_node_id,
60+
physical_file_id,
61+
definition,
62+
)?);
4763
}
4864

4965
for definition in file.type_aliases.iter() {
50-
job.type_aliases
51-
.push(prepare_type_alias(ctx, asg, module_fs_node_id, definition)?);
66+
job.type_aliases.push(prepare_type_alias(
67+
ctx,
68+
asg,
69+
module_fs_node_id,
70+
physical_file_id,
71+
definition,
72+
)?);
5273
}
5374

5475
type_jobs.push(job);
@@ -61,6 +82,7 @@ fn prepare_structure(
6182
ctx: &mut ResolveCtx,
6283
asg: &mut Asg,
6384
module_fs_node_id: FsNodeId,
85+
physical_fs_node_id: FsNodeId,
6486
structure: &ast::Struct,
6587
) -> Result<StructRef, ResolveError> {
6688
let struct_ref = asg.structs.insert(asg::Struct {
@@ -80,6 +102,7 @@ fn prepare_structure(
80102
declare_type(
81103
ctx,
82104
module_fs_node_id,
105+
physical_fs_node_id,
83106
&structure.name,
84107
structure.source,
85108
structure.privacy,
@@ -97,6 +120,7 @@ fn prepare_enum(
97120
ctx: &mut ResolveCtx,
98121
asg: &mut Asg,
99122
module_fs_node_id: FsNodeId,
123+
physical_fs_node_id: FsNodeId,
100124
definition: &ast::Enum,
101125
) -> Result<EnumRef, ResolveError> {
102126
let enum_ref = asg.enums.insert(asg::Enum {
@@ -109,6 +133,7 @@ fn prepare_enum(
109133
declare_type(
110134
ctx,
111135
module_fs_node_id,
136+
physical_fs_node_id,
112137
&definition.name,
113138
definition.source,
114139
definition.privacy,
@@ -122,6 +147,7 @@ fn prepare_trait(
122147
ctx: &mut ResolveCtx,
123148
asg: &mut Asg,
124149
module_fs_node_id: FsNodeId,
150+
physical_fs_node_id: FsNodeId,
125151
definition: &ast::Trait,
126152
) -> Result<TraitRef, ResolveError> {
127153
let trait_ref = asg.traits.insert(asg::Trait {
@@ -140,6 +166,7 @@ fn prepare_trait(
140166
declare_type(
141167
ctx,
142168
module_fs_node_id,
169+
physical_fs_node_id,
143170
&definition.name,
144171
definition.source,
145172
definition.privacy,
@@ -153,6 +180,7 @@ fn prepare_type_alias(
153180
ctx: &mut ResolveCtx,
154181
asg: &mut Asg,
155182
module_fs_node_id: FsNodeId,
183+
physical_fs_node_id: FsNodeId,
156184
definition: &ast::TypeAlias,
157185
) -> Result<TypeAliasRef, ResolveError> {
158186
let type_alias_ref = asg.type_aliases.insert(asg::TypeAlias {
@@ -171,6 +199,7 @@ fn prepare_type_alias(
171199
declare_type(
172200
ctx,
173201
module_fs_node_id,
202+
physical_fs_node_id,
174203
&definition.name,
175204
definition.source,
176205
definition.privacy,
@@ -187,6 +216,7 @@ fn prepare_type_alias(
187216
fn declare_type(
188217
ctx: &mut ResolveCtx,
189218
module_fs_node_id: FsNodeId,
219+
physical_fs_node_id: FsNodeId,
190220
name: &str,
191221
source: Source,
192222
privacy: Privacy,
@@ -202,6 +232,7 @@ fn declare_type(
202232
kind,
203233
source,
204234
privacy,
235+
file_fs_node_id: physical_fs_node_id,
205236
},
206237
)
207238
.is_some()

Diff for: tests/error/cannot_use_private_type/_.adept

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
pragma => adept("3.0")
3+
4+
func main {
5+
name := Name { firstname: c"Isaac" }
6+
test()
7+
}

Diff for: tests/error/cannot_use_private_type/test.adept

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
#[foreign]
3+
func printf(format ptr#char, ...) int
4+
5+
#[private]
6+
struct Name (firstname ptr#char)
7+
8+
func test {
9+
name := Name { firstname: c"John" }
10+
name.ptr().greet()
11+
}
12+
13+
func greet(name ptr#Name) {
14+
printf(c"Hello %s\n", name.firstname)
15+
}

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

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
pragma => adept("3.0")
3+
4+
func main {
5+
test()
6+
}

Diff for: tests/success/private_type/test.adept

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
#[foreign]
3+
func printf(format ptr#char, ...) int
4+
5+
#[private]
6+
struct Name (firstname ptr#char)
7+
8+
func test {
9+
name := Name { firstname: c"John" }
10+
name.ptr().greet()
11+
}
12+
13+
func greet(name ptr#Name) {
14+
printf(c"Hello %s\n", name.firstname)
15+
}

0 commit comments

Comments
 (0)