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 138 : AstStack::AstStack()
27 138 : : m_stack((AstScope**)rtl_allocateZeroMemory(sizeof(AstScope*) * STACKSIZE_INCREMENT))
28 : , m_size(STACKSIZE_INCREMENT)
29 276 : , m_top(0)
30 : {
31 138 : }
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 121104 : sal_uInt32 AstStack::depth()
45 : {
46 121104 : return m_top;
47 : }
48 :
49 559201 : AstScope* AstStack::top()
50 : {
51 559201 : if (m_top < 1)
52 0 : return NULL;
53 559201 : return m_stack[m_top - 1];
54 : }
55 :
56 201533 : AstScope* AstStack::bottom()
57 : {
58 201533 : if (m_top == 0)
59 0 : return NULL;
60 201533 : return m_stack[0];
61 : }
62 :
63 43229 : AstScope* AstStack::nextToTop()
64 : {
65 : AstScope *tmp, *retval;
66 :
67 43229 : if (depth() < 2)
68 0 : return NULL;
69 :
70 43229 : tmp = top(); // Save top
71 43229 : (void) pop(); // Pop it
72 43229 : retval = top(); // Get next one down
73 43229 : (void) push(tmp); // Push top back
74 43229 : return retval; // Return next one down
75 : }
76 :
77 713668 : AstScope* AstStack::topNonNull()
78 : {
79 725798 : for (sal_uInt32 i = m_top; i > 0; i--)
80 : {
81 725798 : if ( m_stack[i - 1] )
82 713668 : return m_stack[i - 1];
83 : }
84 0 : return NULL;
85 : }
86 :
87 476274 : AstStack* AstStack::push(AstScope* pScope)
88 : {
89 : AstScope **tmp;
90 : // AstDeclaration *pDecl = ScopeAsDecl(pScope);
91 : sal_uInt32 newSize;
92 : sal_uInt32 i;
93 :
94 : // Make sure there's space for one more
95 476274 : if (m_size == m_top)
96 : {
97 0 : newSize = m_size;
98 0 : newSize += STACKSIZE_INCREMENT;
99 0 : tmp = (AstScope**)rtl_allocateZeroMemory(sizeof(AstScope*) * newSize);
100 :
101 0 : for(i=0; i < m_size; i++)
102 0 : tmp[i] = m_stack[i];
103 :
104 0 : rtl_freeMemory(m_stack);
105 0 : m_stack = tmp;
106 : }
107 :
108 : // Insert new scope
109 476274 : m_stack[m_top++] = pScope;
110 :
111 476274 : return this;
112 : }
113 :
114 470951 : void AstStack::pop()
115 : {
116 470951 : if (m_top < 1)
117 470951 : return;
118 470951 : --m_top;
119 : }
120 :
121 5185 : void AstStack::clear()
122 : {
123 5185 : m_top = 0;
124 5185 : }
125 :
126 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|