Coccinelle release-1.0.0-rc11
[bpt/coccinelle.git] / python / yes_pycocci.ml
index 8de88e0..c1792b6 100644 (file)
@@ -1,23 +1,27 @@
 (*
-* Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
-* Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller
-* This file is part of Coccinelle.
-* 
-* Coccinelle is free software: you can redistribute it and/or modify
-* it under the terms of the GNU General Public License as published by
-* the Free Software Foundation, according to version 2 of the License.
-* 
-* Coccinelle is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-* GNU General Public License for more details.
-* 
-* You should have received a copy of the GNU General Public License
-* along with Coccinelle.  If not, see <http://www.gnu.org/licenses/>.
-* 
-* The authors reserve the right to distribute this or future versions of
-* Coccinelle under other licenses.
-*)
+ * Copyright 2012, INRIA
+ * Julia Lawall, Gilles Muller
+ * Copyright 2010-2011, INRIA, University of Copenhagen
+ * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
+ * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
+ * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
+ * This file is part of Coccinelle.
+ *
+ * Coccinelle is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, according to version 2 of the License.
+ *
+ * Coccinelle is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Coccinelle.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * The authors reserve the right to distribute this or future versions of
+ * Coccinelle under other licenses.
+ *)
 
 
 open Ast_c
@@ -28,6 +32,8 @@ module StringMap = Map.Make (String)
 
 exception Pycocciexception
 
