Clang import; Always set Scope::classDef token

This commit is contained in:
Daniel Marjamäki 2020-01-23 10:33:29 +01:00
parent 6a07c2f71a
commit fcd6c62a0d
1 changed files with 16 additions and 19 deletions

View File

@ -243,8 +243,8 @@ namespace clangimport {
Token *createTokens(TokenList *tokenList); Token *createTokens(TokenList *tokenList);
Token *addtoken(TokenList *tokenList, const std::string &str, bool valueType=true); Token *addtoken(TokenList *tokenList, const std::string &str, bool valueType=true);
void addTypeTokens(TokenList *tokenList, const std::string &str); void addTypeTokens(TokenList *tokenList, const std::string &str);
Scope *createScope(TokenList *tokenList, Scope::ScopeType scopeType, AstNodePtr astNode); Scope *createScope(TokenList *tokenList, Scope::ScopeType scopeType, AstNodePtr astNode, const Token *def);
Scope *createScope(TokenList *tokenList, Scope::ScopeType scopeType, const std::vector<AstNodePtr> &children); Scope *createScope(TokenList *tokenList, Scope::ScopeType scopeType, const std::vector<AstNodePtr> &children, const Token *def);
Token *createTokensCall(TokenList *tokenList); Token *createTokensCall(TokenList *tokenList);
void createTokensFunctionDecl(TokenList *tokenList); void createTokensFunctionDecl(TokenList *tokenList);
void createTokensForCXXRecord(TokenList *tokenList); void createTokensForCXXRecord(TokenList *tokenList);
@ -452,13 +452,13 @@ void clangimport::AstNode::setValueType(Token *tok)
return; return;
} }
Scope *clangimport::AstNode::createScope(TokenList *tokenList, Scope::ScopeType scopeType, AstNodePtr astNode) Scope *clangimport::AstNode::createScope(TokenList *tokenList, Scope::ScopeType scopeType, AstNodePtr astNode, const Token *def)
{ {
std::vector<AstNodePtr> children{astNode}; std::vector<AstNodePtr> children{astNode};
return createScope(tokenList, scopeType, children); return createScope(tokenList, scopeType, children, def);
} }
Scope *clangimport::AstNode::createScope(TokenList *tokenList, Scope::ScopeType scopeType, const std::vector<AstNodePtr> &children) Scope *clangimport::AstNode::createScope(TokenList *tokenList, Scope::ScopeType scopeType, const std::vector<AstNodePtr> &children, const Token *def)
{ {
SymbolDatabase *symbolDatabase = mData->mSymbolDatabase; SymbolDatabase *symbolDatabase = mData->mSymbolDatabase;
@ -467,6 +467,7 @@ Scope *clangimport::AstNode::createScope(TokenList *tokenList, Scope::ScopeType
symbolDatabase->scopeList.push_back(Scope(nullptr, nullptr, nestedIn)); symbolDatabase->scopeList.push_back(Scope(nullptr, nullptr, nestedIn));
Scope *scope = &symbolDatabase->scopeList.back(); Scope *scope = &symbolDatabase->scopeList.back();
scope->type = scopeType; scope->type = scopeType;
scope->classDef = def;
Token *bodyStart = children[0]->addtoken(tokenList, "{"); Token *bodyStart = children[0]->addtoken(tokenList, "{");
tokenList->back()->scope(scope); tokenList->back()->scope(scope);
for (AstNodePtr astNode: children) { for (AstNodePtr astNode: children) {
@ -635,8 +636,7 @@ Token *clangimport::AstNode::createTokens(TokenList *tokenList)
par1->astOperand1(forToken); par1->astOperand1(forToken);
par1->astOperand2(colon); par1->astOperand2(colon);
Scope *scope = createScope(tokenList, Scope::ScopeType::eFor, children[6]); createScope(tokenList, Scope::ScopeType::eFor, children[6], forToken);
scope->classDef = forToken;
return nullptr; return nullptr;
} }
if (nodeType == CXXMethodDecl) { if (nodeType == CXXMethodDecl) {
@ -703,7 +703,7 @@ Token *clangimport::AstNode::createTokens(TokenList *tokenList)
return children[0]->createTokens(tokenList); return children[0]->createTokens(tokenList);
if (nodeType == DoStmt) { if (nodeType == DoStmt) {
addtoken(tokenList, "do"); addtoken(tokenList, "do");
createScope(tokenList, Scope::ScopeType::eDo, children[0]); createScope(tokenList, Scope::ScopeType::eDo, children[0], tokenList->back());
Token *tok1 = addtoken(tokenList, "while"); Token *tok1 = addtoken(tokenList, "while");
Token *par1 = addtoken(tokenList, "("); Token *par1 = addtoken(tokenList, "(");
Token *expr = children[1]->createTokens(tokenList); Token *expr = children[1]->createTokens(tokenList);
@ -737,8 +737,7 @@ Token *clangimport::AstNode::createTokens(TokenList *tokenList)
sep1->astOperand2(sep2); sep1->astOperand2(sep2);
sep2->astOperand1(expr2); sep2->astOperand1(expr2);
sep2->astOperand2(expr3); sep2->astOperand2(expr3);
Scope *scope = createScope(tokenList, Scope::ScopeType::eFor, children[4]); createScope(tokenList, Scope::ScopeType::eFor, children[4], forToken);
scope->classDef = forToken;
return nullptr; return nullptr;
} }
if (nodeType == FunctionDecl) { if (nodeType == FunctionDecl) {
@ -773,10 +772,10 @@ Token *clangimport::AstNode::createTokens(TokenList *tokenList)
Token *par2 = addtoken(tokenList, ")"); Token *par2 = addtoken(tokenList, ")");
par1->link(par2); par1->link(par2);
par2->link(par1); par2->link(par1);
createScope(tokenList, Scope::ScopeType::eIf, then); createScope(tokenList, Scope::ScopeType::eIf, then, iftok);
if (else_) { if (else_) {
else_->addtoken(tokenList, "else"); else_->addtoken(tokenList, "else");
createScope(tokenList, Scope::ScopeType::eElse, else_); createScope(tokenList, Scope::ScopeType::eElse, else_, tokenList->back());
} }
return nullptr; return nullptr;
} }
@ -836,8 +835,7 @@ Token *clangimport::AstNode::createTokens(TokenList *tokenList)
Token *defToken = addtoken(tokenList, "namespace"); Token *defToken = addtoken(tokenList, "namespace");
Token *nameToken = (mExtTokens[mExtTokens.size() - 2].compare(0,4,"col:") == 0) ? Token *nameToken = (mExtTokens[mExtTokens.size() - 2].compare(0,4,"col:") == 0) ?
addtoken(tokenList, mExtTokens.back()) : nullptr; addtoken(tokenList, mExtTokens.back()) : nullptr;
Scope *scope = createScope(tokenList, Scope::ScopeType::eNamespace, children); Scope *scope = createScope(tokenList, Scope::ScopeType::eNamespace, children, defToken);
scope->classDef = defToken;
if (nameToken) if (nameToken)
scope->className = nameToken->str(); scope->className = nameToken->str();
return nullptr; return nullptr;
@ -860,7 +858,7 @@ Token *clangimport::AstNode::createTokens(TokenList *tokenList)
if (children.empty()) if (children.empty())
addtoken(tokenList, ";"); addtoken(tokenList, ";");
else { else {
Scope *recordScope = createScope(tokenList, Scope::ScopeType::eStruct, children); Scope *recordScope = createScope(tokenList, Scope::ScopeType::eStruct, children, classDef);
mData->mSymbolDatabase->typeList.push_back(Type(classDef, recordScope, classDef->scope())); mData->mSymbolDatabase->typeList.push_back(Type(classDef, recordScope, classDef->scope()));
recordScope->definedType = &mData->mSymbolDatabase->typeList.back(); recordScope->definedType = &mData->mSymbolDatabase->typeList.back();
if (!recordName.empty()) if (!recordName.empty())
@ -885,7 +883,7 @@ Token *clangimport::AstNode::createTokens(TokenList *tokenList)
par2->link(par1); par2->link(par1);
par1->astOperand1(tok1); par1->astOperand1(tok1);
par1->astOperand2(expr); par1->astOperand2(expr);
createScope(tokenList, Scope::ScopeType::eSwitch, children.back()); createScope(tokenList, Scope::ScopeType::eSwitch, children.back(), tok1);
return nullptr; return nullptr;
} }
if (nodeType == TypedefDecl) { if (nodeType == TypedefDecl) {
@ -928,7 +926,7 @@ Token *clangimport::AstNode::createTokens(TokenList *tokenList)
Token *par2 = addtoken(tokenList, ")"); Token *par2 = addtoken(tokenList, ")");
par1->link(par2); par1->link(par2);
par2->link(par1); par2->link(par1);
createScope(tokenList, Scope::ScopeType::eWhile, body); createScope(tokenList, Scope::ScopeType::eWhile, body, whiletok);
return nullptr; return nullptr;
} }
return addtoken(tokenList, "?" + nodeType + "?"); return addtoken(tokenList, "?" + nodeType + "?");
@ -1040,8 +1038,7 @@ void clangimport::AstNode::createTokensForCXXRecord(TokenList *tokenList)
addtoken(tokenList, ";"); addtoken(tokenList, ";");
return; return;
} }
Scope *scope = createScope(tokenList, Scope::ScopeType::eClass, children2); Scope *scope = createScope(tokenList, Scope::ScopeType::eClass, children2, classToken);
scope->classDef = classToken;
scope->className = className; scope->className = className;
mData->mSymbolDatabase->typeList.push_back(Type(classToken, scope, classToken->scope())); mData->mSymbolDatabase->typeList.push_back(Type(classToken, scope, classToken->scope()));
scope->definedType = &mData->mSymbolDatabase->typeList.back(); scope->definedType = &mData->mSymbolDatabase->typeList.back();