Skip to content

Commit

Permalink
feat(compiler): Allow @externalName attribute for foreign names (#1060)
Browse files Browse the repository at this point in the history
  • Loading branch information
ospencer committed Dec 10, 2021
1 parent 80da3a9 commit f4c1311
Show file tree
Hide file tree
Showing 25 changed files with 325 additions and 111 deletions.
26 changes: 24 additions & 2 deletions compiler/grainformat/reformat.re
Original file line number Diff line number Diff line change
Expand Up @@ -1462,8 +1462,30 @@ and print_attributes = attributes =>
Doc.join(
Doc.space,
List.map(
(a: Location.loc(string)) =>
Doc.concat([Doc.text("@"), Doc.text(a.txt)]),
((a: Location.loc(string), args: list(Location.loc(string)))) => {
switch (args) {
| [] => Doc.concat([Doc.text("@"), Doc.text(a.txt)])
| _ =>
Doc.concat([
Doc.text("@"),
Doc.text(a.txt),
Doc.text("("),
Doc.join(
Doc.concat([Doc.comma, Doc.space]),
List.map(
(b: Location.loc(string)) =>
Doc.concat([
Doc.text("\""),
Doc.text(b.txt),
Doc.text("\""),
]),
args,
),
),
Doc.text(")"),
])
}
},
attributes,
),
),
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/codegen/compcore.re
Original file line number Diff line number Diff line change
Expand Up @@ -3409,7 +3409,7 @@ let compile_main = (wasm_mod, env, prog) => {

let compile_functions = (wasm_mod, env, {functions} as prog) => {
let handle_attrs = ({attrs} as func) =>
if (List.mem(Disable_gc, attrs)) {
if (List.mem(Typedtree.Disable_gc, attrs)) {
Config.preserve_config(() => {
Config.no_gc := true;
compile_function(wasm_mod, env, func);
Expand Down
6 changes: 1 addition & 5 deletions compiler/src/codegen/mashtree.re
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ type tag_type = Value_tags.tag_type;
type heap_tag_type = Value_tags.heap_tag_type;

[@deriving sexp]
type attributes = list(attribute)

[@deriving sexp]
and attribute =
| Disable_gc;
type attributes = Typedtree.attributes;

type grain_error = Runtime_errors.grain_error;
let (prim1_of_sexp, sexp_of_prim1) = (
Expand Down
13 changes: 1 addition & 12 deletions compiler/src/codegen/transl_anf.re
Original file line number Diff line number Diff line change
Expand Up @@ -666,17 +666,6 @@ let next_global = (~exported=false, id, ty) => {
Printf.sprintf("global_%d", ret);
};

let transl_attributes = attrs => {
List.map(
({txt}) =>
switch (txt) {
| "disableGC" => Disable_gc
| _ => failwith("impossible by well-formedness")
},
attrs,
);
};

let rec compile_comp = (~id=?, env, c) => {
let desc =
switch (c.comp_desc) {
Expand Down Expand Up @@ -825,7 +814,7 @@ let rec compile_comp = (~id=?, env, c) => {
env,
args,
body,
transl_attributes(c.comp_attributes),
c.comp_attributes,
c.comp_loc,
),
),
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/middle_end/anf_helper.re
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type str = loc(string);
type loc = Location.t;
type env = Env.t;
type ident = Ident.t;
type attributes = Asttypes.attributes;
type attributes = Typedtree.attributes;

let default_loc = Location.dummy_loc;
let default_env = Env.empty;
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/middle_end/anf_helper.rei
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type str = loc(string);
type loc = Location.t;
type env = Env.t;
type ident = Ident.t;
type attributes = Asttypes.attributes;
type attributes = Typedtree.attributes;

module Imm: {
let mk: (~loc: loc=?, ~env: env=?, imm_expression_desc) => imm_expression;
Expand Down
10 changes: 1 addition & 9 deletions compiler/src/middle_end/anf_utils.re
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,7 @@ module ClearLocationsArg: Anf_mapper.MapArgument = {

let leave_imm_expression = i => {...i, imm_loc: Location.dummy_loc};

let leave_comp_expression = c => {
...c,
comp_loc: Location.dummy_loc,
comp_attributes:
List.map(
attr => {...attr, Asttypes.loc: Location.dummy_loc},
c.comp_attributes,
),
};
let leave_comp_expression = c => {...c, comp_loc: Location.dummy_loc};

let leave_anf_expression = a => {...a, anf_loc: Location.dummy_loc};
};
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/middle_end/anftree.re
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type global_flag =
type loc('a) = Location.loc('a);

[@deriving sexp]
type attributes = Asttypes.attributes;
type attributes = Typedtree.attributes;

type analysis = ..;

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/middle_end/anftree.rei
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type global_flag =
type loc('a) = Location.loc('a);

[@deriving sexp]
type attributes = Asttypes.attributes;
type attributes = Typedtree.attributes;

[@deriving sexp]
type partial = Typedtree.partial = | Partial | Total;
Expand Down
13 changes: 11 additions & 2 deletions compiler/src/middle_end/linearize.re
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,15 @@ let rec transl_anf_statement =
| Exported => Global
| Nonexported => Nonglobal
};
let external_name =
List.fold_left(
name =>
fun
| External_name(name) => name
| _ => name,
desc.tvd_name.txt,
attributes,
);
switch (desc.tvd_desc.ctyp_type.desc) {
| TTyArrow(_) =>
let (argsty, retty) =
Expand All @@ -1438,7 +1447,7 @@ let rec transl_anf_statement =
~global,
desc.tvd_id,
desc.tvd_mod.txt,
desc.tvd_name.txt,
external_name,
FunctionShape(argsty, retty),
),
],
Expand All @@ -1452,7 +1461,7 @@ let rec transl_anf_statement =
~global,
desc.tvd_id,
desc.tvd_mod.txt,
desc.tvd_name.txt,
external_name,
GlobalShape(ty),
),
],
Expand Down
16 changes: 14 additions & 2 deletions compiler/src/parsing/ast_iterator.re
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ module Cnst = {
module E = {
let iter = (sub, {pexp_desc: desc, pexp_attributes: attrs, pexp_loc: loc}) => {
sub.location(sub, loc);
List.iter(iter_loc(sub), attrs);
List.iter(
((attr, args)) => {
iter_loc(sub, attr);
List.iter(iter_loc(sub), args);
},
attrs,
);
switch (desc) {
| PExpId(i) => iter_loc(sub, i)
| PExpConstant(c) => sub.constant(sub, c)
Expand Down Expand Up @@ -288,7 +294,13 @@ module VD = {
module TL = {
let iter = (sub, {ptop_desc: desc, ptop_attributes: attrs, ptop_loc: loc}) => {
sub.location(sub, loc);
List.iter(iter_loc(sub), attrs);
List.iter(
((attr, args)) => {
iter_loc(sub, attr);
List.iter(iter_loc(sub), args);
},
attrs,
);
switch (desc) {
| PTopImport(id) => sub.import(sub, id)
| PTopExport(ex) => sub.export(sub, ex)
Expand Down
14 changes: 12 additions & 2 deletions compiler/src/parsing/ast_mapper.re
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ module E = {
let map = (sub, {pexp_desc: desc, pexp_attributes: attrs, pexp_loc: loc}) => {
open Exp;
let loc = sub.location(sub, loc);
let attributes = List.map(map_loc(sub), attrs);
let attributes =
List.map(
((attr, args)) =>
(map_loc(sub, attr), List.map(map_loc(sub), args)),
attrs,
);
switch (desc) {
| PExpId(i) => ident(~loc, ~attributes, map_loc(sub, i))
| PExpConstant(c) => constant(~loc, ~attributes, sub.constant(sub, c))
Expand Down Expand Up @@ -336,7 +341,12 @@ module TL = {
let map = (sub, {ptop_desc: desc, ptop_attributes: attrs, ptop_loc: loc}) => {
open Top;
let loc = sub.location(sub, loc);
let attributes = List.map(map_loc(sub), attrs);
let attributes =
List.map(
((attr, args)) =>
(map_loc(sub, attr), List.map(map_loc(sub), args)),
attrs,
);
switch (desc) {
| PTopImport(decls) =>
Top.import(~loc, ~attributes, sub.import(sub, decls))
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/parsing/asttypes.re
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,4 @@ let mknoloc = Location.mknoloc;

/** Addtional expression information that may affect compilation. */
[@deriving (sexp, yojson)]
type attributes = list(loc(string));
type attributes = list((loc(string), list(loc(string))));
4 changes: 2 additions & 2 deletions compiler/src/parsing/parser.dyp
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ lam_expr :
| ID thickarrow block_or_expr { Exp.lambda ~loc:(symbol_rloc dyp) [Pat.var ~loc:(rhs_loc dyp 1) (mkstr dyp $1)] $3 }

attributes :
| [AT id_str eols? { $2 }]* { $1 }
| [AT id_str [lparen STRING rparen { Location.mkloc $2 (symbol_rloc dyp) }]? eols? { $2, Option.to_list $3 }]* { $1 }

let_expr :
| attributes LET REC value_binds { Exp.let_ ~loc:(symbol_rloc dyp) ~attributes:$1 Recursive Immutable $4 }
Expand Down Expand Up @@ -570,9 +570,9 @@ toplevel_stmt :
| attributes LET value_binds { Top.let_ ~loc:(symbol_rloc dyp) ~attributes:$1 Nonexported Nonrecursive Immutable $3 }
| attributes LET REC MUT value_binds { Top.let_ ~loc:(symbol_rloc dyp) ~attributes:$1 Nonexported Recursive Mutable $5 }
| attributes LET MUT value_binds { Top.let_ ~loc:(symbol_rloc dyp) ~attributes:$1 Nonexported Nonrecursive Mutable $4 }
| attributes IMPORT foreign_stmt { Top.foreign ~loc:(symbol_rloc dyp) ~attributes:$1 Nonexported $3 }
| expr { Top.expr ~loc:(symbol_rloc dyp) $1 }
| import_stmt { Top.import ~loc:(symbol_rloc dyp) $1 }
| IMPORT foreign_stmt { Top.foreign ~loc:(symbol_rloc dyp) Nonexported $2 }
| data_declaration_stmts { Top.data ~loc:(symbol_rloc dyp) $1 }
| export_stmt { $1 }
| primitive_stmt { Top.primitive ~loc:(symbol_rloc dyp) Nonexported $1 }
Expand Down
Loading

0 comments on commit f4c1311

Please sign in to comment.