Skip to content

Commit

Permalink
exhauce PR#6367: introduce Asttypes.arg_label to encode labelled argu…
Browse files Browse the repository at this point in the history
…ments

git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@15737 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
  • Loading branch information
Jacques Garrigue committed Dec 22, 2014
1 parent c0de696 commit 1584803
Show file tree
Hide file tree
Showing 35 changed files with 245 additions and 219 deletions.
3 changes: 3 additions & 0 deletions Changes
Expand Up @@ -59,6 +59,9 @@ Bug fixes:
- PR#6650: Cty_constr not handled correctly by Subst
- PR#6651: Failing component lookup

Features wishes:
- PR#6367: introduce Asttypes.arg_label to encode labelled arguments

OCaml 4.02.2:
-------------

Expand Down
Binary file modified boot/ocamlc
Binary file not shown.
Binary file modified boot/ocamldep
Binary file not shown.
Binary file modified boot/ocamllex
Binary file not shown.
2 changes: 1 addition & 1 deletion bytecomp/translcore.mli
Expand Up @@ -18,7 +18,7 @@ open Typedtree
open Lambda

val transl_exp: expression -> lambda
val transl_apply: lambda -> (label * expression option * optional) list
val transl_apply: lambda -> (arg_label * expression option * optional) list
-> Location.t -> lambda
val transl_let: rec_flag -> value_binding list -> lambda -> lambda
val transl_primitive: Location.t -> Primitive.description -> Env.t
Expand Down
4 changes: 2 additions & 2 deletions ocamldoc/odoc_info.mli
Expand Up @@ -780,11 +780,11 @@ val create_index_lists : 'a list -> ('a -> string) -> 'a list list
val remove_option : Types.type_expr -> Types.type_expr

(** Return [true] if the given label is optional.*)
val is_optional : string -> bool
val is_optional : Asttypes.arg_label -> bool

(** Return the label name for the given label,
i.e. removes the beginning '?' if present.*)
val label_name : string -> string
val label_name : Asttypes.arg_label -> string

