// This file is part of the AspectC++ compiler 'ac++'.
// Copyright (C) 1999-2003  The 'ac++' developers (see aspectc.org)
//                                                                
// This program 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; either version 2 of 
// the License, or (at your option) any later version.            
//                                                                
// This program 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 this program; if not, write to the Free     
// Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
// MA  02111-1307  USA                                            

#include "AspectIncludes.h"
#include "AspectInfo.h"
#include "Naming.h"

#include "Puma/Unit.h"
#include "Puma/CTree.h"
#include "Puma/CProject.h"
#include "Puma/Filename.h"

// helper function that find the unit in which an aspect 'ai' is defined
Unit *AspectIncludes::aspect_unit (ACAspectInfo *ai) {
  return (Unit*)ai->ClassInfo ()->SourceInfo ()->StartToken ()->token ()->
    belonging_to ();
}

// this function declares that a unit 'iu' depends on the aspect 'ai'
void AspectIncludes::insert (Unit *iu, AspectInfo *ai, ACAspectInfo *acai,
  AspectRef::Kind kind) {
  assert (iu);

  // check if there is already an entry for this unit
  iterator entry = find (iu);
  if (entry == end ()) {
    // there is no entry for the unit 'iu' yet => make a new one
    AspectRefSet new_set;
    new_set.insert (AspectRef (ai, acai, kind));
    UnitAspectRefMap::insert (value_type (iu, new_set));
  }
  else {
    // entry found add the aspect reference to the stored set
    AspectRefSet &set = (*entry).second;
    AspectRefSet::iterator ref_entry =
      set.find (AspectRef (ai, acai, AspectRef::AR_UNKNOWN));
    if (ref_entry == set.end ()) {
      // simply insert a new entry if the aspect has not been found
      set.insert (AspectRef (ai, acai, kind));
    }
    else {
      // a definition request dominates a declaration request...
      AspectRef &ref = (AspectRef&)(*ref_entry);
      if (kind > ref._kind)
        ref._kind = kind;
    }
  }
}

// generate the #include directive for a given aspect header unit
string AspectIncludes::generate (CProject &project, ACAspectInfo *acai) {
  string include_directive;

  Unit *unit = aspect_unit (acai);
  
  // get the name, which we have to use to include the file
  Filename include_file = project.getInclString (unit->name ());
    
  include_directive += "#include \"";
  include_directive += include_file.name ();
  include_directive += "\"";
  
  return include_directive;
}

// generate the #include directives for a given unit
string AspectIncludes::generate (CProject &prj, const_iterator entry) const {
  string includes;
  const AspectRefSet &set = (*entry).second;
  for (AspectRefSet::const_iterator iter = set.begin ();
    iter != set.end (); ++iter) {

    switch ((*iter)._kind) {
      case AspectRef::AR_DECL: 
        includes += "class ";
        includes += (*iter)._concrete_aspect->name ();
        includes += ";\n";
        break;
      case AspectRef::AR_ADVICE:
        {
          includes += (*iter)._concrete_aspect->ifct_decls ();
          Unit *unit = aspect_unit (*iter);
          assert (unit && unit->isFile ());
          stringstream define;
          define << "#ifndef __ac_need_";
          Naming::mangle_file (define, (FileUnit*)unit);
          define << endl << "#define __ac_need_";
          Naming::mangle_file (define, (FileUnit*)unit);
          define << endl << "#endif" << endl;
          includes += define.str ();          
        }
        break;
      default:
        cout << "aspect ref type not yet implemented" << endl;
    }
  }
  return includes;
}