+let python_support = true
+
 (* ------------------------------------------------------------------- *)
 (* The following definitions are from
 http://patches.ubuntu.com/by-release/extracted/debian/c/coccinelle/0.1.5dbs-2/01-system-pycaml
@@ -51,7 +57,7 @@ let _pycocci_tuple6 (a,b,c,d,e,f) =
 (* ------------------------------------------------------------------- *)
 
 let check_return_value v =
-  if v =*= (pynull ()) then 
+  if v =*= (pynull ()) then
          (pyerr_print ();
          raise Pycocciexception)
   else ()
@@ -103,13 +109,15 @@ let pycocci_get_class_type fqn =
 
 let pycocci_instantiate_class fqn args =
   let class_type = pycocci_get_class_type fqn in
-  let obj = pyobject_callobject(class_type, args) in
+  let obj =
+    pyeval_callobjectwithkeywords(class_type, args, pynull()) in
   check_return_value obj;
   obj
 
 (* end python interaction *)
 
 let inc_match = ref true
+let exited = ref false
 
 let include_match v =
   let truth = pyobject_istrue (pytuple_getitem (v, 1)) in
@@ -117,6 +125,10 @@ let include_match v =
   inc_match := truth != 0;
   _pycocci_none ()
 
+let sp_exit _ =
+  exited := true;
+  _pycocci_none ()
+
 let build_method (mname, camlfunc, args) pymodule classx classdict =
   let cmx = pymethod_new(pywrap_closure camlfunc, args, classx) in
   let v = pydict_setitemstring(classdict, mname, cmx) in
@@ -141,7 +153,7 @@ let has_environment_binding name =
   let (rule, name) = (Array.get a 1, Array.get a 2) in
   let orule = pystring_asstring rule in
   let oname = pystring_asstring name in
-  let e = List.exists (function (x,y) -> orule =$= x && oname =$= y)
+  let e = List.exists (function (x,y) -> orule =*= x && oname =$= y)
       !the_environment in
   if e then _pycocci_true () else _pycocci_false ()
 
@@ -164,8 +176,8 @@ let pycocci_init () =
   initialised := true;
   Unix.putenv "PYTHONPATH"
       (Printf.sprintf "%s/coccinelle" (Unix.getenv "HOME"));
-  let _ = if not (py_isinitialized () != 0) then 
-       (if !Flag.show_misc then Common.pr2 "Initializing python\n%!"; 
+  let _ = if not (py_isinitialized () != 0) then
+       (if !Flag.show_misc then Common.pr2 "Initializing python\n%!";
        py_initialize()) in
 
   (* set argv *)
@@ -180,8 +192,9 @@ let pycocci_init () =
   let module_dictionary = pyimport_getmoduledict() in
   coccinelle_module := pymodule_new "coccinelle";
   let mx = !coccinelle_module in
-  let (cd, cx) = build_class "Cocci" (!Flag.pyoutput) 
-      [("include_match", include_match, (pynull()));
+  let (cd, cx) = build_class "Cocci" (!Flag.pyoutput)
+      [("exit", sp_exit, (pynull()));
+       ("include_match", include_match, (pynull()));
        ("has_env_binding", has_environment_binding, (pynull()))] mx in
   pyoutputinstance := cx;
   pyoutputdict := cd;
@@ -201,12 +214,13 @@ let added_variables = ref []
 let build_classes env =
   let _ = pycocci_init () in
   inc_match := true;
+  exited := false;
   the_environment := env;
   let mx = !coccinelle_module in
   let dict = pymodule_getdict mx in
   List.iter
     (function
-       "include_match" | "has_env_binding" -> ()
+       "include_match" | "has_env_binding" | "exit" -> ()
       | name ->
          let v = pydict_delitemstring(dict,name) in
          check_int_return_value v)
@@ -220,43 +234,71 @@ let build_variable name value =
   check_int_return_value
     (pydict_setitemstring(pymodule_getdict mx, name, value))
 
-let contains_binding e (_,(r,m)) =
+let get_variable name =
+  let mx = !coccinelle_module in
+  pystring_asstring
+    (pyobject_str(pydict_getitemstring(pymodule_getdict mx, name)))
+
+let contains_binding e (_,(r,m),_) =
   try
-    let _ = List.find (function ((re, rm), _) -> r =$= re && m =$= rm) e in
+    let _ = List.find (function ((re, rm), _) -> r =*= re && m =$= rm) e in
     true
   with Not_found -> false
 
 let construct_variables mv e =
   let find_binding (r,m) =
     try
-      let elem = List.find (function ((re,rm),_) -> r =$= re && m =$= rm) e in
+      let elem = List.find (function ((re,rm),_) -> r =*= re && m =$= rm) e in
       Some elem
     with Not_found -> None
   in
 
+(* Only string in this representation, so no point
   let instantiate_Expression(x) =
     let str = pystring_fromstring (Pycocci_aux.exprrep x) in
     pycocci_instantiate_class "coccilib.elems.Expression"
       (pytuple_fromsingle (str))
   in
+*)
 
+(* Only string in this representation, so no point
   let instantiate_Identifier(x) =
     let str = pystring_fromstring x in
     pycocci_instantiate_class "coccilib.elems.Identifier"
       (pytuple_fromsingle (str))
   in
+*)
 
-  List.iter (function (py,(r,m)) ->
+  let instantiate_term_list py printer lst  =
+    let (str,elements) = printer lst in
+    let str = pystring_fromstring str in
+    let elements =
+      pytuple_fromarray
+       (Array.of_list (List.map pystring_fromstring elements)) in
+    let repr =
+      pycocci_instantiate_class "coccilib.elems.TermList"
+       (pytuple_fromarray (Array.of_list [str;elements])) in
+    let _ = build_variable py repr in () in
+
+  List.iter (function (py,(r,m),_) ->
     match find_binding (r,m) with
       None -> ()
-    | Some (_, Ast_c.MetaExprVal expr) ->
+(*    | Some (_, Ast_c.MetaExprVal (expr,_)) ->
        let expr_repr = instantiate_Expression(expr) in
        let _ = build_variable py expr_repr in
-       ()
-    | Some (_, Ast_c.MetaIdVal id) ->
+       () *)
+  (*  | Some (_, Ast_c.MetaIdVal (id,_)) ->
        let id_repr = instantiate_Identifier(id) in
        let _ = build_variable py id_repr in
-       ()
+       () *)
+    | Some (_, Ast_c.MetaExprListVal (exprlist)) ->
+       instantiate_term_list py Pycocci_aux.exprlistrep exprlist
+    | Some (_, Ast_c.MetaParamListVal (paramlist)) ->
+       instantiate_term_list py Pycocci_aux.paramlistrep paramlist
+    | Some (_, Ast_c.MetaInitListVal (initlist)) ->
+       instantiate_term_list py Pycocci_aux.initlistrep initlist
+    | Some (_, Ast_c.MetaFieldListVal (fieldlist)) ->
+       instantiate_term_list py Pycocci_aux.fieldlistrep fieldlist
     | Some (_, Ast_c.MetaPosValList l) ->
        let locs =
         List.map
@@ -272,21 +314,37 @@ let construct_variables mv e =
        let _ = build_variable py pylocs in
        ()
     | Some (_,binding) ->
-       let _ = build_variable py (pystring_fromstring (Pycocci_aux.stringrep binding))
-       in ()
+       let _ =
+        build_variable py
+          (pystring_fromstring (Pycocci_aux.stringrep binding)) in
+       ()
     ) mv;
 
   ()
 
+let construct_script_variables mv =
+  List.iter
+    (function (_,py) ->
+      let str =
+       pystring_fromstring
+         "initial value: consider using coccinelle.varname" in
+      let _ = build_variable py str in
+      ())
+    mv
+
+let retrieve_script_variables mv =
+  List.map (function (_,py) -> get_variable py) mv
+
 let set_coccifile cocci_file =
        cocci_file_name := cocci_file;
        ()
 
+let pyrun_simplestring s =
+  let res = Pycaml.pyrun_simplestring s in
+  check_int_return_value res;
+  res
 
-let pyrun_simplestring s = 
-  Pycaml.pyrun_simplestring s
-
-let py_isinitialized () = 
+let py_isinitialized () =
   Pycaml.py_isinitialized ()