Skip to content
This repository has been archived by the owner on Jun 4, 2019. It is now read-only.

Commit

Permalink
[pfff] add support for parsing :: inside type expressions
Browse files Browse the repository at this point in the history
Summary:
Diff 1/2 for adding typeconst support. This makes it possible to refer
to types of the form `MyClass::my_named_tconst`.

There are some AST changes here, so propagate those through as well.

Test Plan:
  make tests # doesn't break anything

Diff 2/2 will add some testcases that exercise both tconst definitions
and tconst usages.

Reviewers: dominik, dreeves

Reviewed By: dreeves

Subscribers: dominik

Differential Revision: https://phabricator.fb.com/D1880752

Tasks: 6358822

Signature: t1:1880752:1425321719:ad278426e6f78cd6ccc611e1b5c4cc84f3e78aba
  • Loading branch information
phooji committed Mar 3, 2015
1 parent d1889a8 commit ae6678b
Show file tree
Hide file tree
Showing 16 changed files with 61 additions and 6 deletions.
1 change: 1 addition & 0 deletions lang_php/analyze/foundation/ast_php_simple.ml
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ and hint_type =
| HintTuple of hint_type list
| HintCallback of hint_type list * (hint_type option)
| HintShape of (string_const_expr * hint_type) list
| HintTypeConst of (hint_type * hint_type)

and class_name = hint_type

Expand Down
2 changes: 2 additions & 0 deletions lang_php/analyze/foundation/ast_php_simple_build.ml
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,8 @@ and hint_type env = function
xs +> brace +> comma_list +> List.map (fun (e, _tok, t) ->
expr env e, hint_type env t
))
| HintTypeConst (lhs, _tok, rhs) ->
A.HintTypeConst (hint_type env lhs, hint_type env rhs)

(* ------------------------------------------------------------------------- *)
(* Definitions *)
Expand Down
1 change: 1 addition & 0 deletions lang_php/analyze/foundation/database_prolog_php.ml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ let rec string_of_hint_type x =
"(" ^ (String.concat ", " elts) ^ ")"
| HintCallback _ -> "callback"
| HintShape _ -> "shape"
| HintTypeConst _ -> "typeconst"

let string_of_hint_type_opt h =
match h with
Expand Down
4 changes: 4 additions & 0 deletions lang_php/analyze/foundation/dependencies_toposort_php.ml
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ module Deps = struct
| HintShape xs ->
List.fold_left (fun accp (_s,x) ->
SSet.union accp (hint_type_ accp x)) acc xs
| HintTypeConst (x1, x2) ->
List.fold_left (fun accp x ->
SSet.union accp (hint_type_ accp x)) acc [x1; x2]



and class_def acc c =
Expand Down
3 changes: 3 additions & 0 deletions lang_php/analyze/foundation/graph_code_php.ml
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,9 @@ and hint_type env t =
xs +> List.iter (fun (_ket, t) ->
hint_type env t
)
| HintTypeConst (x1, x2) ->
hint_type env x1;
hint_type env x2

(* ---------------------------------------------------------------------- *)
(* Expr *)
Expand Down
4 changes: 4 additions & 0 deletions lang_php/analyze/foundation/meta_ast_php_simple.ml
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,10 @@ and vof_hint_type =
in Ocaml.VTuple [ v1; v2 ])
v1
in Ocaml.VSum (("HintShape", [ v1 ]))
| HintTypeConst (v1, v2) ->
let v1 = vof_hint_type v1
and v2 = vof_hint_type v2
in Ocaml.VSum (("HintTypeConst", [ v1; v2]))

and
vof_class_def {
Expand Down
1 change: 1 addition & 0 deletions lang_php/analyze/foundation/typing_php.ml
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,7 @@ and parameter env p =
| Some (HintTuple _)
| Some (HintCallback _) -> Tvar (fresh())
| Some (HintShape _) -> failwith "no support for shape yet"
| Some (HintTypeConst _) -> failwith "no support for type consts"
in
(match p.p_default with
| None -> ()
Expand Down
1 change: 1 addition & 0 deletions lang_php/analyze/visual/highlight_php.ml
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@ let visit_program ~tag _prefs hentities (ast, toks) =
| HintCallback _
(* todo: colorize as record the keys? *)
| HintShape _
| HintTypeConst _
->
()
);
Expand Down
9 changes: 8 additions & 1 deletion lang_php/matcher/php_vs_php.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2486,14 +2486,21 @@ and m_hint_type a b =
return (A.HintShape(a1, a2),
B.HintShape(b1, b2)
)))

| A.HintTypeConst(a1, a2, a3), B.HintTypeConst(b1, b2, b3) ->
m_hint_type a1 b1 >>= (fun (a1, b1) ->
m_tok a2 b2 >>= (fun (a2, b2) ->
m_hint_type a3 b3 >>= (fun (a3, b3) ->
return (A.HintTypeConst(a1, a2, a3),
B.HintTypeConst(b1, b2, b3)
))))

