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 452 : AstStack::AstStack()
27 452 : : m_stack((AstScope**)rtl_allocateZeroMemory(sizeof(AstScope*) * STACKSIZE_INCREMENT))
28 : , m_size(STACKSIZE_INCREMENT)
29 904 : , m_top(0)
30 : {
31 452 : }
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 127582 : sal_uInt32 AstStack::depth()
45 : {
46 127582 : return m_top;
47 : }
48 :
49 589467 : AstScope* AstStack::top()
50 : {
51 589467 : if (m_top < 1)
52 0 : return NULL;
53 589467 : return m_stack[m_top - 1];
54 : }
55 :
56 211204 : AstScope* AstStack::bottom()
57 : {
58 211204 : if (m_top == 0)
59 0 : return NULL;
60 211204 : return m_stack[0];
61 : }
62 :
63 46563 : AstScope* AstStack::nextToTop()
64 : {
65 : AstScope *tmp, *retval;
66 :
67 46563 : if (depth() < 2)
68 0 : return NULL;
69 :
70 46563 : tmp = top(); // Save top
71 46563 : (void) pop(); // Pop it
72 46563 : retval = top(); // Get next one down
73 46563 : (void) push(tmp); // Push top back
74 46563 : return retval; // Return next one down
75 : }
76 :
77 746009 : AstScope* AstStack::topNonNull()
78 : {
79 758772 : for (sal_uInt32 i = m_top; i > 0; i--)
80 : {
81 758772 : if ( m_stack[i - 1] )
82 746009 : return m_stack[i - 1];
83 : }
84 0 : return NULL;
85 : }
86 :
87 500885 : 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 500885 : 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 500885 : m_stack[m_top++] = pScope;
110 :
111 500885 : return this;
112 : }
113 :
114 494821 : void AstStack::pop()
115 : {
116 494821 : if (m_top < 1)
117 494821 : return;
118 494821 : --m_top;
119 : }
120 :
121 5600 : void AstStack::clear()
122 : {
123 5600 : m_top = 0;
124 5600 : }
125 :
126 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|