Tokenizer::simplifyTemplates: Broke out the functionality that instantiates a template

This commit is contained in:
Daniel Marjamäki 2011-02-12 20:58:45 +01:00
parent 9021f0f180
commit 4d1aae5859
2 changed files with 327 additions and 310 deletions

View File

@ -2791,56 +2791,14 @@ void Tokenizer::simplifyTemplatesUseDefaultArgumentValues(const std::list<Token
}
}
void Tokenizer::simplifyTemplates()
void Tokenizer::simplifyTemplatesInstantiate(std::list<Token *> &used,
const Token *tok,
std::set<std::string> &expandedtemplates)
{
std::set<std::string> expandedtemplates(simplifyTemplatesExpandSpecialized());
// Locate templates..
std::list<Token *> templates(simplifyTemplatesGetTemplateDeclarations());
if (templates.empty())
{
removeTemplates(_tokens);
return;
}
// There are templates..
// Remove "typename" unless used in template arguments..
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (tok->str() == "typename")
tok->deleteThis();
if (Token::simpleMatch(tok, "template <"))
{
while (tok && tok->str() != ">")
tok = tok->next();
if (!tok)
break;
}
}
// Locate possible instantiations of templates..
std::list<Token *> used(simplifyTemplatesGetTemplateInstantiations());
// No template instantiations? Then remove all templates.
if (used.empty())
{
removeTemplates(_tokens);
return;
}
// Template arguments with default values
simplifyTemplatesUseDefaultArgumentValues(templates, used);
// expand templates
// this variable is not used at the moment. the intention was to
// allow continous instantiations until all templates has been expanded
bool done = false;
//while (!done)
{
done = true;
for (std::list<Token *>::iterator iter1 = templates.begin(); iter1 != templates.end(); ++iter1)
{
Token *tok = *iter1;
std::vector<const Token *> type;
for (tok = tok->tokAt(2); tok && tok->str() != ">"; tok = tok->next())
{
@ -2849,7 +2807,7 @@ void Tokenizer::simplifyTemplates()
}
// bail out if the end of the file was reached
if (!tok)
break;
return;
// get the position of the template name
unsigned char namepos = 0;
@ -2880,7 +2838,7 @@ void Tokenizer::simplifyTemplates()
else
Check::reportError(errmsg);
}
continue;
return;
}
if ((tok->tokAt(namepos)->str() == "*" || tok->tokAt(namepos)->str() == "&"))
++namepos;
@ -2904,7 +2862,7 @@ void Tokenizer::simplifyTemplates()
std::string::size_type sz1 = used.size();
unsigned int recursiveCount = 0;
for (std::list<Token *>::iterator iter2 = used.begin(); iter2 != used.end(); ++iter2)
for (std::list<Token *>::const_iterator iter2 = used.begin(); iter2 != used.end(); ++iter2)
{
// If the size of "used" has changed, simplify calculations
if (sz1 != used.size())
@ -3150,6 +3108,58 @@ void Tokenizer::simplifyTemplates()
}
}
}
}
void Tokenizer::simplifyTemplates()
{
std::set<std::string> expandedtemplates(simplifyTemplatesExpandSpecialized());
// Locate templates..
std::list<Token *> templates(simplifyTemplatesGetTemplateDeclarations());
if (templates.empty())
{
removeTemplates(_tokens);
return;
}
// There are templates..
// Remove "typename" unless used in template arguments..
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (tok->str() == "typename")
tok->deleteThis();
if (Token::simpleMatch(tok, "template <"))
{
while (tok && tok->str() != ">")
tok = tok->next();
if (!tok)
break;
}
}
// Locate possible instantiations of templates..
std::list<Token *> used(simplifyTemplatesGetTemplateInstantiations());
// No template instantiations? Then remove all templates.
if (used.empty())
{
removeTemplates(_tokens);
return;
}
// Template arguments with default values
simplifyTemplatesUseDefaultArgumentValues(templates, used);
// expand templates
bool done = false;
//while (!done)
{
done = true;
for (std::list<Token *>::iterator iter1 = templates.begin(); iter1 != templates.end(); ++iter1)
{
simplifyTemplatesInstantiate(used, *iter1, expandedtemplates);
}
}

View File

@ -423,6 +423,13 @@ public:
void simplifyTemplatesUseDefaultArgumentValues(const std::list<Token *> &templates,
const std::list<Token *> &instantiations);
/**
* Simplify templates : expand all instantiatiations for a template
*/
void simplifyTemplatesInstantiate(std::list<Token *> &used,
const Token *tok,
std::set<std::string> &expandedtemplates);
/**
* Used after simplifyTemplates to perform a little cleanup.
* Sometimes the simplifyTemplates isn't fully successful and then