[HIL] change `address_of` to return option type

Summary:
You can only take the address of variables, field accesses, and array
accesses, the rest doesn't make sense.

Reviewed By: mbouaziz

Differential Revision: D14258484

fbshipit-source-id: 8ddcfe810
master
Jules Villard 6 years ago committed by Facebook Github Bot
parent 2037074ee3
commit c4c66b76f5

@ -65,7 +65,9 @@ module T : sig
val array_offset : access_expression -> Typ.t -> t option -> access_expression val array_offset : access_expression -> Typ.t -> t option -> access_expression
val address_of : access_expression -> access_expression val address_of : access_expression -> access_expression option
val address_of_base : AccessPath.base -> access_expression
val dereference : access_expression -> access_expression val dereference : access_expression -> access_expression
@ -98,7 +100,14 @@ end = struct
let array_offset t typ index = ArrayOffset (t, typ, index) let array_offset t typ index = ArrayOffset (t, typ, index)
let address_of = function Dereference t -> t | t -> AddressOf t let address_of = function
| Dereference _ | AddressOf _ ->
None
| (FieldOffset _ | ArrayOffset _ | Base _) as t ->
Some (AddressOf t)
let address_of_base base = AddressOf (Base base)
let dereference = function AddressOf t -> t | t -> Dereference t let dereference = function AddressOf t -> t | t -> Dereference t
@ -290,7 +299,7 @@ module AccessExpression = struct
let base_of_pvar pvar typ = (Var.of_pvar pvar, typ) let base_of_pvar pvar typ = (Var.of_pvar pvar, typ)
let of_pvar pvar typ = address_of (base (base_of_pvar pvar typ)) let of_pvar pvar typ = address_of_base (base_of_pvar pvar typ)
let of_id id typ = base (base_of_id id typ) let of_id id typ = base (base_of_id id typ)
@ -448,7 +457,7 @@ and access_expr_of_lhs_exp ~include_array_indexes ~f_resolve_id ~add_deref lhs_e
let res = let res =
access_exprs_of_exp ~include_array_indexes ~f_resolve_id ~add_deref:true lhs_exp typ access_exprs_of_exp ~include_array_indexes ~f_resolve_id ~add_deref:true lhs_exp typ
in in
match res with [lhs_ae] -> Some (AccessExpression.address_of lhs_ae) | _ -> None ) match res with [lhs_ae] -> AccessExpression.address_of lhs_ae | _ -> None )
| Exp.Lindex _ when not add_deref -> ( | Exp.Lindex _ when not add_deref -> (
let res = let res =
let typ' = let typ' =
@ -461,7 +470,7 @@ and access_expr_of_lhs_exp ~include_array_indexes ~f_resolve_id ~add_deref lhs_e
in in
access_exprs_of_exp ~include_array_indexes ~f_resolve_id ~add_deref:true lhs_exp typ' access_exprs_of_exp ~include_array_indexes ~f_resolve_id ~add_deref:true lhs_exp typ'
in in
match res with [lhs_ae] -> Some (AccessExpression.address_of lhs_ae) | _ -> None ) match res with [lhs_ae] -> AccessExpression.address_of lhs_ae | _ -> None )
| _ -> ( | _ -> (
let res = access_exprs_of_exp ~include_array_indexes ~f_resolve_id ~add_deref lhs_exp typ in let res = access_exprs_of_exp ~include_array_indexes ~f_resolve_id ~add_deref lhs_exp typ in
match res with [lhs_ae] -> Some lhs_ae | _ -> None ) match res with [lhs_ae] -> Some lhs_ae | _ -> None )

@ -50,6 +50,10 @@ module AccessExpression : sig
val dereference : access_expression -> access_expression val dereference : access_expression -> access_expression
(** guarantees that we never build [Dereference (AddressOf t)] expressions: these become [t] *) (** guarantees that we never build [Dereference (AddressOf t)] expressions: these become [t] *)
val address_of : access_expression -> access_expression option
[@@warning "-32"]
(** address_of doesn't always make sense, eg [address_of (Dereference t)] is [None] *)
val to_accesses_fold : val to_accesses_fold :
access_expression access_expression
-> init:'accum -> init:'accum

Loading…
Cancel
Save