(** Return the given name where the module name or
part of it was removed, according to the list of modules
Expand Down
4 changes: 2 additions & 2 deletions ocamldoc/odoc_misc.mli
Expand Up @@ -107,8 +107,8 @@ val search_string_backward : pat: string -> s: string -> int
val remove_option : Types.type_expr -> Types.type_expr

(** Return [true] if the given label is optional.*)
val is_optional : string -> bool
val is_optional : Asttypes.arg_label -> bool

(** Return the label name for the given label,
i.e. removes the beginning '?' if present.*)
val label_name : string -> string
val label_name : Asttypes.arg_label -> string
4 changes: 2 additions & 2 deletions ocamldoc/odoc_str.ml
Expand Up @@ -148,8 +148,8 @@ let string_of_class_params c =
Printf.bprintf b "%s%s%s%s -> "
(
match label with
"" -> ""
| s -> s^":"
Asttypes.Nolabel -> ""
| s -> Printtyp.string_of_label s ^":"
)
(if parent then "(" else "")
(Odoc_print.string_of_type_expr
Expand Down
14 changes: 4 additions & 10 deletions ocamldoc/odoc_value.ml
Expand Up @@ -95,22 +95,16 @@ let parameter_list_from_arrows typ =
so there is nothing to merge. With this dummy list we can merge the
parameter names from the .ml and the type from the .mli file. *)
let dummy_parameter_list typ =
let normal_name s =
match s with
"" -> s
| _ ->
match s.[0] with
'?' -> String.sub s 1 ((String.length s) - 1)
| _ -> s
in
let normal_name = Odoc_misc.label_name in
Printtyp.mark_loops typ;
let liste_param = parameter_list_from_arrows typ in
let rec iter (label, t) =
match t.Types.desc with
| Types.Ttuple l ->
if label = "" then
let open Asttypes in
if label = Nolabel then
Odoc_parameter.Tuple
(List.map (fun t2 -> iter ("", t2)) l, t)
(List.map (fun t2 -> iter (Nolabel, t2)) l, t)
else
(* if there is a label, then we don't want to decompose the tuple *)
Odoc_parameter.Simple_name
Expand Down
17 changes: 10 additions & 7 deletions parsing/ast_helper.mli
Expand Up @@ -38,7 +38,7 @@ module Typ :

val any: ?loc:loc -> ?attrs:attrs -> unit -> core_type
val var: ?loc:loc -> ?attrs:attrs -> string -> core_type
val arrow: ?loc:loc -> ?attrs:attrs -> label -> core_type -> core_type
val arrow: ?loc:loc -> ?attrs:attrs -> arg_label -> core_type -> core_type
-> core_type
val tuple: ?loc:loc -> ?attrs:attrs -> core_type list -> core_type
val constr: ?loc:loc -> ?attrs:attrs -> lid -> core_type list -> core_type
Expand Down Expand Up @@ -93,11 +93,11 @@ module Exp:
val constant: ?loc:loc -> ?attrs:attrs -> constant -> expression
val let_: ?loc:loc -> ?attrs:attrs -> rec_flag -> value_binding list
-> expression -> expression
val fun_: ?loc:loc -> ?attrs:attrs -> label -> expression option -> pattern
val fun_: ?loc:loc -> ?attrs:attrs -> arg_label -> expression option -> pattern
-> expression -> expression
val function_: ?loc:loc -> ?attrs:attrs -> case list -> expression
val apply: ?loc:loc -> ?attrs:attrs -> expression
-> (label * expression) list -> expression
-> (arg_label * expression) list -> expression
val match_: ?loc:loc -> ?attrs:attrs -> expression -> case list
-> expression
val try_: ?loc:loc -> ?attrs:attrs -> expression -> case list -> expression
Expand Down Expand Up @@ -293,7 +293,7 @@ module Cty:

val constr: ?loc:loc -> ?attrs:attrs -> lid -> core_type list -> class_type
val signature: ?loc:loc -> ?attrs:attrs -> class_signature -> class_type
val arrow: ?loc:loc -> ?attrs:attrs -> label -> core_type -> class_type -> class_type
val arrow: ?loc:loc -> ?attrs:attrs -> arg_label -> core_type -> class_type -> class_type
val extension: ?loc:loc -> ?attrs:attrs -> extension -> class_type
end

Expand All @@ -319,9 +319,12 @@ module Cl:

val constr: ?loc:loc -> ?attrs:attrs -> lid -> core_type list -> class_expr
val structure: ?loc:loc -> ?attrs:attrs -> class_structure -> class_expr
val fun_: ?loc:loc -> ?attrs:attrs -> label -> expression option -> pattern -> class_expr -> class_expr
val apply: ?loc:loc -> ?attrs:attrs -> class_expr -> (label * expression) list -> class_expr
val let_: ?loc:loc -> ?attrs:attrs -> rec_flag -> value_binding list -> class_expr -> class_expr
val fun_: ?loc:loc -> ?attrs:attrs -> arg_label -> expression option -> pattern ->
class_expr -> class_expr
val apply:
?loc:loc -> ?attrs:attrs -> class_expr -> (arg_label * expression) list -> class_expr
val let_:
?loc:loc -> ?attrs:attrs -> rec_flag -> value_binding list -> class_expr -> class_expr
val constraint_: ?loc:loc -> ?attrs:attrs -> class_expr -> class_type -> class_expr
val extension: ?loc:loc -> ?attrs:attrs -> extension -> class_expr
end
Expand Down
5 changes: 5 additions & 0 deletions parsing/asttypes.mli
Expand Up @@ -37,6 +37,11 @@ type closed_flag = Closed | Open

type label = string

type arg_label =
Nolabel
| Labelled of string (* label:T -> ... *)
| Optional of string (* ?label:T -> ... *)

type 'a loc = 'a Location.loc = {
txt : 'a;
loc : Location.t;
Expand Down
80 changes: 40 additions & 40 deletions parsing/parser.mly
Expand Up @@ -73,7 +73,7 @@ let ghunit () =
ghexp (Pexp_construct (mknoloc (Lident "()"), None))

let mkinfix arg1 name arg2 =
mkexp(Pexp_apply(mkoperator name 2, ["", arg1; "", arg2]))
mkexp(Pexp_apply(mkoperator name 2, [Nolabel, arg1; Nolabel, arg2]))

let neg_float_string f =
if String.length f > 0 && f.[0] = '-'
Expand All @@ -93,7 +93,7 @@ let mkuminus name arg =
| ("-" | "-."), Pexp_constant(Const_float f) ->
mkexp(Pexp_constant(Const_float(neg_float_string f)))
| _ ->
mkexp(Pexp_apply(mkoperator ("~" ^ name) 1, ["", arg]))
mkexp(Pexp_apply(mkoperator ("~" ^ name) 1, [Nolabel, arg]))

let mkuplus name arg =
let desc = arg.pexp_desc in
Expand All @@ -104,7 +104,7 @@ let mkuplus name arg =
| "+", Pexp_constant(Const_nativeint _)
| ("+" | "+."), Pexp_constant(Const_float _) -> mkexp desc
| _ ->
mkexp(Pexp_apply(mkoperator ("~" ^ name) 1, ["", arg]))
mkexp(Pexp_apply(mkoperator ("~" ^ name) 1, [Nolabel, arg]))

let mkexp_cons consloc args loc =
Exp.mk ~loc (Pexp_construct(mkloc (Lident "::") consloc, Some args))
Expand Down Expand Up @@ -186,34 +186,34 @@ let bigarray_get arr arg =
match bigarray_untuplify arg with
[c1] ->
mkexp(Pexp_apply(ghexp(Pexp_ident(get 1)),
["", arr; "", c1]))
[Nolabel, arr; Nolabel, c1]))
| [c1;c2] ->
mkexp(Pexp_apply(ghexp(Pexp_ident(get 2)),
["", arr; "", c1; "", c2]))
[Nolabel, arr; Nolabel, c1; Nolabel, c2]))
| [c1;c2;c3] ->
mkexp(Pexp_apply(ghexp(Pexp_ident(get 3)),
["", arr; "", c1; "", c2; "", c3]))
[Nolabel, arr; Nolabel, c1; Nolabel, c2; Nolabel, c3]))
| coords ->
mkexp(Pexp_apply(ghexp(Pexp_ident(get 0)),
["", arr; "", ghexp(Pexp_array coords)]))
[Nolabel, arr; Nolabel, ghexp(Pexp_array coords)]))

