Debian Codegraph @

basic/source/comp/symtbl.cxx

raw
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
21 #include <memory>
22 #include <parser.hxx>
23
24 #include <osl/diagnose.h>
25
26 #include <stdio.h>
27 #include <rtl/character.hxx>
28 #include <basic/sberrors.hxx>
29 #include <utility>
30
31 // All symbol names are laid down int the symbol-pool's stringpool, so that
32 // all symbols are handled in the same case. On saving the code-image, the
33 // global stringpool with the respective symbols is also saved.
34 // The local stringpool holds all the symbols that don't move to the image
35 // (labels, constant names etc.).
36
37 SbiStringPool::SbiStringPool( )
38 {}
39
40 SbiStringPool::~SbiStringPool()
41 {}
42
43 const OUString & SbiStringPool::Find( sal_uInt32 n ) const
44 {
45 if( n == 0 || n > aData.size() )
46 return EMPTY_OUSTRING;
47 else
48 return aData[n - 1];
49 }
50
51 short SbiStringPool::Add( const OUString& rVal )
52 {
53 sal_uInt32 n = aData.size();
54 for( sal_uInt32 i = 0; i < n; ++i )
55 {
56 OUString& p = aData[i];
57 if( p == rVal )
58 return i+1;
59 }
60
61 aData.push_back(rVal);
62 return static_cast<short>(++n);
63 }
64
65 short SbiStringPool::Add(double n, SbxDataType t)
66 {
67 static constexpr OUStringChar null('\0');
68
69 // tdf#143707 - add the type character after the null termination of the string in order to
70 // keep compatibility. After the type character has been added, the buffer contains the value
71 // of the double n, the string termination symbol, and the type character.
72 switch( t )
73 {
74 // tdf#142460 - properly handle boolean values in string pool
75 case SbxBOOL:
76 return Add(OUString::number(static_cast<short>(n)) + null + "b");
77 // tdf#131296 - store numeric value including its type character
78 // See GetSuffixType in basic/source/comp/scanner.cxx for type characters
79 case SbxINTEGER:
80 return Add(OUString::number(static_cast<short>(n)) + null + "%");
81 case SbxLONG:
82 return Add(OUString::number(static_cast<sal_Int32>(n)) + null + "&");
83 case SbxSINGLE:
84 return Add(OUString::number(static_cast<float>(n)) + null + "!");
85 case SbxDOUBLE:
86 return Add(OUString::number(n) + null + "#");
87 case SbxCURRENCY:
88 return Add(OUString::number(n) + null + "@");
89 case SbxDATE:
90 // tdf#168569 - support date values in string pool
91 return Add(OUString::number(n) + null + "d"); // Not in GetSuffixType
92 default: assert(false); break; // should not happen
93 }
94
95 return -1;
96 }
97
98 SbiSymPool::SbiSymPool( SbiStringPool& r, SbiSymScope s, SbiParser* pP ) :
99 rStrings(r),
100 pParent(nullptr),
101 pParser(pP),
102 eScope(s),
103 nProcId(0),
104 nCur(0)
105 {
106 }
107
108 SbiSymPool::~SbiSymPool()
109 {}
110
111
112 SbiSymDef* SbiSymPool::First()
113 {
114 nCur = sal_uInt16(-1);
115 return Next();
116 }
117
118 SbiSymDef* SbiSymPool::Next()
119 {
120 if (m_Data.size() <= ++nCur)
121 return nullptr;
122 else
123 return m_Data[ nCur ].get();
124 }
125
126
127 SbiSymDef* SbiSymPool::AddSym( const OUString& rName )
128 {
129 SbiSymDef* p = new SbiSymDef( rName );
130 p->nPos = m_Data.size();
131 p->nId = rStrings.Add( rName );
132 p->nProcId = nProcId;
133 p->pIn = this;
134 m_Data.insert( m_Data.begin() + p->nPos, std::unique_ptr<SbiSymDef>(p) );
135 return p;
136 }
137
138 SbiProcDef* SbiSymPool::AddProc( const OUString& rName )
139 {
140 SbiProcDef* p = new SbiProcDef( pParser, rName );
141 p->nPos = m_Data.size();
142 p->nId = rStrings.Add( rName );
143 // procs are always local
144 p->nProcId = 0;
145 p->pIn = this;
146 m_Data.insert( m_Data.begin() + p->nPos, std::unique_ptr<SbiProcDef>(p) );
147 return p;
148 }
149
150 // adding an externally constructed symbol definition
151
152 void SbiSymPool::Add( SbiSymDef* pDef )
153 {
154 if( !(pDef && pDef->pIn != this) )
155 return;
156
157 if( pDef->pIn )
158 {
159 #ifdef DBG_UTIL
160
161 pParser->Error( ERRCODE_BASIC_INTERNAL_ERROR, u"Dbl Pool"_ustr );
162 #endif
163 return;
164 }
165
166 pDef->nPos = m_Data.size();
167 if( !pDef->nId )
168 {
169 // A unique name must be created in the string pool
170 // for static variables (Form ProcName:VarName)
171 OUString aName( pDef->aName );
172 if( pDef->IsStatic() )
173 {
174 aName = pParser->aGblStrings.Find( nProcId )
175 + ":"
176 + pDef->aName;
177 }
178 pDef->nId = rStrings.Add( aName );
179 }
180
181 if( !pDef->GetProcDef() )
182 {
183 pDef->nProcId = nProcId;
184 }
185 pDef->pIn = this;
186 m_Data.insert( m_Data.begin() + pDef->nPos, std::unique_ptr<SbiSymDef>(pDef) );
187 }
188
189
190 SbiSymDef* SbiSymPool::Find( const OUString& rName, bool bSearchInParents )
191 {
192 sal_uInt16 nCount = m_Data.size();
193 for( sal_uInt16 i = 0; i < nCount; i++ )
194 {
195 SbiSymDef &r = *m_Data[ nCount - i - 1 ];
196 if( ( !r.nProcId || ( r.nProcId == nProcId)) &&
197 ( r.aName.equalsIgnoreAsciiCase(rName)))
198 {
199 return &r;
200 }
201 }
202 if( bSearchInParents && pParent )
203 {
204 return pParent->Find( rName );
205 }
206 else
207 {
208 return nullptr;
209 }
210 }
211
212
213 // find via position (from 0)
214
215 SbiSymDef* SbiSymPool::Get( sal_uInt16 n )
216 {
217 if (m_Data.size() <= n)
218 {
219 return nullptr;
220 }
221 else
222 {
223 return m_Data[ n ].get();
224 }
225 }
226
227 sal_uInt32 SbiSymPool::Define( const OUString& rName )
228 {
229 SbiSymDef* p = Find( rName );
230 if( p )
231 {
232 if( p->IsDefined() )
233 {
234 pParser->Error( ERRCODE_BASIC_LABEL_DEFINED, rName );
235 }
236 }
237 else
238 {
239 p = AddSym( rName );
240 }
241 return p->Define();
242 }
243
244 sal_uInt32 SbiSymPool::Reference( const OUString& rName )
245 {
246 SbiSymDef* p = Find( rName );
247 if( !p )
248 {
249 p = AddSym( rName );
250 }
251 // to be sure
252 pParser->aGen.GenStmnt();
253 return p->Reference();
254 }
255
256
257 void SbiSymPool::CheckRefs()
258 {
259 for (std::unique_ptr<SbiSymDef> & r : m_Data)
260 {
261 if( !r->IsDefined() )
262 {
263 pParser->Error( ERRCODE_BASIC_UNDEF_LABEL, r->GetName() );
264 }
265 }
266 }
267
268 SbiSymDef::SbiSymDef( OUString _aName ) :
269 aName(std::move(_aName)),
270 eType(SbxEMPTY),
271 pIn(nullptr),
272 nLen(0),
273 nDims(0),
274 nId(0),
275 nTypeId(0),
276 nProcId(0),
277 nPos(0),
278 nChain(0),
279 bNew(false),
280 bChained(false),
281 bByVal(false),
282 bOpt(false),
283 bStatic(false),
284 bAs(false),
285 bGlobal(false),
286 bParamArray(false),
287 bWithEvents(false),
288 bWithBrackets(false),
289 nDefaultId(0),
290 nFixedStringLength(-1)
291 {
292 }
293
294 SbiSymDef::~SbiSymDef()
295 {
296 }
297
298 SbiProcDef* SbiSymDef::GetProcDef()
299 {
300 return nullptr;
301 }
302
303 SbiConstDef* SbiSymDef::GetConstDef()
304 {
305 return nullptr;
306 }
307
308
309 const OUString& SbiSymDef::GetName()
310 {
311 if( pIn )
312 {
313 aName = pIn->rStrings.Find( nId );
314 }
315 return aName;
316 }
317
318
319 void SbiSymDef::SetType( SbxDataType t )
320 {
321 if( t == SbxVARIANT && pIn )
322 {
323 //See if there have been any deftype statements to set the default type
324 //of a variable based on its starting letter
325 sal_Unicode cu = aName[0];
326 if( cu < 256 )
327 {
328 unsigned char ch = static_cast<unsigned char>(cu);
329 if( ch == '_' )
330 {
331 ch = 'Z';
332 }
333 int ch2 = rtl::toAsciiUpperCase( ch );
334 int nIndex = ch2 - 'A';
335 if (nIndex >= 0 && nIndex < N_DEF_TYPES)
336 t = pIn->pParser->eDefTypes[nIndex];
337 }
338 }
339 eType = t;
340 }
341
342 // construct a backchain, if not yet defined
343 // the value that shall be stored as an operand is returned
344
345 sal_uInt32 SbiSymDef::Reference()
346 {
347 if( !bChained )
348 {
349 sal_uInt32 n = nChain;
350 nChain = pIn->pParser->aGen.GetOffset();
351 return n;
352 }
353 else return nChain;
354 }
355
356
357 sal_uInt32 SbiSymDef::Define()
358 {
359 sal_uInt32 n = pIn->pParser->aGen.GetPC();
360 pIn->pParser->aGen.GenStmnt();
361 if( nChain )
362 {
363 pIn->pParser->aGen.BackChain( nChain );
364 }
365 nChain = n;
366 bChained = true;
367 return nChain;
368 }
369
370 // A symbol definition may have its own pool. This is the case
371 // for objects and procedures (local variable)
372
373 SbiSymPool& SbiSymDef::GetPool()
374 {
375 if( !pPool )
376 {
377 pPool = std::make_unique<SbiSymPool>( pIn->pParser->aGblStrings, SbLOCAL, pIn->pParser );// is dumped
378 }
379 return *pPool;
380 }
381
382 SbiSymScope SbiSymDef::GetScope() const
383 {
384 return pIn ? pIn->GetScope() : SbLOCAL;
385 }
386
387
388 // The procedure definition has three pools:
389 // 1) aParams: is filled by the definition. Contains the
390 // parameters' names, like they're used inside the body.
391 // The first element is the return value.
392 // 2) pPool: all local variables
393 // 3) aLabels: labels
394
395 SbiProcDef::SbiProcDef( SbiParser* pParser, const OUString& rName,
396 bool bProcDecl )
397 : SbiSymDef( rName )
398 , aParams( pParser->aGblStrings, SbPARAM, pParser ) // is dumped
399 , aLabels( pParser->aLclStrings, SbLOCAL, pParser ) // is not dumped
400 , mbProcDecl( bProcDecl )
401 {
402 aParams.SetParent( &pParser->aPublics );
403 pPool = std::make_unique<SbiSymPool>( pParser->aGblStrings, SbLOCAL, pParser );
404 pPool->SetParent( &aParams );
405 nLine1 =
406 nLine2 = 0;
407 mePropMode = PropertyMode::NONE;
408 bPublic = true;
409 bCdecl = false;
410 bStatic = false;
411 // For return values the first element of the parameter
412 // list is always defined with name and type of the proc
413 aParams.AddSym( aName );
414 }
415
416 SbiProcDef::~SbiProcDef()
417 {}
418
419 SbiProcDef* SbiProcDef::GetProcDef()
420 {
421 return this;
422 }
423
424 void SbiProcDef::SetType( SbxDataType t )
425 {
426 SbiSymDef::SetType( t );
427 aParams.Get( 0 )->SetType( eType );
428 }
429
430 // match with a forward-declaration
431 // if the match is OK, pOld is replaced by this in the pool
432 // pOld is deleted in any case!
433
434 void SbiProcDef::Match( SbiProcDef* pOld )
435 {
436 SbiSymDef *pn=nullptr;
437 // parameter 0 is the function name
438 sal_uInt16 i;
439 for( i = 1; i < aParams.GetSize(); i++ )
440 {
441 SbiSymDef* po = pOld->aParams.Get( i );
442 pn = aParams.Get( i );
443 // no type matching - that is done during running
444 // but is it maybe called with too little parameters?
445 if( !po && !pn->IsOptional() && !pn->IsParamArray() )
446 {
447 break;
448 }
449 pOld->aParams.Next();
450 }
451
452 if( pn && i < aParams.GetSize() && pOld->pIn )
453 {
454 // mark the whole line
455 pOld->pIn->GetParser()->SetCol1( 0 );
456 pOld->pIn->GetParser()->Error( ERRCODE_BASIC_BAD_DECLARATION, aName );
457 }
458
459 if( !pIn && pOld->pIn )
460 {
461 // Replace old entry with the new one
462 nPos = pOld->nPos;
463 nId = pOld->nId;
464 pIn = pOld->pIn;
465
466 // don't delete pOld twice, if it's stored in m_Data
467 if (pOld == pIn->m_Data[nPos].get())
468 pOld = nullptr;
469 pIn->m_Data[nPos].reset(this);
470 }
471 delete pOld;
472 }
473
474 void SbiProcDef::setPropertyMode( PropertyMode ePropMode )
475 {
476 mePropMode = ePropMode;
477 if( mePropMode == PropertyMode::NONE )
478 return;
479
480 // Prop name = original scanned procedure name
481 maPropName = aName;
482
483 // CompleteProcName includes "Property xxx "
484 // to avoid conflicts with other symbols
485 OUString aCompleteProcName = u"Property "_ustr;
486 switch( mePropMode )
487 {
488 case PropertyMode::Get: aCompleteProcName += "Get "; break;
489 case PropertyMode::Let: aCompleteProcName += "Let "; break;
490 case PropertyMode::Set: aCompleteProcName += "Set "; break;
491 case PropertyMode::NONE: OSL_FAIL( "Illegal PropertyMode PropertyMode::NONE" ); break;
492 }
493 aCompleteProcName += aName;
494 aName = aCompleteProcName;
495 }
496
497
498 SbiConstDef::SbiConstDef( const OUString& rName )
499 : SbiSymDef( rName )
500 {
501 nVal = 0; eType = SbxINTEGER;
502 }
503
504 void SbiConstDef::Set( double n, SbxDataType t )
505 {
506 aVal.clear(); nVal = n; eType = t;
507 }
508
509 void SbiConstDef::Set( const OUString& n )
510 {
511 aVal = n; nVal = 0; eType = SbxSTRING;
512 }
513
514 SbiConstDef::~SbiConstDef()
515 {}
516
517 SbiConstDef* SbiConstDef::GetConstDef()
518 {
519 return this;
520 }
521
522 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */