/****************************************************************************
**
** Copyright (C) 2004-2005 Trolltech AS. All rights reserved.
**
** This file is part of the porting application of the Qt Toolkit.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
**   information about Qt Commercial License Agreements.
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#include "codemodelattributes.h"
#include <iostream>
#include "tokenengine.h"

using namespace std;
using namespace CodeModel;
using namespace TokenEngine;

/*
     Walk the codemodel.
*/
void CodeModelAttributes::createAttributes(TranslationUnit translationUnit)
{
    m_translationUnit = translationUnit;
    parseScope(const_cast<CodeModel::NamespaceScope *>(translationUnit.codeModel()));
}

/*
    Create attributes for each name use and assign to the token.
*/
void CodeModelAttributes::parseNameUse(CodeModel::NameUse *nameUse)
{
    //get the container for this token
    const int nameTokenIndex = nameUse->nameAST()->startToken();
    TokenSectionSequence tokens = m_translationUnit.tokens();
    TokenContainer container = tokens.tokenContainer(nameTokenIndex);
    const int containerIndex = tokens.containerIndex(nameTokenIndex);
    TokenAttributes *attributes = container.tokenAttributes();

    //add attributes this namnUse
    attributes->addAttribute(containerIndex, "nameUse", nameUse->name());
    attributes->addAttribute(containerIndex, "parentScope",
                             nameUse->declaration()->parent()->name() );
    if(CodeModel::Scope * skop = nameUse->declaration()->parent()->parent()) {
        attributes->addAttribute(containerIndex, "grandParentScope", skop->name() );
    }
//    cout << "add name use" << nameUse->name().constData()  << endl;
}
/*
    Create attributes for members and assign to token.
*/
void CodeModelAttributes::parseMember(CodeModel::Member *member)
{
    if(!member || !member->nameAST() || member->name() == QByteArray())
        return;

    //get the container for this token
    const int nameTokenIndex = member->nameAST()->startToken();
    TokenSectionSequence tokens = m_translationUnit.tokens();
    TokenContainer container = tokens.tokenContainer(nameTokenIndex);
    const int containerIndex = tokens.containerIndex(nameTokenIndex);
    TokenAttributes *attributes = container.tokenAttributes();

     //add attributes for this declaration
    attributes->addAttribute(containerIndex, "declaration", member->name());

    CodeModelWalker::parseMember(member);
}


