Skip to content

Commit d086aae

Browse files
committed
[merge] Support opaque resources
Fixes #360 * modules: [tests] Add a test for invalid use of opaque resources [compiler] Write opaque resources to interface files [compiler] Handle opaque resources in interfaces [compiler/parse] Update an old comment in the parser [docs] Add opaque keyword to resource definitions [tests] Add a test for opaque resource syntax [compiler/ast] Resources use the sharing_opaque type [compiler/parse] Refactor how exports are parsed [compiler] Rename sharing_type to sharing_opaque
2 parents 74181ed + 1d04d08 commit d086aae

20 files changed

+259
-105
lines changed

docs/plasma_ref.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ a time.
649649
== Resources
650650

651651
----
652-
ResourceDefinition := 'export'? 'resource' Ident 'from' QualifiedIdent
652+
ResourceDefinition := ('export' 'opaque'?)? 'resource' Ident 'from' QualifiedIdent
653653
----
654654

655655
This defines a new resource. The resource has the given name and is a

src/ast.m

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@
4444
; ast_pragma(ast_pragma).
4545

4646
:- type ast_interface_entry
47-
---> asti_resource(q_name, ast_resource)
47+
---> asti_resource(
48+
q_name,
49+
50+
% Opaque resources won't have a definition.
51+
maybe(ast_resource)
52+
)
4853
; asti_type(q_name, ast_type(q_name))
4954
; asti_function(q_name, ast_function_decl).
5055

@@ -63,7 +68,7 @@
6368
---> ast_type(
6469
at_params :: list(string),
6570
at_costructors :: list(at_constructor(Name)),
66-
at_export :: sharing_type,
71+
at_export :: sharing_opaque,
6772
at_context :: context
6873
)
6974
% An abstractly-imported type.
@@ -77,7 +82,7 @@
7782
:- type ast_resource
7883
---> ast_resource(
7984
ar_from :: q_name,
80-
ar_sharing :: sharing,
85+
ar_sharing :: sharing_opaque,
8186
ar_context :: context
8287
).
8388

src/builtins.m

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@
212212
MaybeName = nq_name_det("Maybe"),
213213
core_set_type(MaybeType,
214214
type_init(q_name_append(builtin_module_name, MaybeName),
215-
[MaybeParamName], [NoneId, SomeId], st_private, i_imported,
215+
[MaybeParamName], [NoneId, SomeId], so_private, i_imported,
216216
builtin_context),
217217
!Core),
218218
root_name(MaybeName, bi_type(MaybeType, arity(1)), !Map).
@@ -291,7 +291,7 @@
291291
BoolName = nq_name_det("Bool"),
292292
core_set_type(BoolId,
293293
type_init(q_name_append(builtin_module_name, BoolName), [],
294-
[FalseId, TrueId], st_private, i_imported, builtin_context),
294+
[FalseId, TrueId], so_private, i_imported, builtin_context),
295295
!Core),
296296
root_name(BoolName, bi_type(BoolId, arity(0)), !Map),
297297

@@ -472,7 +472,7 @@
472472

473473
core_set_type(ListId,
474474
type_init(q_name_append_str(builtin_module_name, "List"), [T],
475-
[NilId, ConsId], st_private, i_imported, builtin_context),
475+
[NilId, ConsId], so_private, i_imported, builtin_context),
476476
!Core),
477477

478478
root_name(nq_name_det("List"), bi_type(ListId, arity(1)), !Map).
@@ -515,7 +515,7 @@
515515
IOResultName = nq_name_det("IOResult"),
516516
core_set_type(IOResultType,
517517
type_init(q_name_append(builtin_module_name, IOResultName),
518-
[OkParamName], [OkId, EOFId], st_private, i_imported, builtin_context),
518+
[OkParamName], [OkId, EOFId], so_private, i_imported, builtin_context),
519519
!Core),
520520
root_name(IOResultName, bi_type(IOResultType, arity(1)), !Map),
521521