let bigarray_set arr arg newval =
let set order = bigarray_function order true in
match bigarray_untuplify arg with
[c1] ->
mkexp(Pexp_apply(ghexp(Pexp_ident(set 1)),
["", arr; "", c1; "", newval]))
[Nolabel, arr; Nolabel, c1; Nolabel, newval]))
| [c1;c2] ->
mkexp(Pexp_apply(ghexp(Pexp_ident(set 2)),
["", arr; "", c1; "", c2; "", newval]))
[Nolabel, arr; Nolabel, c1; Nolabel, c2; Nolabel, newval]))
| [c1;c2;c3] ->
mkexp(Pexp_apply(ghexp(Pexp_ident(set 3)),
["", arr; "", c1; "", c2; "", c3; "", newval]))
[Nolabel, arr; Nolabel, c1; Nolabel, c2; Nolabel, c3; Nolabel, newval]))
| coords ->
mkexp(Pexp_apply(ghexp(Pexp_ident(set 0)),
["", arr;
"", ghexp(Pexp_array coords);
"", newval]))
[Nolabel, arr;
Nolabel, ghexp(Pexp_array coords);
Nolabel, newval]))

let lapply p1 p2 =
if !Clflags.applicative_functors
Expand Down Expand Up @@ -948,13 +948,13 @@ class_type:
{ $1 }
| QUESTION LIDENT COLON simple_core_type_or_tuple_no_attr MINUSGREATER
class_type
{ mkcty(Pcty_arrow("?" ^ $2 , mkoption $4, $6)) }
{ mkcty(Pcty_arrow(Optional $2 , mkoption $4, $6)) }
| OPTLABEL simple_core_type_or_tuple_no_attr MINUSGREATER class_type
{ mkcty(Pcty_arrow("?" ^ $1, mkoption $2, $4)) }
{ mkcty(Pcty_arrow(Optional $1, mkoption $2, $4)) }
| LIDENT COLON simple_core_type_or_tuple_no_attr MINUSGREATER class_type
{ mkcty(Pcty_arrow($1, $3, $5)) }
{ mkcty(Pcty_arrow(Labelled $1, $3, $5)) }
| simple_core_type_or_tuple_no_attr MINUSGREATER class_type
{ mkcty(Pcty_arrow("", $1, $3)) }
{ mkcty(Pcty_arrow(Nolabel, $1, $3)) }
;
class_signature:
LBRACKET core_type_comma_list RBRACKET clty_longident
Expand Down Expand Up @@ -1051,21 +1051,21 @@ seq_expr:
;
labeled_simple_pattern:
QUESTION LPAREN label_let_pattern opt_default RPAREN
{ ("?" ^ fst $3, $4, snd $3) }
{ (Optional (fst $3), $4, snd $3) }
| QUESTION label_var
{ ("?" ^ fst $2, None, snd $2) }
{ (Optional (fst $2), None, snd $2) }
| OPTLABEL LPAREN let_pattern opt_default RPAREN
{ ("?" ^ $1, $4, $3) }
{ (Optional $1, $4, $3) }
| OPTLABEL pattern_var
{ ("?" ^ $1, None, $2) }
{ (Optional $1, None, $2) }
| TILDE LPAREN label_let_pattern RPAREN
{ (fst $3, None, snd $3) }
{ (Labelled (fst $3), None, snd $3) }
| TILDE label_var
{ (fst $2, None, snd $2) }
{ (Labelled (fst $2), None, snd $2) }
| LABEL simple_pattern
{ ($1, None, $2) }
{ (Labelled $1, None, $2) }
| simple_pattern
{ ("", None, $1) }
{ (Nolabel, None, $1) }
;
pattern_var:
LIDENT { mkpat(Ppat_var (mkrhs $1 1)) }
Expand Down Expand Up @@ -1181,10 +1181,10 @@ expr:
{ mkexp(Pexp_setfield($1, mkrhs $3 3, $5)) }
| simple_expr DOT LPAREN seq_expr RPAREN LESSMINUS expr
{ mkexp(Pexp_apply(ghexp(Pexp_ident(array_function ".()" true)),
["",$1; "",$4; "",$7])) }
[Nolabel,$1; Nolabel,$4; Nolabel,$7])) }
| simple_expr DOT LBRACKET seq_expr RBRACKET LESSMINUS expr
{ mkexp(Pexp_apply(ghexp(Pexp_ident(array_function ".[]" true)),
["",$1; "",$4; "",$7])) }
[Nolabel,$1; Nolabel,$4; Nolabel,$7])) }
| simple_expr DOT LBRACE expr RBRACE LESSMINUS expr
{ bigarray_set $1 $4 $7 }
| label LESSMINUS expr
Expand Down Expand Up @@ -1230,12 +1230,12 @@ simple_expr:
{ unclosed "(" 3 ")" 5 }
| simple_expr DOT LPAREN seq_expr RPAREN
{ mkexp(Pexp_apply(ghexp(Pexp_ident(array_function ".()" false)),
["",$1; "",$4])) }
[Nolabel,$1; Nolabel,$4])) }
| simple_expr DOT LPAREN seq_expr error
{ unclosed "(" 3 ")" 5 }
| simple_expr DOT LBRACKET seq_expr RBRACKET
{ mkexp(Pexp_apply(ghexp(Pexp_ident(array_function ".[]" false)),
["",$1; "",$4])) }
[Nolabel,$1; Nolabel,$4])) }
| simple_expr DOT LBRACKET seq_expr error
{ unclosed "[" 3 "]" 5 }
| simple_expr DOT LBRACE expr RBRACE
Expand Down Expand Up @@ -1272,9 +1272,9 @@ simple_expr:
| mod_longident DOT LBRACKET expr_semi_list opt_semi error
{ unclosed "[" 3 "]" 6 }
| PREFIXOP simple_expr
{ mkexp(Pexp_apply(mkoperator $1 1, ["",$2])) }
{ mkexp(Pexp_apply(mkoperator $1 1, [Nolabel,$2])) }
| BANG simple_expr
{ mkexp(Pexp_apply(mkoperator "!" 1, ["",$2])) }
{ mkexp(Pexp_apply(mkoperator "!" 1, [Nolabel,$2])) }
| NEW ext_attributes class_longident
{ mkexp_attrs (Pexp_new(mkrhs $3 3)) $2 }
| LBRACELESS field_expr_list GREATERRBRACE
Expand Down Expand Up @@ -1313,19 +1313,19 @@ simple_labeled_expr_list:
;
labeled_simple_expr:
simple_expr %prec below_SHARP
{ ("", $1) }
{ (Nolabel, $1) }
| label_expr
{ $1 }
;
label_expr:
LABEL simple_expr %prec below_SHARP
{ ($1, $2) }
{ (Labelled $1, $2) }
| TILDE label_ident
{ $2 }
{ (Labelled (fst $2), snd $2) }
| QUESTION label_ident
{ ("?" ^ fst $2, snd $2) }
{ (Optional (fst $2), snd $2) }
| OPTLABEL simple_expr %prec below_SHARP
{ ("?" ^ $1, $2) }
{ (Optional $1, $2) }
;
label_ident:
LIDENT { ($1, mkexp(Pexp_ident(mkrhs (Lident $1) 1))) }
Expand Down Expand Up @@ -1794,13 +1794,13 @@ core_type2:
simple_core_type_or_tuple
{ $1 }
| QUESTION LIDENT COLON core_type2 MINUSGREATER core_type2
{ mktyp(Ptyp_arrow("?" ^ $2 , mkoption $4, $6)) }
{ mktyp(Ptyp_arrow(Optional $2 , mkoption $4, $6)) }
| OPTLABEL core_type2 MINUSGREATER core_type2
{ mktyp(Ptyp_arrow("?" ^ $1 , mkoption $2, $4)) }
{ mktyp(Ptyp_arrow(Optional $1 , mkoption $2, $4)) }
| LIDENT COLON core_type2 MINUSGREATER core_type2
{ mktyp(Ptyp_arrow($1, $3, $5)) }
{ mktyp(Ptyp_arrow(Labelled $1, $3, $5)) }
| core_type2 MINUSGREATER core_type2
{ mktyp(Ptyp_arrow("", $1, $3)) }
{ mktyp(Ptyp_arrow(Nolabel, $1, $3)) }
;

simple_core_type:
Expand Down

0 comments on commit 1584803

Please sign in to comment.