Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <rtl/alloc.h>
21 : #include <idlc/aststack.hxx>
22 : #include <idlc/astscope.hxx>
23 :
24 : #define STACKSIZE_INCREMENT 64
25 :
26 658 : AstStack::AstStack()
27 658 : : m_stack((AstScope**)rtl_allocateZeroMemory(sizeof(AstScope*) * STACKSIZE_INCREMENT))
28 : , m_size(STACKSIZE_INCREMENT)
29 1316 : , m_top(0)
30 : {
31 658 : }
32 :
33 0 : AstStack::~AstStack()
34 : {
35 0 : for(sal_uInt32 i=0; i < m_top; i++)
36 : {
37 0 : if (m_stack[i])
38 0 : delete(m_stack[i]);
39 : }
40 :
41 0 : rtl_freeMemory(m_stack);
42 0 : }
43 :
44 :
45 2472 : AstScope* AstStack::top()
46 : {
47 2472 : if (m_top < 1)
48 0 : return NULL;
49 2472 : return m_stack[m_top - 1];
50 : }
51 :
52 502 : AstScope* AstStack::bottom()
53 : {
54 502 : if (m_top == 0)
55 0 : return NULL;
56 502 : return m_stack[0];
57 : }
58 :
59 134 : AstScope* AstStack::nextToTop()
60 : {
61 : AstScope *tmp, *retval;
62 :
63 134 : if (depth() < 2)
64 0 : return NULL;
65 :
66 134 : tmp = top(); // Save top
67 134 : (void) pop(); // Pop it
68 134 : retval = top(); // Get next one down
69 134 : (void) push(tmp); // Push top back
70 134 : return retval; // Return next one down
71 : }
72 :
73 3148 : AstScope* AstStack::topNonNull()
74 : {
75 3188 : for (sal_uInt32 i = m_top; i > 0; i--)
76 : {
77 3188 : if ( m_stack[i - 1] )
78 3148 : return m_stack[i - 1];
79 : }
80 0 : return NULL;
81 : }
82 :
83 3150 : AstStack* AstStack::push(AstScope* pScope)
84 : {
85 : AstScope **tmp;
86 : // AstDeclaration *pDecl = ScopeAsDecl(pScope);
87 : sal_uInt32 newSize;
88 : sal_uInt32 i;
89 :
90 : // Make sure there's space for one more
91 3150 : if (m_size == m_top)
92 : {
93 0 : newSize = m_size;
94 0 : newSize += STACKSIZE_INCREMENT;
95 0 : tmp = (AstScope**)rtl_allocateZeroMemory(sizeof(AstScope*) * newSize);
96 :
97 0 : for(i=0; i < m_size; i++)
98 0 : tmp[i] = m_stack[i];
99 :
100 0 : rtl_freeMemory(m_stack);
101 0 : m_stack = tmp;
102 : }
103 :
104 : // Insert new scope
105 3150 : m_stack[m_top++] = pScope;
106 :
107 3150 : return this;
108 : }
109 :
110 1810 : void AstStack::pop()
111 : {
112 1810 : if (m_top < 1)
113 1810 : return;
114 1810 : --m_top;
115 : }
116 :
117 658 : void AstStack::clear()
118 : {
119 658 : m_top = 0;
120 658 : }
121 :
122 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|