[opengtl-commits] [398] garbage collect variables at the end of context

[ Thread Index | Date Index | More lists.tuxfamily.org/opengtl-commits Archives ]


Revision: 398
Author:   cyrille
Date:     2008-09-20 11:47:34 +0200 (Sat, 20 Sep 2008)

Log Message:
-----------
garbage collect variables at the end of context

Modified Paths:
--------------
    trunk/OpenGTL/OpenCTL/OpenCTL/compiler/ParserNG.cpp
    trunk/OpenGTL/OpenGTL/GTLCore/AST/ConvertExpression.h
    trunk/OpenGTL/OpenGTL/GTLCore/AST/Expression.h
    trunk/OpenGTL/OpenGTL/GTLCore/AST/GarbageCollectionStatement.h
    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/OpenShiva/OpenShiva/Parser_p.cpp


Modified: trunk/OpenGTL/OpenCTL/OpenCTL/compiler/ParserNG.cpp
===================================================================
--- trunk/OpenGTL/OpenCTL/OpenCTL/compiler/ParserNG.cpp	2008-09-20 00:37:34 UTC (rev 397)
+++ trunk/OpenGTL/OpenCTL/OpenCTL/compiler/ParserNG.cpp	2008-09-20 09:47:34 UTC (rev 398)
@@ -254,7 +254,7 @@
     case GTLCore::Token::STARTBRACE:
     {
       variablesManager()->startContext();
-      AST::Statement* statement = parseStatementList();
+      AST::StatementsList* statement = appendCurrentContextGarbageCollecting(parseStatementList());
       variablesManager()->endContext();
       return statement;
     }

Modified: trunk/OpenGTL/OpenGTL/GTLCore/AST/ConvertExpression.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/AST/ConvertExpression.h	2008-09-20 00:37:34 UTC (rev 397)
+++ trunk/OpenGTL/OpenGTL/GTLCore/AST/ConvertExpression.h	2008-09-20 09:47:34 UTC (rev 398)
@@ -35,6 +35,7 @@
         ~ConvertExpression();
         virtual void markAsReturnExpression();
         virtual bool isConstant() const;
+        virtual bool isReturnStatement() const { return false; }
       protected:
         const AST::Expression* value() const;
       private:

Modified: trunk/OpenGTL/OpenGTL/GTLCore/AST/Expression.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/AST/Expression.h	2008-09-20 00:37:34 UTC (rev 397)
+++ trunk/OpenGTL/OpenGTL/GTLCore/AST/Expression.h	2008-09-20 09:47:34 UTC (rev 398)
@@ -42,6 +42,7 @@
         virtual bool isConstant() const = 0;
         virtual llvm::BasicBlock* generateStatement( GenerationContext&, llvm::BasicBlock* ) const;
         virtual GTLCore::ExpressionResult generateValue( GenerationContext&, llvm::BasicBlock* bb ) const = 0;
+        virtual bool isReturnStatement() const { return false; }
         /**
          * This function is called inside a \ref ReturnStatement to warn the \ref Expression
          * that the result will be returned.

Modified: trunk/OpenGTL/OpenGTL/GTLCore/AST/GarbageCollectionStatement.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/AST/GarbageCollectionStatement.h	2008-09-20 00:37:34 UTC (rev 397)
+++ trunk/OpenGTL/OpenGTL/GTLCore/AST/GarbageCollectionStatement.h	2008-09-20 09:47:34 UTC (rev 398)
@@ -33,6 +33,7 @@
         PointerGarbageCollectionStatement( llvm::Value* _pointer, const GTLCore::Type* _type );
         virtual ~PointerGarbageCollectionStatement();
         virtual llvm::BasicBlock* generateStatement( GenerationContext&, llvm::BasicBlock* ) const;
+        virtual bool isReturnStatement() const { return false; }
       private:
         llvm::Value* m_pointer;
         const GTLCore::Type* m_type;
@@ -42,6 +43,7 @@
         VariablesGarbageCollectionStatement( std::list< VariableNG* > _variablesToCollect );
         virtual ~VariablesGarbageCollectionStatement();
         virtual llvm::BasicBlock* generateStatement( GenerationContext&, llvm::BasicBlock* ) const;
+        virtual bool isReturnStatement() const { return false; }
       private:
         std::list< VariableNG* > m_variablesToCollect;
     };

Modified: trunk/OpenGTL/OpenGTL/GTLCore/AST/Statement.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/AST/Statement.cpp	2008-09-20 00:37:34 UTC (rev 397)
+++ trunk/OpenGTL/OpenGTL/GTLCore/AST/Statement.cpp	2008-09-20 09:47:34 UTC (rev 398)
@@ -72,6 +72,17 @@
   deleteAll( m_list );
 }
 
+bool StatementsList::isReturnStatement() const
+{
+  if( m_list.empty() ) return false;
+  return (*--m_list.end())->isReturnStatement();
+}
+
+void StatementsList::appendStatement( Statement* _statement )
+{
+  m_list.push_back( _statement );
+}
+
 //---------------------------------------------------//
 //--------------- VariableDeclaration ---------------//
 //---------------------------------------------------//

Modified: trunk/OpenGTL/OpenGTL/GTLCore/AST/Statement.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/AST/Statement.h	2008-09-20 00:37:34 UTC (rev 397)
+++ trunk/OpenGTL/OpenGTL/GTLCore/AST/Statement.h	2008-09-20 09:47:34 UTC (rev 398)
@@ -45,6 +45,11 @@
       public:
         virtual ~Statement() {}
         virtual llvm::BasicBlock* generateStatement( GenerationContext&, llvm::BasicBlock* ) const = 0;
+        /**
+         * @return true if the statement is a \ref ReturnStatement or \ref StatementsList whose
+         *         last statement return true to \ref isReturnStatement
+         */
+        virtual bool isReturnStatement() const = 0;
       protected:
         llvm::BasicBlock* createBlock( GenerationContext&) const;
     };
@@ -58,6 +63,7 @@
         virtual ~DummyStatement() {}
         virtual llvm::BasicBlock* generateStatement( GenerationContext&, llvm::BasicBlock* bb ) const
         { return bb; }
+        virtual bool isReturnStatement() const { return false; }
     };
     /**
      * @internal
@@ -68,6 +74,8 @@
         StatementsList( const std::list<Statement*>& list ) : m_list( list) {}
         ~StatementsList();
         virtual llvm::BasicBlock* generateStatement( GenerationContext&, llvm::BasicBlock* ) const;
+        virtual bool isReturnStatement() const;
+        void appendStatement( Statement* );
       private:
         std::list<Statement*> m_list;
     };
@@ -85,6 +93,7 @@
         void setFunctionIntialiser( AST::Statement* _functionInitialiser )
         { m_functionInitialiser = _functionInitialiser; }
         virtual llvm::BasicBlock* generateStatement( GenerationContext&, llvm::BasicBlock* ) const;
+        virtual bool isReturnStatement() const { return false; }
       private:
         GTLCore::VariableNG* m_variable;
         Expression* m_initialiser;
@@ -103,6 +112,7 @@
         }
         ~IfStatement();
         virtual llvm::BasicBlock* generateStatement( GenerationContext&, llvm::BasicBlock* ) const;
+        virtual bool isReturnStatement() const { return false; }
       private:
         Expression* m_expression;
         Statement* m_ifStatement;
@@ -119,6 +129,7 @@
         }
         ~IfElseStatement();
         virtual llvm::BasicBlock* generateStatement( GenerationContext&, llvm::BasicBlock* ) const;
+        virtual bool isReturnStatement() const { return false; }
       private:
         Expression* m_expression;
         Statement* m_ifStatement;
@@ -136,6 +147,7 @@
         }
         ~WhileStatement();
         virtual llvm::BasicBlock* generateStatement( GenerationContext&, llvm::BasicBlock* ) const;
+        virtual bool isReturnStatement() const { return false; }
       private:
         Expression* m_expression;
         Statement* m_whileStatement;
@@ -151,6 +163,7 @@
         {}
         ~ForStatement();
         virtual llvm::BasicBlock* generateStatement( GenerationContext&, llvm::BasicBlock* ) const;
+        virtual bool isReturnStatement() const { return false; }
       private:
         Statement* m_initStatement;
         Expression* m_testExpression;
@@ -171,6 +184,7 @@
         ReturnStatement( Expression* _returnExpr, AST::Statement* _garbageCollectionStatement );
         ~ReturnStatement();
         virtual llvm::BasicBlock* generateStatement( GenerationContext&, llvm::BasicBlock* ) const;
+        virtual bool isReturnStatement() const { return true; }
       private:
         Expression* m_returnExpr;
         AST::Statement* m_garbageCollectionStatement;
@@ -186,6 +200,7 @@
         }
         ~PrintStatement();
         virtual llvm::BasicBlock* generateStatement( GenerationContext&, llvm::BasicBlock* ) const;
+        virtual bool isReturnStatement() const { return false; }
       private:
         std::list<AST::Expression*> m_expressions;
     };

Modified: trunk/OpenGTL/OpenGTL/GTLCore/ParserBase_p.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/ParserBase_p.cpp	2008-09-20 00:37:34 UTC (rev 397)
+++ trunk/OpenGTL/OpenGTL/GTLCore/ParserBase_p.cpp	2008-09-20 09:47:34 UTC (rev 398)
@@ -716,7 +716,7 @@
               d->variablesManager.declareParameter( ScopedName("",params[i]->parameter().name() ), fd->parametersVariable()[i] );
             }
             d->variablesManager.startContext();
-            AST::Statement* statement = parseStatementList();
+            AST::Statement* statement =appendCurrentContextGarbageCollecting(parseStatementList());
             d->variablesManager.endContext();
             if( statement )
             {
@@ -742,8 +742,29 @@
   }
 }
 
-AST::Statement* ParserBase::parseStatementList()
+AST::StatementsList* ParserBase::appendCurrentContextGarbageCollecting( AST::StatementsList* _statementsList )
 {
+  if( not _statementsList->isReturnStatement() )
+  {
+    _statementsList->appendStatement( d->variablesManager.garbageCollectCurrentContext() );
+  }
+  return _statementsList;
+}
+
+AST::Statement* ParserBase::appendCurrentContextGarbageCollecting( AST::Statement* _statement )
+{
+  if( not _statement->isReturnStatement() )
+  {
+    std::list< AST::Statement* > statements;
+    statements.push_back( _statement );
+    statements.push_back( d->variablesManager.garbageCollectCurrentContext() );
+    return new AST::StatementsList( statements );
+  }
+  return _statement;
+}
+
+AST::StatementsList* ParserBase::parseStatementList()
+{
   GTL_ASSERT( d->currentToken.type == Token::STARTBRACE );
   getNextToken();
   std::list<AST::Statement*> list;
@@ -855,7 +876,7 @@
       if( isOfType( d->currentToken, Token::ENDBRACKET ) )
       {
         getNextToken(); // eat the ')'
-        AST::Statement* statement = parseStatementOrList();
+        AST::Statement* statement = appendCurrentContextGarbageCollecting(parseStatementOrList());
         d->variablesManager.endContext();
         return new AST::ForStatement(initexpression, comparexpression, updateexpression, statement);
       }

Modified: trunk/OpenGTL/OpenGTL/GTLCore/ParserBase_p.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/ParserBase_p.h	2008-09-20 00:37:34 UTC (rev 397)
+++ trunk/OpenGTL/OpenGTL/GTLCore/ParserBase_p.h	2008-09-20 09:47:34 UTC (rev 398)
@@ -43,6 +43,7 @@
     class CoumpoundExpression;
     class Statement;
     class AccessorExpression;
+    class StatementsList;
   };
   /**
    * @internal
@@ -78,7 +79,12 @@
        */
       AST::Expression* parseBinaryOperator( bool _constantExpression, AST::Expression* _lhs );
       void parseFunction();
-      AST::Statement* parseStatementList();
+      /**
+       * Parse a list of statement.
+       * 
+       * It doesn't creates a context.
+       */
+      AST::StatementsList* parseStatementList();
       virtual AST::Statement* parseStatement() = 0;
       /**
        * Parse either an unique statement or a list of statement, depending on wether
@@ -146,6 +152,12 @@
       AST::Expression* createBinaryOperator( const Token& token, AST::Expression* lhs, AST::Expression* rhs );
       bool isType( const Token& token );
     protected:
+      /**
+       * Append to an expression list the statement to garbage collect the current context.
+       * Unless the last element of \ref _statementsList is a return statement.
+       */
+      AST::StatementsList* appendCurrentContextGarbageCollecting( AST::StatementsList* _statementsList );
+      AST::Statement* appendCurrentContextGarbageCollecting( AST::Statement* _statement );
       // Context
       std::list<int> expressionsListToIntegersList( const std::list< AST::Expression* >& );
       void setNameSpace( const String& nameSpace );

Modified: trunk/OpenGTL/OpenShiva/OpenShiva/Parser_p.cpp
===================================================================
--- trunk/OpenGTL/OpenShiva/OpenShiva/Parser_p.cpp	2008-09-20 00:37:34 UTC (rev 397)
+++ trunk/OpenGTL/OpenShiva/OpenShiva/Parser_p.cpp	2008-09-20 09:47:34 UTC (rev 398)
@@ -135,7 +135,7 @@
     case GTLCore::Token::STARTBRACE:
     {
       variablesManager()->startContext();
-      AST::Statement* statement = parseStatementList();
+      AST::Statement* statement = appendCurrentContextGarbageCollecting(parseStatementList());
       variablesManager()->endContext();
       return statement;
     }


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/