@@ -558,7 +558,7 @@
558558
EnvironmentName = nq_name_det("Environment"),
559559
EnvironmentQName = q_name_append(builtin_module_name, EnvironmentName),
560560
register_builtin_resource(EnvironmentName,
561-
r_other(EnvironmentQName, RIO, s_private, i_imported, builtin_context),
561+
r_other(EnvironmentQName, RIO, so_private, i_imported, builtin_context),
562562
REnv, !Map, !Core),
563563
SetenvName = q_name_append_str(builtin_module_name, "setenv"),
564564
register_builtin_func_root(nq_name_det("setenv"),
@@ -571,7 +571,7 @@
571571
TimeName = nq_name_det("Time"),
572572
TimeQName = q_name_append(builtin_module_name, TimeName),
573573
register_builtin_resource(TimeName,
574-
r_other(TimeQName, RIO, s_private, i_imported, builtin_context),
574+
r_other(TimeQName, RIO, so_private, i_imported, builtin_context),
575575
RTime, !Map, !Core),
576576
GettimeofdayName = q_name_append_str(builtin_module_name, "gettimeofday"),
577577
register_builtin_func_builtin(nq_name_det("gettimeofday"),
@@ -615,7 +615,7 @@
615615
core_set_type(CodepointCategoryId,
616616
type_init(q_name_append(builtin_module_name,
617617
CodepointCategoryTypeName), [],
618-
[WhitespaceId, OtherId], st_private, i_imported, builtin_context),
618+
[WhitespaceId, OtherId], so_private, i_imported, builtin_context),
619619
!Core),
620620
root_name(CodepointCategoryTypeName,
621621
bi_type(CodepointCategoryId, arity(0)), !Map),

src/common_types.m

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
---> s_public
2323
; s_private.
2424

25-
% Types have a 3rd option, to export the type name but not its details
26-
% (constructors & fields).
25+
% Types and resources have a 3rd option, to export the name but not its
26+
% details.
2727
%
28-
:- type sharing_type
29-
---> st_private
30-
; st_public
31-
; st_public_opaque.
28+
:- type sharing_opaque
29+
---> so_private
30+
; so_public
31+
; so_public_opaque.
3232

3333
% Is an exported function an entrypoint.
3434
%

src/compile.m

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,12 @@
298298
( if
299299
env_add_resource(q_name(Name), ResId, !Env),
300300
Sharing = Res ^ ar_sharing,
301-
( Sharing = s_public,
301+
( ( Sharing = so_public
302+
; Sharing = so_public_opaque
303+
),
302304
env_add_resource(q_name_append(ModuleName, Name), ResId,
303305
!ImportEnv)
304-
; Sharing = s_private
306+
; Sharing = so_private
305307
)
306308
then
307309
true
@@ -320,12 +322,12 @@
320322
env_add_type(q_name(Name), Arity, TypeId, !Env),
321323
Sharing = Type ^ at_export,
322324
(
323-
( Sharing = st_public
324-
; Sharing = st_public_opaque
325+
( Sharing = so_public
326+
; Sharing = so_public_opaque
325327
),
326328
env_add_type(q_name_append(ModuleName, Name), Arity, TypeId,
327329
!ImportEnv)
328-
; Sharing = st_private
330+
; Sharing = so_private
329331
)
330332
then
331333
true
@@ -344,15 +346,18 @@
344346
filter_entries(Entries, _, Resources0, Types0, _, _),
345347

346348
filter_map((pred(NamedRes::in, Name::out) is semidet :-
347-
NamedRes = nq_named(NQName, ast_resource(_, s_public, _)),
349+
NamedRes = nq_named(NQName, ast_resource(_, Sharing, _)),
350+
( Sharing = so_public
351+
; Sharing = so_public_opaque
352+
),
348353
Name = q_name_append(ModuleName, NQName)
349354
),
350355
Resources0, Resources),
351356

352357
filter_map((pred(NamedRes::in, {Name, Arity}::out) is semidet :-
353358
NamedRes = nq_named(NQName, ast_type(Params, _, Sharing, _)),
354-
( Sharing = st_public
355-
; Sharing = st_public_opaque
359+
( Sharing = so_public
360+
; Sharing = so_public_opaque
356361
),
357362
Name = q_name_append(ModuleName, NQName),
358363
Arity = arity(length(Params))

src/core.m

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,7 @@
280280

281281
type_is_exported(_ - Type) :-
282282
Sharing = utype_get_sharing(Type),
283-
( Sharing = st_public
284-
; Sharing = st_public_opaque
285-
).
283+
sharing_is_exported(Sharing).
286284

287285
core_get_type(Core, TypeId) = Type :-
288286
lookup(Core ^ c_types, TypeId, Type).
@@ -334,6 +332,12 @@
334332

335333
:- pred resource_is_exported(pair(resource_id, resource)::in) is semidet.
336334

337-
resource_is_exported(_ - r_other(_, _, s_public, _, _)).
335+
resource_is_exported(_ - r_other(_, _, Sharing, _, _)) :-
336+
sharing_is_exported(Sharing).
337+
338+
:- pred sharing_is_exported(sharing_opaque::in) is semidet.
339+
340+
sharing_is_exported(so_public).
341+
sharing_is_exported(so_public_opaque).
338342

339343
%-----------------------------------------------------------------------%

src/core.pretty.m

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
% we probably should parameterise it and other pretty-printer functions
3838
% here.
3939
%
40-
:- func type_decl_pretty(core, user_type) = pretty.
40+
:- func type_interface_pretty(core, user_type) = pretty.
4141

4242
% Print the argument parts of a function type. You can either put
4343
% "func" in front of this or the name of the variable at a call site.
@@ -61,7 +61,7 @@
6161

6262
% Pretty print a resource definition.
6363
%
64-
:- func resource_decl_pretty(core, resource) = pretty.
64+
:- func resource_interface_pretty(core, resource) = pretty.
6565

6666
:- func constructor_name_pretty(core, set(ctor_id)) = pretty.
6767

@@ -88,11 +88,11 @@
8888

8989
%-----------------------------------------------------------------------%
9090

91-
type_decl_pretty(Core, Type) = p_expr(Pretty) :-
91+
type_interface_pretty(Core, Type) = p_expr(Pretty) :-
9292
Sharing = utype_get_sharing(Type),
93-
( Sharing = st_private,
93+
( Sharing = so_private,
9494
unexpected($file, $pred, "st_private")
95-
; Sharing = st_public,
95+
; Sharing = so_public,
9696
MaybeParams = utype_get_params(Type),
9797
( MaybeParams = yes(Params)
9898
; MaybeParams = no,
@@ -112,7 +112,7 @@
112112
unexpected($file, $pred, "Public type without constructors")
113113
),
114114
Pretty = PrettyHead ++ PrettyBody
115-
; Sharing = st_public_opaque,
115+
; Sharing = so_public_opaque,
116116
Pretty = [p_str("type "),
117117
q_name_pretty(utype_get_name(Type)),
118118
p_str("/"),
@@ -405,11 +405,18 @@
405405
resource_pretty(Core, ResId) =
406406
p_str(resource_to_string(core_get_resource(Core, ResId))).
407407

408-
resource_decl_pretty(_, r_io) = unexpected($file, $pred, "IO").
409-
resource_decl_pretty(Core, r_other(Name, From, _, _, _)) =
410-
p_expr([p_str("resource"), p_spc, q_name_pretty(Name),
411-
p_spc, p_str("from"), p_spc, resource_pretty(Core, From)]).
412-
resource_decl_pretty(_, r_abstract(Name)) =
408+
resource_interface_pretty(_, r_io) = unexpected($file, $pred, "IO").
409+
resource_interface_pretty(Core, r_other(Name, From, Sharing, _, _)) = Pretty :-
410+
( Sharing = so_public,
411+
Pretty = p_expr([p_str("resource"), p_spc, q_name_pretty(Name),
412+
p_spc, p_str("from"), p_spc, resource_pretty(Core, From)])
413+
; Sharing = so_public_opaque,
414+
Pretty = p_expr([p_str("resource"), p_spc, q_name_pretty(Name)])
415+
; Sharing = so_private,
416+
Pretty = p_empty
417+
).
418+
419+
resource_interface_pretty(_, r_abstract(Name)) =
413420
p_expr([p_str("resource"), p_spc, q_name_pretty(Name)]).
414421

415422
%-----------------------------------------------------------------------%

src/core.resource.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
; r_other(
2323
ro_name :: q_name,
2424
ro_from :: resource_id,
25-
ro_sharing :: sharing,
25+
ro_sharing :: sharing_opaque,
2626
ro_imported :: imported,
2727
ro_context :: context
2828
)

src/core.types.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161

6262
:- type user_type.
6363

64-
:- func type_init(q_name, list(string), list(ctor_id), sharing_type,
64+
:- func type_init(q_name, list(string), list(ctor_id), sharing_opaque,
6565
imported, context) = user_type.
6666

6767
:- func type_init_abstract(q_name, arity, context) = user_type.
@@ -72,7 +72,7 @@
7272

7373
:- func utype_get_ctors(user_type) = maybe(list(ctor_id)).
7474

75-
:- func utype_get_sharing(user_type) = sharing_type.
75+
:- func utype_get_sharing(user_type) = sharing_opaque.
7676

7777
:- func utype_get_imported(user_type) = imported.
7878

@@ -162,7 +162,7 @@
162162
t_symbol :: q_name,
163163
t_params :: list(string),
164164
t_ctors :: list(ctor_id),
165-
t_sharing :: sharing_type,
165+
t_sharing :: sharing_opaque,
166166
t_imported :: imported,
167167
t_context :: context
168168
)
@@ -191,7 +191,7 @@
191191
).
192192

193193
utype_get_sharing(user_type(_, _, _, Sharing, _, _)) = Sharing.
194-
utype_get_sharing(abstract_type(_, _, _)) = st_private.
194+
utype_get_sharing(abstract_type(_, _, _)) = so_private.
195195

196196
utype_get_imported(user_type(_, _, _, _, Imported, _)) = Imported.
197197
utype_get_imported(abstract_type(_, _, _)) = i_imported.

0 commit comments

Comments
 (0)