Debian Codegraph @

basic/source/comp/dim.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 #include <basic/sberrors.hxx>
21 #include <basic/sbstar.hxx>
22 #include <basic/sbx.hxx>
23 #include <sbunoobj.hxx>
24 #include <parser.hxx>
25 #include <sb.hxx>
26 #include <osl/diagnose.h>
27 #include <com/sun/star/reflection/theCoreReflection.hpp>
28 #include <comphelper/processfactory.hxx>
29 #include <com/sun/star/uno/Exception.hpp>
30 #include <basic/codecompletecache.hxx>
31 #include <memory>
32
33 using namespace ::com::sun::star;
34 using namespace ::com::sun::star::uno;
35
36 // Declaration of a variable
37 // If there are errors it will be parsed up to the comma or the newline.
38 // Return-value: a new instance, which were inserted and then deleted.
39 // Array-Index were returned as SbiExprList
40
41 SbiSymDef* SbiParser::VarDecl( SbiExprListPtr* ppDim, bool bStatic, bool bConst )
42 {
43 bool bWithEvents = false;
44 if( Peek() == WITHEVENTS )
45 {
46 Next();
47 bWithEvents = true;
48 }
49 if( !TestSymbol() ) return nullptr;
50 SbxDataType t = eScanType;
51 SbiSymDef* pDef = bConst ? new SbiConstDef( aSym ) : new SbiSymDef( aSym );
52 SbiExprListPtr pDim;
53 // Brackets?
54 if( Peek() == LPAREN )
55 {
56 pDim = SbiExprList::ParseDimList( this );
57 if( !pDim->GetDims() )
58 pDef->SetWithBrackets();
59 }
60 pDef->SetType( t );
61 if( bStatic )
62 pDef->SetStatic();
63 if( bWithEvents )
64 pDef->SetWithEvents();
65 TypeDecl( *pDef );
66 if (ppDim)
67 *ppDim = std::move(pDim);
68 else if (pDim)
69 {
70 if(pDim->GetDims() )
71 Error( ERRCODE_BASIC_EXPECTED, u"()"_ustr );
72 }
73 return pDef;
74 }
75
76 // Resolving of an AS-Type-Declaration
77 // The data type were inserted into the handed over variable
78
79 void SbiParser::TypeDecl( SbiSymDef& rDef, bool bAsNewAlreadyParsed )
80 {
81 SbxDataType eType = rDef.GetType();
82 if( !(bAsNewAlreadyParsed || Peek() == AS) )
83 return;
84
85 short nSize = 0;
86 if( !bAsNewAlreadyParsed )
87 Next();
88 rDef.SetDefinedAs();
89 SbiToken eTok = Next();
90 if( !bAsNewAlreadyParsed && eTok == NEW )
91 {
92 rDef.SetNew();
93 eTok = Next();
94 }
95 switch( eTok )
96 {
97 case ANY:
98 if( rDef.IsNew() )
99 Error( ERRCODE_BASIC_SYNTAX );
100 eType = SbxVARIANT; break;
101 case TINTEGER:
102 case TLONG:
103 case TSINGLE:
104 case TDOUBLE:
105 case TCURRENCY:
106 case TDATE:
107 case TSTRING:
108 case TOBJECT:
109 case ERROR_:
110 case TBOOLEAN:
111 case TVARIANT:
112 case TBYTE:
113 if( rDef.IsNew() )
114 Error( ERRCODE_BASIC_SYNTAX );
115 eType = (eTok==TBYTE) ? SbxBYTE : SbxDataType( eTok - TINTEGER + SbxINTEGER );
116 if( eType == SbxSTRING )
117 {
118 // STRING*n ?
119 if( Peek() == MUL )
120 { // fixed size!
121 Next();
122 SbiConstExpression aSize( this );
123 nSize = aSize.GetShortValue();
124 if( nSize < 0 || (bVBASupportOn && nSize <= 0) )
125 Error( ERRCODE_BASIC_OUT_OF_RANGE );
126 else
127 rDef.SetFixedStringLength( nSize );
128 }
129 }
130 break;
131 case SYMBOL: // can only be a TYPE or an object class!
132 if( eScanType != SbxVARIANT )
133 Error( ERRCODE_BASIC_SYNTAX );
134 else
135 {
136 OUString aCompleteName = aSym;
137
138 // #52709 DIM AS NEW for Uno with full-qualified name
139 if( Peek() == DOT )
140 {
141 OUString aDotStr( '.' );
142 while( Peek() == DOT )
143 {
144 aCompleteName += aDotStr;
145 Next();
146 SbiToken ePeekTok = Peek();
147 if( ePeekTok == SYMBOL || IsKwd( ePeekTok ) )
148 {
149 Next();
150 aCompleteName += aSym;
151 }
152 else
153 {
154 Next();
155 Error( ERRCODE_BASIC_UNEXPECTED, SYMBOL );
156 break;
157 }
158 }
159 }
160 else if( rEnumArray->Find( aCompleteName, SbxClassType::Object ) || ( IsVBASupportOn() && VBAConstantHelper::instance().isVBAConstantType( aCompleteName ) ) )
161 {
162 eType = SbxLONG;
163 break;
164 }
165
166 // Take over in the string pool
167 rDef.SetTypeId( aGblStrings.Add( aCompleteName ) );
168
169 if( rDef.IsNew() && pProc == nullptr )
170 aRequiredTypes.push_back( aCompleteName );
171 }
172 eType = SbxOBJECT;
173 break;
174 case FIXSTRING: // new syntax for complex UNO types
175 rDef.SetTypeId( aGblStrings.Add( aSym ) );
176 eType = SbxOBJECT;
177 break;
178 default:
179 Error( ERRCODE_BASIC_UNEXPECTED, eTok );
180 Next();
181 }
182 // The variable could have been declared with a suffix
183 if( rDef.GetType() != SbxVARIANT )
184 {
185 if( rDef.GetType() != eType )
186 Error( ERRCODE_BASIC_VAR_DEFINED, rDef.GetName() );
187 else if( eType == SbxSTRING && rDef.GetLen() != nSize )
188 Error( ERRCODE_BASIC_VAR_DEFINED, rDef.GetName() );
189 }
190 rDef.SetType( eType );
191 rDef.SetLen( nSize );
192 }
193
194 // Here variables, arrays and structures were defined.
195 // DIM/PRIVATE/PUBLIC/GLOBAL
196
197 void SbiParser::Dim()
198 {
199 DefVar( SbiOpcode::DIM_, pProc && bVBASupportOn && pProc->IsStatic() );
200 }
201
202 void SbiParser::DefVar( SbiOpcode eOp, bool bStatic )
203 {
204 SbiSymPool* pOldPool = pPool;
205 bool bSwitchPool = false;
206 bool bPersistentGlobal = false;
207 SbiToken eFirstTok = eCurTok;
208
209 if( pProc && ( eCurTok == GLOBAL || eCurTok == PUBLIC || eCurTok == PRIVATE ) )
210 Error( ERRCODE_BASIC_NOT_IN_SUBR, eCurTok );
211 if( eCurTok == PUBLIC || eCurTok == GLOBAL )
212 {
213 bSwitchPool = true; // at the right moment switch to the global pool
214 if( eCurTok == GLOBAL )
215 bPersistentGlobal = true;
216 }
217 // behavior in VBA is that a module scope variable's lifetime is
218 // tied to the document. e.g. a module scope variable is global
219 if( GetBasic()->IsDocBasic() && bVBASupportOn && !pProc )
220 bPersistentGlobal = true;
221 // PRIVATE is a synonymous for DIM
222 // _CONST_?
223 bool bConst = false;
224 if( eCurTok == CONST_ )
225 bConst = true;
226 else if( Peek() == CONST_ )
227 {
228 Next();
229 bConst = true;
230 }
231
232 // #110004 It can also be a sub/function
233 if( !bConst && (eCurTok == SUB || eCurTok == FUNCTION || eCurTok == PROPERTY ||
234 eCurTok == STATIC || eCurTok == ENUM || eCurTok == DECLARE || eCurTok == TYPE) )
235 {
236 // Next token is read here, because !bConst
237 bool bPrivate = ( eFirstTok == PRIVATE );
238
239 if( eCurTok == STATIC )
240 {
241 Next();
242 DefStatic( bPrivate );
243 }
244 else if( eCurTok == SUB || eCurTok == FUNCTION || eCurTok == PROPERTY )
245 {
246 // End global chain if necessary (not done in
247 // SbiParser::Parse() under these conditions
248 if( bNewGblDefs && nGblChain == 0 )
249 {
250 nGblChain = aGen.Gen( SbiOpcode::JUMP_, 0 );
251 bNewGblDefs = false;
252 }
253 Next();
254 DefProc( false, bPrivate );
255 return;
256 }
257 else if( eCurTok == ENUM )
258 {
259 Next();
260 DefEnum( bPrivate );
261 return;
262 }
263 else if( eCurTok == DECLARE )
264 {
265 Next();
266 DefDeclare( bPrivate );
267 return;
268 }
269 // #i109049
270 else if( eCurTok == TYPE )
271 {
272 Next();
273 DefType(); // TODO: Use bPrivate in DefType()
274 return;
275 }
276 }
277
278 // SHARED were ignored
279 if( Peek() == SHARED ) Next();
280
281 // PRESERVE only at REDIM
282 if( Peek() == PRESERVE )
283 {
284 Next();
285 if( eOp == SbiOpcode::REDIM_ )
286 eOp = SbiOpcode::REDIMP_;
287 else
288 Error( ERRCODE_BASIC_UNEXPECTED, eCurTok );
289 }
290 SbiSymDef* pDef;
291 SbiExprListPtr pDim;
292
293 // #40689, Statics -> Module-Initialising, skip in Sub
294 sal_uInt32 nEndOfStaticLbl = 0;
295 if( !bVBASupportOn && bStatic )
296 {
297 nEndOfStaticLbl = aGen.Gen( SbiOpcode::JUMP_, 0 );
298 aGen.Statement(); // catch up on static here
299 }
300
301 bool bDefined = false;
302 while( ( pDef = VarDecl( &pDim, bStatic, bConst ) ) != nullptr )
303 {
304 /*fprintf(stderr, "Actual sub: \n");
305 fprintf(stderr, "Symbol name: %s\n",OUStringToOString(pDef->GetName(),RTL_TEXTENCODING_UTF8).getStr());*/
306 EnableErrors();
307 // search variable:
308 if( bSwitchPool )
309 pPool = &aGlobals;
310 SbiSymDef* pOld = pPool->Find( pDef->GetName() );
311 // search also in the Runtime-Library
312 bool bRtlSym = false;
313 if( !pOld )
314 {
315 pOld = CheckRTLForSym( pDef->GetName(), SbxVARIANT );
316 if( pOld )
317 bRtlSym = true;
318 }
319 if( pOld && eOp != SbiOpcode::REDIM_ && eOp != SbiOpcode::REDIMP_ )
320 {
321 if( pDef->GetScope() == SbLOCAL )
322 if (auto eOldScope = pOld->GetScope(); eOldScope != SbLOCAL && eOldScope != SbPARAM)
323 pOld = nullptr;
324 }
325 if( pOld )
326 {
327 bDefined = true;
328 // always an error at a RTL-S
329 if( !bRtlSym && (eOp == SbiOpcode::REDIM_ || eOp == SbiOpcode::REDIMP_) )
330 {
331 // compare the attributes at a REDIM
332 SbxDataType eDefType;
333 bool bError_ = false;
334 if( pOld->IsStatic() )
335 {
336 bError_ = true;
337 }
338 else if( pOld->GetType() != ( eDefType = pDef->GetType() ) )
339 {
340 if( eDefType != SbxVARIANT || pDef->IsDefinedAs() )
341 bError_ = true;
342 }
343 if( bError_ )
344 Error( ERRCODE_BASIC_VAR_DEFINED, pDef->GetName() );
345 }
346 else
347 Error( ERRCODE_BASIC_VAR_DEFINED, pDef->GetName() );
348 delete pDef; pDef = pOld;
349 }
350 else
351 pPool->Add( pDef );
352
353 // #36374: Create the variable in front of the distinction IsNew()
354 // Otherwise error at Dim Identifier As New Type and option explicit
355 if( !bDefined && eOp != SbiOpcode::REDIM_ && eOp != SbiOpcode::REDIMP_
356 && ( !bConst || pDef->GetScope() == SbGLOBAL ) )
357 {
358 // Declare variable or global constant
359 SbiOpcode eOp2;
360 switch ( pDef->GetScope() )
361 {
362 case SbGLOBAL: eOp2 = bPersistentGlobal ? SbiOpcode::GLOBAL_P_ : SbiOpcode::GLOBAL_;
363 goto global;
364 case SbPUBLIC: eOp2 = bPersistentGlobal ? SbiOpcode::PUBLIC_P_ : SbiOpcode::PUBLIC_;
365 // #40689, no own Opcode anymore
366 if( bVBASupportOn && bStatic )
367 {
368 eOp2 = SbiOpcode::STATIC_;
369 break;
370 }
371 global: aGen.BackChain( nGblChain );
372 nGblChain = 0;
373 bGblDefs = bNewGblDefs = true;
374 break;
375 default: eOp2 = SbiOpcode::LOCAL_;
376 }
377 sal_uInt32 nOpnd2 = sal::static_int_cast< sal_uInt16 >( pDef->GetType() );
378 if( pDef->IsWithEvents() )
379 nOpnd2 |= SBX_TYPE_WITH_EVENTS_FLAG;
380
381 if( bCompatible && pDef->IsNew() )
382 nOpnd2 |= SBX_TYPE_DIM_AS_NEW_FLAG;
383
384 short nFixedStringLength = pDef->GetFixedStringLength();
385 if( nFixedStringLength >= 0 )
386 nOpnd2 |= (SBX_FIXED_LEN_STRING_FLAG + (sal_uInt32(nFixedStringLength) << 17)); // len = all bits above 0x10000
387
388 if( pDim != nullptr && pDim->GetDims() > 0 )
389 nOpnd2 |= SBX_TYPE_VAR_TO_DIM_FLAG;
390
391 aGen.Gen( eOp2, pDef->GetId(), nOpnd2 );
392 }
393
394 // Initialising for self-defined data types
395 // and per NEW created variable
396 if( pDef->GetType() == SbxOBJECT
397 && pDef->GetTypeId() )
398 {
399 if( !bCompatible && !pDef->IsNew() )
400 {
401 OUString aTypeName( aGblStrings.Find( pDef->GetTypeId() ) );
402 if( rTypeArray->Find( aTypeName, SbxClassType::Object ) == nullptr )
403 {
404 if( CodeCompleteOptions::IsExtendedTypeDeclaration() )
405 {
406 if(!IsUnoInterface(aTypeName))
407 Error( ERRCODE_BASIC_UNDEF_TYPE, aTypeName );
408 }
409 else
410 Error( ERRCODE_BASIC_UNDEF_TYPE, aTypeName );
411 }
412 }
413
414 if( bConst )
415 {
416 Error( ERRCODE_BASIC_SYNTAX );
417 }
418
419 if( pDim )
420 {
421 if( eOp == SbiOpcode::REDIMP_ )
422 {
423 SbiExpression aExpr( this, *pDef, nullptr );
424 aExpr.Gen();
425 aGen.Gen( SbiOpcode::REDIMP_ERASE_ );
426
427 pDef->SetDims( pDim->GetDims() );
428 SbiExpression aExpr2( this, *pDef, std::move(pDim) );
429 aExpr2.Gen();
430 aGen.Gen( SbiOpcode::DCREATE_REDIMP_, pDef->GetId(), pDef->GetTypeId() );
431 }
432 else
433 {
434 // tdf#145371, tdf#136755 - only delete the variable beforehand REDIM
435 if (eOp == SbiOpcode::REDIM_)
436 {
437 SbiExpression aExpr(this, *pDef, nullptr);
438 aExpr.Gen();
439 aGen.Gen(bVBASupportOn ? SbiOpcode::ERASE_CLEAR_ : SbiOpcode::ERASE_);
440 }
441
442 pDef->SetDims( pDim->GetDims() );
443 SbiExpression aExpr2( this, *pDef, std::move(pDim) );
444 aExpr2.Gen();
445 aGen.Gen( SbiOpcode::DCREATE_, pDef->GetId(), pDef->GetTypeId() );
446 }
447 }
448 else
449 {
450 SbiExpression aExpr( this, *pDef );
451 aExpr.Gen();
452
453 /* tdf#88442
454 * Don't initialize a
455 * Global X as New SomeObjectType
456 * if it has already been initialized.
457 * This approach relies on JUMPT evaluating Object->NULL as being 'false'
458 * But the effect of this code is similar to inserting
459 * If IsNull(YourGlobal)
460 * Set YourGlobal = ' new obj
461 * End If ' If IsNull(YourGlobal)
462 * Only for globals. For locals that check is skipped as it's unnecessary
463 */
464 sal_uInt32 come_from = 0;
465 if ( pDef->GetScope() == SbGLOBAL )
466 {
467 come_from = aGen.Gen( SbiOpcode::JUMPT_, 0 );
468 aGen.Gen( SbiOpcode::FIND_, pDef->GetId(), pDef->GetTypeId() );
469 }
470
471 SbiOpcode eOp_ = pDef->IsNew() ? SbiOpcode::CREATE_ : SbiOpcode::TCREATE_;
472 aGen.Gen( eOp_, pDef->GetId(), pDef->GetTypeId() );
473 if ( bVBASupportOn )
474 aGen.Gen( SbiOpcode::VBASET_ );
475 else
476 aGen.Gen( SbiOpcode::SET_ );
477
478 if ( come_from )
479 {
480 // See other tdf#88442 comment above where come_from is
481 // initialized. This is effectively 'inserting' the
482 // End If ' If IsNull(YourGlobal)
483 aGen.BackChain( come_from );
484 }
485 }
486 }
487 else
488 {
489 if( bConst )
490 {
491 // Definition of the constants
492 if( pDim )
493 {
494 Error( ERRCODE_BASIC_SYNTAX );
495 }
496 SbiExpression aVar( this, *pDef );
497 if( !TestToken( EQ ) )
498 goto MyBreak; // (see below)
499 SbiConstExpression aExpr( this );
500 if( !bDefined && aExpr.IsValid() )
501 {
502 if( pDef->GetScope() == SbGLOBAL )
503 {
504 // Create code only for the global constant!
505 aVar.Gen();
506 aExpr.Gen();
507 aGen.Gen( SbiOpcode::PUTC_ );
508 }
509 SbiConstDef* pConst = pDef->GetConstDef();
510 if( aExpr.GetType() == SbxSTRING )
511 pConst->Set( aExpr.GetString() );
512 else
513 pConst->Set( aExpr.GetValue(), aExpr.GetType() );
514 }
515 }
516 else if( pDim )
517 {
518 // Dimension the variable
519 // Delete the var at REDIM beforehand
520 if( eOp == SbiOpcode::REDIM_ )
521 {
522 SbiExpression aExpr( this, *pDef, nullptr );
523 aExpr.Gen();
524 if ( bVBASupportOn )
525 // delete the array but
526 // clear the variable ( this
527 // allows the processing of
528 // the param to happen as normal without errors ( ordinary ERASE just clears the array )
529 aGen.Gen( SbiOpcode::ERASE_CLEAR_ );
530 else
531 aGen.Gen( SbiOpcode::ERASE_ );
532 }
533 else if( eOp == SbiOpcode::REDIMP_ )
534 {
535 SbiExpression aExpr( this, *pDef, nullptr );
536 aExpr.Gen();
537 aGen.Gen( SbiOpcode::REDIMP_ERASE_ );
538 }
539 pDef->SetDims( pDim->GetDims() );
540 if( bPersistentGlobal )
541 pDef->SetGlobal( true );
542 SbiExpression aExpr( this, *pDef, std::move(pDim) );
543 aExpr.Gen();
544 pDef->SetGlobal( false );
545 aGen.Gen( (eOp == SbiOpcode::STATIC_) ? SbiOpcode::DIM_ : eOp );
546 }
547 }
548 if( !TestComma() )
549 goto MyBreak;
550
551 // Implementation of bSwitchPool (see above): pPool must not be set to &aGlobals
552 // at the VarDecl-Call.
553 // Apart from that the behavior should be absolutely identical,
554 // i.e., pPool had to be reset always at the end of the loop.
555 // also at a break
556 pPool = pOldPool;
557 continue; // Skip MyBreak
558 MyBreak:
559 pPool = pOldPool;
560 break;
561 }
562
563 // #40689, finalize the jump over statics declarations
564 if( !bVBASupportOn && bStatic )
565 {
566 // maintain the global chain
567 nGblChain = aGen.Gen( SbiOpcode::JUMP_, 0 );
568 bGblDefs = bNewGblDefs = true;
569
570 // Register for Sub a jump to the end of statics
571 aGen.BackChain( nEndOfStaticLbl );
572 }
573
574 }
575
576 // Here were Arrays redimensioned.
577
578 void SbiParser::ReDim()
579 {
580 DefVar( SbiOpcode::REDIM_, pProc && bVBASupportOn && pProc->IsStatic() );
581 }
582
583 // ERASE array, ...
584
585 void SbiParser::Erase()
586 {
587 while( !bAbort )
588 {
589 SbiExpression aExpr( this, SbLVALUE );
590 aExpr.Gen();
591 aGen.Gen( SbiOpcode::ERASE_ );
592 if( !TestComma() ) break;
593 }
594 }
595
596 // Declaration of a data type
597
598 void SbiParser::Type()
599 {
600 DefType();
601 }
602
603 void SbiParser::DefType()
604 {
605 // Read the new Token lesen. It had to be a symbol
606 if (!TestSymbol())
607 return;
608
609 if (rTypeArray->Find(aSym,SbxClassType::Object))
610 {
611 Error( ERRCODE_BASIC_VAR_DEFINED, aSym );
612 return;
613 }
614
615 SbxObject *pType = new SbxObject(aSym);
616
617 bool bDone = false;
618
619 while( !bDone && !IsEof() )
620 {
621 std::unique_ptr<SbiSymDef> pElem;
622 SbiExprListPtr pDim;
623 switch( Peek() )
624 {
625 case ENDTYPE :
626 bDone = true;
627 Next();
628 break;
629
630 case EOLN :
631 case REM :
632 Next();
633 break;
634
635 default:
636 pElem.reset(VarDecl(&pDim, false, false));
637 if( !pElem )
638 bDone = true; // Error occurred
639 }
640 if( pElem )
641 {
642 SbxArray *pTypeMembers = pType->GetProperties();
643 OUString aElemName = pElem->GetName();
644 if( pTypeMembers->Find( aElemName, SbxClassType::DontCare) )
645 {
646 Error (ERRCODE_BASIC_VAR_DEFINED);
647 }
648 else
649 {
650 SbxDataType eElemType = pElem->GetType();
651 SbxProperty *pTypeElem = new SbxProperty( aElemName, eElemType );
652 if( pDim )
653 {
654 SbxDimArray* pArray = new SbxDimArray( pElem->GetType() );
655 if ( pDim->GetSize() )
656 {
657 // Dimension the target array
658
659 for ( short i=0; i<pDim->GetSize();++i )
660 {
661 sal_Int32 lb = nBase;
662 SbiExprNode* pNode = pDim->Get(i)->GetExprNode();
663 sal_Int32 ub = pNode->GetNumber();
664 if ( !pDim->Get( i )->IsBased() ) // each dim is low/up
665 {
666 if ( ++i >= pDim->GetSize() ) // trouble
667 StarBASIC::FatalError( ERRCODE_BASIC_INTERNAL_ERROR );
668 pNode = pDim->Get(i)->GetExprNode();
669 lb = ub;
670 ub = pNode->GetNumber();
671 }
672 else if ( !bCompatible )
673 ub += nBase;
674 pArray->AddDim(lb, ub);
675 }
676 pArray->setHasFixedSize( true );
677 }
678 else
679 pArray->unoAddDim(0, -1); // variant array
680 SbxFlagBits nSavFlags = pTypeElem->GetFlags();
681 // need to reset the FIXED flag
682 // when calling PutObject ( because the type will not match Object )
683 pTypeElem->ResetFlag( SbxFlagBits::Fixed );
684 pTypeElem->PutObject( pArray );
685 pTypeElem->SetFlags( nSavFlags );
686 }
687 // Nested user type?
688 if( eElemType == SbxOBJECT )
689 {
690 sal_uInt16 nElemTypeId = pElem->GetTypeId();
691 if( nElemTypeId != 0 )
692 {
693 OUString aTypeName( aGblStrings.Find( nElemTypeId ) );
694 SbxObject* pTypeObj = static_cast< SbxObject* >( rTypeArray->Find( aTypeName, SbxClassType::Object ) );
695 if( pTypeObj != nullptr )
696 {
697 SbxObjectRef pCloneObj = cloneTypeObjectImpl( *pTypeObj );
698 pTypeElem->PutObject( pCloneObj.get() );
699 }
700 }
701 }
702 pTypeMembers->Insert(pTypeElem, pTypeMembers->Count());
703 }
704 }
705 }
706
707 pType->Remove( u"Name"_ustr, SbxClassType::DontCare );
708 pType->Remove( u"Parent"_ustr, SbxClassType::DontCare );
709
710 rTypeArray->Insert(pType, rTypeArray->Count());
711 }
712
713
714 // Declaration of Enum type
715
716 void SbiParser::Enum()
717 {
718 DefEnum( false );
719 }
720
721 void SbiParser::DefEnum( bool bPrivate )
722 {
723 // Read the new Token. It had to be a symbol
724 if (!TestSymbol())
725 return;
726
727 OUString aEnumName = aSym;
728 if( rEnumArray->Find(aEnumName,SbxClassType::Object) )
729 {
730 Error( ERRCODE_BASIC_VAR_DEFINED, aSym );
731 return;
732 }
733
734 SbxObject *pEnum = new SbxObject( aEnumName );
735 if( bPrivate )
736 {
737 pEnum->SetFlag( SbxFlagBits::Private );
738 }
739 SbiSymDef* pElem;
740 bool bDone = false;
741
742 // Starting with -1 to make first default value 0 after ++
743 sal_Int32 nCurrentEnumValue = -1;
744 while( !bDone && !IsEof() )
745 {
746 switch( Peek() )
747 {
748 case ENDENUM :
749 pElem = nullptr;
750 bDone = true;
751 Next();
752 break;
753
754 case EOLN :
755 case REM :
756 pElem = nullptr;
757 Next();
758 break;
759
760 default:
761 {
762 SbiExprListPtr pDim;
763 pElem = VarDecl( &pDim, false, true );
764 if( !pElem )
765 {
766 bDone = true; // Error occurred
767 break;
768 }
769 else if( pDim )
770 {
771 Error( ERRCODE_BASIC_SYNTAX );
772 bDone = true; // Error occurred
773 break;
774 }
775
776 SbiExpression aVar( this, *pElem );
777 if( Peek() == EQ )
778 {
779 Next();
780
781 SbiConstExpression aExpr( this );
782 if( aExpr.IsValid() )
783 {
784 SbxVariableRef xConvertVar = new SbxVariable();
785 if( aExpr.GetType() == SbxSTRING )
786 xConvertVar->PutString( aExpr.GetString() );
787 else
788 xConvertVar->PutDouble( aExpr.GetValue() );
789
790 nCurrentEnumValue = xConvertVar->GetLong();
791 }
792 }
793 else
794 nCurrentEnumValue++;
795
796 SbiSymPool* pPoolToUse = bPrivate ? pPool : &aGlobals;
797
798 SbiSymDef* pOld = pPoolToUse->Find( pElem->GetName() );
799 if( pOld )
800 {
801 Error( ERRCODE_BASIC_VAR_DEFINED, pElem->GetName() );
802 bDone = true; // Error occurred
803 break;
804 }
805
806 pPool->Add( pElem );
807
808 if( !bPrivate )
809 {
810 aGen.BackChain( nGblChain );
811 nGblChain = 0;
812 bGblDefs = bNewGblDefs = true;
813 aGen.Gen(
814 SbiOpcode::GLOBAL_, pElem->GetId(),
815 sal::static_int_cast< sal_uInt16 >( pElem->GetType() ) );
816
817 aVar.Gen();
818 sal_uInt16 nStringId = aGen.GetParser()->aGblStrings.Add( nCurrentEnumValue, SbxLONG );
819 aGen.Gen( SbiOpcode::NUMBER_, nStringId );
820 aGen.Gen( SbiOpcode::PUTC_ );
821 }
822
823 SbiConstDef* pConst = pElem->GetConstDef();
824 pConst->Set( nCurrentEnumValue, SbxLONG );
825 }
826 }
827 if( pElem )
828 {
829 SbxArray *pEnumMembers = pEnum->GetProperties();
830 SbxProperty *pEnumElem = new SbxProperty( pElem->GetName(), SbxLONG );
831 pEnumElem->PutLong( nCurrentEnumValue );
832 pEnumElem->ResetFlag( SbxFlagBits::Write );
833 pEnumElem->SetFlag( SbxFlagBits::Const );
834 pEnumMembers->Insert(pEnumElem, pEnumMembers->Count());
835 }
836 }
837
838 pEnum->Remove( u"Name"_ustr, SbxClassType::DontCare );
839 pEnum->Remove( u"Parent"_ustr, SbxClassType::DontCare );
840
841 rEnumArray->Insert(pEnum, rEnumArray->Count());
842 }
843
844
845 // Procedure-Declaration
846 // the first Token is already read in (SUB/FUNCTION)
847 // xxx Name [LIB "name"[ALIAS "name"]][(Parameter)][AS TYPE]
848
849 SbiProcDef* SbiParser::ProcDecl( bool bDecl )
850 {
851 bool bFunc = ( eCurTok == FUNCTION );
852 bool bProp = ( eCurTok == GET || eCurTok == SET || eCurTok == LET );
853 if( !TestSymbol() ) return nullptr;
854 OUString aName( aSym );
855 SbxDataType eType = eScanType;
856 SbiProcDef* pDef = new SbiProcDef( this, aName, true );
857 pDef->SetType( eType );
858 if( Peek() == CDECL_ )
859 {
860 Next(); pDef->SetCdecl(true);
861 }
862 if( Peek() == LIB )
863 {
864 Next();
865 if( Next() == FIXSTRING )
866 {
867 pDef->GetLib() = aSym;
868 }
869 else
870 {
871 Error( ERRCODE_BASIC_SYNTAX );
872 }
873 }
874 if( Peek() == ALIAS )
875 {
876 Next();
877 if( Next() == FIXSTRING )
878 {
879 pDef->GetAlias() = aSym;
880 }
881 else
882 {
883 Error( ERRCODE_BASIC_SYNTAX );
884 }
885 }
886 if( !bDecl )
887 {
888 // CDECL, LIB and ALIAS are invalid
889 if( !pDef->GetLib().isEmpty() )
890 {
891 Error( ERRCODE_BASIC_UNEXPECTED, LIB );
892 }
893 if( !pDef->GetAlias().isEmpty() )
894 {
895 Error( ERRCODE_BASIC_UNEXPECTED, ALIAS );
896 }
897 if( pDef->IsCdecl() )
898 {
899 Error( ERRCODE_BASIC_UNEXPECTED, CDECL_ );
900 }
901 pDef->SetCdecl( false );
902 pDef->GetLib().clear();
903 pDef->GetAlias().clear();
904 }
905 else if( pDef->GetLib().isEmpty() )
906 {
907 // ALIAS and CDECL only together with LIB
908 if( !pDef->GetAlias().isEmpty() )
909 {
910 Error( ERRCODE_BASIC_UNEXPECTED, ALIAS );
911 }
912 if( pDef->IsCdecl() )
913 {
914 Error( ERRCODE_BASIC_UNEXPECTED, CDECL_ );
915 }
916 pDef->SetCdecl( false );
917 pDef->GetAlias().clear();
918 }
919 // Brackets?
920 if( Peek() == LPAREN )
921 {
922 Next();
923 if( Peek() == RPAREN )
924 {
925 Next();
926 }
927 else
928 {
929 for(;;)
930 {
931 bool bByVal = false;
932 bool bOptional = false;
933 bool bParamArray = false;
934 while( Peek() == BYVAL || Peek() == BYREF || Peek() == OPTIONAL_ )
935 {
936 if( Peek() == BYVAL )
937 {
938 bByVal = true;
939 }
940 else if ( Peek() == BYREF )
941 {
942 bByVal = false;
943 }
944 else if ( Peek() == OPTIONAL_ )
945 {
946 bOptional = true;
947 }
948 Next();
949 }
950 if( bCompatible && Peek() == PARAMARRAY )
951 {
952 if( bByVal || bOptional )
953 {
954 Error( ERRCODE_BASIC_UNEXPECTED, PARAMARRAY );
955 }
956 Next();
957 bParamArray = true;
958 }
959 SbiSymDef* pPar = VarDecl( nullptr, false, false );
960 if( !pPar )
961 {
962 break;
963 }
964 if( bByVal )
965 {
966 pPar->SetByVal(true);
967 }
968 if( bOptional )
969 {
970 pPar->SetOptional();
971 }
972 if( bParamArray )
973 {
974 pPar->SetParamArray();
975 }
976 if (SbiSymDef* pOldDef = pDef->GetParams().Find(pPar->GetName(), false))
977 {
978 Error(ERRCODE_BASIC_VAR_DEFINED, pPar->GetName());
979 delete pPar;
980 pPar = pOldDef;
981 }
982 else
983 pDef->GetParams().Add( pPar );
984 SbiToken eTok = Next();
985 if( eTok != COMMA && eTok != RPAREN )
986 {
987 bool bError2 = true;
988 if( bOptional && bCompatible && eTok == EQ )
989 {
990 {
991 SbiConstExpression aDefaultExpr(this);
992 SbxDataType eType2 = aDefaultExpr.GetType();
993
994 sal_uInt16 nStringId;
995 if (eType2 == SbxSTRING)
996 {
997 nStringId = aGblStrings.Add(aDefaultExpr.GetString());
998 }
999 else
1000 {
1001 nStringId = aGblStrings.Add(aDefaultExpr.GetValue(), eType2);
1002 }
1003 pPar->SetDefaultId(nStringId);
1004 }
1005
1006 eTok = Next();
1007 if( eTok == COMMA || eTok == RPAREN )
1008 {
1009 bError2 = false;
1010 }
1011 }
1012 if( bError2 )
1013 {
1014 Error( ERRCODE_BASIC_EXPECTED, RPAREN );
1015 break;
1016 }
1017 }
1018 if( eTok == RPAREN )
1019 {
1020 break;
1021 }
1022 }
1023 }
1024 }
1025 TypeDecl( *pDef );
1026 if( eType != SbxVARIANT && pDef->GetType() != eType )
1027 {
1028 Error( ERRCODE_BASIC_BAD_DECLARATION, aName );
1029 }
1030 if( pDef->GetType() == SbxVARIANT && !( bFunc || bProp ) )
1031 {
1032 pDef->SetType( SbxEMPTY );
1033 }
1034 return pDef;
1035 }
1036
1037 // DECLARE
1038
1039 void SbiParser::Declare()
1040 {
1041 DefDeclare( false );
1042 }
1043
1044 void SbiParser::DefDeclare( bool bPrivate )
1045 {
1046 Next();
1047 if( eCurTok == PTRSAFE )
1048 Next();
1049
1050 if( eCurTok != SUB && eCurTok != FUNCTION )
1051 {
1052 Error( ERRCODE_BASIC_UNEXPECTED, eCurTok );
1053 }
1054 else
1055 {
1056 bool bFunction = (eCurTok == FUNCTION);
1057
1058 SbiProcDef* pDef = ProcDecl( true );
1059 if( pDef )
1060 {
1061 if( pDef->GetLib().isEmpty() )
1062 {
1063 Error( ERRCODE_BASIC_EXPECTED, LIB );
1064 }
1065 // Is it already there?
1066 SbiSymDef* pOld = aPublics.Find( pDef->GetName() );
1067 if( pOld )
1068 {
1069 SbiProcDef* p = pOld->GetProcDef();
1070 if( !p )
1071 {
1072 // Declared as a variable
1073 Error( ERRCODE_BASIC_BAD_DECLARATION, pDef->GetName() );
1074 delete pDef;
1075 pDef = nullptr;
1076 }
1077 else
1078 {
1079 pDef->Match( p );
1080 }
1081 }
1082 else
1083 {
1084 aPublics.Add( pDef );
1085 }
1086 if ( pDef )
1087 {
1088 pDef->SetPublic( !bPrivate );
1089
1090 // New declare handling
1091 if( !pDef->GetLib().isEmpty())
1092 {
1093 if( bNewGblDefs && nGblChain == 0 )
1094 {
1095 nGblChain = aGen.Gen( SbiOpcode::JUMP_, 0 );
1096 bNewGblDefs = false;
1097 }
1098
1099 sal_uInt16 nSavLine = nLine;
1100 aGen.Statement();
1101 pDef->Define();
1102 pDef->SetLine1( nSavLine );
1103 pDef->SetLine2( nSavLine );
1104
1105 SbiSymPool& rPool = pDef->GetParams();
1106 sal_uInt16 nParCount = rPool.GetSize();
1107
1108 SbxDataType eType = pDef->GetType();
1109 if( bFunction )
1110 {
1111 aGen.Gen( SbiOpcode::PARAM_, 0, sal::static_int_cast< sal_uInt16 >( eType ) );
1112 }
1113 if( nParCount > 1 )
1114 {
1115 aGen.Gen( SbiOpcode::ARGC_ );
1116
1117 for( sal_uInt16 i = 1 ; i < nParCount ; ++i )
1118 {
1119 SbiSymDef* pParDef = rPool.Get( i );
1120 SbxDataType eParType = pParDef->GetType();
1121
1122 aGen.Gen( SbiOpcode::PARAM_, i, sal::static_int_cast< sal_uInt16 >( eParType ) );
1123 aGen.Gen( SbiOpcode::ARGV_ );
1124
1125 sal_uInt16 nTyp = sal::static_int_cast< sal_uInt16 >( pParDef->GetType() );
1126 if( pParDef->IsByVal() )
1127 {
1128 // Reset to avoid additional byval in call to wrapper function
1129 pParDef->SetByVal( false );
1130 nTyp |= 0x8000;
1131 }
1132 aGen.Gen( SbiOpcode::ARGTYP_, nTyp );
1133 }
1134 }
1135
1136 aGen.Gen( SbiOpcode::LIB_, aGblStrings.Add( pDef->GetLib() ) );
1137
1138 SbiOpcode eOp = pDef->IsCdecl() ? SbiOpcode::CALLC_ : SbiOpcode::CALL_;
1139 sal_uInt16 nId = pDef->GetId();
1140 if( !pDef->GetAlias().isEmpty() )
1141 {
1142 nId = ( nId & 0x8000 ) | aGblStrings.Add( pDef->GetAlias() );
1143 }
1144 if( nParCount > 1 )
1145 {
1146 nId |= 0x8000;
1147 }
1148 aGen.Gen( eOp, nId, sal::static_int_cast< sal_uInt16 >( eType ) );
1149
1150 if( bFunction )
1151 {
1152 aGen.Gen( SbiOpcode::PUT_ );
1153 }
1154 aGen.Gen( SbiOpcode::LEAVE_ );
1155 }
1156 }
1157 }
1158 }
1159 }
1160
1161 void SbiParser::Attribute()
1162 {
1163 // TODO: Need to implement the method as an attributed object.
1164 while( Next() != EQ )
1165 {
1166 if( Next() != DOT)
1167 {
1168 break;
1169 }
1170 }
1171
1172 if( eCurTok != EQ )
1173 {
1174 Error( ERRCODE_BASIC_SYNTAX );
1175 }
1176 else
1177 {
1178 SbiExpression aValue( this );
1179 }
1180 // Don't generate any code - just discard it.
1181 }
1182
1183 // Call of a SUB or a FUNCTION
1184
1185 void SbiParser::Call()
1186 {
1187 SbiExpression aVar( this, SbSYMBOL );
1188 aVar.Gen( FORCE_CALL );
1189 aGen.Gen( SbiOpcode::GET_ );
1190 }
1191
1192 // SUB/FUNCTION
1193
1194 void SbiParser::SubFunc()
1195 {
1196 DefProc( false, false );
1197 }
1198
1199 // Read in of a procedure
1200
1201 void SbiParser::DefProc( bool bStatic, bool bPrivate )
1202 {
1203 sal_uInt16 l1 = nLine;
1204 bool bSub = ( eCurTok == SUB );
1205 bool bProperty = ( eCurTok == PROPERTY );
1206 PropertyMode ePropertyMode = PropertyMode::NONE;
1207 if( bProperty )
1208 {
1209 Next();
1210 if( eCurTok == GET )
1211 {
1212 ePropertyMode = PropertyMode::Get;
1213 }
1214 else if( eCurTok == LET )
1215 {
1216 ePropertyMode = PropertyMode::Let;
1217 }
1218 else if( eCurTok == SET )
1219 {
1220 ePropertyMode = PropertyMode::Set;
1221 }
1222 else
1223 {
1224 Error( ERRCODE_BASIC_EXPECTED, u"Get or Let or Set"_ustr );
1225 }
1226 }
1227
1228 SbiToken eExit = eCurTok;
1229 SbiProcDef* pDef = ProcDecl( false );
1230 if( !pDef )
1231 {
1232 return;
1233 }
1234 pDef->setPropertyMode( ePropertyMode );
1235
1236 // Is the Proc already declared?
1237 SbiSymDef* pOld = aPublics.Find( pDef->GetName() );
1238 if( pOld )
1239 {
1240 pProc = pOld->GetProcDef();
1241 if( !pProc )
1242 {
1243 // Declared as a variable
1244 Error( ERRCODE_BASIC_BAD_DECLARATION, pDef->GetName() );
1245 delete pDef;
1246 return;
1247 }
1248 // #100027: Multiple declaration -> Error
1249 // #112787: Not for setup, REMOVE for 8
1250 else if( pProc->IsUsedForProcDecl() )
1251 {
1252 PropertyMode ePropMode = pDef->getPropertyMode();
1253 if( ePropMode == PropertyMode::NONE || ePropMode == pProc->getPropertyMode() )
1254 {
1255 Error( ERRCODE_BASIC_PROC_DEFINED, pDef->GetName() );
1256 delete pDef;
1257 return;
1258 }
1259 }
1260
1261 pDef->Match( pProc );
1262 }
1263 else
1264 {
1265 aPublics.Add( pDef );
1266 }
1267 assert(pDef);
1268 pProc = pDef;
1269 pProc->SetPublic( !bPrivate );
1270
1271 // Now we set the search hierarchy for symbols as well as the
1272 // current procedure.
1273 aPublics.SetProcId( pProc->GetId() );
1274 pProc->GetParams().SetParent( &aPublics );
1275 if( bStatic )
1276 {
1277 if ( bVBASupportOn )
1278 {
1279 pProc->SetStatic();
1280 }
1281 else
1282 {
1283 Error( ERRCODE_BASIC_NOT_IMPLEMENTED ); // STATIC SUB ...
1284 }
1285 }
1286 else
1287 {
1288 pProc->SetStatic( false );
1289 }
1290 // Normal case: Local variable->parameter->global variable
1291 pProc->GetLocals().SetParent( &pProc->GetParams() );
1292 pPool = &pProc->GetLocals();
1293
1294 pProc->Define();
1295 OpenBlock( eExit );
1296 StmntBlock( bSub ? ENDSUB : (bProperty ? ENDPROPERTY : ENDFUNC) );
1297 sal_uInt16 l2 = nLine;
1298 pProc->SetLine1( l1 );
1299 pProc->SetLine2( l2 );
1300 pPool = &aPublics;
1301 aPublics.SetProcId( 0 );
1302 // Open labels?
1303 pProc->GetLabels().CheckRefs();
1304 CloseBlock();
1305 aGen.Gen( SbiOpcode::LEAVE_ );
1306 pProc = nullptr;
1307 }
1308
1309 // STATIC variable|procedure
1310
1311 void SbiParser::Static()
1312 {
1313 DefStatic( false );
1314 }
1315
1316 void SbiParser::DefStatic( bool bPrivate )
1317 {
1318 SbiSymPool* p;
1319
1320 switch( Peek() )
1321 {
1322 case SUB:
1323 case FUNCTION:
1324 case PROPERTY:
1325 // End global chain if necessary (not done in
1326 // SbiParser::Parse() under these conditions
1327 if( bNewGblDefs && nGblChain == 0 )
1328 {
1329 nGblChain = aGen.Gen( SbiOpcode::JUMP_, 0 );
1330 bNewGblDefs = false;
1331 }
1332 Next();
1333 DefProc( true, bPrivate );
1334 break;
1335 default:
1336 if( !pProc )
1337 {
1338 Error( ERRCODE_BASIC_NOT_IN_SUBR );
1339 }
1340 // Reset the Pool, so that STATIC-Declarations go into the
1341 // global Pool
1342 p = pPool;
1343 pPool = &aPublics;
1344 DefVar( SbiOpcode::STATIC_, true );
1345 pPool = p;
1346 break;
1347 }
1348 }
1349
1350 bool SbiParser::IsUnoInterface(const OUString& sTypeName)
1351 {
1352 try
1353 {
1354 return css::reflection::theCoreReflection::get(
1355 comphelper::getProcessComponentContext())->forName(sTypeName).is();
1356 }
1357 catch(const Exception&)
1358 {
1359 OSL_FAIL("Could not create reflection.CoreReflection.");
1360 }
1361 return false;
1362 }
1363
1364 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */