From c00f8d69777ca3e454265ba1726f30345d7c7507 Mon Sep 17 00:00:00 2001 From: DenisDudarenko Date: Sat, 16 Mar 2024 15:31:06 +0300 Subject: [PATCH] Fix searching letters and vars --- .../Transformations/set_implicit_none.cpp | 136 +++++++++++++----- 1 file changed, 101 insertions(+), 35 deletions(-) diff --git a/sapfor/experts/Sapfor_2017/_src/Transformations/set_implicit_none.cpp b/sapfor/experts/Sapfor_2017/_src/Transformations/set_implicit_none.cpp index cb719e4..dfefa2e 100644 --- a/sapfor/experts/Sapfor_2017/_src/Transformations/set_implicit_none.cpp +++ b/sapfor/experts/Sapfor_2017/_src/Transformations/set_implicit_none.cpp @@ -3,23 +3,32 @@ using std::vector; using std::map; +using std::set; map types; -vector allVars; +set allVars; vector varsWithoutDecl; const char commonIntLetters[6] = {'i', 'j', 'k', 'm', 'n', 'l'}; void InitTypes(); void FillCommonTypes(); +void CleanTypes(); void FunctionImplicitCheck(SgStatement* function); +void FindAllVars(SgExpression* expr); +char AddLettersToMap(SgExpression* params, SgType* type); void ImplicitCheck(SgFile* file) { - for (int funcNum = 0; funcNum < file->numberOfFunctions(); funcNum++) + for (int functionNumber = 0; functionNumber < file->numberOfFunctions(); functionNumber++) { - SgStatement* function = file->functions(funcNum); + InitTypes(); + FillCommonTypes(); + + SgStatement* function = file->functions(functionNumber); FunctionImplicitCheck(function); + + CleanTypes(); } return; @@ -30,27 +39,23 @@ void FunctionImplicitCheck(SgStatement* function) auto implicitNoneDeclaration = new SgStatement(IMPL_DECL); auto hasImplicitNone = false; - InitTypes(); - FillCommonTypes(); - - for (SgStatement* statement = function->lexNext(); statement = statement->lexNext(); statement != NULL) + for (SgStatement* statement = function; statement = statement->lexNext(); statement != NULL) { if (statement->variant() == IMPL_DECL) { - SgImplicitStmt* implicitSt = isSgImplicitStmt(statement); - if (implicitSt) + SgImplicitStmt* implicitStatement = isSgImplicitStmt(statement); + if (implicitStatement != NULL) { - int numberOfTypes = implicitSt->numberOfImplicitTypes(); + int numberOfTypes = implicitStatement->numberOfImplicitTypes(); if (numberOfTypes > 0) { for (int j = 0; j < numberOfTypes; j++) { - SgType* type = implicitSt->implicitType(j); - SgExpression* letters = implicitSt->implicitRangeList(j); + SgType* type = implicitStatement->implicitType(j); + SgExpression* lettersExpression = implicitStatement->implicitRangeList(j); - // get real letters - types['o'] = type; + AddLettersToMap(lettersExpression, type); } } else @@ -65,19 +70,19 @@ void FunctionImplicitCheck(SgStatement* function) } } - for (SgSymbol* symbol = function->symbol(); symbol != NULL; symbol = symbol->next()) + for (SgStatement* statement = function; statement != function->lastExecutable(); statement = statement->lexNext()) { - if (symbol != NULL && symbol->declaredInStmt() != NULL) + for (int expressionNumber = 0; expressionNumber < 3; expressionNumber++) { - allVars.push_back(symbol); + FindAllVars(statement->expr(expressionNumber)); } } for (auto var : allVars) { vector _; - SgStatement* declaredIn = declaratedInStmt(var, &_, false); - if (declaredIn == NULL) + SgStatement* declaredInStatement = declaratedInStmt(var, &_, false); + if (declaredInStatement == NULL) { char c = var->identifier()[0]; @@ -94,51 +99,112 @@ void FunctionImplicitCheck(SgStatement* function) if (!hasImplicitNone) { - for (SgStatement* i = function->lexNext(); i != NULL;) + for (SgStatement* statement = function->lexNext(); statement != NULL;) { - if (i->variant() == IMPL_DECL) + if (statement->variant() == IMPL_DECL) { - auto tmp = i; - i = i->lexNext(); - tmp->deleteStmt(); + auto tmpStatement = statement; + statement = statement->lexNext(); + tmpStatement->deleteStmt(); } else { - i = i->lexNext(); + statement = statement->lexNext(); } } function->insertStmtBefore(*implicitNoneDeclaration, *function); } - printf("%s", function->unparse()); - return; } void InitTypes() { - for (char i = 'a'; i <= 'z'; i++) + for (char letter = 'a'; letter <= 'z'; letter++) { - types[i] = NULL; + types[letter] = NULL; } } void FillCommonTypes() { - for (char i: commonIntLetters) + for (char letter : commonIntLetters) { - if (types[i] == NULL) + if (types[letter] == NULL) { - types[i] = new SgType(T_INT); + types[letter] = new SgType(T_INT); } } - for (auto i : types) + for (auto letter : types) { - if (i.second == NULL) + if (letter.second == NULL) { - types[i.first] = new SgType(T_FLOAT); + types[letter.first] = new SgType(T_FLOAT); } } } + +void CleanTypes() +{ + types.clear(); + allVars.clear(); + varsWithoutDecl.clear(); +} + +void FindAllVars(SgExpression* expr) +{ + if (expr == NULL) + { + return; + } + + if (expr->symbol() != NULL) + { + allVars.insert(expr->symbol()); + } + + FindAllVars(expr->lhs()); + FindAllVars(expr->rhs()); +} + +char AddLettersToMap(SgExpression* expr, SgType* type) +{ + if (expr == NULL) + { + return NULL; + } + + if (expr->variant() == CHAR_VAL) + { + SgValueExp* val = isSgValueExp(expr); + return val->charValue(); + } + + char leftVal = AddLettersToMap(expr->lhs(), type); + char rightVal = AddLettersToMap(expr->rhs(), type); + + if (expr->variant() == DDOT) + { + if (leftVal != NULL && rightVal != NULL) + { + for (char letter = leftVal; letter <= rightVal; letter++) + { + types[letter] = type; + } + } + } + + if (expr->variant() == EXPR_LIST) + { + if (leftVal != NULL) { + types[leftVal] = type; + } + if (rightVal != NULL) { + types[rightVal] = type; + } + } + + return NULL; +} \ No newline at end of file