xref: /trunk/main/basic/source/comp/exprgen.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_basic.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "sbcomp.hxx"
32*cdf0e10cSrcweir #include "expr.hxx"
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir // Umsetztabelle fuer Token-Operatoren und Opcodes
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir typedef struct {
37*cdf0e10cSrcweir         SbiToken  eTok;                 // Token
38*cdf0e10cSrcweir         SbiOpcode eOp;                  // Opcode
39*cdf0e10cSrcweir } OpTable;
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir static OpTable aOpTable [] = {
42*cdf0e10cSrcweir     { EXPON,_EXP },
43*cdf0e10cSrcweir     { MUL,  _MUL },
44*cdf0e10cSrcweir     { DIV,  _DIV },
45*cdf0e10cSrcweir     { IDIV, _IDIV },
46*cdf0e10cSrcweir     { MOD,  _MOD },
47*cdf0e10cSrcweir     { PLUS, _PLUS },
48*cdf0e10cSrcweir     { MINUS,_MINUS },
49*cdf0e10cSrcweir     { EQ,   _EQ },
50*cdf0e10cSrcweir     { NE,   _NE },
51*cdf0e10cSrcweir     { LE,   _LE },
52*cdf0e10cSrcweir     { GE,   _GE },
53*cdf0e10cSrcweir     { LT,   _LT },
54*cdf0e10cSrcweir     { GT,   _GT },
55*cdf0e10cSrcweir     { AND,  _AND },
56*cdf0e10cSrcweir     { OR,   _OR },
57*cdf0e10cSrcweir     { XOR,  _XOR },
58*cdf0e10cSrcweir     { EQV,  _EQV },
59*cdf0e10cSrcweir     { IMP,  _IMP },
60*cdf0e10cSrcweir     { NOT,  _NOT },
61*cdf0e10cSrcweir     { NEG,  _NEG },
62*cdf0e10cSrcweir     { CAT,  _CAT },
63*cdf0e10cSrcweir     { LIKE, _LIKE },
64*cdf0e10cSrcweir     { IS,   _IS },
65*cdf0e10cSrcweir     { NIL,  _NOP }};
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir // Ausgabe eines Elements
68*cdf0e10cSrcweir void SbiExprNode::Gen( RecursiveMode eRecMode )
69*cdf0e10cSrcweir {
70*cdf0e10cSrcweir     if( IsConstant() )
71*cdf0e10cSrcweir     {
72*cdf0e10cSrcweir         switch( GetType() )
73*cdf0e10cSrcweir         {
74*cdf0e10cSrcweir             case SbxEMPTY:   pGen->Gen( _EMPTY ); break;
75*cdf0e10cSrcweir             case SbxINTEGER: pGen->Gen( _CONST,  (short) nVal ); break;
76*cdf0e10cSrcweir             case SbxSTRING:
77*cdf0e10cSrcweir             {
78*cdf0e10cSrcweir                 sal_uInt16 nStringId = pGen->GetParser()->aGblStrings.Add( aStrVal, sal_True );
79*cdf0e10cSrcweir                 pGen->Gen( _SCONST, nStringId ); break;
80*cdf0e10cSrcweir             }
81*cdf0e10cSrcweir             default:
82*cdf0e10cSrcweir             {
83*cdf0e10cSrcweir                 sal_uInt16 nStringId = pGen->GetParser()->aGblStrings.Add( nVal, eType );
84*cdf0e10cSrcweir                 pGen->Gen( _NUMBER, nStringId );
85*cdf0e10cSrcweir             }
86*cdf0e10cSrcweir         }
87*cdf0e10cSrcweir     }
88*cdf0e10cSrcweir     else if( IsOperand() )
89*cdf0e10cSrcweir     {
90*cdf0e10cSrcweir         SbiExprNode* pWithParent_ = NULL;
91*cdf0e10cSrcweir         SbiOpcode eOp;
92*cdf0e10cSrcweir         if( aVar.pDef->GetScope() == SbPARAM )
93*cdf0e10cSrcweir         {
94*cdf0e10cSrcweir             eOp = _PARAM;
95*cdf0e10cSrcweir             if( 0 == aVar.pDef->GetPos() )
96*cdf0e10cSrcweir             {
97*cdf0e10cSrcweir                 bool bTreatFunctionAsParam = true;
98*cdf0e10cSrcweir                 if( eRecMode == FORCE_CALL )
99*cdf0e10cSrcweir                 {
100*cdf0e10cSrcweir                     bTreatFunctionAsParam = false;
101*cdf0e10cSrcweir                 }
102*cdf0e10cSrcweir                 else if( eRecMode == UNDEFINED )
103*cdf0e10cSrcweir                 {
104*cdf0e10cSrcweir                     if( aVar.pPar && aVar.pPar->IsBracket() )
105*cdf0e10cSrcweir                         bTreatFunctionAsParam = false;
106*cdf0e10cSrcweir                 }
107*cdf0e10cSrcweir                 if( !bTreatFunctionAsParam )
108*cdf0e10cSrcweir                     eOp = aVar.pDef->IsGlobal() ? _FIND_G : _FIND;
109*cdf0e10cSrcweir             }
110*cdf0e10cSrcweir         }
111*cdf0e10cSrcweir         // AB: 17.12.1995, Spezialbehandlung fuer WITH
112*cdf0e10cSrcweir         else if( (pWithParent_ = GetWithParent()) != NULL )
113*cdf0e10cSrcweir         {
114*cdf0e10cSrcweir             eOp = _ELEM;            // .-Ausdruck in WITH
115*cdf0e10cSrcweir         }
116*cdf0e10cSrcweir         else
117*cdf0e10cSrcweir         {
118*cdf0e10cSrcweir             eOp = ( aVar.pDef->GetScope() == SbRTL ) ? _RTL :
119*cdf0e10cSrcweir                 (aVar.pDef->IsGlobal() ? _FIND_G : _FIND);
120*cdf0e10cSrcweir         }
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir         if( eOp == _FIND )
123*cdf0e10cSrcweir         {
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir             SbiProcDef* pProc = aVar.pDef->GetProcDef();
126*cdf0e10cSrcweir             if ( pGen->GetParser()->bClassModule )
127*cdf0e10cSrcweir                 eOp = _FIND_CM;
128*cdf0e10cSrcweir             else if ( aVar.pDef->IsStatic() || (pProc && pProc->IsStatic()) )
129*cdf0e10cSrcweir             {
130*cdf0e10cSrcweir                 eOp = _FIND_STATIC;
131*cdf0e10cSrcweir             }
132*cdf0e10cSrcweir         }
133*cdf0e10cSrcweir         for( SbiExprNode* p = this; p; p = p->aVar.pNext )
134*cdf0e10cSrcweir         {
135*cdf0e10cSrcweir             if( p == this && pWithParent_ != NULL )
136*cdf0e10cSrcweir                 pWithParent_->Gen();
137*cdf0e10cSrcweir             p->GenElement( eOp );
138*cdf0e10cSrcweir             eOp = _ELEM;
139*cdf0e10cSrcweir         }
140*cdf0e10cSrcweir     }
141*cdf0e10cSrcweir     else if( IsTypeOf() )
142*cdf0e10cSrcweir     {
143*cdf0e10cSrcweir         pLeft->Gen();
144*cdf0e10cSrcweir         pGen->Gen( _TESTCLASS, nTypeStrId );
145*cdf0e10cSrcweir     }
146*cdf0e10cSrcweir     else if( IsNew() )
147*cdf0e10cSrcweir     {
148*cdf0e10cSrcweir         pGen->Gen( _CREATE, 0, nTypeStrId );
149*cdf0e10cSrcweir     }
150*cdf0e10cSrcweir     else
151*cdf0e10cSrcweir     {
152*cdf0e10cSrcweir         pLeft->Gen();
153*cdf0e10cSrcweir         if( pRight )
154*cdf0e10cSrcweir             pRight->Gen();
155*cdf0e10cSrcweir         for( OpTable* p = aOpTable; p->eTok != NIL; p++ )
156*cdf0e10cSrcweir         {
157*cdf0e10cSrcweir             if( p->eTok == eTok )
158*cdf0e10cSrcweir             {
159*cdf0e10cSrcweir                 pGen->Gen( p->eOp ); break;
160*cdf0e10cSrcweir             }
161*cdf0e10cSrcweir         }
162*cdf0e10cSrcweir     }
163*cdf0e10cSrcweir }
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir // Ausgabe eines Operanden-Elements
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir void SbiExprNode::GenElement( SbiOpcode eOp )
168*cdf0e10cSrcweir {
169*cdf0e10cSrcweir #ifdef DBG_UTIL
170*cdf0e10cSrcweir     if( (eOp < _RTL || eOp > _CALLC) && eOp != _FIND_G && eOp != _FIND_CM )
171*cdf0e10cSrcweir         pGen->GetParser()->Error( SbERR_INTERNAL_ERROR, "Opcode" );
172*cdf0e10cSrcweir #endif
173*cdf0e10cSrcweir     SbiSymDef* pDef = aVar.pDef;
174*cdf0e10cSrcweir     // Das ID ist entweder die Position oder das String-ID
175*cdf0e10cSrcweir     // Falls das Bit 0x8000 gesetzt ist, hat die Variable
176*cdf0e10cSrcweir     // eine Parameterliste.
177*cdf0e10cSrcweir     sal_uInt16 nId = ( eOp == _PARAM ) ? pDef->GetPos() : pDef->GetId();
178*cdf0e10cSrcweir     // Parameterliste aufbauen
179*cdf0e10cSrcweir     if( aVar.pPar && aVar.pPar->GetSize() )
180*cdf0e10cSrcweir     {
181*cdf0e10cSrcweir         nId |= 0x8000;
182*cdf0e10cSrcweir         aVar.pPar->Gen();
183*cdf0e10cSrcweir     }
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir     pGen->Gen( eOp, nId, sal::static_int_cast< sal_uInt16 >( GetType() ) );
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir     if( aVar.pvMorePar )
188*cdf0e10cSrcweir     {
189*cdf0e10cSrcweir         SbiExprListVector* pvMorePar = aVar.pvMorePar;
190*cdf0e10cSrcweir         SbiExprListVector::iterator it;
191*cdf0e10cSrcweir         for( it = pvMorePar->begin() ; it != pvMorePar->end() ; ++it )
192*cdf0e10cSrcweir         {
193*cdf0e10cSrcweir             SbiExprList* pExprList = *it;
194*cdf0e10cSrcweir             pExprList->Gen();
195*cdf0e10cSrcweir             pGen->Gen( _ARRAYACCESS );
196*cdf0e10cSrcweir         }
197*cdf0e10cSrcweir     }
198*cdf0e10cSrcweir }
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir // Erzeugen einer Argv-Tabelle
201*cdf0e10cSrcweir // Das erste Element bleibt immer frei fuer Returnwerte etc.
202*cdf0e10cSrcweir // Siehe auch SbiProcDef::SbiProcDef() in symtbl.cxx
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir void SbiExprList::Gen()
205*cdf0e10cSrcweir {
206*cdf0e10cSrcweir     if( pFirst )
207*cdf0e10cSrcweir     {
208*cdf0e10cSrcweir         pParser->aGen.Gen( _ARGC );
209*cdf0e10cSrcweir         // AB 10.1.96: Typ-Anpassung bei DECLARE
210*cdf0e10cSrcweir         sal_uInt16 nCount = 1 /*, nParAnz = 0*/;
211*cdf0e10cSrcweir //      SbiSymPool* pPool = NULL;
212*cdf0e10cSrcweir         for( SbiExpression* pExpr = pFirst; pExpr; pExpr = pExpr->pNext,nCount++ )
213*cdf0e10cSrcweir         {
214*cdf0e10cSrcweir             pExpr->Gen();
215*cdf0e10cSrcweir             if( pExpr->GetName().Len() )
216*cdf0e10cSrcweir             {
217*cdf0e10cSrcweir                 // named arg
218*cdf0e10cSrcweir                 sal_uInt16 nSid = pParser->aGblStrings.Add( pExpr->GetName() );
219*cdf0e10cSrcweir                 pParser->aGen.Gen( _ARGN, nSid );
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir                 /* TODO: Check after Declare concept change
222*cdf0e10cSrcweir                 // AB 10.1.96: Typanpassung bei named -> passenden Parameter suchen
223*cdf0e10cSrcweir                 if( pProc )
224*cdf0e10cSrcweir                 {
225*cdf0e10cSrcweir                     // Vorerst: Error ausloesen
226*cdf0e10cSrcweir                     pParser->Error( SbERR_NO_NAMED_ARGS );
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir                     // Spaeter, wenn Named Args bei DECLARE moeglich
229*cdf0e10cSrcweir                     //for( sal_uInt16 i = 1 ; i < nParAnz ; i++ )
230*cdf0e10cSrcweir                     //{
231*cdf0e10cSrcweir                     //  SbiSymDef* pDef = pPool->Get( i );
232*cdf0e10cSrcweir                     //  const String& rName = pDef->GetName();
233*cdf0e10cSrcweir                     //  if( rName.Len() )
234*cdf0e10cSrcweir                     //  {
235*cdf0e10cSrcweir                     //      if( pExpr->GetName().ICompare( rName )
236*cdf0e10cSrcweir                     //          == COMPARE_EQUAL )
237*cdf0e10cSrcweir                     //      {
238*cdf0e10cSrcweir                     //          pParser->aGen.Gen( _ARGTYP, pDef->GetType() );
239*cdf0e10cSrcweir                     //          break;
240*cdf0e10cSrcweir                     //      }
241*cdf0e10cSrcweir                     //  }
242*cdf0e10cSrcweir                     //}
243*cdf0e10cSrcweir                 }
244*cdf0e10cSrcweir                 */
245*cdf0e10cSrcweir             }
246*cdf0e10cSrcweir             else
247*cdf0e10cSrcweir             {
248*cdf0e10cSrcweir                 pParser->aGen.Gen( _ARGV );
249*cdf0e10cSrcweir             }
250*cdf0e10cSrcweir         }
251*cdf0e10cSrcweir     }
252*cdf0e10cSrcweir }
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir void SbiExpression::Gen( RecursiveMode eRecMode )
255*cdf0e10cSrcweir {
256*cdf0e10cSrcweir     // AB: 17.12.1995, Spezialbehandlung fuer WITH
257*cdf0e10cSrcweir     // Wenn pExpr == .-Ausdruck in With, zunaechst Gen fuer Basis-Objekt
258*cdf0e10cSrcweir     pExpr->Gen( eRecMode );
259*cdf0e10cSrcweir     if( bByVal )
260*cdf0e10cSrcweir         pParser->aGen.Gen( _BYVAL );
261*cdf0e10cSrcweir     if( bBased )
262*cdf0e10cSrcweir     {
263*cdf0e10cSrcweir         sal_uInt16 uBase = pParser->nBase;
264*cdf0e10cSrcweir         if( pParser->IsCompatible() )
265*cdf0e10cSrcweir             uBase |= 0x8000;        // #109275 Flag compatiblity
266*cdf0e10cSrcweir         pParser->aGen.Gen( _BASED, uBase );
267*cdf0e10cSrcweir         pParser->aGen.Gen( _ARGV );
268*cdf0e10cSrcweir     }
269*cdf0e10cSrcweir }
270*cdf0e10cSrcweir 
271