xref: /trunk/main/basic/source/comp/dim.cxx (revision 7a7771fb4d20b857459c676939e2cc2f4dec9635)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 // MARKER(update_precomp.py): autogen include statement, do not remove
23 #include "precompiled_basic.hxx"
24 #include <basic/sbx.hxx>
25 #include "sbcomp.hxx"
26 
27 SbxObject* cloneTypeObjectImpl( const SbxObject& rTypeObj );
28 
29 // Deklaration einer Variablen
30 // Bei Fehlern wird bis zum Komma oder Newline geparst.
31 // Returnwert: eine neue Instanz, die eingefügt und dann gelöscht wird.
32 // Array-Indexe werden als SbiDimList zurückgegeben
33 
VarDecl(SbiDimList ** ppDim,sal_Bool bStatic,sal_Bool bConst)34 SbiSymDef* SbiParser::VarDecl( SbiDimList** ppDim, sal_Bool bStatic, sal_Bool bConst )
35 {
36     bool bWithEvents = false;
37     if( Peek() == WITHEVENTS )
38     {
39         Next();
40         bWithEvents = true;
41     }
42     if( !TestSymbol() ) return NULL;
43     SbxDataType t = eScanType;
44     SbiSymDef* pDef = bConst ? new SbiConstDef( aSym ) : new SbiSymDef( aSym );
45     SbiDimList* pDim = NULL;
46     // Klammern?
47     if( Peek() == LPAREN )
48     {
49         pDim = new SbiDimList( this );
50         if( !pDim->GetDims() )
51             pDef->SetWithBrackets();
52     }
53     pDef->SetType( t );
54     if( bStatic )
55         pDef->SetStatic();
56     if( bWithEvents )
57         pDef->SetWithEvents();
58     TypeDecl( *pDef );
59     if( !ppDim && pDim )
60     {
61         if(pDim->GetDims() )
62             Error( SbERR_EXPECTED, "()" );
63         delete pDim;
64     }
65     else if( ppDim )
66         *ppDim = pDim;
67     return pDef;
68 }
69 
70 // Aufloesen einer AS-Typdeklaration
71 // Der Datentyp wird in die uebergebene Variable eingetragen
72 
TypeDecl(SbiSymDef & rDef,sal_Bool bAsNewAlreadyParsed)73 void SbiParser::TypeDecl( SbiSymDef& rDef, sal_Bool bAsNewAlreadyParsed )
74 {
75     SbxDataType eType = rDef.GetType();
76     short nSize = 0;
77     if( bAsNewAlreadyParsed || Peek() == AS )
78     {
79         if( !bAsNewAlreadyParsed )
80             Next();
81         rDef.SetDefinedAs();
82         String aType;
83         SbiToken eTok = Next();
84         if( !bAsNewAlreadyParsed && eTok == NEW )
85         {
86             rDef.SetNew();
87             eTok = Next();
88         }
89         switch( eTok )
90         {
91             case ANY:
92                 if( rDef.IsNew() )
93                     Error( SbERR_SYNTAX );
94                 eType = SbxVARIANT; break;
95             case TINTEGER:
96             case TLONG:
97             case TSINGLE:
98             case TDOUBLE:
99             case TCURRENCY:
100             case TDATE:
101             case TSTRING:
102             case TOBJECT:
103             case _ERROR_:
104             case TBOOLEAN:
105             case TVARIANT:
106             case TBYTE:
107                 if( rDef.IsNew() )
108                     Error( SbERR_SYNTAX );
109                 eType = (eTok==TBYTE) ? SbxBYTE : SbxDataType( eTok - TINTEGER + SbxINTEGER );
110                 if( eType == SbxSTRING )
111                 {
112                     // STRING*n ?
113                     if( Peek() == MUL )
114                     {       // fixed size!
115                         Next();
116                         SbiConstExpression aSize( this );
117                         nSize = aSize.GetShortValue();
118                         if( nSize < 0 || (bVBASupportOn && nSize <= 0) )
119                             Error( SbERR_OUT_OF_RANGE );
120                         else
121                             rDef.SetFixedStringLength( nSize );
122                     }
123                 }
124                 break;
125             case SYMBOL: // kann nur ein TYPE oder eine Objektklasse sein!
126                 if( eScanType != SbxVARIANT )
127                     Error( SbERR_SYNTAX );
128                 else
129                 {
130                     String aCompleteName = aSym;
131 
132                     // #52709 DIM AS NEW fuer Uno mit voll-qualifizierten Namen
133                     if( Peek() == DOT )
134                     {
135                         String aDotStr( '.' );
136                         while( Peek() == DOT )
137                         {
138                             aCompleteName += aDotStr;
139                             Next();
140                             SbiToken ePeekTok = Peek();
141                             if( ePeekTok == SYMBOL || IsKwd( ePeekTok ) )
142                             {
143                                 Next();
144                                 aCompleteName += aSym;
145                             }
146                             else
147                             {
148                                 Next();
149                                 Error( SbERR_UNEXPECTED, SYMBOL );
150                                 break;
151                             }
152                         }
153                     }
154                     else if( rEnumArray->Find( aCompleteName, SbxCLASS_OBJECT ) )
155                     {
156                         eType = SbxLONG;
157                         break;
158                     }
159 
160                     // In den String-Pool uebernehmen
161                     rDef.SetTypeId( aGblStrings.Add( aCompleteName ) );
162 
163                     if( rDef.IsNew() && pProc == NULL )
164                         aRequiredTypes.push_back( aCompleteName );
165                 }
166                 eType = SbxOBJECT;
167                 break;
168             case FIXSTRING: // new syntax for complex UNO types
169                 rDef.SetTypeId( aGblStrings.Add( aSym ) );
170                 eType = SbxOBJECT;
171                 break;
172             default:
173                 Error( SbERR_UNEXPECTED, eTok );
174                 Next();
175         }
176         // Die Variable koennte mit Suffix deklariert sein
177         if( rDef.GetType() != SbxVARIANT )
178         {
179             if( rDef.GetType() != eType )
180                 Error( SbERR_VAR_DEFINED, rDef.GetName() );
181             else if( eType == SbxSTRING && rDef.GetLen() != nSize )
182                 Error( SbERR_VAR_DEFINED, rDef.GetName() );
183         }
184         rDef.SetType( eType );
185         rDef.SetLen( nSize );
186     }
187 }
188 
189 // Hier werden Variable, Arrays und Strukturen definiert.
190 // DIM/PRIVATE/PUBLIC/GLOBAL
191 
Dim()192 void SbiParser::Dim()
193 {
194     DefVar( _DIM, ( pProc && bVBASupportOn ) ? pProc->IsStatic() : sal_False );
195 }
196 
DefVar(SbiOpcode eOp,sal_Bool bStatic)197 void SbiParser::DefVar( SbiOpcode eOp, sal_Bool bStatic )
198 {
199     SbiSymPool* pOldPool = pPool;
200     sal_Bool bSwitchPool = sal_False;
201     sal_Bool bPersistantGlobal = sal_False;
202     SbiToken eFirstTok = eCurTok;
203     if( pProc && ( eCurTok == GLOBAL || eCurTok == PUBLIC || eCurTok == PRIVATE ) )
204         Error( SbERR_NOT_IN_SUBR, eCurTok );
205     if( eCurTok == PUBLIC || eCurTok == GLOBAL )
206     {
207         bSwitchPool = sal_True;     // im richtigen Moment auf globalen Pool schalten
208         if( eCurTok == GLOBAL )
209             bPersistantGlobal = sal_True;
210     }
211     // behavior in VBA is that a module scope variable's lifetime is
212     // tied to the document. e.g. a module scope variable is global
213     if( GetBasic()->IsDocBasic() && bVBASupportOn && !pProc )
214         bPersistantGlobal = sal_True;
215     // PRIVATE ist Synonym fuer DIM
216     // _CONST_?
217     sal_Bool bConst = sal_False;
218     if( eCurTok == _CONST_ )
219         bConst = sal_True;
220     else if( Peek() == _CONST_ )
221         Next(), bConst = sal_True;
222 
223     // #110004 It can also be a sub/function
224     if( !bConst && (eCurTok == SUB || eCurTok == FUNCTION || eCurTok == PROPERTY ||
225                     eCurTok == STATIC || eCurTok == ENUM || eCurTok == DECLARE || eCurTok == TYPE) )
226     {
227         // Next token is read here, because !bConst
228         bool bPrivate = ( eFirstTok == PRIVATE );
229 
230         if( eCurTok == STATIC )
231         {
232             Next();
233             DefStatic( bPrivate );
234         }
235         else if( eCurTok == SUB || eCurTok == FUNCTION || eCurTok == PROPERTY )
236         {
237             // End global chain if necessary (not done in
238             // SbiParser::Parse() under these conditions
239             if( bNewGblDefs && nGblChain == 0 )
240             {
241                 nGblChain = aGen.Gen( _JUMP, 0 );
242                 bNewGblDefs = sal_False;
243             }
244             Next();
245             DefProc( sal_False, bPrivate );
246             return;
247         }
248         else if( eCurTok == ENUM )
249         {
250             Next();
251             DefEnum( bPrivate );
252             return;
253         }
254         else if( eCurTok == DECLARE )
255         {
256             Next();
257             DefDeclare( bPrivate );
258             return;
259         }
260         // #i109049
261         else if( eCurTok == TYPE )
262         {
263             Next();
264             DefType( bPrivate );
265             return;
266         }
267     }
268 
269 #ifdef SHARED
270 #define tmpSHARED
271 #undef SHARED
272 #endif
273     // SHARED wird ignoriert
274     if( Peek() == SHARED ) Next();
275 #ifdef tmpSHARED
276 #define SHARED
277 #undef tmpSHARED
278 #endif
279     // PRESERVE nur bei REDIM
280     if( Peek() == PRESERVE )
281     {
282         Next();
283         if( eOp == _REDIM )
284             eOp = _REDIMP;
285         else
286             Error( SbERR_UNEXPECTED, eCurTok );
287     }
288     SbiSymDef* pDef;
289     SbiDimList* pDim;
290 
291     // AB 9.7.97, #40689, Statics -> Modul-Initialisierung, in Sub ueberspringen
292     sal_uInt32 nEndOfStaticLbl = 0;
293     if( !bVBASupportOn && bStatic )
294     {
295         nEndOfStaticLbl = aGen.Gen( _JUMP, 0 );
296         aGen.Statement();   // bei static hier nachholen
297     }
298 
299     sal_Bool bDefined = sal_False;
300     while( ( pDef = VarDecl( &pDim, bStatic, bConst ) ) != NULL )
301     {
302         EnableErrors();
303         // Variable suchen:
304         if( bSwitchPool )
305             pPool = &aGlobals;
306         SbiSymDef* pOld = pPool->Find( pDef->GetName() );
307         // AB 31.3.1996, #25651#, auch in Runtime-Library suchen
308         sal_Bool bRtlSym = sal_False;
309         if( !pOld )
310         {
311             pOld = CheckRTLForSym( pDef->GetName(), SbxVARIANT );
312             if( pOld )
313                 bRtlSym = sal_True;
314         }
315         if( pOld && !(eOp == _REDIM || eOp == _REDIMP) )
316         {
317             if( pDef->GetScope() == SbLOCAL && pOld->GetScope() != SbLOCAL )
318                 pOld = NULL;
319         }
320         if( pOld )
321         {
322             bDefined = sal_True;
323             // Bei RTL-Symbol immer Fehler
324             if( !bRtlSym && (eOp == _REDIM || eOp == _REDIMP) )
325             {
326                 // Bei REDIM die Attribute vergleichen
327                 SbxDataType eDefType;
328                 bool bError_ = false;
329                 if( pOld->IsStatic() )
330                 {
331                     bError_ = true;
332                 }
333                 else if( pOld->GetType() != ( eDefType = pDef->GetType() ) )
334                 {
335                     if( !( eDefType == SbxVARIANT && !pDef->IsDefinedAs() ) )
336                         bError_ = true;
337                 }
338                 if( bError_ )
339                     Error( SbERR_VAR_DEFINED, pDef->GetName() );
340             }
341             else
342                 Error( SbERR_VAR_DEFINED, pDef->GetName() );
343             delete pDef; pDef = pOld;
344         }
345         else
346             pPool->Add( pDef );
347 
348         // #36374: Variable vor Unterscheidung IsNew() anlegen
349         // Sonst Error bei Dim Identifier As New Type und option explicit
350         if( !bDefined && !(eOp == _REDIM || eOp == _REDIMP)
351                       && ( !bConst || pDef->GetScope() == SbGLOBAL ) )
352         {
353             // Variable oder globale Konstante deklarieren
354             SbiOpcode eOp2;
355             switch ( pDef->GetScope() )
356             {
357                 case SbGLOBAL:  eOp2 = bPersistantGlobal ? _GLOBAL_P : _GLOBAL;
358                                 goto global;
359                 case SbPUBLIC:  eOp2 = bPersistantGlobal ? _PUBLIC_P : _PUBLIC;
360                                 // AB 9.7.97, #40689, kein eigener Opcode mehr
361                                 if( bVBASupportOn && bStatic )
362                                 {
363                                     eOp2 = _STATIC;
364                                     break;
365                                 }
366                 global:         aGen.BackChain( nGblChain );
367                                 nGblChain = 0;
368                                 bGblDefs = bNewGblDefs = sal_True;
369                                 break;
370                 default:        eOp2 = _LOCAL;
371             }
372             sal_uInt32 nOpnd2 = sal::static_int_cast< sal_uInt16 >( pDef->GetType() );
373             if( pDef->IsWithEvents() )
374                 nOpnd2 |= SBX_TYPE_WITH_EVENTS_FLAG;
375 
376             if( bCompatible && pDef->IsNew() )
377                 nOpnd2 |= SBX_TYPE_DIM_AS_NEW_FLAG;
378 
379             short nFixedStringLength = pDef->GetFixedStringLength();
380             if( nFixedStringLength >= 0 )
381                 nOpnd2 |= (SBX_FIXED_LEN_STRING_FLAG + (sal_uInt32(nFixedStringLength) << 17));     // len = all bits above 0x10000
382 
383             if( pDim != NULL && pDim->GetDims() > 0 )
384                 nOpnd2 |= SBX_TYPE_VAR_TO_DIM_FLAG;
385 
386             aGen.Gen( eOp2, pDef->GetId(), nOpnd2 );
387         }
388 
389         // Initialisierung fuer selbstdefinierte Datentypen
390         // und per NEW angelegte Variable
391         if( pDef->GetType() == SbxOBJECT
392          && pDef->GetTypeId() )
393         {
394             if( !bCompatible && !pDef->IsNew() )
395             {
396                 String aTypeName( aGblStrings.Find( pDef->GetTypeId() ) );
397                 if( rTypeArray->Find( aTypeName, SbxCLASS_OBJECT ) == NULL )
398                     Error( SbERR_UNDEF_TYPE, aTypeName );
399             }
400 
401             if( bConst )
402             {
403                 Error( SbERR_SYNTAX );
404             }
405 
406             if( pDim )
407             {
408                 if( eOp == _REDIMP )
409                 {
410                     SbiExpression aExpr( this, *pDef, NULL );
411                     aExpr.Gen();
412                     aGen.Gen( _REDIMP_ERASE );
413 
414                     pDef->SetDims( pDim->GetDims() );
415                     SbiExpression aExpr2( this, *pDef, pDim );
416                     aExpr2.Gen();
417                     aGen.Gen( _DCREATE_REDIMP, pDef->GetId(), pDef->GetTypeId() );
418                 }
419                 else
420                 {
421                     pDef->SetDims( pDim->GetDims() );
422                     SbiExpression aExpr( this, *pDef, pDim );
423                     aExpr.Gen();
424                     aGen.Gen( _DCREATE, pDef->GetId(), pDef->GetTypeId() );
425                 }
426             }
427             else
428             {
429                 SbiExpression aExpr( this, *pDef );
430                 aExpr.Gen();
431                 SbiOpcode eOp_ = pDef->IsNew() ? _CREATE : _TCREATE;
432                 aGen.Gen( eOp_, pDef->GetId(), pDef->GetTypeId() );
433                 aGen.Gen( _SET );
434             }
435         }
436         else
437         {
438             if( bConst )
439             {
440                 // Konstanten-Definition
441                 if( pDim )
442                 {
443                     Error( SbERR_SYNTAX );
444                     delete pDim;
445                 }
446                 SbiExpression aVar( this, *pDef );
447                 if( !TestToken( EQ ) )
448                     goto MyBreak;   // AB 24.6.1996 (s.u.)
449                 SbiConstExpression aExpr( this );
450                 if( !bDefined && aExpr.IsValid() )
451                 {
452                     if( pDef->GetScope() == SbGLOBAL )
453                     {
454                         // Nur Code fuer globale Konstante erzeugen!
455                         aVar.Gen();
456                         aExpr.Gen();
457                         aGen.Gen( _PUTC );
458                     }
459                     SbiConstDef* pConst = pDef->GetConstDef();
460                     if( aExpr.GetType() == SbxSTRING )
461                         pConst->Set( aExpr.GetString() );
462                     else
463                         pConst->Set( aExpr.GetValue(), aExpr.GetType() );
464                 }
465             }
466             else if( pDim )
467             {
468                 // Die Variable dimensionieren
469                 // Bei REDIM die Var vorher loeschen
470                 if( eOp == _REDIM )
471                 {
472                     SbiExpression aExpr( this, *pDef, NULL );
473                     aExpr.Gen();
474                     if ( bVBASupportOn )
475                         // delete the array but
476                         // clear the variable ( this
477                         // allows the processing of
478                         // the param to happen as normal without errors ( ordinary ERASE just clears the array )
479                         aGen.Gen( _ERASE_CLEAR );
480                     else
481                         aGen.Gen( _ERASE );
482                 }
483                 else if( eOp == _REDIMP )
484                 {
485                     SbiExpression aExpr( this, *pDef, NULL );
486                     aExpr.Gen();
487                     aGen.Gen( _REDIMP_ERASE );
488                 }
489                 pDef->SetDims( pDim->GetDims() );
490                 if( bPersistantGlobal )
491                     pDef->SetGlobal( sal_True );
492                 SbiExpression aExpr( this, *pDef, pDim );
493                 aExpr.Gen();
494                 pDef->SetGlobal( sal_False );
495                 aGen.Gen( (eOp == _STATIC) ? _DIM : eOp );
496             }
497         }
498         if( !TestComma() )
499             goto MyBreak;   // AB 24.6.1996 (s.u.)
500 
501         // #27963# AB, 24.6.1996
502         // Einfuehrung bSwitchPool (s.o.): pPool darf beim VarDecl-Aufruf
503         // noch nicht auf &aGlobals gesetzt sein.
504         // Ansonsten soll das Verhalten aber absolut identisch bleiben,
505         // d.h. pPool muss immer am Schleifen-Ende zurueckgesetzt werden.
506         // auch bei break
507         pPool = pOldPool;
508         continue;       // MyBreak überspingen
509     MyBreak:
510         pPool = pOldPool;
511         break;
512     }
513 
514     // AB 9.7.97, #40689, Sprung ueber Statics-Deklaration abschliessen
515     if( !bVBASupportOn && bStatic )
516     {
517         // globalen Chain pflegen
518         nGblChain = aGen.Gen( _JUMP, 0 );
519         bGblDefs = bNewGblDefs = sal_True;
520 
521         // fuer Sub Sprung auf Ende der statics eintragen
522         aGen.BackChain( nEndOfStaticLbl );
523     }
524 
525     //pPool = pOldPool;
526 }
527 
528 // Hier werden Arrays redimensioniert.
529 
ReDim()530 void SbiParser::ReDim()
531 {
532     DefVar( _REDIM, ( pProc && bVBASupportOn ) ? pProc->IsStatic() : sal_False );
533 }
534 
535 // ERASE array, ...
536 
Erase()537 void SbiParser::Erase()
538 {
539     while( !bAbort )
540     {
541         SbiExpression aExpr( this, SbLVALUE );
542         aExpr.Gen();
543         aGen.Gen( _ERASE );
544         if( !TestComma() ) break;
545     }
546 }
547 
548 // Deklaration eines Datentyps
549 
Type()550 void SbiParser::Type()
551 {
552     DefType( sal_False );
553 }
554 
DefType(sal_Bool bPrivate)555 void SbiParser::DefType( sal_Bool bPrivate )
556 {
557     // TODO: Use bPrivate
558     (void)bPrivate;
559 
560     // Neues Token lesen, es muss ein Symbol sein
561     if (!TestSymbol())
562         return;
563 
564     if (rTypeArray->Find(aSym,SbxCLASS_OBJECT))
565     {
566         Error( SbERR_VAR_DEFINED, aSym );
567         return;
568     }
569 
570     SbxObject *pType = new SbxObject(aSym);
571 
572     SbiSymDef* pElem;
573     SbiDimList* pDim = NULL;
574     sal_Bool bDone = sal_False;
575 
576     while( !bDone && !IsEof() )
577     {
578         switch( Peek() )
579         {
580             case ENDTYPE :
581                 pElem = NULL;
582                 bDone = sal_True;
583                 Next();
584             break;
585 
586             case EOLN :
587             case REM :
588                 pElem = NULL;
589                 Next();
590             break;
591 
592             default:
593                 pDim = NULL;
594                 pElem = VarDecl(&pDim,sal_False,sal_False);
595                 if( !pElem )
596                     bDone = sal_True;   // Error occurred
597         }
598         if( pElem )
599         {
600             SbxArray *pTypeMembers = pType->GetProperties();
601             String aElemName = pElem->GetName();
602             if( pTypeMembers->Find( aElemName, SbxCLASS_DONTCARE) )
603                 Error (SbERR_VAR_DEFINED);
604             else
605             {
606                 SbxDataType eElemType = pElem->GetType();
607                 SbxProperty *pTypeElem = new SbxProperty( aElemName, eElemType );
608                 if( pDim )
609                 {
610                     SbxDimArray* pArray = new SbxDimArray( pElem->GetType() );
611                     if ( pDim->GetSize() )
612                     {
613                         // Dimension the target array
614 
615                     for ( short i=0; i<pDim->GetSize();++i )
616                         {
617                             sal_Int32 ub = -1;
618                             sal_Int32 lb = nBase;
619                             SbiExprNode* pNode = pDim->Get(i)->GetExprNode();
620                             ub = pNode->GetNumber();
621                             if ( !pDim->Get( i )->IsBased() ) // each dim is low/up
622                             {
623                                 if ( ++i >= pDim->GetSize() ) // trouble
624                                     StarBASIC::FatalError( SbERR_INTERNAL_ERROR );
625                                 pNode = pDim->Get(i)->GetExprNode();
626                                 lb = ub;
627                                 ub = pNode->GetNumber();
628                             }
629                         else if ( !bCompatible )
630                             ub += nBase;
631                         // VBA: lb = nBase (respects Option Base), ub = declared value
632                             pArray->AddDim32( lb, ub );
633                         }
634                         pArray->setHasFixedSize( true );
635                     }
636                     else
637                         pArray->unoAddDim( 0, -1 ); // variant array
638                     sal_uInt16 nSavFlags = pTypeElem->GetFlags();
639                     // need to reset the FIXED flag
640                     // when calling PutObject ( because the type will not match Object )
641                     pTypeElem->ResetFlag( SBX_FIXED );
642                     pTypeElem->PutObject( pArray );
643                     pTypeElem->SetFlags( nSavFlags );
644                 }
645                 // Nested user type?
646                 if( eElemType == SbxOBJECT )
647                 {
648                     sal_uInt16 nElemTypeId = pElem->GetTypeId();
649                     if( nElemTypeId != 0 )
650                     {
651                         String aTypeName( aGblStrings.Find( nElemTypeId ) );
652                         SbxObject* pTypeObj = static_cast< SbxObject* >( rTypeArray->Find( aTypeName, SbxCLASS_OBJECT ) );
653                         if( pTypeObj != NULL )
654                         {
655                             SbxObject* pCloneObj = cloneTypeObjectImpl( *pTypeObj );
656                             pTypeElem->PutObject( pCloneObj );
657                         }
658                     }
659                 }
660                 delete pDim;
661                 pTypeMembers->Insert( pTypeElem, pTypeMembers->Count() );
662             }
663             delete pElem;
664         }
665     }
666 
667     pType->Remove( XubString( RTL_CONSTASCII_USTRINGPARAM("Name") ), SbxCLASS_DONTCARE );
668     pType->Remove( XubString( RTL_CONSTASCII_USTRINGPARAM("Parent") ), SbxCLASS_DONTCARE );
669 
670     rTypeArray->Insert (pType,rTypeArray->Count());
671 }
672 
673 
674 // Declaration of Enum type
675 
Enum()676 void SbiParser::Enum()
677 {
678     DefEnum( sal_False );
679 }
680 
DefEnum(sal_Bool bPrivate)681 void SbiParser::DefEnum( sal_Bool bPrivate )
682 {
683     // Neues Token lesen, es muss ein Symbol sein
684     if (!TestSymbol())
685         return;
686 
687     String aEnumName = aSym;
688     if( rEnumArray->Find(aEnumName,SbxCLASS_OBJECT) )
689     {
690         Error( SbERR_VAR_DEFINED, aSym );
691         return;
692     }
693 
694     SbxObject *pEnum = new SbxObject( aEnumName );
695     if( bPrivate )
696         pEnum->SetFlag( SBX_PRIVATE );
697 
698     SbiSymDef* pElem;
699     SbiDimList* pDim;
700     sal_Bool bDone = sal_False;
701 
702     // Starting with -1 to make first default value 0 after ++
703     sal_Int32 nCurrentEnumValue = -1;
704     while( !bDone && !IsEof() )
705     {
706         switch( Peek() )
707         {
708             case ENDENUM :
709                 pElem = NULL;
710                 bDone = sal_True;
711                 Next();
712             break;
713 
714             case EOLN :
715             case REM :
716                 pElem = NULL;
717                 Next();
718             break;
719 
720             default:
721             {
722                 // TODO: Check existing!
723                 sal_Bool bDefined = sal_False;
724 
725                 pDim = NULL;
726                 pElem = VarDecl( &pDim, sal_False, sal_True );
727                 if( !pElem )
728                 {
729                     bDone = sal_True;   // Error occurred
730                     break;
731                 }
732                 else if( pDim )
733                 {
734                     delete pDim;
735                     Error( SbERR_SYNTAX );
736                     bDone = sal_True;   // Error occurred
737                     break;
738                 }
739 
740                 SbiExpression aVar( this, *pElem );
741                 if( Peek() == EQ )
742                 {
743                     Next();
744 
745                     SbiConstExpression aExpr( this );
746                     if( !bDefined && aExpr.IsValid() )
747                     {
748                         SbxVariableRef xConvertVar = new SbxVariable();
749                         if( aExpr.GetType() == SbxSTRING )
750                             xConvertVar->PutString( aExpr.GetString() );
751                         else
752                             xConvertVar->PutDouble( aExpr.GetValue() );
753 
754                         nCurrentEnumValue = xConvertVar->GetLong();
755                     }
756                 }
757                 else
758                     nCurrentEnumValue++;
759 
760                 SbiSymPool* pPoolToUse = bPrivate ? pPool : &aGlobals;
761 
762                 SbiSymDef* pOld = pPoolToUse->Find( pElem->GetName() );
763                 if( pOld )
764                 {
765                     Error( SbERR_VAR_DEFINED, pElem->GetName() );
766                     bDone = sal_True;   // Error occurred
767                     break;
768                 }
769 
770                 pPool->Add( pElem );
771 
772                 if( !bPrivate )
773                 {
774                     SbiOpcode eOp = _GLOBAL;
775                     aGen.BackChain( nGblChain );
776                     nGblChain = 0;
777                     bGblDefs = bNewGblDefs = sal_True;
778                     aGen.Gen(
779                         eOp, pElem->GetId(),
780                         sal::static_int_cast< sal_uInt16 >( pElem->GetType() ) );
781 
782                     aVar.Gen();
783                     sal_uInt16 nStringId = aGen.GetParser()->aGblStrings.Add( nCurrentEnumValue, SbxLONG );
784                     aGen.Gen( _NUMBER, nStringId );
785                     aGen.Gen( _PUTC );
786                 }
787 
788                 SbiConstDef* pConst = pElem->GetConstDef();
789                 pConst->Set( nCurrentEnumValue, SbxLONG );
790             }
791         }
792         if( pElem )
793         {
794             SbxArray *pEnumMembers = pEnum->GetProperties();
795             SbxProperty *pEnumElem = new SbxProperty( pElem->GetName(), SbxLONG );
796             pEnumElem->PutLong( nCurrentEnumValue );
797             pEnumElem->ResetFlag( SBX_WRITE );
798             pEnumElem->SetFlag( SBX_CONST );
799             pEnumMembers->Insert( pEnumElem, pEnumMembers->Count() );
800         }
801     }
802 
803     pEnum->Remove( XubString( RTL_CONSTASCII_USTRINGPARAM("Name") ), SbxCLASS_DONTCARE );
804     pEnum->Remove( XubString( RTL_CONSTASCII_USTRINGPARAM("Parent") ), SbxCLASS_DONTCARE );
805 
806     rEnumArray->Insert( pEnum, rEnumArray->Count() );
807 }
808 
809 
810 // Prozedur-Deklaration
811 // das erste Token ist bereits eingelesen (SUB/FUNCTION)
812 // xxx Name [LIB "name"[ALIAS "name"]][(Parameter)][AS TYPE]
813 
ProcDecl(sal_Bool bDecl)814 SbiProcDef* SbiParser::ProcDecl( sal_Bool bDecl )
815 {
816     sal_Bool bFunc = sal_Bool( eCurTok == FUNCTION );
817     sal_Bool bProp = sal_Bool( eCurTok == GET || eCurTok == SET || eCurTok == LET );
818     if( !TestSymbol() ) return NULL;
819     String aName( aSym );
820     SbxDataType eType = eScanType;
821     SbiProcDef* pDef = new SbiProcDef( this, aName, true );
822     pDef->SetType( eType );
823     if( Peek() == _CDECL_ )
824     {
825         Next(); pDef->SetCdecl();
826     }
827     if( Peek() == LIB )
828     {
829         Next();
830         if( Next() == FIXSTRING )
831             pDef->GetLib() = aSym;
832         else
833             Error( SbERR_SYNTAX );
834     }
835     if( Peek() == ALIAS )
836     {
837         Next();
838         if( Next() == FIXSTRING )
839             pDef->GetAlias() = aSym;
840         else
841             Error( SbERR_SYNTAX );
842     }
843     if( !bDecl )
844     {
845         // CDECL, LIB und ALIAS sind unzulaessig
846         if( pDef->GetLib().Len() )
847             Error( SbERR_UNEXPECTED, LIB );
848         if( pDef->GetAlias().Len() )
849             Error( SbERR_UNEXPECTED, ALIAS );
850         if( pDef->IsCdecl() )
851             Error( SbERR_UNEXPECTED, _CDECL_ );
852         pDef->SetCdecl( sal_False );
853         pDef->GetLib().Erase();
854         pDef->GetAlias().Erase();
855     }
856     else if( !pDef->GetLib().Len() )
857     {
858         // ALIAS und CDECL nur zusammen mit LIB
859         if( pDef->GetAlias().Len() )
860             Error( SbERR_UNEXPECTED, ALIAS );
861         if( pDef->IsCdecl() )
862             Error( SbERR_UNEXPECTED, _CDECL_ );
863         pDef->SetCdecl( sal_False );
864         pDef->GetAlias().Erase();
865     }
866     // Klammern?
867     if( Peek() == LPAREN )
868     {
869         Next();
870         if( Peek() == RPAREN )
871             Next();
872         else
873           for(;;) {
874             sal_Bool bByVal = sal_False;
875             sal_Bool bOptional = sal_False;
876             sal_Bool bParamArray = sal_False;
877             while( Peek() == BYVAL || Peek() == BYREF || Peek() == _OPTIONAL_ )
878             {
879                 if      ( Peek() == BYVAL )     Next(), bByVal = sal_True;
880                 else if ( Peek() == BYREF )     Next(), bByVal = sal_False;
881                 else if ( Peek() == _OPTIONAL_ )    Next(), bOptional = sal_True;
882             }
883             if( bCompatible && Peek() == PARAMARRAY )
884             {
885                 if( bByVal || bOptional )
886                     Error( SbERR_UNEXPECTED, PARAMARRAY );
887                 Next();
888                 bParamArray = sal_True;
889             }
890             SbiSymDef* pPar = VarDecl( NULL, sal_False, sal_False );
891             if( !pPar )
892                 break;
893             if( bByVal )
894                 pPar->SetByVal();
895             if( bOptional )
896                 pPar->SetOptional();
897             if( bParamArray )
898                 pPar->SetParamArray();
899             pDef->GetParams().Add( pPar );
900             SbiToken eTok = Next();
901             if( eTok != COMMA && eTok != RPAREN )
902             {
903                 sal_Bool bError2 = sal_True;
904                 if( bOptional && bCompatible && eTok == EQ )
905                 {
906                     SbiConstExpression* pDefaultExpr = new SbiConstExpression( this );
907                     SbxDataType eType2 = pDefaultExpr->GetType();
908 
909                     sal_uInt16 nStringId;
910                     if( eType2 == SbxSTRING )
911                         nStringId = aGblStrings.Add( pDefaultExpr->GetString() );
912                     else
913                         nStringId = aGblStrings.Add( pDefaultExpr->GetValue(), eType2 );
914 
915                     pPar->SetDefaultId( nStringId );
916                     delete pDefaultExpr;
917 
918                     eTok = Next();
919                     if( eTok == COMMA || eTok == RPAREN )
920                         bError2 = sal_False;
921                 }
922                 if( bError2 )
923                 {
924                     Error( SbERR_EXPECTED, RPAREN );
925                     break;
926                 }
927             }
928             if( eTok == RPAREN )
929                 break;
930         }
931     }
932     TypeDecl( *pDef );
933     if( eType != SbxVARIANT && pDef->GetType() != eType )
934         Error( SbERR_BAD_DECLARATION, aName );
935 //  if( pDef->GetType() == SbxOBJECT )
936 //      pDef->SetType( SbxVARIANT ),
937 //      Error( SbERR_SYNTAX );
938     if( pDef->GetType() == SbxVARIANT && !( bFunc || bProp ) )
939         pDef->SetType( SbxEMPTY );
940     return pDef;
941 }
942 
943 // DECLARE
944 
Declare()945 void SbiParser::Declare()
946 {
947     DefDeclare( sal_False );
948 }
949 
DefDeclare(sal_Bool bPrivate)950 void SbiParser::DefDeclare( sal_Bool bPrivate )
951 {
952     Next();
953     if( eCurTok != SUB && eCurTok != FUNCTION )
954       Error( SbERR_UNEXPECTED, eCurTok );
955     else
956     {
957         bool bFunction = (eCurTok == FUNCTION);
958 
959         SbiProcDef* pDef = ProcDecl( sal_True );
960         if( pDef )
961         {
962             if( !pDef->GetLib().Len() )
963                 Error( SbERR_EXPECTED, LIB );
964             // gibts den schon?
965             SbiSymDef* pOld = aPublics.Find( pDef->GetName() );
966             if( pOld )
967             {
968                 SbiProcDef* p = pOld->GetProcDef();
969                 if( !p )
970                 {
971                     // Als Variable deklariert
972                     Error( SbERR_BAD_DECLARATION, pDef->GetName() );
973                     delete pDef;
974                     pDef = NULL;
975                 }
976                 else
977                     pDef->Match( p );
978             }
979             else
980                 aPublics.Add( pDef );
981 
982             if ( pDef )
983             {
984                 pDef->SetPublic( !bPrivate );
985 
986                 // New declare handling
987                 if( pDef->GetLib().Len() > 0 )
988                 {
989                     if( bNewGblDefs && nGblChain == 0 )
990                     {
991                         nGblChain = aGen.Gen( _JUMP, 0 );
992                         bNewGblDefs = sal_False;
993                     }
994 
995                     sal_uInt16 nSavLine = nLine;
996                     aGen.Statement();
997                     pDef->Define();
998                     pDef->SetLine1( nSavLine );
999                     pDef->SetLine2( nSavLine );
1000 
1001                     SbiSymPool& rPool = pDef->GetParams();
1002                     sal_uInt16 nParCount = rPool.GetSize();
1003 
1004                     SbxDataType eType = pDef->GetType();
1005                     if( bFunction )
1006                         aGen.Gen( _PARAM, 0, sal::static_int_cast< sal_uInt16 >( eType ) );
1007 
1008                     if( nParCount > 1 )
1009                     {
1010                         aGen.Gen( _ARGC );
1011 
1012                         for( sal_uInt16 i = 1 ; i < nParCount ; ++i )
1013                         {
1014                             SbiSymDef* pParDef = rPool.Get( i );
1015                             SbxDataType eParType = pParDef->GetType();
1016 
1017                             aGen.Gen( _PARAM, i, sal::static_int_cast< sal_uInt16 >( eParType ) );
1018                             aGen.Gen( _ARGV );
1019 
1020                             sal_uInt16 nTyp = sal::static_int_cast< sal_uInt16 >( pParDef->GetType() );
1021                             if( pParDef->IsByVal() )
1022                             {
1023                                 // Reset to avoid additional byval in call to wrapper function
1024                                 pParDef->SetByVal( sal_False );
1025                                 nTyp |= 0x8000;
1026                             }
1027                             aGen.Gen( _ARGTYP, nTyp );
1028                         }
1029                     }
1030 
1031                     aGen.Gen( _LIB, aGblStrings.Add( pDef->GetLib() ) );
1032 
1033                     SbiOpcode eOp = pDef->IsCdecl() ? _CALLC : _CALL;
1034                     sal_uInt16 nId = pDef->GetId();
1035                     if( pDef->GetAlias().Len() )
1036                         nId = ( nId & 0x8000 ) | aGblStrings.Add( pDef->GetAlias() );
1037                     if( nParCount > 1 )
1038                         nId |= 0x8000;
1039                     aGen.Gen( eOp, nId, sal::static_int_cast< sal_uInt16 >( eType ) );
1040 
1041                     if( bFunction )
1042                         aGen.Gen( _PUT );
1043 
1044                     aGen.Gen( _LEAVE );
1045                 }
1046             }
1047         }
1048     }
1049 }
1050 
1051 // Aufruf einer SUB oder FUNCTION
1052 
Call()1053 void SbiParser::Call()
1054 {
1055     String aName( aSym );
1056     SbiExpression aVar( this, SbSYMBOL );
1057     aVar.Gen( FORCE_CALL );
1058     aGen.Gen( _GET );
1059 }
1060 
1061 // SUB/FUNCTION
1062 
SubFunc()1063 void SbiParser::SubFunc()
1064 {
1065     DefProc( sal_False, sal_False );
1066 }
1067 
1068 // Einlesen einer Prozedur
1069 
1070 sal_Bool runsInSetup( void );
1071 
DefProc(sal_Bool bStatic,sal_Bool bPrivate)1072 void SbiParser::DefProc( sal_Bool bStatic, sal_Bool bPrivate )
1073 {
1074     sal_uInt16 l1 = nLine, l2 = nLine;
1075     sal_Bool bSub = sal_Bool( eCurTok == SUB );
1076     sal_Bool bProperty = sal_Bool( eCurTok == PROPERTY );
1077     PropertyMode ePropertyMode = PROPERTY_MODE_NONE;
1078     if( bProperty )
1079     {
1080         Next();
1081         if( eCurTok == GET )
1082             ePropertyMode = PROPERTY_MODE_GET;
1083         else if( eCurTok == LET )
1084             ePropertyMode = PROPERTY_MODE_LET;
1085         else if( eCurTok == SET )
1086             ePropertyMode = PROPERTY_MODE_SET;
1087         else
1088             Error( SbERR_EXPECTED, "Get or Let or Set" );
1089     }
1090 
1091     SbiToken eExit = eCurTok;
1092     SbiProcDef* pDef = ProcDecl( sal_False );
1093     if( !pDef )
1094         return;
1095     pDef->setPropertyMode( ePropertyMode );
1096 
1097     // Ist die Proc bereits deklariert?
1098     SbiSymDef* pOld = aPublics.Find( pDef->GetName() );
1099     if( pOld )
1100     {
1101         bool bError_ = false;
1102 
1103         pProc = pOld->GetProcDef();
1104         if( !pProc )
1105         {
1106             // Als Variable deklariert
1107             Error( SbERR_BAD_DECLARATION, pDef->GetName() );
1108             delete pDef;
1109             pProc = NULL;
1110             bError_ = true;
1111         }
1112         // #100027: Multiple declaration -> Error
1113         // #112787: Not for setup, REMOVE for 8
1114         else if( !runsInSetup() && pProc->IsUsedForProcDecl() )
1115         {
1116             PropertyMode ePropMode = pDef->getPropertyMode();
1117             if( ePropMode == PROPERTY_MODE_NONE || ePropMode == pProc->getPropertyMode() )
1118             {
1119                 Error( SbERR_PROC_DEFINED, pDef->GetName() );
1120                 delete pDef;
1121                 pProc = NULL;
1122                 bError_ = true;
1123             }
1124         }
1125 
1126         if( !bError_ )
1127         {
1128             pDef->Match( pProc );
1129             pProc = pDef;
1130         }
1131     }
1132     else
1133         aPublics.Add( pDef ), pProc = pDef;
1134 
1135     if( !pProc )
1136         return;
1137     pProc->SetPublic( !bPrivate );
1138 
1139     // Nun setzen wir die Suchhierarchie fuer Symbole sowie die aktuelle
1140     // Prozedur.
1141     aPublics.SetProcId( pProc->GetId() );
1142     pProc->GetParams().SetParent( &aPublics );
1143     if( bStatic )
1144         {
1145         if ( bVBASupportOn )
1146             pProc->SetStatic( sal_True );
1147         else
1148             Error( SbERR_NOT_IMPLEMENTED ); // STATIC SUB ...
1149         }
1150     else
1151     {
1152         pProc->SetStatic( sal_False );
1153         }
1154     // Normalfall: Lokale Variable->Parameter->Globale Variable
1155     pProc->GetLocals().SetParent( &pProc->GetParams() );
1156     pPool = &pProc->GetLocals();
1157 
1158     pProc->Define();
1159     OpenBlock( eExit );
1160     StmntBlock( bSub ? ENDSUB : (bProperty ? ENDPROPERTY : ENDFUNC) );
1161     l2 = nLine;
1162     pProc->SetLine1( l1 );
1163     pProc->SetLine2( l2 );
1164     pPool = &aPublics;
1165     aPublics.SetProcId( 0 );
1166     // Offene Labels?
1167     pProc->GetLabels().CheckRefs();
1168     CloseBlock();
1169     aGen.Gen( _LEAVE );
1170     pProc = NULL;
1171 }
1172 
1173 // STATIC variable|procedure
1174 
Static()1175 void SbiParser::Static()
1176 {
1177     DefStatic( sal_False );
1178 }
1179 
DefStatic(sal_Bool bPrivate)1180 void SbiParser::DefStatic( sal_Bool bPrivate )
1181 {
1182     switch( Peek() )
1183     {
1184         case SUB:
1185         case FUNCTION:
1186         case PROPERTY:
1187             // End global chain if necessary (not done in
1188             // SbiParser::Parse() under these conditions
1189             if( bNewGblDefs && nGblChain == 0 )
1190             {
1191                 nGblChain = aGen.Gen( _JUMP, 0 );
1192                 bNewGblDefs = sal_False;
1193             }
1194             Next();
1195             DefProc( sal_True, bPrivate );
1196             break;
1197         default: {
1198             if( !pProc )
1199                 Error( SbERR_NOT_IN_SUBR );
1200             // Pool umsetzen, damit STATIC-Deklarationen im globalen
1201             // Pool landen
1202             SbiSymPool* p = pPool; pPool = &aPublics;
1203             DefVar( _STATIC, sal_True );
1204             pPool = p;
1205             } break;
1206     }
1207 }
1208 
1209 /* vim: set noet sw=4 ts=4: */
1210