[opengtl-commits] [393] move the variables management code in the variables manager |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/opengtl-commits Archives
]
Revision: 393
Author: cyrille
Date: 2008-09-14 09:49:17 +0200 (Sun, 14 Sep 2008)
Log Message:
-----------
move the variables management code in the variables manager
Modified Paths:
--------------
trunk/OpenGTL/OpenCTL/OpenCTL/compiler/ParserNG.cpp
trunk/OpenGTL/OpenGTL/GTLCore/AST/Statement.cpp
trunk/OpenGTL/OpenGTL/GTLCore/AST/Statement.h
trunk/OpenGTL/OpenGTL/GTLCore/ParserBase_p.cpp
trunk/OpenGTL/OpenGTL/GTLCore/ParserBase_p.h
trunk/OpenGTL/OpenGTL/GTLCore/VariablesManager_p.cpp
trunk/OpenGTL/OpenGTL/GTLCore/VariablesManager_p.h
trunk/OpenGTL/OpenShiva/OpenShiva/Parser_p.cpp
Modified: trunk/OpenGTL/OpenCTL/OpenCTL/compiler/ParserNG.cpp
===================================================================
--- trunk/OpenGTL/OpenCTL/OpenCTL/compiler/ParserNG.cpp 2008-09-13 21:26:11 UTC (rev 392)
+++ trunk/OpenGTL/OpenCTL/OpenCTL/compiler/ParserNG.cpp 2008-09-14 07:49:17 UTC (rev 393)
@@ -42,6 +42,7 @@
#include "GTLCore/Function_p.h"
#include "GTLCore/TypeManager.h"
+#include "GTLCore/VariablesManager_p.h"
namespace AST = GTLCore::AST;
using namespace OpenCTL;
@@ -67,7 +68,7 @@
OCTL_DEBUG("Parsing with ParserNG");
OCTL_ASSERT( not d->tree );
d->tree = new AST::Tree;
- startContext();
+ variablesManager()->startContext();
// parse module header
parseModuleHeader();
// parse module body
@@ -186,7 +187,7 @@
reportError("Constant '" + scopedName.toString() + "' has allready been declared", currentToken());
} else {
AST::GlobalConstantDeclaration* gcd = new AST::GlobalConstantDeclaration( scopedName, type , expression );
- declareVariable( scopedName, gcd->variable() );
+ variablesManager()->declareConstant( scopedName, gcd->variable() );
d->tree->append( gcd );
}
}
@@ -252,9 +253,9 @@
return parseReturnStatement();
case GTLCore::Token::STARTBRACE:
{
- startContext();
+ variablesManager()->startContext();
AST::Statement* statement = parseStatementList();
- endContext();
+ variablesManager()->endContext();
return statement;
}
case GTLCore::Token::PRINT:
Modified: trunk/OpenGTL/OpenGTL/GTLCore/AST/Statement.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/AST/Statement.cpp 2008-09-13 21:26:11 UTC (rev 392)
+++ trunk/OpenGTL/OpenGTL/GTLCore/AST/Statement.cpp 2008-09-14 07:49:17 UTC (rev 393)
@@ -207,7 +207,7 @@
//------------------------- ReturnStatement -------------------------//
-ReturnStatement::ReturnStatement( Expression* _returnExpr, std::list<VariableNG*> _variablesToClean ) : m_returnExpr( _returnExpr ), m_variablesToClean( _variablesToClean )
+ReturnStatement::ReturnStatement( Expression* _returnExpr, AST::Statement* _garbageCollectionStatement ) : m_returnExpr( _returnExpr ), m_garbageCollectionStatement( _garbageCollectionStatement )
{
if( m_returnExpr )
{
@@ -218,11 +218,11 @@
ReturnStatement::~ReturnStatement()
{
delete m_returnExpr;
+ delete m_garbageCollectionStatement;
}
llvm::BasicBlock* ReturnStatement::generateStatement( GenerationContext& _context, llvm::BasicBlock* _bb) const
{
- GTL_DEBUG( m_variablesToClean.size() << " " << _context.function()->name() );
// FIXME share the cleanup code
if( m_returnExpr )
{
@@ -232,11 +232,7 @@
_bb = Visitor::getVisitorFor( m_returnExpr->type() )->mark( _context, _bb, result.value(), m_returnExpr->type(), CodeGenerator::integerToConstant( 1 ) );
}
_bb = _context.flushDelayedStatement( _bb );
- for( std::list<VariableNG*>::const_iterator it = m_variablesToClean.begin();
- it != m_variablesToClean.end(); ++it)
- {
- _bb = (*it)->cleanUp( _context, _bb, result.value());
- }
+// _bb = m_garbageCollectionStatement->generateStatement( _context, _bb ); // FIXME uncomment
llvm::Value* resultValue = result.value();
if( m_returnExpr->type()->dataType() != Type::ARRAY
and m_returnExpr->type()->dataType() != Type::STRUCTURE )
@@ -250,11 +246,7 @@
llvm::ReturnInst::Create( resultValue, _bb);
} else {
_bb = _context.flushDelayedStatement( _bb );
- for( std::list<VariableNG*>::const_iterator it = m_variablesToClean.begin();
- it != m_variablesToClean.end(); ++it)
- {
- _bb = (*it)->cleanUp(_context, _bb, 0);
- }
+// _bb = m_garbageCollectionStatement->generateStatement( _context, _bb ); // FIXME uncomment
llvm::ReturnInst::Create( _bb );
}
return _bb;
Modified: trunk/OpenGTL/OpenGTL/GTLCore/AST/Statement.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/AST/Statement.h 2008-09-13 21:26:11 UTC (rev 392)
+++ trunk/OpenGTL/OpenGTL/GTLCore/AST/Statement.h 2008-09-14 07:49:17 UTC (rev 393)
@@ -168,12 +168,12 @@
* @param _variablesToClean list of variables used so far, that will need to be
* cleaned by the return expression
*/
- ReturnStatement( Expression* _returnExpr, std::list<VariableNG*> _variablesToClean );
+ ReturnStatement( Expression* _returnExpr, AST::Statement* _garbageCollectionStatement );
~ReturnStatement();
virtual llvm::BasicBlock* generateStatement( GenerationContext&, llvm::BasicBlock* ) const;
private:
Expression* m_returnExpr;
- std::list<VariableNG*> m_variablesToClean;
+ AST::Statement* m_garbageCollectionStatement;
};
/**
* @internal
Modified: trunk/OpenGTL/OpenGTL/GTLCore/ParserBase_p.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/ParserBase_p.cpp 2008-09-13 21:26:11 UTC (rev 392)
+++ trunk/OpenGTL/OpenGTL/GTLCore/ParserBase_p.cpp 2008-09-14 07:49:17 UTC (rev 393)
@@ -46,24 +46,19 @@
#include "Token_p.h"
#include "Function_p.h"
#include "Utils_p.h"
+#include "VariablesManager_p.h"
#include "TypeManager.h"
#include "TypeManager_p.h"
using namespace GTLCore;
-struct ParserBase::Context
-{
- std::map< ScopedName, VariableNG* > variables;
-};
-
struct ParserBase::Private {
Token currentToken;
CompilerBase* compiler;
LexerBase* lexer;
String nameSpace;
- std::list< Context > contextes;
- std::list<VariableNG*> functionVariables;
+ VariablesManager variablesManager;
};
ParserBase::ParserBase(CompilerBase* _compiler, LexerBase* _lexer) : d(new Private)
@@ -601,7 +596,7 @@
if( not returnType ) return;
// Clear the list of variables
- d->functionVariables.clear();
+ d->variablesManager.reset();
// Next is the name of the function
if( isOfType( d->currentToken, Token::IDENTIFIER ) )
@@ -715,14 +710,14 @@
if( isOfType( d->currentToken, Token::STARTBRACE ) )
{
AST::FunctionDeclaration* fd = new AST::FunctionDeclaration( name, returnType, params );
- startContext();
// TODO insert functions arguments in the context
for( unsigned int i = 0; i < params.size(); ++i)
{
- d->contextes.begin()->variables[ ScopedName("",params[i]->parameter().name() )] = fd->parametersVariable()[i];
+ d->variablesManager.declareParameter( ScopedName("",params[i]->parameter().name() ), fd->parametersVariable()[i] );
}
+ d->variablesManager.startContext();
AST::Statement* statement = parseStatementList();
- endContext();
+ d->variablesManager.endContext();
if( statement )
{
bool v = d->compiler->declareFunction( name, fd->function() );
@@ -803,7 +798,7 @@
AST::Statement* ParserBase::parseStatementOrList()
{
- startContext();
+ d->variablesManager.startContext();
AST::Statement* statement;
if( d->currentToken.type == Token::STARTBRACE )
{
@@ -811,7 +806,7 @@
} else {
statement = parseStatement();
}
- endContext();
+ d->variablesManager.endContext();
return statement;
}
@@ -835,7 +830,7 @@
AST::Statement* ParserBase::parseForStatement()
{
- startContext();
+ d->variablesManager.startContext();
GTL_ASSERT( d->currentToken.type == Token::FOR );
getNextToken();
if( isOfType( d->currentToken, Token::STARTBRACKET ) )
@@ -861,7 +856,7 @@
{
getNextToken(); // eat the ')'
AST::Statement* statement = parseStatementOrList();
- endContext();
+ d->variablesManager.endContext();
return new AST::ForStatement(initexpression, comparexpression, updateexpression, statement);
}
delete updateexpression;
@@ -869,7 +864,7 @@
}
delete initexpression;
}
- endContext();
+ d->variablesManager.endContext();
return 0;
}
@@ -905,13 +900,13 @@
if( d->currentToken.type == Token::SEMI )
{ // TODO check type
getNextToken(); // eat the coma
- return new AST::ReturnStatement( 0, functionVariables() );
+ return new AST::ReturnStatement( 0, d->variablesManager.garbageCollectEverything() );
} else {
AST::Expression* expr = parseExpression(false);
if(expr and isOfType( d->currentToken, Token::SEMI ) )
{
getNextToken();
- return new AST::ReturnStatement( expr, functionVariables() );
+ return new AST::ReturnStatement( expr, d->variablesManager.garbageCollectEverything() );
} else {
return 0;
}
@@ -946,7 +941,7 @@
if( isOfType( d->currentToken, Token::IDENTIFIER ) )
{
ScopedName name("", d->currentToken.string);
- if( d->contextes.begin()->variables.find( name ) == d->contextes.begin()->variables.end() )
+ if( not d->variablesManager.hasVariableInCurrentContext( name ) )
{
getNextToken();
std::list<AST::Expression*> initialsize = parseArraySize(false);
@@ -974,7 +969,7 @@
// Create the variable declaration statement
AST::VariableDeclaration* variable = new AST::VariableDeclaration( type, initialiser, constant, initialsize );
// Insert the variable in the context
- d->contextes.begin()->variables[name] = variable->variable();
+ d->variablesManager.declareVariable( name, variable->variable());
GTL_ASSERT( variable->variable() );
GTL_DEBUG(initialised << " " << Token::typeToString( d->currentToken.type ) );
if( d->currentToken.type == Token::COMA and not initialised ) {
@@ -1139,7 +1134,7 @@
return 0;
}
} else {
- VariableNG* var = getVariable( name );
+ VariableNG* var = d->variablesManager.getVariable( name );
if(not var)
{
AST::Expression* stdconst = d->compiler->standardConstant( name.name() );
@@ -1355,49 +1350,6 @@
return token.isNativeType() or ( token.type == Token::IDENTIFIER and d->compiler->typeManager()->d->isKnownType( token.string ) );
}
-// Context management
-
-void ParserBase::startContext()
-{
- d->contextes.push_front( Context() );
-}
-
-void ParserBase::endContext()
-{
- d->contextes.pop_front();
-}
-
-VariableNG* ParserBase::getVariable( const ScopedName& n ) const
-{
- GTL_DEBUG("getVariable " << n );
- for( std::list<Context>::const_iterator cit = d->contextes.begin();
- cit != d->contextes.end(); cit++)
- {
- for( std::map<ScopedName, VariableNG*>::const_iterator it = cit->variables.begin();
- it != cit->variables.end(); ++it)
- {
- GTL_DEBUG( " storage: " << it->first << " namespace: " << d->nameSpace );
- if( it->first == n
- or ( it->first.nameSpace() == d->nameSpace and it->first.name() == n.name() ) )
- {
- return it->second;
- }
- }
- }
- return 0;
-}
-
-std::list<VariableNG*> ParserBase::functionVariables() const
-{
- return d->functionVariables;
-}
-
-void ParserBase::declareVariable( const ScopedName& _scopedName, VariableNG* _variable)
-{
- d->contextes.begin()->variables[_scopedName] = _variable;
- d->functionVariables.push_back( _variable );
-}
-
std::list<int> ParserBase::expressionsListToIntegersList( const std::list< AST::Expression* >& list )
{
std::list<int> integersList;
@@ -1426,6 +1378,7 @@
void ParserBase::setNameSpace( const String& _nameSpace )
{
d->nameSpace = _nameSpace;
+ d->variablesManager.setNameSpace( _nameSpace );
}
const String& ParserBase::nameSpace() const
@@ -1437,3 +1390,8 @@
{
return d->compiler->typeManager();
}
+
+VariablesManager* ParserBase::variablesManager()
+{
+ return &d->variablesManager;
+}
Modified: trunk/OpenGTL/OpenGTL/GTLCore/ParserBase_p.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/ParserBase_p.h 2008-09-13 21:26:11 UTC (rev 392)
+++ trunk/OpenGTL/OpenGTL/GTLCore/ParserBase_p.h 2008-09-14 07:49:17 UTC (rev 393)
@@ -36,6 +36,7 @@
class LexerBase;
class Parameter;
class TypeManager;
+ class VariablesManager;
namespace AST {
class Tree;
class Expression;
@@ -146,18 +147,10 @@
bool isType( const Token& token );
protected:
// Context
- struct Context;
- VariableNG* getVariable( const ScopedName& ) const;
- /**
- * @return the list of variables in the function (so far)
- */
- std::list<VariableNG*> functionVariables() const;
- void declareVariable( const ScopedName& , VariableNG* );
- void startContext();
- void endContext();
std::list<int> expressionsListToIntegersList( const std::list< AST::Expression* >& );
void setNameSpace( const String& nameSpace );
TypeManager* typeManager();
+ VariablesManager* variablesManager();
private:
struct Private;
Private* const d;
Modified: trunk/OpenGTL/OpenGTL/GTLCore/VariablesManager_p.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/VariablesManager_p.cpp 2008-09-13 21:26:11 UTC (rev 392)
+++ trunk/OpenGTL/OpenGTL/GTLCore/VariablesManager_p.cpp 2008-09-14 07:49:17 UTC (rev 393)
@@ -19,3 +19,120 @@
#include "VariablesManager_p.h"
+#include <map>
+#include <list>
+
+#include "Debug.h"
+#include "ScopedName.h"
+
+using namespace GTLCore;
+
+struct VariablesManager::Context
+{
+ std::map< ScopedName, VariableNG* > variables;
+};
+
+struct VariablesManager::Private {
+ std::list< Context > contextes;
+ std::map< ScopedName, VariableNG* > parameters;
+ std::map< ScopedName, VariableNG* > constants;
+ String nameSpace;
+
+ VariableNG* getVariableInMap( const std::map< ScopedName, VariableNG* >&, const ScopedName& );
+};
+
+
+VariableNG* VariablesManager::Private::getVariableInMap( const std::map< ScopedName, VariableNG* >& map, const ScopedName& n)
+{
+ for( std::map<ScopedName, VariableNG*>::const_iterator it = map.begin();
+ it != map.end(); ++it)
+ {
+ GTL_DEBUG( " storage: " << it->first << " namespace: " << nameSpace );
+ if( it->first == n
+ or ( it->first.nameSpace() == nameSpace and it->first.name() == n.name() ) )
+ {
+ return it->second;
+ }
+ }
+ return 0;
+}
+
+VariablesManager::VariablesManager() : d(new Private)
+{
+}
+
+VariablesManager::~VariablesManager()
+{
+ delete d;
+}
+
+void VariablesManager::setNameSpace( const GTLCore::String& _nameSpace)
+{
+ d->nameSpace = _nameSpace;
+}
+
+void VariablesManager::declareVariable( const ScopedName& _scopedName, VariableNG* _variable)
+{
+ d->contextes.begin()->variables[_scopedName] = _variable;
+}
+
+
+VariableNG* VariablesManager::getVariable( const ScopedName& n ) const
+{
+ GTL_DEBUG("getVariable " << n );
+ for( std::list<Context>::const_iterator cit = d->contextes.begin();
+ cit != d->contextes.end(); cit++)
+ {
+ VariableNG* var = d->getVariableInMap( cit->variables, n );
+ if(var)
+ {
+ return var;
+ }
+ }
+ VariableNG* var = d->getVariableInMap( d->parameters, n );
+ if(var)
+ {
+ return var;
+ }
+ return d->getVariableInMap( d->constants, n );
+}
+
+bool VariablesManager::hasVariableInCurrentContext( const ScopedName& _scopedName ) const
+{
+ return (d->contextes.begin()->variables.find( _scopedName ) != d->contextes.begin()->variables.end());
+}
+
+void VariablesManager::startContext()
+{
+ d->contextes.push_front( Context() );
+}
+
+void VariablesManager::endContext()
+{
+ d->contextes.pop_front();
+}
+
+void VariablesManager::declareParameter( const ScopedName& _scopedName, VariableNG* _variable)
+{
+ d->parameters[_scopedName] = _variable;
+}
+
+void VariablesManager::reset()
+{
+ d->parameters.clear();
+}
+
+AST::Statement* VariablesManager::garbageCollectCurrentContext() const
+{
+ return 0;
+}
+
+AST::Statement* VariablesManager::garbageCollectEverything( ) const
+{
+ return 0;
+}
+
+void VariablesManager::declareConstant( const ScopedName& _scopedName, VariableNG* _variable)
+{
+ d->constants[_scopedName] = _variable;
+}
Modified: trunk/OpenGTL/OpenGTL/GTLCore/VariablesManager_p.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/VariablesManager_p.h 2008-09-13 21:26:11 UTC (rev 392)
+++ trunk/OpenGTL/OpenGTL/GTLCore/VariablesManager_p.h 2008-09-14 07:49:17 UTC (rev 393)
@@ -26,6 +26,7 @@
namespace GTLCore {
class ScopedName;
+ class String;
class VariableNG;
namespace AST {
class Statement;
@@ -38,14 +39,23 @@
*/
class VariablesManager {
public:
+ VariablesManager();
+ ~VariablesManager();
VariableNG* getVariable( const ScopedName& ) const;
+ bool hasVariableInCurrentContext( const ScopedName& ) const;
+ void setNameSpace( const String& _nameSpace);
void declareParameter( const ScopedName& , VariableNG* );
void declareVariable( const ScopedName& , VariableNG* );
+ void declareConstant( const ScopedName& , VariableNG* );
void startContext();
void endContext();
-
+ void reset();
AST::Statement* garbageCollectCurrentContext() const;
- AST::Statement* garbageCollectEverything( llvm::Value* _ignore ) const;
+ AST::Statement* garbageCollectEverything( ) const;
+ private:
+ struct Context;
+ struct Private;
+ Private* const d;
};
}
Modified: trunk/OpenGTL/OpenShiva/OpenShiva/Parser_p.cpp
===================================================================
--- trunk/OpenGTL/OpenShiva/OpenShiva/Parser_p.cpp 2008-09-13 21:26:11 UTC (rev 392)
+++ trunk/OpenGTL/OpenShiva/OpenShiva/Parser_p.cpp 2008-09-14 07:49:17 UTC (rev 393)
@@ -27,6 +27,7 @@
#include <GTLCore/AST/Statement.h>
#include <GTLCore/AST/Tree.h>
#include <GTLCore/TypeManager.h>
+#include <GTLCore/VariablesManager_p.h>
namespace AST = GTLCore::AST;
using namespace OpenShiva;
@@ -51,7 +52,7 @@
SHIVA_DEBUG("Parsing with ParserNG");
SHIVA_ASSERT( not d->tree );
d->tree = new GTLCore::AST::Tree();
- startContext();
+ variablesManager()->startContext();
getNextToken();
if(isOfType( currentToken(), GTLCore::Token::KERNEL ) )
{
@@ -133,9 +134,9 @@
return parseReturnStatement();
case GTLCore::Token::STARTBRACE:
{
- startContext();
+ variablesManager()->startContext();
AST::Statement* statement = parseStatementList();
- endContext();
+ variablesManager()->endContext();
return statement;
}
case GTLCore::Token::PRINT: