1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
22 #include <basic/sberrors.hxx>
23 #include <basic/sbmeth.hxx>
24 #include <basic/sbstar.hxx>
25 #include <basic/sbx.hxx>
27 #include <rtl/ustring.hxx>
28 #include <com/sun/star/uno/Sequence.hxx>
29 #include <osl/file.hxx>
30 #include <i18nlangtag/lang.h>
35 #include <com/sun/star/lang/XComponent.hpp>
36 #include <com/sun/star/container/XEnumeration.hpp>
37 #include <unotools/localedatawrapper.hxx>
38 #include <o3tl/deleter.hxx>
39 #include <o3tl/typed_flags_set.hxx>
41 class SbiInstance
; // active StarBASIC process
42 class SbiRuntime
; // active StarBASIC procedure instance
44 struct SbiArgv
; // Argv stack element
45 struct SbiGosub
; // GOSUB stack element
46 class SbiImage
; // Code-Image
50 class SvNumberFormatter
; // time/date functions
51 enum class SbiImageFlags
;
61 struct SbiForStack
{ // for/next stack:
62 SbiForStack
* pNext
; // Chain
63 SbxVariableRef refVar
; // loop variable
64 SbxVariableRef refEnd
; // end expression / for each: Array/BasicCollection object
65 SbxVariableRef refInc
; // increment expression
69 sal_Int32 nCurCollectionIndex
;
70 std::unique_ptr
<sal_Int32
[]>
72 std::unique_ptr
<sal_Int32
[]>
74 std::unique_ptr
<sal_Int32
[]>
76 css::uno::Reference
< css::container::XEnumeration
> xEnumeration
;
80 , eForType(ForType::To
)
81 , nCurCollectionIndex(0)
85 #define MAXRECURSION 500 //to prevent dead-recursions
87 enum class SbAttributes
{
96 template<> struct typed_flags
<SbAttributes
> : is_typed_flags
<SbAttributes
, 0x13> {};
105 std::unique_ptr
<osl::Directory
> pDir
;
106 SbAttributes nDirFlags
;
109 OUString sFullNameToBeChecked
;
110 std::unique_ptr
<WildCard
> pWildCard
;
112 css::uno::Sequence
< OUString
> aDirSeq
;
118 // The instance matches a running StarBASIC. Many basics running at the same
119 // time are managed by chained instances. There is all the data that only lives
120 // when the BASIC is living too, like the I/O-system.
122 typedef std::vector
< css::uno::Reference
< css::lang::XComponent
> > ComponentVector_t
;
127 friend class SbiRuntime
;
132 std::unique_ptr
<SbiIoSystem
, o3tl::default_delete
<SbiIoSystem
>> pIosys
;
134 std::unique_ptr
<SbiDdeControl
> pDdeCtrl
;
135 // DLL-Calls (DECLARE)
136 std::unique_ptr
<SbiDllMgr
> pDllMgr
;
137 std::shared_ptr
<SvNumberFormatter
> pNumberFormatter
;
139 LanguageType meFormatterLangType
;
140 DateOrder meFormatterDateOrder
;
141 sal_uInt32 nStdDateIdx
, nStdTimeIdx
, nStdDateTimeIdx
;
144 OUString aErrorMsg
; // last error message for $ARG
145 sal_Int32 nErl
; // current error line
146 bool bReschedule
; // Flag: sal_True = Reschedule in main loop
147 bool bCompatibility
; // Flag: sal_True = VBA runtime compatibility mode
149 ComponentVector_t ComponentVector
;
151 SbiRuntime
* pRun
; // Call-Stack
153 // #31460 new concept for StepInto/Over/Out,
154 // explanation see runtime.cxx at SbiInstance::CalcBreakCallLevel()
156 sal_uInt16 nBreakCallLvl
;
157 void CalcBreakCallLevel( BasicDebugFlags nFlags
);
159 SbiInstance( StarBASIC
* );
162 void Error( ErrCode
); // trappable Error
163 void Error( ErrCode
, const OUString
& rMsg
); // trappable Error with message
164 void ErrorVB( sal_Int32 nVBNumber
, const OUString
& rMsg
);
165 void setErrorVB( sal_Int32 nVBNumber
);
166 void FatalError( ErrCode
); // non-trappable Error
167 void FatalError( ErrCode
, const OUString
& ); // non-trappable Error
168 void Abort(); // with current error code
171 ErrCode
const & GetErr() const { return nErr
; }
172 const OUString
& GetErrorMsg() const { return aErrorMsg
; }
173 sal_Int32
GetErl() const { return nErl
; }
174 void EnableReschedule( bool bEnable
) { bReschedule
= bEnable
; }
175 bool IsReschedule() const { return bReschedule
; }
176 void EnableCompatibility( bool bEnable
) { bCompatibility
= bEnable
; }
177 bool IsCompatibility() const { return bCompatibility
; }
179 ComponentVector_t
& getComponentVector() { return ComponentVector
; }
181 SbMethod
* GetCaller( sal_uInt16
);
182 SbModule
* GetActiveModule();
184 SbiIoSystem
* GetIoSystem() { return pIosys
.get(); }
185 SbiDdeControl
* GetDdeControl() { return pDdeCtrl
.get(); }
186 StarBASIC
* GetBasic() { return pBasic
; }
187 SbiDllMgr
* GetDllMgr();
188 SbiRTLData
& GetRTLData() const { return const_cast<SbiRTLData
&>(aRTLData
); }
190 std::shared_ptr
<SvNumberFormatter
> const & GetNumberFormatter();
191 sal_uInt32
GetStdDateIdx() const { return nStdDateIdx
; }
192 sal_uInt32
GetStdTimeIdx() const { return nStdTimeIdx
; }
193 sal_uInt32
GetStdDateTimeIdx() const { return nStdDateTimeIdx
; }
195 // offer NumberFormatter also static
196 static std::shared_ptr
<SvNumberFormatter
> PrepareNumberFormatter( sal_uInt32
&rnStdDateIdx
,
197 sal_uInt32
&rnStdTimeIdx
, sal_uInt32
&rnStdDateTimeIdx
,
198 LanguageType
const * peFormatterLangType
=nullptr, DateOrder
const * peFormatterDateOrder
=nullptr );
201 // There's one instance of this class for every executed sub-program.
202 // This instance is the heart of the BASIC-machine and contains only local data.
206 friend void SbRtl_CallByName( StarBASIC
* pBasic
, SbxArray
& rPar
, bool bWrite
);
208 typedef void( SbiRuntime::*pStep0
)();
209 typedef void( SbiRuntime::*pStep1
)( sal_uInt32 nOp1
);
210 typedef void( SbiRuntime::*pStep2
)( sal_uInt32 nOp1
, sal_uInt32 nOp2
);
211 static pStep0 aStep0
[]; // opcode-table group 0
212 static pStep1 aStep1
[];
213 static pStep2 aStep2
[];
215 StarBASIC
& rBasic
; // StarBASIC instance
216 SbiInstance
* pInst
; // current thread
217 SbModule
* pMod
; // current module
218 SbMethod
* pMeth
; // method instance
219 SbiIoSystem
* pIosys
; // I/O-System
220 const SbiImage
* pImg
; // Code-Image
221 SbxArrayRef refExprStk
; // expression stack
222 SbxArrayRef refCaseStk
; // CASE expression stack
223 SbxArrayRef refRedimpArray
; // Array saved to use for REDIM PRESERVE
224 SbxVariableRef refRedim
; // Array saved to use for REDIM
225 SbxVariableRef xDummyVar
; // substitute for variables that weren't found
226 SbxVariable
* mpExtCaller
; // Caller ( external - e.g. button name, shape, range object etc. - only in vba mode )
227 SbiForStack
* pForStk
; // FOR/NEXT-Stack
228 sal_uInt16 nExprLvl
; // depth of the expr-stack
229 sal_uInt16 nForLvl
; // #118235: Maintain for level
230 const sal_uInt8
* pCode
; // current Code-Pointer
231 const sal_uInt8
* pStmnt
; // beginning of the last statement
232 const sal_uInt8
* pError
; // address of the current error handler
233 const sal_uInt8
* pRestart
; // restart-address
234 const sal_uInt8
* pErrCode
; // restart-address RESUME NEXT
235 const sal_uInt8
* pErrStmnt
; // restart-address RESUME 0
236 OUString aLibName
; // Lib-name for declare-call
237 SbxArrayRef refParams
; // current procedure parameters
238 SbxArrayRef refLocals
; // local variable
240 // #74254, one refSaveObj is not enough! new: pRefSaveList (see above)
243 bool bError
; // true: handle errors
244 bool bInError
; // true: in an error handler
245 bool bBlocked
; // true: blocked by next call level, #i48868
247 BasicDebugFlags nFlags
; // Debugging-Flags
249 sal_uInt16 nOps
; // opcode counter
250 sal_uInt32 m_nLastTime
;
252 std::vector
<SbxVariableRef
> aRefSaved
; // #74254 save temporary references
253 std::vector
<SbiGosub
> pGosubStk
; // GOSUB stack
254 std::vector
<SbiArgv
> pArgvStk
; // ARGV-Stack
257 SbxVariable
* FindElement
258 ( SbxObject
* pObj
, sal_uInt32 nOp1
, sal_uInt32 nOp2
, ErrCode
, bool bLocal
, bool bStatic
= false );
259 void SetupArgs( SbxVariable
*, sal_uInt32
);
260 SbxVariable
* CheckArray( SbxVariable
* );
262 void PushVar( SbxVariable
* );
263 SbxVariableRef
PopVar();
264 SbxVariable
* GetTOS();
266 void ClearExprStack();
268 void PushGosub( const sal_uInt8
* );
273 void ClearArgvStack();
278 void ClearForStack();
280 void StepArith( SbxOperator
);
281 void StepUnary( SbxOperator
);
282 void StepCompare( SbxOperator
);
284 void SetParameters( SbxArray
* );
286 // HAS TO BE IMPLEMENTED SOME TIME
287 void DllCall( const OUString
&, const OUString
&, SbxArray
*, SbxDataType
, bool );
289 // #56204 swap out DIM-functionality into help method (step0.cxx)
290 void DimImpl(const SbxVariableRef
& refVar
);
292 static bool implIsClass( SbxObject
const * pObj
, const OUString
& aClass
);
294 void StepSETCLASS_impl( sal_uInt32 nOp1
, bool bHandleDflt
);
296 // the following routines are called by the single
297 // stepper and implement the single opcodes
298 void StepNOP(), StepEXP(), StepMUL(), StepDIV();
299 void StepMOD(), StepPLUS(), StepMINUS(), StepNEG();
300 void StepEQ(), StepNE(), StepLT(), StepGT();
301 void StepLE(), StepGE(), StepIDIV(), StepAND();
302 void StepOR(), StepXOR(), StepEQV(), StepIMP();
303 void StepNOT(), StepCAT(), StepLIKE(), StepIS();
305 void StepARGV(), StepINPUT(), StepLINPUT(), StepSTOP();
306 void StepGET(), StepSET(), StepVBASET(), StepPUT(), StepPUTC();
307 void StepSET_Impl( SbxVariableRef
& refVal
, SbxVariableRef
& refVar
, bool bDefaultHandling
= false );
308 void StepDIM(), StepREDIM(), StepREDIMP(), StepERASE();
309 void StepINITFOR(), StepNEXT(), StepERROR(), StepINITFOREACH();
310 void StepCASE(), StepENDCASE(), StepSTDERROR();
311 void StepNOERROR(), StepCHANNEL(), StepCHANNEL0(), StepPRINT();
312 void StepPRINTF(), StepWRITE(), StepRENAME(), StepPROMPT();
313 void StepRESTART(), StepEMPTY(), StepLEAVE();
314 void StepLSET(), StepRSET(), StepREDIMP_ERASE(), StepERASE_CLEAR();
315 void StepARRAYACCESS(), StepBYVAL();
316 // all opcodes with one operand
317 void StepLOADNC( sal_uInt32
), StepLOADSC( sal_uInt32
), StepLOADI( sal_uInt32
);
318 void StepARGN( sal_uInt32
), StepBASED( sal_uInt32
), StepPAD( sal_uInt32
);
319 void StepJUMP( sal_uInt32
), StepJUMPT( sal_uInt32
);
320 void StepJUMPF( sal_uInt32
), StepONJUMP( sal_uInt32
);
321 void StepGOSUB( sal_uInt32
), StepRETURN( sal_uInt32
);
322 void StepTESTFOR( sal_uInt32
), StepCASETO( sal_uInt32
), StepERRHDL( sal_uInt32
);
323 void StepRESUME( sal_uInt32
), StepSETCLASS( sal_uInt32
), StepVBASETCLASS( sal_uInt32
), StepTESTCLASS( sal_uInt32
), StepLIB( sal_uInt32
);
324 bool checkClass_Impl( const SbxVariableRef
& refVal
, const OUString
& aClass
, bool bRaiseErrors
, bool bDefault
);
325 void StepCLOSE( sal_uInt32
), StepPRCHAR( sal_uInt32
), StepARGTYP( sal_uInt32
);
326 // all opcodes with two operands
327 void StepRTL( sal_uInt32
, sal_uInt32
), StepPUBLIC( sal_uInt32
, sal_uInt32
), StepPUBLIC_P( sal_uInt32
, sal_uInt32
);
328 void StepPUBLIC_Impl( sal_uInt32
, sal_uInt32
, bool bUsedForClassModule
);
329 void StepFIND_Impl( SbxObject
* pObj
, sal_uInt32 nOp1
, sal_uInt32 nOp2
, ErrCode
, bool bStatic
= false );
330 void StepFIND( sal_uInt32
, sal_uInt32
), StepELEM( sal_uInt32
, sal_uInt32
);
331 void StepGLOBAL( sal_uInt32
, sal_uInt32
), StepLOCAL( sal_uInt32
, sal_uInt32
);
332 void StepPARAM( sal_uInt32
, sal_uInt32
), StepCREATE( sal_uInt32
, sal_uInt32
);
333 void StepCALL( sal_uInt32
, sal_uInt32
), StepCALLC( sal_uInt32
, sal_uInt32
);
334 void StepCASEIS( sal_uInt32
, sal_uInt32
), StepSTMNT( sal_uInt32
, sal_uInt32
);
335 SbxVariable
* StepSTATIC_Impl(
336 OUString
const & aName
, SbxDataType t
, sal_uInt32 nOp2
);
337 void StepOPEN( sal_uInt32
, sal_uInt32
), StepSTATIC( sal_uInt32
, sal_uInt32
);
338 void StepTCREATE(sal_uInt32
,sal_uInt32
), StepDCREATE(sal_uInt32
,sal_uInt32
);
339 void StepGLOBAL_P( sal_uInt32
, sal_uInt32
),StepFIND_G( sal_uInt32
, sal_uInt32
);
340 void StepDCREATE_REDIMP(sal_uInt32
,sal_uInt32
), StepDCREATE_IMPL(sal_uInt32
,sal_uInt32
);
341 void StepFIND_CM( sal_uInt32
, sal_uInt32
);
342 void StepFIND_STATIC( sal_uInt32
, sal_uInt32
);
343 static void implHandleSbxFlags( SbxVariable
* pVar
, SbxDataType t
, sal_uInt32 nOp2
);
345 void SetVBAEnabled( bool bEnabled
);
346 bool IsImageFlag( SbiImageFlags n
) const;
347 sal_uInt16
GetBase() const;
348 sal_Int32 nLine
,nCol1
,nCol2
;
349 SbiRuntime
* pNext
; // Stack-Chain
351 // tdf#79426, tdf#125180 - adds the information about a missing parameter
352 static void SetIsMissing( SbxVariable
* );
353 // tdf#79426, tdf#125180 - checks if a variable contains the information about a missing parameter
354 static bool IsMissing( SbxVariable
*, sal_uInt16
);
356 SbiRuntime( SbModule
*, SbMethod
*, sal_uInt32
);
358 void Error( ErrCode
, bool bVBATranslationAlreadyDone
= false ); // set error if != 0
359 void Error( ErrCode
, const OUString
& ); // set error if != 0
360 void FatalError( ErrCode
); // error handling = standard, set error
361 void FatalError( ErrCode
, const OUString
& ); // error handling = standard, set error
362 static sal_Int32
translateErrorToVba( ErrCode nError
, OUString
& rMsg
);
363 bool Step(); // single step (one opcode)
364 void Stop() { bRun
= false; }
365 void block() { bBlocked
= true; }
366 void unblock() { bBlocked
= false; }
367 SbModule
* GetModule() { return pMod
; }
368 BasicDebugFlags
GetDebugFlags() const { return nFlags
; }
369 void SetDebugFlags( BasicDebugFlags nFl
) { nFlags
= nFl
; }
370 SbMethod
* GetCaller() { return pMeth
;}
371 SbxVariable
* GetExternalCaller(){ return mpExtCaller
; }
373 SbiForStack
* FindForStackItemForCollection( class BasicCollection
const * pCollection
);
375 SbxBase
* FindElementExtern( const OUString
& rName
);
376 static bool isVBAEnabled();
380 inline void checkArithmeticOverflow( double d
)
382 if( !std::isfinite( d
) )
383 StarBASIC::Error( ERRCODE_BASIC_MATH_OVERFLOW
);
386 inline void checkArithmeticOverflow( SbxVariable
const * pVar
)
388 if( pVar
->GetType() == SbxDOUBLE
)
390 double d
= pVar
->GetDouble();
391 checkArithmeticOverflow( d
);
396 StarBASIC
* GetCurrentBasic( StarBASIC
* pRTBasic
);
398 // Returns true if UNO is available, otherwise the old
399 // file system implementation has to be used
400 // (Implemented in iosys.cxx)
403 // Converts possibly relative paths to absolute paths
404 // according to the setting done by ChDir/ChDrive
405 // (Implemented in methods.cxx)
406 OUString
getFullPath( const OUString
& aRelPath
);
408 // Implementation of StepRENAME with UCB
409 // (Implemented in methods.cxx, so step0.cxx
410 // has not to be infected with UNO)
411 void implStepRenameUCB( const OUString
& aSource
, const OUString
& aDest
);
413 void implStepRenameOSL( const OUString
& aSource
, const OUString
& aDest
);
414 bool IsBaseIndexOne();
416 void removeDimAsNewRecoverItem( SbxVariable
* pVar
);
418 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */