Index: /CMT/HEAD/ChangeLog
===================================================================
--- /CMT/HEAD/ChangeLog	(revision 562)
+++ /CMT/HEAD/ChangeLog	(revision 563)
@@ -1,2 +1,22 @@
+2011-03-16    <rybkin@lal.in2p3.fr> 446
+
+	* source/cmt_use.h: In class Use, change signature of functions
+	set_auto_imports_state, set_auto_imports
+	* source/cmt_use.cxx: In helper class UseContext, introduce and implement
+	function mask_auto_imports, in class use_action_iterator, in function
+	get_use, do not deal with auto_imports state of added Use object. In class
+	Use, function add, ensure that finally chosen Use object gets appended to
+	sub_uses of context_use, it is used when setting auto_imports states.
+	Implement functions set_auto_imports_state, set_auto_imports with modified
+	signatures making sure all sub uses are operated upon recursively, order
+	from last to first, depth first, and first encountered taking precedence
+	* source/cmt_parser.cxx: In class Cmt, in function parser, configure
+	auto_imports state of all uses after all requirements have been read. In
+	class StandardMacroBuilder, in function fill_for_all_constituents, ensure
+	that all sub uses of imported packages are also imported if not	imported
+	yet and not specified with -no_auto_imports option
+	* source/cmt_project.cxx: In class ProjectFactory function create_project,
+	use set_auto_imports with modified signature
+	
 2011-03-08    <rybkin@lal.in2p3.fr> 445
 
Index: /CMT/HEAD/source/cmt_parser.cxx
===================================================================
--- /CMT/HEAD/source/cmt_parser.cxx	(revision 562)
+++ /CMT/HEAD/source/cmt_parser.cxx	(revision 563)
@@ -6729,4 +6729,25 @@
     }
 
+  //
+  // Set auto_imports
+  //
+
+  Use::UsePtrVector uses;
+  Use* use (0);
+  use = Use::find (CmtSystem::get_home_package ());
+  if (use != 0 && !use->discarded && !use->m_hidden && use->located ())
+    uses.push_back (use);
+  uses.push_back (&(Use::current()));
+  use = Use::find (CmtSystem::get_user_context_package ());
+  if (use != 0 && !use->discarded && !use->m_hidden && use->located ())
+    uses.push_back (use);
+
+  cmt_map <Use*, bool> visited;
+  for (int i = uses.size () - 1; i >= 0; i--)
+    {
+      Use* use = uses[i];
+      use->set_auto_imports (use->auto_imports, use->auto_imports, visited);
+    }
+
   if (Me.m_debug) cout << "parser3> current_tag=" << Me.m_current_tag << endl;
 
@@ -8935,5 +8956,6 @@
 	 */
 	cmt_vector<bool> auto_imports_states (base_auto_imports_states);
-	
+	Use::UsePtrVector uses;
+
 	for (i = 0; i < constituent.imports.size (); i++)
 	  {
@@ -8955,10 +8977,10 @@
 		if (Cmt::get_debug ())
 		  {
-		    cout << constituent.name  << " needs imports " << import << " "
-			 << use_index << " " 
-			 << u->get_package()->get_name() << " "
-			 << " u->auto_imports=" << u->auto_imports
-			 << " Off=" << Off
-			 << " On=" << On
+		    cout << constituent.name  << " needs imports " << import
+			 << " i=" << use_index << " package=" 
+			 << u->get_package()->get_name()
+			 << " auto_imports=" << u->auto_imports
+			 << " (Off=" << Off
+			 << " On=" << On << ")"
 			 << endl;
 		  }
@@ -8967,7 +8989,8 @@
 		if (p->is_cmt ()) continue;
 		
-		if (u->auto_imports != Off) continue;
-		
-		Use::set_auto_imports_state (use_index, auto_imports_states);
+		uses.push_back (u);
+		//u->set_auto_imports_state (auto_imports_states, visited);
+		//if (u->auto_imports != Off) continue;
+		//Use::set_auto_imports_state (use_index, auto_imports_states);
 	      }
 	    else
@@ -8978,6 +9001,18 @@
 				       " cannot be imported (requested for " +
 				       constituent.name +")");
