| 1 |
#include "CxxTemplateFunction.h" |
| 2 |
#include "CxxScannerTokens.h" |
| 3 |
#include <set> |
| 4 |
|
| 5 |
CxxTemplateFunction::CxxTemplateFunction(TagEntryPtr tag) |
| 6 |
{ |
| 7 |
m_scanner = ::LexerNew(tag->GetPatternClean(), kLexerOpt_None); |
| 8 |
m_sigScanner = ::LexerNew(tag->GetSignature(), kLexerOpt_None); |
| 9 |
} |
| 10 |
|
| 11 |
CxxTemplateFunction::~CxxTemplateFunction() |
| 12 |
{ |
| 13 |
if(m_scanner) { |
| 14 |
::LexerDestroy(&m_scanner); |
| 15 |
} |
| 16 |
if(m_sigScanner) { |
| 17 |
::LexerDestroy(&m_sigScanner); |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
void CxxTemplateFunction::ParseDefinitionList() |
| 22 |
{ |
| 23 |
m_list.Clear(); |
| 24 |
int depth(0); |
| 25 |
CxxLexerToken token; |
| 26 |
|
| 27 |
// scan until we find 'template' keyword |
| 28 |
while(::LexerNext(m_scanner, token)) { |
| 29 |
if(token.GetType() == T_TEMPLATE) { |
| 30 |
break; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
if(!token.GetType()) return; // EOF |
| 35 |
|
| 36 |
// Loop until we found the open brace '<' |
| 37 |
while(::LexerNext(m_scanner, token)) { |
| 38 |
if(token.GetType() == '<') { |
| 39 |
++depth; |
| 40 |
break; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
// could not find it |
| 45 |
if(!depth) return; |
| 46 |
|
| 47 |
bool cont = true; |
| 48 |
wxString currentToken; |
| 49 |
while(cont && ::LexerNext(m_scanner, token)) { |
| 50 |
switch(token.GetType()) { |
| 51 |
case T_TYPENAME: |
| 52 |
case T_CLASS: |
| 53 |
// ignore these keywords |
| 54 |
break; |
| 55 |
case ',': |
| 56 |
if(!currentToken.IsEmpty()) { |
| 57 |
m_list.Add(currentToken.Trim().Trim(false)); |
| 58 |
currentToken.Clear(); |
| 59 |
} |
| 60 |
break; |
| 61 |
case '>': |
| 62 |
--depth; |
| 63 |
if(depth == 0) { |
| 64 |
// done |
| 65 |
if(!currentToken.IsEmpty()) { |
| 66 |
m_list.Add(currentToken.Trim().Trim(false)); |
| 67 |
currentToken.Clear(); |
| 68 |
} |
| 69 |
cont = false; |
| 70 |
} else { |
| 71 |
currentToken << token.GetWXString() << " "; |
| 72 |
} |
| 73 |
break; |
| 74 |
case '<': |
| 75 |
++depth; |
| 76 |
currentToken << token.GetWXString() << " "; |
| 77 |
break; |
| 78 |
default: |
| 79 |
currentToken << token.GetWXString() << " "; |
| 80 |
break; |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
bool CxxTemplateFunction::CanTemplateArgsDeduced() |
| 86 |
{ |
| 87 |
ParseDefinitionList(); // Initializes the m_list array |
| 88 |
|
| 89 |
std::set<wxString> words; |
| 90 |
CxxLexerToken token; |
| 91 |
|
| 92 |
// Collect all the identifiers and keep them inside a set |
| 93 |
while(::LexerNext(m_sigScanner, token)) { |
| 94 |
if(token.GetType() == T_IDENTIFIER) { |
| 95 |
words.insert(token.GetWXString()); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
// Loop over the function arguments list and check that all of them |
| 100 |
// exists in the set we created from the function signature |
| 101 |
for(size_t i=0; i<m_list.GetCount(); ++i) { |
| 102 |
if(words.count(m_list.Item(i)) == 0) { |
| 103 |
// this template argument could not be found in the function signature... |
| 104 |
return false; |
| 105 |
} |
| 106 |
} |
| 107 |
return true; |
| 108 |
} |