11c78a5d6SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
31c78a5d6SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
41c78a5d6SAndrew Rist * or more contributor license agreements. See the NOTICE file
51c78a5d6SAndrew Rist * distributed with this work for additional information
61c78a5d6SAndrew Rist * regarding copyright ownership. The ASF licenses this file
71c78a5d6SAndrew Rist * to you under the Apache License, Version 2.0 (the
81c78a5d6SAndrew Rist * "License"); you may not use this file except in compliance
91c78a5d6SAndrew Rist * with the License. You may obtain a copy of the License at
101c78a5d6SAndrew Rist *
111c78a5d6SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
121c78a5d6SAndrew Rist *
131c78a5d6SAndrew Rist * Unless required by applicable law or agreed to in writing,
141c78a5d6SAndrew Rist * software distributed under the License is distributed on an
151c78a5d6SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
161c78a5d6SAndrew Rist * KIND, either express or implied. See the License for the
171c78a5d6SAndrew Rist * specific language governing permissions and limitations
181c78a5d6SAndrew Rist * under the License.
191c78a5d6SAndrew Rist *
201c78a5d6SAndrew Rist *************************************************************/
211c78a5d6SAndrew Rist
221c78a5d6SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir #ifndef ADC_CPP_SOWNSTCK_HXX
25cdf0e10cSrcweir #define ADC_CPP_SOWNSTCK_HXX
26cdf0e10cSrcweir
27cdf0e10cSrcweir
28cdf0e10cSrcweir
29cdf0e10cSrcweir // USED SERVICES
30cdf0e10cSrcweir // BASE CLASSES
31cdf0e10cSrcweir #include "cxt2ary.hxx"
32cdf0e10cSrcweir // COMPONENTS
33cdf0e10cSrcweir #include <ary/cpp/c_types4cpp.hxx>
34cdf0e10cSrcweir #include <estack.hxx>
35cdf0e10cSrcweir // PARAMETERS
36cdf0e10cSrcweir #include <ary/cpp/c_namesp.hxx>
37cdf0e10cSrcweir #include <x_parse.hxx>
38cdf0e10cSrcweir
39cdf0e10cSrcweir
40cdf0e10cSrcweir namespace cpp
41cdf0e10cSrcweir {
42cdf0e10cSrcweir
43cdf0e10cSrcweir using ary::cpp::E_Protection;
44cdf0e10cSrcweir
45cdf0e10cSrcweir
46cdf0e10cSrcweir /** Implementation struct for cpp::ContextForAry.
47cdf0e10cSrcweir */
48cdf0e10cSrcweir struct ContextForAry::S_OwnerStack
49cdf0e10cSrcweir {
50cdf0e10cSrcweir public:
51cdf0e10cSrcweir S_OwnerStack();
52cdf0e10cSrcweir void SetGlobalNamespace(
53cdf0e10cSrcweir ary::cpp::Namespace &
54cdf0e10cSrcweir io_rGlobalNamespace );
55cdf0e10cSrcweir ~S_OwnerStack();
56cdf0e10cSrcweir
57cdf0e10cSrcweir void Reset();
58cdf0e10cSrcweir
59cdf0e10cSrcweir void OpenNamespace(
60cdf0e10cSrcweir ary::cpp::Namespace &
61cdf0e10cSrcweir io_rOpenedNamespace );
62cdf0e10cSrcweir void OpenExternC();
63cdf0e10cSrcweir void OpenClass(
64cdf0e10cSrcweir ary::cpp::Class & io_rOpenedClass );
65cdf0e10cSrcweir void OpenEnum(
66cdf0e10cSrcweir ary::cpp::Enum & io_rOpenedEnum );
67cdf0e10cSrcweir void CloseBlock();
68cdf0e10cSrcweir void CloseClass();
69cdf0e10cSrcweir void CloseEnum();
70cdf0e10cSrcweir void SetCurProtection(
71cdf0e10cSrcweir ary::cpp::E_Protection
72cdf0e10cSrcweir i_eProtection );
73cdf0e10cSrcweir ary::cpp::Namespace &
74cdf0e10cSrcweir CurNamespace() const;
75cdf0e10cSrcweir ary::cpp::Class * CurClass() const;
76cdf0e10cSrcweir ary::cpp::Enum * CurEnum() const;
77cdf0e10cSrcweir
78cdf0e10cSrcweir Owner & CurOwner() const;
79cdf0e10cSrcweir ary::cpp::E_Protection
80cdf0e10cSrcweir CurProtection() const;
IsExternCcpp::ContextForAry::S_OwnerStack81cdf0e10cSrcweir bool IsExternC() const { return nExternC > 0; }
82cdf0e10cSrcweir
83cdf0e10cSrcweir private:
84cdf0e10cSrcweir typedef std::pair< ary::cpp::Class*, E_Protection > ClassEnv;
85cdf0e10cSrcweir typedef EStack< ary::cpp::Namespace* > Stack_Namespaces;
86cdf0e10cSrcweir typedef EStack< ClassEnv > Stack_Classes;
87cdf0e10cSrcweir typedef ary::cpp::InputContext::Owner Ifc_Owner;
88cdf0e10cSrcweir
89cdf0e10cSrcweir void SetOwner_2CurNamespace();
90cdf0e10cSrcweir void SetOwner_2CurClass();
91cdf0e10cSrcweir void SetOwner_2None();
92cdf0e10cSrcweir
93cdf0e10cSrcweir // DATA
94cdf0e10cSrcweir Stack_Namespaces aStack_Namespaces;
95cdf0e10cSrcweir Stack_Classes aStack_Classes;
96cdf0e10cSrcweir ary::cpp::Enum * pCurEnum;
97cdf0e10cSrcweir uintt nExternC; /// This is a number, for the case that extern "C" blocks are nested.
98cdf0e10cSrcweir
99cdf0e10cSrcweir Dyn<Owner_Namespace>
100cdf0e10cSrcweir pOwner_Namespace;
101cdf0e10cSrcweir Dyn<Owner_Class> pOwner_Class;
102cdf0e10cSrcweir Ifc_Owner * pOwner_Cur;
103cdf0e10cSrcweir };
104cdf0e10cSrcweir
105cdf0e10cSrcweir
106cdf0e10cSrcweir // IMPLEMENTATION
107cdf0e10cSrcweir
108cdf0e10cSrcweir /* The implementation is in header, though not inline, because this file is included
109cdf0e10cSrcweir in cxt2ary.cxx only!
110cdf0e10cSrcweir */
111cdf0e10cSrcweir
112cdf0e10cSrcweir inline ary::cpp::Namespace &
113cdf0e10cSrcweir ContextForAry::
CurNamespace() const114cdf0e10cSrcweir S_OwnerStack::CurNamespace() const
115cdf0e10cSrcweir {
116*a0d53b35SJürgen Schmidt csv_assert( ! aStack_Namespaces.empty() );
117cdf0e10cSrcweir return *aStack_Namespaces.top();
118cdf0e10cSrcweir }
119cdf0e10cSrcweir
120cdf0e10cSrcweir inline ary::cpp::Class *
121cdf0e10cSrcweir ContextForAry::
CurClass() const122cdf0e10cSrcweir S_OwnerStack::CurClass() const
123cdf0e10cSrcweir {
124c945f255SHerbert Dürr return !aStack_Classes.empty()
125cdf0e10cSrcweir ? aStack_Classes.top().first
126cdf0e10cSrcweir : (ary::cpp::Class *) 0;
127cdf0e10cSrcweir }
128cdf0e10cSrcweir
129cdf0e10cSrcweir inline void
130cdf0e10cSrcweir ContextForAry::
SetOwner_2CurNamespace()131cdf0e10cSrcweir S_OwnerStack::SetOwner_2CurNamespace()
132cdf0e10cSrcweir {
133c945f255SHerbert Dürr csv_assert( !aStack_Namespaces.empty() );
134cdf0e10cSrcweir pOwner_Cur = pOwner_Namespace.MutablePtr();
135cdf0e10cSrcweir pOwner_Namespace->SetAnotherNamespace( CurNamespace() );
136cdf0e10cSrcweir }
137cdf0e10cSrcweir
138cdf0e10cSrcweir inline void
139cdf0e10cSrcweir ContextForAry::
SetOwner_2CurClass()140cdf0e10cSrcweir S_OwnerStack::SetOwner_2CurClass()
141cdf0e10cSrcweir {
142c945f255SHerbert Dürr csv_assert( !aStack_Classes.empty() );
143cdf0e10cSrcweir pOwner_Cur = pOwner_Class.MutablePtr();
144cdf0e10cSrcweir pOwner_Class->SetAnotherClass( *CurClass() );
145cdf0e10cSrcweir }
146cdf0e10cSrcweir
147cdf0e10cSrcweir ContextForAry::
S_OwnerStack()148cdf0e10cSrcweir S_OwnerStack::S_OwnerStack()
149cdf0e10cSrcweir : // aStack_Namespaces,
150cdf0e10cSrcweir // aStack_Classes,
151cdf0e10cSrcweir pCurEnum(0),
152cdf0e10cSrcweir nExternC(0),
153cdf0e10cSrcweir pOwner_Namespace(new Owner_Namespace),
154cdf0e10cSrcweir pOwner_Class(new Owner_Class),
155cdf0e10cSrcweir pOwner_Cur(0)
156cdf0e10cSrcweir {
157cdf0e10cSrcweir }
158cdf0e10cSrcweir
159cdf0e10cSrcweir void
160cdf0e10cSrcweir ContextForAry::
Reset()161cdf0e10cSrcweir S_OwnerStack::Reset()
162cdf0e10cSrcweir {
163cdf0e10cSrcweir while ( aStack_Namespaces.top()->Owner().IsValid() )
164cdf0e10cSrcweir aStack_Namespaces.pop();
165cdf0e10cSrcweir while ( NOT aStack_Classes.empty() )
166cdf0e10cSrcweir aStack_Classes.pop();
167cdf0e10cSrcweir pCurEnum = 0;
168cdf0e10cSrcweir nExternC = 0;
169cdf0e10cSrcweir SetOwner_2CurNamespace();
170cdf0e10cSrcweir }
171cdf0e10cSrcweir
172cdf0e10cSrcweir inline void
173cdf0e10cSrcweir ContextForAry::
SetGlobalNamespace(ary::cpp::Namespace & io_rGlobalNamespace)174cdf0e10cSrcweir S_OwnerStack::SetGlobalNamespace( ary::cpp::Namespace & io_rGlobalNamespace )
175cdf0e10cSrcweir {
176c945f255SHerbert Dürr csv_assert( aStack_Namespaces.empty() );
177cdf0e10cSrcweir aStack_Namespaces.push(&io_rGlobalNamespace);
178cdf0e10cSrcweir SetOwner_2CurNamespace();
179cdf0e10cSrcweir }
180cdf0e10cSrcweir
181cdf0e10cSrcweir ContextForAry::
~S_OwnerStack()182cdf0e10cSrcweir S_OwnerStack::~S_OwnerStack()
183cdf0e10cSrcweir {
184cdf0e10cSrcweir }
185cdf0e10cSrcweir
186cdf0e10cSrcweir inline void
187cdf0e10cSrcweir ContextForAry::
OpenNamespace(ary::cpp::Namespace & io_rOpenedNamespace)188cdf0e10cSrcweir S_OwnerStack::OpenNamespace( ary::cpp::Namespace & io_rOpenedNamespace )
189cdf0e10cSrcweir {
190c945f255SHerbert Dürr csv_assert( !aStack_Namespaces.empty() OR io_rOpenedNamespace.Parent() == 0 );
191cdf0e10cSrcweir aStack_Namespaces.push(&io_rOpenedNamespace);
192cdf0e10cSrcweir SetOwner_2CurNamespace();
193cdf0e10cSrcweir }
194cdf0e10cSrcweir
195cdf0e10cSrcweir inline void
196cdf0e10cSrcweir ContextForAry::
OpenExternC()197cdf0e10cSrcweir S_OwnerStack::OpenExternC()
198cdf0e10cSrcweir {
199cdf0e10cSrcweir ++nExternC;
200cdf0e10cSrcweir // SetOwner_2None();
201cdf0e10cSrcweir }
202cdf0e10cSrcweir
203cdf0e10cSrcweir inline void
204cdf0e10cSrcweir ContextForAry::
SetCurProtection(ary::cpp::E_Protection i_eProtection)205cdf0e10cSrcweir S_OwnerStack::SetCurProtection( ary::cpp::E_Protection i_eProtection )
206cdf0e10cSrcweir {
207*a0d53b35SJürgen Schmidt csv_assert( ! aStack_Classes.empty() );
208cdf0e10cSrcweir aStack_Classes.top().second = i_eProtection;
209cdf0e10cSrcweir }
210cdf0e10cSrcweir
211cdf0e10cSrcweir inline ary::cpp::Enum *
212cdf0e10cSrcweir ContextForAry::
CurEnum() const213cdf0e10cSrcweir S_OwnerStack::CurEnum() const
214cdf0e10cSrcweir {
215cdf0e10cSrcweir return pCurEnum;
216cdf0e10cSrcweir }
217cdf0e10cSrcweir
218cdf0e10cSrcweir
219cdf0e10cSrcweir inline ary::cpp::InputContext::Owner &
220cdf0e10cSrcweir ContextForAry::
CurOwner() const221cdf0e10cSrcweir S_OwnerStack::CurOwner() const
222cdf0e10cSrcweir {
223cdf0e10cSrcweir csv_assert( pOwner_Cur != 0 );
224cdf0e10cSrcweir return *pOwner_Cur;
225cdf0e10cSrcweir }
226cdf0e10cSrcweir
227cdf0e10cSrcweir inline E_Protection
228cdf0e10cSrcweir ContextForAry::
CurProtection() const229cdf0e10cSrcweir S_OwnerStack::CurProtection() const
230cdf0e10cSrcweir {
231c945f255SHerbert Dürr return !aStack_Classes.empty()
232cdf0e10cSrcweir ? aStack_Classes.top().second
233cdf0e10cSrcweir : ary::cpp::PROTECT_global;
234cdf0e10cSrcweir }
235cdf0e10cSrcweir
236cdf0e10cSrcweir inline void
237cdf0e10cSrcweir ContextForAry::
SetOwner_2None()238cdf0e10cSrcweir S_OwnerStack::SetOwner_2None()
239cdf0e10cSrcweir {
240cdf0e10cSrcweir pOwner_Cur = 0;
241cdf0e10cSrcweir }
242cdf0e10cSrcweir
243cdf0e10cSrcweir void
244cdf0e10cSrcweir ContextForAry::
OpenClass(ary::cpp::Class & io_rOpenedClass)245cdf0e10cSrcweir S_OwnerStack::OpenClass( ary::cpp::Class & io_rOpenedClass )
246cdf0e10cSrcweir {
247cdf0e10cSrcweir E_Protection eDefaultProtection
248cdf0e10cSrcweir = io_rOpenedClass.ClassKey() == ary::cpp::CK_class
249cdf0e10cSrcweir ? ary::cpp::PROTECT_private
250cdf0e10cSrcweir : ary::cpp::PROTECT_public;
251cdf0e10cSrcweir aStack_Classes.push( ClassEnv(&io_rOpenedClass, eDefaultProtection) );
252cdf0e10cSrcweir SetOwner_2CurClass();
253cdf0e10cSrcweir }
254cdf0e10cSrcweir
255cdf0e10cSrcweir inline void
256cdf0e10cSrcweir ContextForAry::
OpenEnum(ary::cpp::Enum & io_rOpenedEnum)257cdf0e10cSrcweir S_OwnerStack::OpenEnum( ary::cpp::Enum & io_rOpenedEnum )
258cdf0e10cSrcweir {
259cdf0e10cSrcweir csv_assert( pCurEnum == 0 );
260cdf0e10cSrcweir pCurEnum = &io_rOpenedEnum;
261cdf0e10cSrcweir SetOwner_2None();
262cdf0e10cSrcweir }
263cdf0e10cSrcweir
264cdf0e10cSrcweir void
265cdf0e10cSrcweir ContextForAry::
CloseBlock()266cdf0e10cSrcweir S_OwnerStack::CloseBlock()
267cdf0e10cSrcweir {
268cdf0e10cSrcweir if (nExternC > 0)
269cdf0e10cSrcweir {
270cdf0e10cSrcweir --nExternC;
271cdf0e10cSrcweir }
272cdf0e10cSrcweir else
273cdf0e10cSrcweir {
274c945f255SHerbert Dürr // csv_assert( aStack_Classes.empty() );
275c945f255SHerbert Dürr if ( !aStack_Classes.empty() )
276cdf0e10cSrcweir throw X_Parser(X_Parser::x_UnspecifiedSyntaxError, "", String::Null_(), 0);
277cdf0e10cSrcweir
278cdf0e10cSrcweir csv_assert( pCurEnum == 0 );
279cdf0e10cSrcweir aStack_Namespaces.pop();
280cdf0e10cSrcweir
281c945f255SHerbert Dürr // csv_assert( !aStack_Namespaces.empty() );
282c945f255SHerbert Dürr if( aStack_Namespaces.empty() )
283cdf0e10cSrcweir throw X_Parser(X_Parser::x_UnspecifiedSyntaxError, "", String::Null_(), 0);
284cdf0e10cSrcweir
285cdf0e10cSrcweir }
286cdf0e10cSrcweir SetOwner_2CurNamespace();
287cdf0e10cSrcweir }
288cdf0e10cSrcweir
289cdf0e10cSrcweir void
290cdf0e10cSrcweir ContextForAry::
CloseClass()291cdf0e10cSrcweir S_OwnerStack::CloseClass()
292cdf0e10cSrcweir {
293c945f255SHerbert Dürr // csv_assert( !aStack_Classes.empty() );
294c945f255SHerbert Dürr if ( aStack_Classes.empty() )
295cdf0e10cSrcweir throw X_Parser(X_Parser::x_UnspecifiedSyntaxError, "", String::Null_(), 0);
296cdf0e10cSrcweir
297cdf0e10cSrcweir aStack_Classes.pop();
298c945f255SHerbert Dürr if ( !aStack_Classes.empty() )
299cdf0e10cSrcweir SetOwner_2CurClass();
300cdf0e10cSrcweir else
301cdf0e10cSrcweir SetOwner_2CurNamespace();
302cdf0e10cSrcweir }
303cdf0e10cSrcweir
304cdf0e10cSrcweir void
305cdf0e10cSrcweir ContextForAry::
CloseEnum()306cdf0e10cSrcweir S_OwnerStack::CloseEnum()
307cdf0e10cSrcweir {
308cdf0e10cSrcweir csv_assert( pCurEnum != 0 );
309cdf0e10cSrcweir pCurEnum = 0;
310c945f255SHerbert Dürr if ( !aStack_Classes.empty() )
311cdf0e10cSrcweir SetOwner_2CurClass();
312cdf0e10cSrcweir else
313cdf0e10cSrcweir SetOwner_2CurNamespace();
314cdf0e10cSrcweir }
315cdf0e10cSrcweir
316cdf0e10cSrcweir
317cdf0e10cSrcweir } // namespace cpp
318cdf0e10cSrcweir
319cdf0e10cSrcweir
320cdf0e10cSrcweir #endif
321cdf0e10cSrcweir
322