// 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 "Puma/CTemplateInstance.h"
#include "Puma/CTemplateInfo.h"
#include "Puma/DeducedArgument.h"
#include "Puma/CConstant.h"

#include "MatchTemplateArg.h"

bool MatchTemplateArgList::matches (CTemplateInstance *instance) const {
  unsigned deduced_args = instance->DeducedArgs ();
  unsigned arg = 0;
  for (const_iterator iter = begin (); iter != end (); ++iter) {
    // check if the current template match arg is '...'
    if ((*iter)->is_ellipses ())
      return true;
    // check if we have more match arguments than template arguments
    if (arg + 1 > deduced_args)
      return false;
    // check if the current match argument matches the template argument
    else if (!(*iter)->matches (instance->DeducedArg (arg)))
      return false;
    // go to the next argument
    arg++;
  }
  // check if we have examined all deduced template arguments
  if (arg != deduced_args)
    return false;
    
  // everything fine, the list matches!
  return true;
}

bool MTA_Type::matches (DeducedArgument *arg) const {
  return arg->Type () && _match_type.matches (arg->Type ());
}

bool MTA_Value::matches (DeducedArgument *arg) const {
  // TODO: more kinds of values
  CConstant *val = arg->Value ();
  return val && (val->isSigned () || val->isUnsigned ()) &&
    val->convert_to_int () == _val;
}
