Added GPLv3 headers all over the place.
[fail.git] / src / core / idlast / namespace.cpp
blobfc4d78bd16a6f673bbf2e11b33641923e2bcafbe
1 /*
2 Fail game engine
3 Copyright 2007 Antoine Chavasse <a.chavasse@gmail.com>
5 This file is part of Fail.
7 Fail is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 3
9 as published by the Free Software Foundation.
11 Fail is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <stack>
20 #include "core/idlast/namespace.h"
22 using namespace fail;
23 using namespace fail::idlast;
25 Pointer< Namespace > Namespace::addNamespace( Namespace* pNamespace_ )
27 Pointer< Namespace > pNS = m_Namespaces.get( pNamespace_->getName() );
28 if( pNS )
29 return pNS;
31 if( !m_Namespaces.add( pNamespace_ ) )
32 return 0;
34 pNamespace_->m_pParent = this;
35 return pNamespace_;
38 Pointer< Namespace > Namespace::findNamespace( std::string Name_ ) const
40 Pointer< Namespace > pNS = m_Namespaces.get( Name_ );
41 if( pNS )
42 return pNS;
44 if( !m_pParent )
45 return NULL;
47 return m_pParent->findNamespace( Name_ );
50 Pointer< Class > Namespace::findClass( std::string Name_, bool bStruct_ ) const
52 Pointer< Class > pClass;
54 if( bStruct_ )
55 pClass = m_Structs.get( Name_ );
56 else
57 pClass = m_Classes.get( Name_ );
59 if( pClass )
60 return pClass;
62 if( !m_pParent )
63 return NULL;
65 return m_pParent->findClass( Name_, bStruct_ );
68 void Namespace::resolveRefs() const
70 std::stack< const Namespace* > WorkStack;
72 WorkStack.push( this );
73 while( !WorkStack.empty() )
75 const Namespace* pCurrentNamespace = WorkStack.top();
76 WorkStack.pop();
78 // Push child namespaces on the stack
79 const Dictionary< Namespace >& Namespaces = pCurrentNamespace->getNamespaces();
80 Dictionary< Namespace >::const_iterator nsit;
81 for( nsit = Namespaces.begin(); nsit != Namespaces.end(); ++nsit )
82 WorkStack.push( nsit->second );
84 // Process classes
85 const Dictionary< Class >& Classes = pCurrentNamespace->getClasses();
86 Dictionary< Class >::const_iterator clit;
87 for( clit = Classes.begin(); clit != Classes.end(); ++clit )
88 clit->second->resolveRefs( false );
90 // Process structs
91 const Dictionary< Class >& Structs = pCurrentNamespace->getStructs();
92 Dictionary< Class >::const_iterator stit;
93 for( stit = Structs.begin(); stit != Structs.end(); ++stit )
94 stit->second->resolveRefs( true );
97 // Iterate through classes again to resolve the polymorphism flag
98 WorkStack.push( this );
99 while( !WorkStack.empty() )
101 const Namespace* pCurrentNamespace = WorkStack.top();
102 WorkStack.pop();
104 // Push child namespaces on the stack
105 const Dictionary< Namespace >& Namespaces = pCurrentNamespace->getNamespaces();
106 Dictionary< Namespace >::const_iterator nsit;
107 for( nsit = Namespaces.begin(); nsit != Namespaces.end(); ++nsit )
108 WorkStack.push( nsit->second );
110 // Process classes
111 const Dictionary< Class >& Classes = pCurrentNamespace->getClasses();
112 Dictionary< Class >::const_iterator clit;
113 for( clit = Classes.begin(); clit != Classes.end(); ++clit )
114 clit->second->resolvePolymorphism();