xref: /trunk/main/idlc/source/aststack.cxx (revision 2fe1ca3d)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_idlc.hxx"
26 #include <rtl/alloc.h>
27 #include <idlc/aststack.hxx>
28 #include <idlc/astscope.hxx>
29 
30 #define STACKSIZE_INCREMENT 64
31 
AstStack()32 AstStack::AstStack()
33 	: m_stack((AstScope**)rtl_allocateZeroMemory(sizeof(AstScope*) * STACKSIZE_INCREMENT))
34 	, m_size(STACKSIZE_INCREMENT)
35 	, m_top(0)
36 {
37 }
38 
~AstStack()39 AstStack::~AstStack()
40 {
41 	for(sal_uInt32 i=0; i < m_top; i++)
42 	{
43 		if (m_stack[i])
44 			delete(m_stack[i]);
45 	}
46 
47 	rtl_freeMemory(m_stack);
48 }
49 
depth()50 sal_uInt32 AstStack::depth()
51 {
52 	return m_top;
53 }
54 
top()55 AstScope* AstStack::top()
56 {
57 	if (m_top < 1)
58 		return NULL;
59 	return m_stack[m_top - 1];
60 }
61 
bottom()62 AstScope* AstStack::bottom()
63 {
64 	if (m_top == 0)
65 		return NULL;
66 	return m_stack[0];
67 }
68 
nextToTop()69 AstScope* AstStack::nextToTop()
70 {
71 	AstScope *tmp, *retval;
72 
73 	if (depth() < 2)
74 		return NULL;
75 
76 	tmp = top();		// Save top
77 	(void) pop();		// Pop it
78 	retval = top();		// Get next one down
79 	(void) push(tmp);	// Push top back
80 	return retval;		// Return next one down
81 }
82 
topNonNull()83 AstScope* AstStack::topNonNull()
84 {
85 	for (sal_uInt32 i = m_top; i > 0; i--)
86 	{
87 		if ( m_stack[i - 1] )
88 			return m_stack[i - 1];
89   	}
90 	return NULL;
91 }
92 
push(AstScope * pScope)93 AstStack* AstStack::push(AstScope* pScope)
94 {
95 	AstScope		**tmp;
96 //	AstDeclaration	*pDecl = ScopeAsDecl(pScope);
97 	sal_uInt32	newSize;
98 	sal_uInt32	i;
99 
100 	// Make sure there's space for one more
101 	if (m_size == m_top)
102 	{
103 		newSize = m_size;
104 		newSize += STACKSIZE_INCREMENT;
105 		tmp	= (AstScope**)rtl_allocateZeroMemory(sizeof(AstScope*) * newSize);
106 
107 		for(i=0; i < m_size; i++)
108 			tmp[i] = m_stack[i];
109 
110 		rtl_freeMemory(m_stack);
111 		m_stack = tmp;
112 	}
113 
114 	// Insert new scope
115 	m_stack[m_top++] = pScope;
116 
117 	return this;
118 }
119 
pop()120 void AstStack::pop()
121 {
122 	AstScope *pScope;
123 
124 	if (m_top < 1)
125 		return;
126 	pScope = m_stack[--m_top];
127 }
128 
clear()129 void AstStack::clear()
130 {
131 	m_top = 0;
132 }
133 
134