+		if (Cmt::get_debug ())
+		  {
+		    cout << "import [" << i << "] "
+			 << ("package " + import +
+			     " cannot be imported (requested for " +
+			     constituent.name +")")  << endl;
+		  }
 	      }
 	  }
+
+	cmt_map <Use*, bool> visited;
+	static bool yes (true);
+	visited.add (&(Use::current()), yes);
+	Use::set_auto_imports_state (uses, auto_imports_states, visited);
 	
 	if (Cmt::get_debug ())
@@ -8994,10 +9029,11 @@
 	  {
 	    //	    if (base_auto_imports_states[i]== On && auto_imports_states[i] == Off) continue;
-	    if (base_auto_imports_states[i] == On || auto_imports_states[i] == Off) continue;
+	    if (base_auto_imports_states[i] || !auto_imports_states[i]) continue;
+	    //if (base_auto_imports_states[i] == On || auto_imports_states[i] == Off) continue;
 	    /**
 	     *  Consider the package (i) for import only if it is:
-	     *   o not imported yet (i.e., base_auto_imports_states[i] != On)
+	     *   o not imported yet (i.e., base_auto_imports_states[i] != true)
 	     *   o requested for import according to -import=xxx modifiers for
-	     *     the current constituent (i.e., auto_imports_states[i] != Off)
+	     *     the current constituent (i.e., auto_imports_states[i] != false)
 	     */
 	    Use* u = Uses[i];
Index: /CMT/HEAD/source/cmt_project.cxx
===================================================================
--- /CMT/HEAD/source/cmt_project.cxx	(revision 562)
+++ /CMT/HEAD/source/cmt_project.cxx	(revision 563)
@@ -645,5 +645,7 @@
       project_use->selected  = true;
       project_use->m_located = true;
-      project_use->set_auto_imports(Off);
+      cmt_map <Use*, bool> visited;
+      project_use->set_auto_imports(Unspecified, Off, visited);
+      //project_use->set_auto_imports(Off);
       project_use->initial_scope = ScopePublic;      
       project->set_use(project_use);
Index: /CMT/HEAD/source/cmt_use.cxx
===================================================================
--- /CMT/HEAD/source/cmt_use.cxx	(revision 562)
+++ /CMT/HEAD/source/cmt_use.cxx	(revision 563)
@@ -72,5 +72,6 @@
   UseContext ()
   {
-    m_auto_imports = On;
+    m_auto_imports = Unspecified;
+    //m_auto_imports = On;
   }
 
@@ -99,4 +100,23 @@
 
     return (c.m_auto_imports);
+  }
+
+  static State mask_auto_imports (State context_state, State auto_imports)
+  {
+    switch (auto_imports)
+      {
+      case Unspecified:
+	return context_state;
+	break;
+      case Off:
+	return Off;
+	break;
+      case On:
+	if (context_state != Off)
+	  return On;
+	else
+	  return context_state;
+	break;
+      }
   }
 
@@ -882,5 +902,5 @@
     // Here the version may contain wild cards
 
-    UseContext save = UseContext::current ();
+    //UseContext save = UseContext::current ();
 
     /**
@@ -888,4 +908,6 @@
      *  currently being parsed.
      */
+    //UseContext::set_current (UseContext::mask_auto_imports (UseContext::get_current_auto_imports (), auto_imports));
+    /*
     switch (auto_imports)
       {
@@ -894,5 +916,5 @@
 	// unspecified => we forward the state saved in the current use context
 
-	UseContext::set_current (UseContext::get_current_auto_imports ());
+	//UseContext::set_current (UseContext::get_current_auto_imports ());
 	break;
       case Off:
@@ -913,5 +935,5 @@
 	break;
       }
-
+*/
     if (Cmt::get_debug ())
       {
@@ -934,5 +956,19 @@
 		show_packages ();
               }