| A.Hint _, _
| A.HintArray _, _
| A.HintQuestion _, _
| A.HintTuple _, _
| A.HintCallback _, _
| A.HintShape _, _
| A.HintTypeConst _, _
-> fail ()
and m_hint_type_ret (a1, a2, a3) (b1, b2, b3) =
m_tok a1 b1 >>= (fun (a1, b1) ->
Expand Down
6 changes: 4 additions & 2 deletions lang_php/matcher/refactoring_code_php.ml
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,17 @@ let refactor refactorings (ast, tokens) =
(match p.p_type with
| None -> ()
| Some x ->
let tok =
let rec leftmost_tok x =
match x with
| HintArray tok -> tok
| Hint (name, _typeargs) -> Ast.info_of_name name
| HintQuestion (tok, _t) -> tok
| HintTuple (t, _, _) -> t
| HintCallback (lparen,_,_) -> lparen
| HintShape (tok, _) -> tok
in
| HintTypeConst (hint, _, _) -> leftmost_tok hint
in
let tok = leftmost_tok x in
if tok_pos_equal_refactor_pos tok pos_opt then begin
tok.PI.transfo <-
PI.AddBefore (PI.AddStr ("?"));
Expand Down
6 changes: 5 additions & 1 deletion lang_php/parsing/ast_php.ml
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,15 @@ type hint_type =
| HintCallback of
(tok (* "function" *)
* (hint_type comma_list_dots paren) (* params *)
* (tok * tok option * hint_type) option (* return type *)
* (tok * tok option * hint_type) option (* return type *)
) paren
| HintShape of
tok (* "shape" *) *
(string_const_expr * tok (* '=>' *) * hint_type) comma_list paren
| HintTypeConst of
hint_type (* lhs *)
* tok (* '::' *)
* hint_type (* rhs *)

and type_args = hint_type comma_list single_angle

Expand Down
5 changes: 5 additions & 0 deletions lang_php/parsing/map_php.ml
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,11 @@ and map_hint_type =
in (v1, v2, v3)))
v2
in HintShape ((v1, v2))
| HintTypeConst ((v1, v2, v3)) ->
let v1 = map_hint_type v1
and v2 = map_tok v2
and v3 = map_hint_type v3
in HintTypeConst ((v1, v2, v3))

and map_is_ref v = map_of_option map_tok v
and map_lambda_def (v1, v2) =
Expand Down
6 changes: 6 additions & 0 deletions lang_php/parsing/meta_ast_php.ml
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,12 @@ and vof_hint_type =
in Ocaml.VTuple [ v1; v2; v3 ]))
v2
in Ocaml.VSum (("HintShape", [ v1; v2 ]))
| HintTypeConst ((v1, v2, v3)) ->
let v1 = vof_hint_type v1
and v2 = vof_tok v2
and v3 = vof_hint_type v3
in Ocaml.VSum (("HintTypeConst", [ v1; v2; v3]))


and vof_is_ref v = vof_option vof_tok v

Expand Down
6 changes: 6 additions & 0 deletions lang_php/parsing/parser_php.mly
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,11 @@ variance_opt:
/*(*************************************************************************)*/

type_php:
| primary_type_php { $1 }
/*(*facebook-ext: classes can define type constants referenced using `::`*)*/
| type_php TCOLCOL primary_type_php { HintTypeConst ($1, $2, $3) }

primary_type_php:
| class_name { $1 }
| T_SELF { Hint (Self $1, None) }
| T_PARENT { Hint (Parent $1, None) }
Expand All @@ -945,6 +950,7 @@ type_php:
| TOPAR T_FUNCTION TOPAR type_php_or_dots_list TCPAR return_type TCPAR
{ HintCallback ($1, ($2, ($3, $4, $5), Some $6), $7)}


/*(* similar to parameter_list, but without names for the parameters *)*/
type_php_or_dots_list:
| /*(*empty*)*/ { [] }
Expand Down
7 changes: 7 additions & 0 deletions lang_php/parsing/visitor_php.ml
Original file line number Diff line number Diff line change
Expand Up @@ -873,9 +873,16 @@ and v_hint_type x =
in ()))
v2
in ()
| HintTypeConst ((v1, v2, v3)) ->
let v1 = v_hint_type v1
and v2 = v_tok v2
and v3 = v_hint_type v3
in ()
in
vin.khint_type (k, all_functions) x



and v_is_ref v = v_option v_tok v

and v_constraint (v1, v2) =
Expand Down
5 changes: 3 additions & 2 deletions lang_php/pretty/ast_pp_build.ml
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,9 @@ and hint_type env = function
in
A.HintCallback (args, ret)
| HintShape _ ->
failwith "no support shape"

failwith "no support for shape"
| HintTypeConst _ ->
failwith "no support for type consts"

and class_name_reference env a = expr env a

Expand Down

0 comments on commit ae6678b

Please sign in to comment.