Release of coccinelle 1.0.0-rc9
[bpt/coccinelle.git] / python / yes_pycocci.ml
index 50bd32c..39e0468 100644 (file)
@@ -1,5 +1,7 @@
 (*
- * Copyright 2005-2010, Ecole des Mines de Nantes, University of Copenhagen
+ * Copyright 2010, 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.
  *
@@ -105,13 +107,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
@@ -119,6 +123,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
@@ -183,7 +191,8 @@ let pycocci_init () =
   coccinelle_module := pymodule_new "coccinelle";
   let mx = !coccinelle_module in
   let (cd, cx) = build_class "Cocci" (!Flag.pyoutput)
-      [("include_match", include_match, (pynull()));
+      [("exit", sp_exit, (pynull()));
+       ("include_match", include_match, (pynull()));
        ("has_env_binding", has_environment_binding, (pynull()))] mx in
   pyoutputinstance := cx;
   pyoutputdict := cd;
@@ -203,12 +212,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)
@@ -222,7 +232,12 @@ 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
     true
@@ -248,14 +263,14 @@ let construct_variables mv e =
       (pytuple_fromsingle (str))
   in
 
-  List.iter (function (py,(r,m)) ->
+  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
        ()
@@ -274,18 +289,38 @@ 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 vl =
+       let str =
+         pystring_fromstring
+           "initial value: consider using coccinelle.varname" in
+       pycocci_instantiate_class "coccilib.elems.Identifier"
+         (pytuple_fromsingle (str)) in
+      let _ = build_variable py vl 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 =
-  Pycaml.pyrun_simplestring s
+  let res = Pycaml.pyrun_simplestring s in
+  check_int_return_value res;
+  res
 
 let py_isinitialized () =
   Pycaml.py_isinitialized ()