From 36ea71a983db071f18c2c6f25b9fad77fbc3756c Mon Sep 17 00:00:00 2001 From: Sam Blackshear Date: Tue, 1 Aug 2017 14:43:26 -0700 Subject: [PATCH] [HIL] add function for getting type of HIL expression Reviewed By: grievejia Differential Revision: D5522476 fbshipit-source-id: f6f38ff --- infer/src/IR/HilExp.ml | 40 ++++++++++++++++++++++++++++++++++++++++ infer/src/IR/HilExp.mli | 3 +++ 2 files changed, 43 insertions(+) diff --git a/infer/src/IR/HilExp.ml b/infer/src/IR/HilExp.ml index 0ebb5e3ff..d1b3f26de 100644 --- a/infer/src/IR/HilExp.ml +++ b/infer/src/IR/HilExp.ml @@ -41,6 +41,46 @@ let rec pp fmt = function -> let pp_length fmt = Option.iter ~f:(F.fprintf fmt "[%a]" pp) in F.fprintf fmt "sizeof(%a%a)" (Typ.pp_full Pp.text) typ pp_length length +let rec get_typ tenv = function + | AccessPath access_path + -> AccessPath.Raw.get_typ access_path tenv + | UnaryOperator (_, _, typ_opt) + -> typ_opt + | BinaryOperator ((Lt | Gt | Le | Ge | Eq | Ne | LAnd | LOr), _, _) + -> Some (Typ.mk (Typ.Tint Typ.IBool)) + | BinaryOperator (_, e1, e2) -> ( + match + (* TODO: doing this properly will require taking account of language-specific coercion + semantics. Only return a type when the operands have the same type for now *) + (get_typ tenv e1, get_typ tenv e2) + with + | Some typ1, Some typ2 when Typ.equal typ1 typ2 + -> Some typ1 + | _ + -> None ) + | Exception t + -> get_typ tenv t + | Closure _ | Constant Cfun _ + -> (* We don't have a way to represent function types *) + None + | Constant Cint _ + -> (* TODO: handle signedness *) + Some (Typ.mk (Typ.Tint Typ.IInt)) + | Constant Cfloat _ + -> Some (Typ.mk (Typ.Tfloat Typ.FFloat)) + | Constant Cclass _ + -> (* TODO: this only happens in Java. We probably need to change it to `Cclass of Typ.Name.t` + to give a useful result here *) + None + | Constant Cstr _ + -> (* TODO: this will need to behave differently depending on whether we're in C++ or Java *) + None + | Cast (typ, _) + -> Some typ + | Sizeof _ + -> (* sizeof returns a size_t, which is an unsigned int *) + Some (Typ.mk (Typ.Tint Typ.IUInt)) + let get_access_paths exp0 = let rec get_access_paths_ exp acc = match exp with diff --git a/infer/src/IR/HilExp.mli b/infer/src/IR/HilExp.mli index 7018814b7..09ee3ce4b 100644 --- a/infer/src/IR/HilExp.mli +++ b/infer/src/IR/HilExp.mli @@ -26,6 +26,9 @@ type t = val pp : F.formatter -> t -> unit +val get_typ : Tenv.t -> t -> Typ.t option +(** Get the type of the expression. Warning: not fully implemented *) + val of_sil : f_resolve_id:(Var.t -> AccessPath.Raw.t option) -> Exp.t -> Typ.t -> t (** Convert SIL expression to HIL expression *)