-
+	    /*
+	    if (&(Use::current()) == parent)
+	      {
+		cmt_map <Use*, bool> visited;
+		static bool yes (true);
+		visited.add (&(Use::current()), yes);
+		new_use->set_auto_imports
+		  (UseContext::mask_auto_imports (Use::current().auto_imports,
+						  auto_imports),
+		   auto_imports, visited);
+	      }
+	    */
+	    //new_use->set_auto_imports (UseContext::get_current_auto_imports (),
+	    //		       auto_imports);
+	    /*
             switch (new_use->auto_imports)
               {
@@ -947,6 +983,8 @@
 		break;
 	      case Off:
-		if (UseContext::get_current_auto_imports () == On)
+		if (UseContext::get_current_auto_imports () != Off)
+		  //if (UseContext::get_current_auto_imports () == On)
 		  {
+	    */
 		    /**
 		     *  Warning : this Use had been previously specified as -no_auto_imports
@@ -960,9 +998,11 @@
 		     *
 		     */
-		    new_use->set_auto_imports (On);
+	    /*
+		    new_use->set_auto_imports (UseContext::get_current_auto_imports ());
+		    //new_use->set_auto_imports (On);
 		  }
 		break;
               }
-
+	    */
 
             if (Cmt::get_debug ())
@@ -973,6 +1013,6 @@
               }
 
-            UseContext& c = UseContext::current ();
-            c = save;
+            //UseContext& c = UseContext::current ();
+            //c = save;
 
             Use::reorder (new_use, parent);
@@ -1180,4 +1220,75 @@
 }
 
+void Use::set_auto_imports_state (const Use::UsePtrVector& uses,
+				  cmt_vector<bool>& auto_imports_states,
+				  cmt_map <Use*, bool>& visited)
+{
+  if (0 == uses.size ()) return;
+  static bool yes (true);
+
+  static int level (-1);
+  if (Cmt::get_debug ())
+    {
+      level++;
+      cout << "Use::set_auto_imports_state>|";
+      for (int i = level; i > 0; i--) cout << "-";
+      for (int i = uses.size () - 1; i >= 0; i--)
+	{
+	  cout << " " << uses[i]->get_package_name ()
+	       << "(" << uses[i]->auto_imports << ")";
+	}
+      cout << ">" << endl;
+    }
+
+  for (int i = uses.size () - 1; i >= 0; i--)
+    {
+      Use* use = uses[i];
+      if (visited.has (use)) continue;
+      visited.add (use, yes);
+      if (!auto_imports_states[use->m_index])
+	auto_imports_states[use->m_index] = true;
+
+      Use::UsePtrVector subuses;
+      for (int i = 0; i < use->sub_uses.size (); i++)
+	{
+	  if (use->sub_uses[i]->m_index >= 0)
+	    {
+	      if (use->sub_use_auto_imports[i] != Off)
+		{
+		  subuses.push_back (use->sub_uses[i]);
+		}
+	      else
+		{
+		  visited.add (use->sub_uses[i], yes);
+		}
+	    }
+	  else
+	    {
+	      visited.add (use->sub_uses[i], yes);
+	      if (Cmt::get_debug ())
+		{
+		  cout << "Use::set_auto_imports_state> " << use->get_package_name ()
+		       << ": sub_use " << use->sub_uses[i]->get_package_name () << "["
+		       << use->sub_uses[i] << "] invalid" << endl;
+		}
+	    }
+	}
+      Use::set_auto_imports_state (subuses, auto_imports_states, visited);
+    }
+
+  if (Cmt::get_debug ())
+    {
+      cout << "Use::set_auto_imports_state><";
+      for (int i = level; i > 0; i--) cout << "-";
+      for (int i = uses.size () - 1; i >= 0; i--)
+	{
+	  cout << " " << uses[i]->get_package_name ()
+	       << "(" << (auto_imports_states[uses[i]->m_index] ? "+" : "-") << ")";
+	}
+      cout << "|" << endl;
+      level--;
+    }
+}
+
 /**
  *   1) Given a Use object identified by its index in the ordered_uses() array,
@@ -1188,4 +1299,5 @@
  *
  */
+ /*
 void Use::set_auto_imports_state (int use_index,
                                   cmt_vector<bool>& auto_imports_states)
@@ -1217,4 +1329,5 @@
   // check if this is already done (recursivity ending)
   if (auto_imports_states[use_index]) return;
+  auto_imports_states[use_index] = true;
 
   Use* use = uses[use_index];
@@ -1222,6 +1335,6 @@
   // We only have to deal with Use objects that were
   // actually turned Off
-  if (use->auto_imports != Off) return;
-
+  //  if (use->auto_imports != Off) return;
+  */
   /**
    *  Here, use points to a Use object which had been declared as
@@ -1234,9 +1347,18 @@
    */
   
-  auto_imports_states[use_index] = true;
-
-  int size = 0;
-
-  size = use->sub_uses.size ();
+  //  auto_imports_states[use_index] = true;
+  /*
+  cmt_string s;
+  static const cmt_string state_text[] = {"Unspecified", "Off", "On"};
+  
+  if (Cmt::get_debug ())
+    {
+      s = "Use::set_auto_imports_state>(";
+      s += use->get_package_name ();
+      s += ")";
+      //cout << s << endl;
+    }
+
+  int size = use->sub_uses.size ();
   for (int i = 0; i < size; i++)
     {
@@ -1244,9 +1366,27 @@
 
       // first find the index of this use.
-
+      
       int j = u->m_index;
 
+      if (Cmt::get_debug ())
+	{
+	  char num[32]; sprintf (num, "%#x", u);
+	  s += " ";
+	  s += use->sub_uses[i]->get_package_name ();
+	  s += "(";
+	  s += state_text[use->sub_use_auto_imports[i] + 1];
+	  s += ")[p=";
+	  s += num;
+	  s += "]";
+	}
+      
       if (j >= 0)
 	{
+	  if (use->sub_use_auto_imports[i] != Off)
+	    {
+	      set_auto_imports_state (j, auto_imports_states);
+	    }
+  */
+	  /*
 	  if (u->sub_use_auto_imports[i] == Off)
 	    {
@@ -1257,8 +1397,23 @@
 	      auto_imports_states[j] = true;
 	    }
-        }
-    }
-}
-
+	    */
+  /*
+        }
+      else
+	{
+	  if (Cmt::get_debug ())
+	    {
+	      cout << "Use::set_auto_imports_state> " << use->get_package_name ()
+		   << ": sub_use " << u->get_package_name () << "[" << u
+		   << "] invalid" << endl;
+	    }
+	}
+    }
+  if (Cmt::get_debug ())
+    {
+      cout << s << endl;
+    }
+}
+  */
 //----------------------------------------------------------
 //
@@ -2553,5 +2708,5 @@
                const cmt_string& native_version,
                Use* context_use,
-               State specified_auto_imports)
+               const State specified_auto_imports)
 {
   bool do_need_new = false;
@@ -2699,6 +2854,6 @@
 
       //CmtError::set (CmtError::package_not_found, use->get_package_name ());
-      //use->m_located = false;
-      use = 0;
+      use->m_located = false;
+      //use = 0;
     }
 
@@ -2735,6 +2890,11 @@
            */
 
-          if (use != 0) use->discard ();
+          //if (use != 0) use->discard ();
+          use->discard ();
           use = old_use;
+	  if (context_use != 0)
+	    {
+	      context_use->sub_uses[context_use->sub_uses.size () - 1] = use;
+	    }
           found = use->move_to (native_version);
         }
@@ -2763,4 +2923,8 @@
             {
 	      use = use->set_selected_version (selected_use);
+	      if (context_use != 0)
+		{
+		  context_use->sub_uses[context_use->sub_uses.size () - 1] = use;
+		}
             }
           
@@ -3637,4 +3801,11 @@
       // it with the new one.
 
+      if (Cmt::get_debug ())
+	{
+	  cout << "set_selected_version> Replace "
+	       << get_package_name () << "(" << m_index << ")[" << this << "] with "
+	       << selected_use->get_package_name () << "(" << selected_use->m_index << ")[" << selected_use << "]" << endl;
+	}
+
       selected_use->m_index = m_index;
       selected_use->auto_imports = auto_imports;
@@ -3652,4 +3823,104 @@
 }
 
+void Use::set_auto_imports (State context_state, State specified_state,
+			    cmt_map <Use*, bool>& visited)
+{
+  if (visited.has (this)) return;
+
+  static int level (-1);
+  if (Cmt::get_debug ())
+    {
+      level++;
+      cout << "Use::set_auto_imports>|";
+      for (int i = level; i > 0; i--) cout << "-";
+      cout << get_package_name () << "> " 
+           << auto_imports << " -> " 
+	   << "(" << context_state << ")" << specified_state << endl;
+    }
+
+  static bool yes (true);
+  visited.add (this, yes);
+  //  if (auto_imports == context_state) return;
+  
+  //State old_state = auto_imports;
+  
+  //auto_imports = context_state;
+
+  switch (context_state)
+    {
+    case Unspecified:
+    case On:
+      //if (auto_imports == specified_state) return;
+      auto_imports = specified_state;
+      /*
+      switch (auto_imports)
+	{
+	case Unspecified:
+	  break;
+	case Off:
+	  context_state = Off;
+	  break;
+	case On:
+	  context_state = On;
+	  break;
+	}
+      */
+      context_state = UseContext::mask_auto_imports (context_state, auto_imports);
+      break;
+    case Off:
+      //if (auto_imports == context_state) return;
+      auto_imports = context_state;
+      break;
+    }
+  cmt_string s;
+  static const cmt_string state_text[] = {"Unspecified", "Off", "On"};
+  
+  if (Cmt::get_debug ())
+    {
+      s = "Use::set_auto_imports><";
+      for (int i = level; i > 0; i--) s += "-";
+      s += get_package_name ();
+      s += "|";
+      //cout << s << endl;
+    }
+  
+  for (int i = sub_uses.size () - 1; i >= 0; i--)
+    //for (int i = 0; i < sub_uses.size (); i++)
+    {
+      State state = sub_use_auto_imports[i];
+      if (Cmt::get_debug ())
+	{
+	  s += " ";
+	  s += sub_uses[i]->get_package_name ();
+	  s += "(";
+	  s += state_text[state + 1];
+	  s += ")";
+	}
+      if (sub_uses[i]->m_index >= 0 && !sub_uses[i]->discarded)
+	{
+	  sub_uses[i]->set_auto_imports (context_state, state, visited);
+	}
+      else
+	{
+	  visited.add (sub_uses[i], yes);
+	  if (Cmt::get_debug ())
+	    {
+	      char num[32]; sprintf (num, "%#x", sub_uses[i]);
+	      s += "[";
+	      s += num;
+	      s += ", discarded=";
+	      s += (sub_uses[i]->discarded ? "1" : "0");
+	      s += "] invalid";
+	    }
+	}
+    }
+
+  if (Cmt::get_debug ())
+    {
+      cout << s << endl;
+      level--;
+    }
+}
+/*
 void Use::set_auto_imports (State new_state)
 {
@@ -3692,5 +3963,5 @@
               s += u->get_package_name ();
               s += "(";
-              s += state_text[state];
+              s += state_text[state + 1];
               s += ")";
             }
@@ -3708,5 +3979,5 @@
     }
 }
-
+*/
 void Use::set_native_version (bool state)
 {
Index: /CMT/HEAD/source/cmt_use.h
===================================================================
--- /CMT/HEAD/source/cmt_use.h	(revision 562)
+++ /CMT/HEAD/source/cmt_use.h	(revision 563)
@@ -32,6 +32,9 @@
                          const cmt_string& version, 
                          const cmt_string& path);
-  static void set_auto_imports_state (int use_index,
-                                      cmt_vector<bool>& auto_imports_states);
+  //static void set_auto_imports_state (int use_index,
+  //cmt_vector<bool>& auto_imports_states);
+  static void set_auto_imports_state (const Use::UsePtrVector& uses,
+				      cmt_vector<bool>& auto_imports_states,
+				      cmt_map <Use*, bool>& visited);
   static Use* add (const cmt_string& path,
                    const cmt_string& package,
@@ -41,5 +44,5 @@
                    const cmt_string& native_version,
                    Use* context_use,
-                   State specified_auto_imports = Unspecified);
+                   const State specified_auto_imports = Unspecified);
   static void move (Use* use1);
   static void reorder (Use* use1, Use* use2);
@@ -120,5 +123,9 @@
 
   void change_path (const cmt_string& path);
-  void set_auto_imports (State new_state);
+  void set_auto_imports (State context_state, State specified_state,
+			 cmt_map <Use*, bool>& visited);
+  //  void set_auto_imports (State new_state);
+  //void set_auto_imports_state (cmt_vector<bool>& auto_imports_states,
+  //cmt_map <Use*, bool>& visited);
   void set_native_version (bool state);
   bool has_native_version () const;
