Fix searching letters and vars
This commit is contained in:
@@ -3,23 +3,32 @@
|
|||||||
|
|
||||||
using std::vector;
|
using std::vector;
|
||||||
using std::map;
|
using std::map;
|
||||||
|
using std::set;
|
||||||
|
|
||||||
map<char, SgType*> types;
|
map<char, SgType*> types;
|
||||||
vector<SgSymbol*> allVars;
|
set<SgSymbol*> allVars;
|
||||||
vector<SgSymbol*> varsWithoutDecl;
|
vector<SgSymbol*> varsWithoutDecl;
|
||||||
const char commonIntLetters[6] = {'i', 'j', 'k', 'm', 'n', 'l'};
|
const char commonIntLetters[6] = {'i', 'j', 'k', 'm', 'n', 'l'};
|
||||||
|
|
||||||
void InitTypes();
|
void InitTypes();
|
||||||
void FillCommonTypes();
|
void FillCommonTypes();
|
||||||
|
void CleanTypes();
|
||||||
void FunctionImplicitCheck(SgStatement* function);
|
void FunctionImplicitCheck(SgStatement* function);
|
||||||
|
void FindAllVars(SgExpression* expr);
|
||||||
|
char AddLettersToMap(SgExpression* params, SgType* type);
|
||||||
|
|
||||||
void ImplicitCheck(SgFile* file)
|
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);
|
FunctionImplicitCheck(function);
|
||||||
|
|
||||||
|
CleanTypes();
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
@@ -30,27 +39,23 @@ void FunctionImplicitCheck(SgStatement* function)
|
|||||||
auto implicitNoneDeclaration = new SgStatement(IMPL_DECL);
|
auto implicitNoneDeclaration = new SgStatement(IMPL_DECL);
|
||||||
auto hasImplicitNone = false;
|
auto hasImplicitNone = false;
|
||||||
|
|
||||||
InitTypes();
|
for (SgStatement* statement = function; statement = statement->lexNext(); statement != NULL)
|
||||||
FillCommonTypes();
|
|
||||||
|
|
||||||
for (SgStatement* statement = function->lexNext(); statement = statement->lexNext(); statement != NULL)
|
|
||||||
{
|
{
|
||||||
if (statement->variant() == IMPL_DECL)
|
if (statement->variant() == IMPL_DECL)
|
||||||
{
|
{
|
||||||
SgImplicitStmt* implicitSt = isSgImplicitStmt(statement);
|
SgImplicitStmt* implicitStatement = isSgImplicitStmt(statement);
|
||||||
if (implicitSt)
|
if (implicitStatement != NULL)
|
||||||
{
|
{
|
||||||
int numberOfTypes = implicitSt->numberOfImplicitTypes();
|
int numberOfTypes = implicitStatement->numberOfImplicitTypes();
|
||||||
|
|
||||||
if (numberOfTypes > 0)
|
if (numberOfTypes > 0)
|
||||||
{
|
{
|
||||||
for (int j = 0; j < numberOfTypes; j++)
|
for (int j = 0; j < numberOfTypes; j++)
|
||||||
{
|
{
|
||||||
SgType* type = implicitSt->implicitType(j);
|
SgType* type = implicitStatement->implicitType(j);
|
||||||
SgExpression* letters = implicitSt->implicitRangeList(j);
|
SgExpression* lettersExpression = implicitStatement->implicitRangeList(j);
|
||||||
|
|
||||||
// get real letters
|
AddLettersToMap(lettersExpression, type);
|
||||||
types['o'] = type;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
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)
|
for (auto var : allVars)
|
||||||
{
|
{
|
||||||
vector<SgStatement*> _;
|
vector<SgStatement*> _;
|
||||||
SgStatement* declaredIn = declaratedInStmt(var, &_, false);
|
SgStatement* declaredInStatement = declaratedInStmt(var, &_, false);
|
||||||
if (declaredIn == NULL)
|
if (declaredInStatement == NULL)
|
||||||
{
|
{
|
||||||
char c = var->identifier()[0];
|
char c = var->identifier()[0];
|
||||||
|
|
||||||
@@ -94,51 +99,112 @@ void FunctionImplicitCheck(SgStatement* function)
|
|||||||
|
|
||||||
if (!hasImplicitNone)
|
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;
|
auto tmpStatement = statement;
|
||||||
i = i->lexNext();
|
statement = statement->lexNext();
|
||||||
tmp->deleteStmt();
|
tmpStatement->deleteStmt();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
i = i->lexNext();
|
statement = statement->lexNext();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function->insertStmtBefore(*implicitNoneDeclaration, *function);
|
function->insertStmtBefore(*implicitNoneDeclaration, *function);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("%s", function->unparse());
|
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void InitTypes()
|
void InitTypes()
|
||||||
{
|
{
|
||||||
for (char i = 'a'; i <= 'z'; i++)
|
for (char letter = 'a'; letter <= 'z'; letter++)
|
||||||
{
|
{
|
||||||
types[i] = NULL;
|
types[letter] = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void FillCommonTypes()
|
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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user