xref: /trunk/main/sc/source/core/tool/token.cxx (revision 1354dfcdad4129e7cc561fdcfbb4f6c8958f0428)
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 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sc.hxx"
26 
27 
28 
29 // INCLUDE ---------------------------------------------------------------
30 
31 #include <cstddef>
32 #include <cstdio>
33 
34 #include <string.h>
35 #include <tools/mempool.hxx>
36 #include <tools/debug.hxx>
37 
38 #include "token.hxx"
39 #include "tokenarray.hxx"
40 #include "compiler.hxx"
41 #include <formula/compiler.hrc>
42 #include "rechead.hxx"
43 #include "parclass.hxx"
44 #include "jumpmatrix.hxx"
45 #include "rangeseq.hxx"
46 #include "externalrefmgr.hxx"
47 #include "document.hxx"
48 
49 using ::std::vector;
50 
51 #include <com/sun/star/sheet/ComplexReference.hpp>
52 #include <com/sun/star/sheet/ExternalReference.hpp>
53 #include <com/sun/star/sheet/ReferenceFlags.hpp>
54 
55 using namespace formula;
56 using namespace com::sun::star;
57 
58 namespace
59 {
lcl_SingleRefToCalc(ScSingleRefData & rRef,const sheet::SingleReference & rAPI)60     void lcl_SingleRefToCalc( ScSingleRefData& rRef, const sheet::SingleReference& rAPI )
61     {
62         rRef.InitFlags();
63 
64         rRef.nCol    = static_cast<SCsCOL>(rAPI.Column);
65         rRef.nRow    = static_cast<SCsROW>(rAPI.Row);
66         rRef.nTab    = static_cast<SCsTAB>(rAPI.Sheet);
67         rRef.nRelCol = static_cast<SCsCOL>(rAPI.RelativeColumn);
68         rRef.nRelRow = static_cast<SCsROW>(rAPI.RelativeRow);
69         rRef.nRelTab = static_cast<SCsTAB>(rAPI.RelativeSheet);
70 
71         rRef.SetColRel(     ( rAPI.Flags & sheet::ReferenceFlags::COLUMN_RELATIVE ) != 0 );
72         rRef.SetRowRel(     ( rAPI.Flags & sheet::ReferenceFlags::ROW_RELATIVE    ) != 0 );
73         rRef.SetTabRel(     ( rAPI.Flags & sheet::ReferenceFlags::SHEET_RELATIVE  ) != 0 );
74         rRef.SetColDeleted( ( rAPI.Flags & sheet::ReferenceFlags::COLUMN_DELETED  ) != 0 );
75         rRef.SetRowDeleted( ( rAPI.Flags & sheet::ReferenceFlags::ROW_DELETED     ) != 0 );
76         rRef.SetTabDeleted( ( rAPI.Flags & sheet::ReferenceFlags::SHEET_DELETED   ) != 0 );
77         rRef.SetFlag3D(     ( rAPI.Flags & sheet::ReferenceFlags::SHEET_3D        ) != 0 );
78         rRef.SetRelName(    ( rAPI.Flags & sheet::ReferenceFlags::RELATIVE_NAME   ) != 0 );
79     }
80 
lcl_ExternalRefToCalc(ScSingleRefData & rRef,const sheet::SingleReference & rAPI)81     void lcl_ExternalRefToCalc( ScSingleRefData& rRef, const sheet::SingleReference& rAPI )
82     {
83         rRef.InitFlags();
84 
85         rRef.nCol    = static_cast<SCsCOL>(rAPI.Column);
86         rRef.nRow    = static_cast<SCsROW>(rAPI.Row);
87         rRef.nTab    = 0;
88         rRef.nRelCol = static_cast<SCsCOL>(rAPI.RelativeColumn);
89         rRef.nRelRow = static_cast<SCsROW>(rAPI.RelativeRow);
90         rRef.nRelTab = 0;
91 
92         rRef.SetColRel(     ( rAPI.Flags & sheet::ReferenceFlags::COLUMN_RELATIVE ) != 0 );
93         rRef.SetRowRel(     ( rAPI.Flags & sheet::ReferenceFlags::ROW_RELATIVE    ) != 0 );
94         rRef.SetTabRel(     false );    // sheet index must be absolute for external refs
95         rRef.SetColDeleted( ( rAPI.Flags & sheet::ReferenceFlags::COLUMN_DELETED  ) != 0 );
96         rRef.SetRowDeleted( ( rAPI.Flags & sheet::ReferenceFlags::ROW_DELETED     ) != 0 );
97         rRef.SetTabDeleted( false );    // sheet must not be deleted for external refs
98         rRef.SetFlag3D(     ( rAPI.Flags & sheet::ReferenceFlags::SHEET_3D        ) != 0 );
99         rRef.SetRelName(    false );
100     }
101 //
102 } // namespace
103 //
104 // ImpTokenIterator wird je Interpreter angelegt, mehrfache auch durch
105 // SubCode via FormulaTokenIterator Push/Pop moeglich
106 IMPL_FIXEDMEMPOOL_NEWDEL( ImpTokenIterator, 32, 16 )
107 
108 // Align MemPools on 4k boundaries - 64 bytes (4k is a MUST for OS/2)
109 
110 // Since RawTokens are temporary for the compiler, don't align on 4k and waste memory.
111 // ScRawToken size is FixMembers + MAXSTRLEN + ~4 ~= 1036
112 IMPL_FIXEDMEMPOOL_NEWDEL( ScRawToken, 8, 4 )
113 // Some ScDoubleRawToken, FixMembers + sizeof(double) ~= 16
114 const sal_uInt16 nMemPoolDoubleRawToken = 0x0400 / sizeof(ScDoubleRawToken);
115 IMPL_FIXEDMEMPOOL_NEWDEL( ScDoubleRawToken, nMemPoolDoubleRawToken, nMemPoolDoubleRawToken )
116 
117 // Need a whole bunch of ScSingleRefToken
118 const sal_uInt16 nMemPoolSingleRefToken = (0x4000 - 64) / sizeof(ScSingleRefToken);
119 IMPL_FIXEDMEMPOOL_NEWDEL( ScSingleRefToken, nMemPoolSingleRefToken, nMemPoolSingleRefToken )
120 // Need quite a lot of ScDoubleRefToken
121 const sal_uInt16 nMemPoolDoubleRefToken = (0x2000 - 64) / sizeof(ScDoubleRefToken);
IMPL_FIXEDMEMPOOL_NEWDEL(ScDoubleRefToken,nMemPoolDoubleRefToken,nMemPoolDoubleRefToken)122 IMPL_FIXEDMEMPOOL_NEWDEL( ScDoubleRefToken, nMemPoolDoubleRefToken, nMemPoolDoubleRefToken )
123 
124 // --- helpers --------------------------------------------------------------
125 
126 inline sal_Bool lcl_IsReference( OpCode eOp, StackVar eType )
127 {
128     return
129         (eOp == ocPush && (eType == svSingleRef || eType == svDoubleRef))
130         || (eOp == ocColRowNameAuto && eType == svDoubleRef)
131         || (eOp == ocColRowName && eType == svSingleRef)
132         || (eOp == ocMatRef && eType == svSingleRef)
133         ;
134 }
135 
136 
137 // --- class ScRawToken -----------------------------------------------------
138 
GetStrLen(const sal_Unicode * pStr)139 xub_StrLen ScRawToken::GetStrLen( const sal_Unicode* pStr )
140 {
141     if ( !pStr )
142         return 0;
143     const sal_Unicode* p = pStr;
144     while ( *p )
145         p++;
146     return sal::static_int_cast<xub_StrLen>( p - pStr );
147 }
148 
149 
SetOpCode(OpCode e)150 void ScRawToken::SetOpCode( OpCode e )
151 {
152     eOp   = e;
153     switch (eOp)
154     {
155         case ocIf:
156             eType = svJump;
157             nJump[ 0 ] = 3; // If, Else, Behind
158             break;
159         case ocChose:
160             eType = svJump;
161             nJump[ 0 ] = MAXJUMPCOUNT+1;
162             break;
163         case ocIfna:
164         case ocIfError:
165             eType = svJump;
166             nJump[ 0 ] = 2; // Fallback, Behind
167             break;
168         case ocMissing:
169             eType = svMissing;
170             break;
171         case ocSep:
172         case ocOpen:
173         case ocClose:
174         case ocArrayRowSep:
175         case ocArrayColSep:
176         case ocArrayOpen:
177         case ocArrayClose:
178             eType = svSep;
179             break;
180         default:
181             eType = svByte;
182             sbyte.cByte = 0;
183             sbyte.bHasForceArray = ScParameterClassification::HasForceArray( eOp);
184     }
185     nRefCnt = 0;
186 }
187 
SetString(const sal_Unicode * pStr)188 void ScRawToken::SetString( const sal_Unicode* pStr )
189 {
190     eOp   = ocPush;
191     eType = svString;
192     if ( pStr )
193     {
194         xub_StrLen nLen = GetStrLen( pStr ) + 1;
195         if( nLen > MAXSTRLEN )
196             nLen = MAXSTRLEN;
197         memcpy( cStr, pStr, GetStrLenBytes( nLen ) );
198         cStr[ nLen-1 ] = 0;
199     }
200     else
201         cStr[0] = 0;
202     nRefCnt = 0;
203 }
204 
SetSingleReference(const ScSingleRefData & rRef)205 void ScRawToken::SetSingleReference( const ScSingleRefData& rRef )
206 {
207     eOp       = ocPush;
208     eType     = svSingleRef;
209     aRef.Ref1 =
210     aRef.Ref2 = rRef;
211     nRefCnt   = 0;
212 }
213 
SetDoubleReference(const ScComplexRefData & rRef)214 void ScRawToken::SetDoubleReference( const ScComplexRefData& rRef )
215 {
216     eOp   = ocPush;
217     eType = svDoubleRef;
218     aRef  = rRef;
219     nRefCnt = 0;
220 }
221 
SetDouble(double rVal)222 void ScRawToken::SetDouble(double rVal)
223 {
224     eOp   = ocPush;
225     eType = svDouble;
226     nValue = rVal;
227     nRefCnt = 0;
228 }
229 
SetName(sal_uInt16 n)230 void ScRawToken::SetName( sal_uInt16 n )
231 {
232     eOp    = ocName;
233     eType  = svIndex;
234     nIndex = n;
235     nRefCnt = 0;
236 }
237 
SetExternalSingleRef(sal_uInt16 nFileId,const String & rTabName,const ScSingleRefData & rRef)238 void ScRawToken::SetExternalSingleRef( sal_uInt16 nFileId, const String& rTabName, const ScSingleRefData& rRef )
239 {
240     eOp = ocExternalRef;
241     eType = svExternalSingleRef;
242     nRefCnt = 0;
243 
244     extref.nFileId = nFileId;
245     extref.aRef.Ref1 =
246     extref.aRef.Ref2 = rRef;
247 
248     xub_StrLen n = rTabName.Len();
249     memcpy(extref.cTabName, rTabName.GetBuffer(), n*sizeof(sal_Unicode));
250     extref.cTabName[n] = 0;
251 }
252 
SetExternalDoubleRef(sal_uInt16 nFileId,const String & rTabName,const ScComplexRefData & rRef)253 void ScRawToken::SetExternalDoubleRef( sal_uInt16 nFileId, const String& rTabName, const ScComplexRefData& rRef )
254 {
255     eOp = ocExternalRef;
256     eType = svExternalDoubleRef;
257     nRefCnt = 0;
258 
259     extref.nFileId = nFileId;
260     extref.aRef = rRef;
261 
262     xub_StrLen n = rTabName.Len();
263     memcpy(extref.cTabName, rTabName.GetBuffer(), n*sizeof(sal_Unicode));
264     extref.cTabName[n] = 0;
265 }
266 
SetExternalName(sal_uInt16 nFileId,const String & rName)267 void ScRawToken::SetExternalName( sal_uInt16 nFileId, const String& rName )
268 {
269     eOp = ocExternalRef;
270     eType = svExternalName;
271     nRefCnt = 0;
272 
273     extname.nFileId = nFileId;
274 
275     xub_StrLen n = rName.Len();
276     memcpy(extname.cName, rName.GetBuffer(), n*sizeof(sal_Unicode));
277     extname.cName[n] = 0;
278 }
279 
280 //UNUSED2008-05  void ScRawToken::SetInt(int rVal)
281 //UNUSED2008-05  {
282 //UNUSED2008-05      eOp   = ocPush;
283 //UNUSED2008-05      eType = svDouble;
284 //UNUSED2008-05      nValue = (double)rVal;
285 //UNUSED2008-05      nRefCnt = 0;
286 //UNUSED2008-05
287 //UNUSED2008-05  }
288 //UNUSED2008-05  void ScRawToken::SetMatrix( ScMatrix* p )
289 //UNUSED2008-05  {
290 //UNUSED2008-05      eOp   = ocPush;
291 //UNUSED2008-05      eType = svMatrix;
292 //UNUSED2008-05      pMat  = p;
293 //UNUSED2008-05      nRefCnt = 0;
294 //UNUSED2008-05  }
295 //UNUSED2008-05
296 //UNUSED2008-05  ScComplexRefData& ScRawToken::GetReference()
297 //UNUSED2008-05  {
298 //UNUSED2008-05      DBG_ASSERT( lcl_IsReference( eOp, GetType() ), "GetReference: no Ref" );
299 //UNUSED2008-05      return aRef;
300 //UNUSED2008-05  }
301 //UNUSED2008-05
302 //UNUSED2008-05  void ScRawToken::SetReference( ScComplexRefData& rRef )
303 //UNUSED2008-05  {
304 //UNUSED2008-05      DBG_ASSERT( lcl_IsReference( eOp, GetType() ), "SetReference: no Ref" );
305 //UNUSED2008-05      aRef = rRef;
306 //UNUSED2008-05      if( GetType() == svSingleRef )
307 //UNUSED2008-05          aRef.Ref2 = aRef.Ref1;
308 //UNUSED2008-05  }
309 
SetExternal(const sal_Unicode * pStr)310 void ScRawToken::SetExternal( const sal_Unicode* pStr )
311 {
312     eOp   = ocExternal;
313     eType = svExternal;
314     xub_StrLen nLen = GetStrLen( pStr ) + 1;
315     if( nLen >= MAXSTRLEN )
316         nLen = MAXSTRLEN-1;
317     // Platz fuer Byte-Parameter lassen!
318     memcpy( cStr+1, pStr, GetStrLenBytes( nLen ) );
319     cStr[ nLen+1 ] = 0;
320     nRefCnt = 0;
321 }
322 
lcl_ScRawTokenOffset()323 sal_uInt16 lcl_ScRawTokenOffset()
324 {
325     // offset of sbyte in ScRawToken
326     // offsetof(ScRawToken, sbyte) gives a warning with gcc, because ScRawToken is no POD
327 
328     ScRawToken aToken;
329     return static_cast<sal_uInt16>( reinterpret_cast<char*>(&aToken.sbyte) - reinterpret_cast<char*>(&aToken) );
330 }
331 
Clone() const332 ScRawToken* ScRawToken::Clone() const
333 {
334     ScRawToken* p;
335     if ( eType == svDouble )
336     {
337         p = (ScRawToken*) new ScDoubleRawToken;
338         p->eOp = eOp;
339         p->eType = eType;
340         p->nValue = nValue;
341     }
342     else
343     {
344         static sal_uInt16 nOffset = lcl_ScRawTokenOffset();     // offset of sbyte
345         sal_uInt16 n = nOffset;
346 
347         if (eOp == ocExternalRef)
348         {
349             switch (eType)
350             {
351                 case svExternalSingleRef:
352                 case svExternalDoubleRef: n += sizeof(extref); break;
353                 case svExternalName:      n += sizeof(extname); break;
354                 default:
355                 {
356                     DBG_ERROR1( "unknown ScRawToken::Clone() external type %d", int(eType));
357                 }
358             }
359         }
360         else
361         {
362             switch( eType )
363             {
364                 case svSep:         break;
365                 case svByte:        n += sizeof(ScRawToken::sbyte); break;
366                 case svDouble:      n += sizeof(double); break;
367                 case svString:      n = sal::static_int_cast<sal_uInt16>( n + GetStrLenBytes( cStr ) + GetStrLenBytes( 1 ) ); break;
368                 case svSingleRef:
369                 case svDoubleRef:   n += sizeof(aRef); break;
370                 case svMatrix:      n += sizeof(ScMatrix*); break;
371                 case svIndex:       n += sizeof(sal_uInt16); break;
372                 case svJump:        n += nJump[ 0 ] * 2 + 2; break;
373                 case svExternal:    n = sal::static_int_cast<sal_uInt16>( n + GetStrLenBytes( cStr+1 ) + GetStrLenBytes( 2 ) ); break;
374                 default:
375                 {
376                     DBG_ERROR1( "unknown ScRawToken::Clone() type %d", int(eType));
377                 }
378             }
379         }
380         p = (ScRawToken*) new sal_uInt8[ n ];
381         memcpy( p, this, n * sizeof(sal_uInt8) );
382     }
383     p->nRefCnt = 0;
384     p->bRaw = sal_False;
385     return p;
386 }
387 
388 
NewString(const sal_Unicode * pStr,xub_StrLen nLen)389 ScRawToken* ScRawToken::NewString( const sal_Unicode* pStr, xub_StrLen nLen )
390 {
391     static sal_uInt16 nOffset = lcl_ScRawTokenOffset();     // offset of sbyte and cStr
392     size_t n = nOffset + GetStrLenBytes( nLen ) + GetStrLenBytes( 1 );
393     ScRawToken* p = (ScRawToken*) new sal_uInt8[ n ];
394     p->eOp = ocPush;
395     p->eType = svString;
396     p->nRefCnt = 0;
397     p->bRaw = sal_False;
398     memcpy( p->cStr, pStr, GetStrLenBytes( nLen ) );
399     p->cStr[ nLen ] = 0;
400     return p;
401 }
402 
403 
CreateToken() const404 FormulaToken* ScRawToken::CreateToken() const
405 {
406 #ifdef DBG_UTIL
407 #define IF_NOT_OPCODE_ERROR(o,c) if (eOp!=o) DBG_ERROR1( #c "::ctor: OpCode %d lost, converted to " #o "; maybe inherit from FormulaToken instead!", int(eOp))
408 #else
409 #define IF_NOT_OPCODE_ERROR(o,c)
410 #endif
411     switch ( GetType() )
412     {
413         case svByte :
414             return new FormulaByteToken( eOp, sbyte.cByte, sbyte.bHasForceArray );
415         case svDouble :
416             IF_NOT_OPCODE_ERROR( ocPush, FormulaDoubleToken);
417             return new FormulaDoubleToken( nValue );
418         case svString :
419             if (eOp == ocPush)
420                 return new FormulaStringToken( String( cStr ) );
421             else
422                 return new FormulaStringOpToken( eOp, String( cStr ) );
423         case svSingleRef :
424             if (eOp == ocPush)
425                 return new ScSingleRefToken( aRef.Ref1 );
426             else
427                 return new ScSingleRefToken( aRef.Ref1, eOp );
428         case svDoubleRef :
429             if (eOp == ocPush)
430                 return new ScDoubleRefToken( aRef );
431             else
432                 return new ScDoubleRefToken( aRef, eOp );
433         case svMatrix :
434             IF_NOT_OPCODE_ERROR( ocPush, ScMatrixToken);
435             return new ScMatrixToken( pMat );
436         case svIndex :
437             return new FormulaIndexToken( eOp, nIndex );
438         case svExternalSingleRef:
439             {
440                 String aTabName(extref.cTabName);
441                 return new ScExternalSingleRefToken(extref.nFileId, aTabName, extref.aRef.Ref1);
442             }
443         case svExternalDoubleRef:
444             {
445                 String aTabName(extref.cTabName);
446                 return new ScExternalDoubleRefToken(extref.nFileId, aTabName, extref.aRef);
447             }
448         case svExternalName:
449             {
450                 String aName(extname.cName);
451                 return new ScExternalNameToken( extname.nFileId, aName );
452             }
453         case svJump :
454             return new FormulaJumpToken( eOp, (short*) nJump );
455         case svExternal :
456             return new FormulaExternalToken( eOp, sbyte.cByte, String( cStr+1 ) );
457         case svFAP :
458             return new FormulaFAPToken( eOp, sbyte.cByte, NULL );
459         case svMissing :
460             IF_NOT_OPCODE_ERROR( ocMissing, FormulaMissingToken);
461             return new FormulaMissingToken;
462         case svSep :
463             return new FormulaToken( svSep,eOp );
464         case svUnknown :
465             return new FormulaUnknownToken( eOp );
466         default:
467             {
468                 DBG_ERROR1( "unknown ScRawToken::CreateToken() type %d", int(GetType()));
469                 return new FormulaUnknownToken( ocBad );
470             }
471     }
472 #undef IF_NOT_OPCODE_ERROR
473 }
474 
475 
Delete()476 void ScRawToken::Delete()
477 {
478     if ( bRaw )
479         delete this;                            // FixedMemPool ScRawToken
480     else
481     {   // created per Clone
482         switch ( eType )
483         {
484             case svDouble :
485                 delete (ScDoubleRawToken*) this;    // FixedMemPool ScDoubleRawToken
486             break;
487             default:
488                 delete [] (sal_uInt8*) this;
489         }
490     }
491 }
492 
493 
494 // --- class ScToken --------------------------------------------------------
495 
lcl_ScToken_InitSingleRef()496 ScSingleRefData lcl_ScToken_InitSingleRef()
497 {
498     ScSingleRefData aRef;
499     aRef.InitAddress( ScAddress() );
500     return aRef;
501 }
502 
lcl_ScToken_InitDoubleRef()503 ScComplexRefData lcl_ScToken_InitDoubleRef()
504 {
505     ScComplexRefData aRef;
506     aRef.Ref1 = lcl_ScToken_InitSingleRef();
507     aRef.Ref2 = aRef.Ref1;
508     return aRef;
509 }
510 
~ScToken()511 ScToken::~ScToken()
512 {
513 }
514 
515 //  TextEqual: if same formula entered (for optimization in sort)
TextEqual(const FormulaToken & _rToken) const516 sal_Bool ScToken::TextEqual( const FormulaToken& _rToken ) const
517 {
518     if ( eType == svSingleRef || eType == svDoubleRef )
519     {
520         //  in relative Refs only compare relative parts
521 
522         if ( eType != _rToken.GetType() || GetOpCode() != _rToken.GetOpCode() )
523             return sal_False;
524 
525         const ScToken& rToken = static_cast<const ScToken&>(_rToken);
526         ScComplexRefData aTemp1;
527         if ( eType == svSingleRef )
528         {
529             aTemp1.Ref1 = GetSingleRef();
530             aTemp1.Ref2 = aTemp1.Ref1;
531         }
532         else
533             aTemp1 = GetDoubleRef();
534 
535         ScComplexRefData aTemp2;
536         if ( rToken.eType == svSingleRef )
537         {
538             aTemp2.Ref1 = rToken.GetSingleRef();
539             aTemp2.Ref2 = aTemp2.Ref1;
540         }
541         else
542             aTemp2 = rToken.GetDoubleRef();
543 
544         ScAddress aPos;
545         aTemp1.SmartRelAbs(aPos);
546         aTemp2.SmartRelAbs(aPos);
547 
548         //  memcmp doesn't work because of the alignment byte after bFlags.
549         //  After SmartRelAbs only absolute parts have to be compared.
550         return aTemp1.Ref1.nCol   == aTemp2.Ref1.nCol   &&
551                aTemp1.Ref1.nRow   == aTemp2.Ref1.nRow   &&
552                aTemp1.Ref1.nTab   == aTemp2.Ref1.nTab   &&
553                aTemp1.Ref1.bFlags == aTemp2.Ref1.bFlags &&
554                aTemp1.Ref2.nCol   == aTemp2.Ref2.nCol   &&
555                aTemp1.Ref2.nRow   == aTemp2.Ref2.nRow   &&
556                aTemp1.Ref2.nTab   == aTemp2.Ref2.nTab   &&
557                aTemp1.Ref2.bFlags == aTemp2.Ref2.bFlags;
558     }
559     else
560         return *this == _rToken;     // else normal operator==
561 }
562 
563 
Is3DRef() const564 sal_Bool ScToken::Is3DRef() const
565 {
566     switch ( eType )
567     {
568         case svDoubleRef :
569             if ( GetSingleRef2().IsFlag3D() )
570                 return sal_True;
571         //! fallthru
572         case svSingleRef :
573             if ( GetSingleRef().IsFlag3D() )
574                 return sal_True;
575             break;
576         case svExternalSingleRef:
577         case svExternalDoubleRef:
578             return sal_True;
579         default:
580         {
581             // added to avoid warnings
582         }
583     }
584     return sal_False;
585 }
586 
587 // static
ExtendRangeReference(FormulaToken & rTok1,FormulaToken & rTok2,const ScAddress & rPos,bool bReuseDoubleRef)588 FormulaTokenRef ScToken::ExtendRangeReference( FormulaToken & rTok1, FormulaToken & rTok2,
589         const ScAddress & rPos, bool bReuseDoubleRef )
590 {
591 
592     StackVar sv1, sv2;
593     // Doing a RangeOp with RefList is probably utter nonsense, but Xcl
594     // supports it, so do we.
595     if (((sv1 = rTok1.GetType()) != svSingleRef && sv1 != svDoubleRef && sv1 != svRefList &&
596             sv1 != svExternalSingleRef && sv1 != svExternalDoubleRef ) ||
597         ((sv2 = rTok2.GetType()) != svSingleRef && sv2 != svDoubleRef && sv2 != svRefList))
598         return NULL;
599 
600     ScToken *p1 = static_cast<ScToken*>(&rTok1);
601     ScToken *p2 = static_cast<ScToken*>(&rTok2);
602 
603     ScTokenRef xRes;
604     bool bExternal = (sv1 == svExternalSingleRef);
605     if ((sv1 == svSingleRef || bExternal) && sv2 == svSingleRef)
606     {
607         // Range references like Sheet1.A1:A2 are generalized and built by
608         // first creating a DoubleRef from the first SingleRef, effectively
609         // generating Sheet1.A1:A1, and then extending that with A2 as if
610         // Sheet1.A1:A1:A2 was encountered, so the mechanisms to adjust the
611         // references apply as well.
612 
613         /* Given the current structure of external references an external
614          * reference can only be extended if the second reference does not
615          * point to a different sheet. 'file'#Sheet1.A1:A2 is ok,
616          * 'file'#Sheet1.A1:Sheet2.A2 is not. Since we can't determine from a
617          * svSingleRef whether the sheet would be different from the one given
618          * in the external reference, we have to bail out if there is any sheet
619          * specified. NOTE: Xcl does handle external 3D references as in
620          * '[file]Sheet1:Sheet2'!A1:A2
621          *
622          * FIXME: For OOo syntax be smart and remember an external singleref
623          * encountered and if followed by ocRange and singleref, create an
624          * external singleref for the second singleref. Both could then be
625          * merged here. For Xcl syntax already parse an external range
626          * reference entirely, cumbersome. */
627 
628         const ScSingleRefData& rRef2 = p2->GetSingleRef();
629         if (bExternal && rRef2.IsFlag3D())
630             return NULL;
631 
632         ScComplexRefData aRef;
633         aRef.Ref1 = aRef.Ref2 = p1->GetSingleRef();
634         aRef.Ref2.SetFlag3D( false);
635         aRef.Extend( rRef2, rPos);
636         if (bExternal)
637             xRes = new ScExternalDoubleRefToken( p1->GetIndex(), p1->GetString(), aRef);
638         else
639             xRes = new ScDoubleRefToken( aRef);
640     }
641     else
642     {
643         bExternal |= (sv1 == svExternalDoubleRef);
644         const ScRefList* pRefList = NULL;
645         if (sv1 == svDoubleRef)
646         {
647             xRes = (bReuseDoubleRef && p1->GetRef() == 1 ? p1 : static_cast<ScToken*>(p1->Clone()));
648             sv1 = svUnknown;    // mark as handled
649         }
650         else if (sv2 == svDoubleRef)
651         {
652             xRes = (bReuseDoubleRef && p2->GetRef() == 1 ? p2 : static_cast<ScToken*>(p2->Clone()));
653             sv2 = svUnknown;    // mark as handled
654         }
655         else if (sv1 == svRefList)
656             pRefList = p1->GetRefList();
657         else if (sv2 == svRefList)
658             pRefList = p2->GetRefList();
659         if (pRefList)
660         {
661             if (!pRefList->size())
662                 return NULL;
663             if (bExternal)
664                 return NULL;    // external reference list not possible
665             xRes = new ScDoubleRefToken( (*pRefList)[0] );
666         }
667         if (!xRes)
668             return NULL;    // shouldn't happen..
669         StackVar sv[2] = { sv1, sv2 };
670         ScToken* pt[2] = { p1, p2 };
671         ScComplexRefData& rRef = xRes->GetDoubleRef();
672         for (size_t i=0; i<2; ++i)
673         {
674             switch (sv[i])
675             {
676                 case svSingleRef:
677                     rRef.Extend( pt[i]->GetSingleRef(), rPos);
678                     break;
679                 case svDoubleRef:
680                     rRef.Extend( pt[i]->GetDoubleRef(), rPos);
681                     break;
682                 case svRefList:
683                     {
684                         const ScRefList* p = pt[i]->GetRefList();
685                         if (!p->size())
686                             return NULL;
687                         ScRefList::const_iterator it( p->begin());
688                         ScRefList::const_iterator end( p->end());
689                         for ( ; it != end; ++it)
690                         {
691                             rRef.Extend( *it, rPos);
692                         }
693                     }
694                     break;
695                 case svExternalSingleRef:
696                     if (rRef.Ref1.IsFlag3D() || rRef.Ref2.IsFlag3D())
697                         return NULL;    // no other sheets with external refs
698                     else
699                         rRef.Extend( pt[i]->GetSingleRef(), rPos);
700                     break;
701                 case svExternalDoubleRef:
702                     if (rRef.Ref1.IsFlag3D() || rRef.Ref2.IsFlag3D())
703                         return NULL;    // no other sheets with external refs
704                     else
705                         rRef.Extend( pt[i]->GetDoubleRef(), rPos);
706                     break;
707                 default:
708                     ;   // nothing, prevent compiler warning
709             }
710         }
711     }
712     return FormulaTokenRef(xRes.get());
713 }
714 
GetSingleRef() const715 const ScSingleRefData& ScToken::GetSingleRef() const
716 {
717     DBG_ERRORFILE( "ScToken::GetSingleRef: virtual dummy called" );
718     static ScSingleRefData aDummySingleRef = lcl_ScToken_InitSingleRef();
719     return aDummySingleRef;
720 }
721 
GetSingleRef()722 ScSingleRefData& ScToken::GetSingleRef()
723 {
724     DBG_ERRORFILE( "ScToken::GetSingleRef: virtual dummy called" );
725     static ScSingleRefData aDummySingleRef = lcl_ScToken_InitSingleRef();
726     return aDummySingleRef;
727 }
728 
GetDoubleRef() const729 const ScComplexRefData& ScToken::GetDoubleRef() const
730 {
731     DBG_ERRORFILE( "ScToken::GetDoubleRef: virtual dummy called" );
732     static ScComplexRefData aDummyDoubleRef = lcl_ScToken_InitDoubleRef();
733     return aDummyDoubleRef;
734 }
735 
GetDoubleRef()736 ScComplexRefData& ScToken::GetDoubleRef()
737 {
738     DBG_ERRORFILE( "ScToken::GetDoubleRef: virtual dummy called" );
739     static ScComplexRefData aDummyDoubleRef = lcl_ScToken_InitDoubleRef();
740     return aDummyDoubleRef;
741 }
742 
GetSingleRef2() const743 const ScSingleRefData& ScToken::GetSingleRef2() const
744 {
745     DBG_ERRORFILE( "ScToken::GetSingleRef2: virtual dummy called" );
746     static ScSingleRefData aDummySingleRef = lcl_ScToken_InitSingleRef();
747     return aDummySingleRef;
748 }
749 
GetSingleRef2()750 ScSingleRefData& ScToken::GetSingleRef2()
751 {
752     DBG_ERRORFILE( "ScToken::GetSingleRef2: virtual dummy called" );
753     static ScSingleRefData aDummySingleRef = lcl_ScToken_InitSingleRef();
754     return aDummySingleRef;
755 }
756 
CalcAbsIfRel(const ScAddress &)757 void ScToken::CalcAbsIfRel( const ScAddress& /* rPos */ )
758 {
759     DBG_ERRORFILE( "ScToken::CalcAbsIfRel: virtual dummy called" );
760 }
761 
CalcRelFromAbs(const ScAddress &)762 void ScToken::CalcRelFromAbs( const ScAddress& /* rPos */ )
763 {
764     DBG_ERRORFILE( "ScToken::CalcRelFromAbs: virtual dummy called" );
765 }
766 
GetMatrix() const767 const ScMatrix* ScToken::GetMatrix() const
768 {
769     DBG_ERRORFILE( "ScToken::GetMatrix: virtual dummy called" );
770     return NULL;
771 }
772 
GetMatrix()773 ScMatrix* ScToken::GetMatrix()
774 {
775     DBG_ERRORFILE( "ScToken::GetMatrix: virtual dummy called" );
776     return NULL;
777 }
778 
779 
GetJumpMatrix() const780 ScJumpMatrix* ScToken::GetJumpMatrix() const
781 {
782     DBG_ERRORFILE( "ScToken::GetJumpMatrix: virtual dummy called" );
783     return NULL;
784 }
GetRefList() const785 const ScRefList* ScToken::GetRefList() const
786 {
787     DBG_ERRORFILE( "ScToken::GetRefList: virtual dummy called" );
788     return NULL;
789 }
790 
GetRefList()791 ScRefList* ScToken::GetRefList()
792 {
793     DBG_ERRORFILE( "ScToken::GetRefList: virtual dummy called" );
794     return NULL;
795 }
796 // ==========================================================================
797 // real implementations of virtual functions
798 // --------------------------------------------------------------------------
799 
800 
801 
802 
GetSingleRef() const803 const ScSingleRefData&    ScSingleRefToken::GetSingleRef() const  { return aSingleRef; }
GetSingleRef()804 ScSingleRefData&          ScSingleRefToken::GetSingleRef()        { return aSingleRef; }
CalcAbsIfRel(const ScAddress & rPos)805 void                    ScSingleRefToken::CalcAbsIfRel( const ScAddress& rPos )
806                             { aSingleRef.CalcAbsIfRel( rPos ); }
CalcRelFromAbs(const ScAddress & rPos)807 void                    ScSingleRefToken::CalcRelFromAbs( const ScAddress& rPos )
808                             { aSingleRef.CalcRelFromAbs( rPos ); }
operator ==(const FormulaToken & r) const809 sal_Bool ScSingleRefToken::operator==( const FormulaToken& r ) const
810 {
811     return FormulaToken::operator==( r ) && aSingleRef == static_cast<const ScToken&>(r).GetSingleRef();
812 }
813 
814 
GetSingleRef() const815 const ScSingleRefData&    ScDoubleRefToken::GetSingleRef() const  { return aDoubleRef.Ref1; }
GetSingleRef()816 ScSingleRefData&          ScDoubleRefToken::GetSingleRef()        { return aDoubleRef.Ref1; }
GetDoubleRef() const817 const ScComplexRefData&     ScDoubleRefToken::GetDoubleRef() const  { return aDoubleRef; }
GetDoubleRef()818 ScComplexRefData&           ScDoubleRefToken::GetDoubleRef()        { return aDoubleRef; }
GetSingleRef2() const819 const ScSingleRefData&    ScDoubleRefToken::GetSingleRef2() const { return aDoubleRef.Ref2; }
GetSingleRef2()820 ScSingleRefData&          ScDoubleRefToken::GetSingleRef2()       { return aDoubleRef.Ref2; }
CalcAbsIfRel(const ScAddress & rPos)821 void                    ScDoubleRefToken::CalcAbsIfRel( const ScAddress& rPos )
822                             { aDoubleRef.CalcAbsIfRel( rPos ); }
CalcRelFromAbs(const ScAddress & rPos)823 void                    ScDoubleRefToken::CalcRelFromAbs( const ScAddress& rPos )
824                             { aDoubleRef.CalcRelFromAbs( rPos ); }
operator ==(const FormulaToken & r) const825 sal_Bool ScDoubleRefToken::operator==( const FormulaToken& r ) const
826 {
827     return FormulaToken::operator==( r ) && aDoubleRef == static_cast<const ScToken&>(r).GetDoubleRef();
828 }
829 
830 
GetRefList() const831 const ScRefList*        ScRefListToken::GetRefList() const  { return &aRefList; }
GetRefList()832       ScRefList*        ScRefListToken::GetRefList()        { return &aRefList; }
CalcAbsIfRel(const ScAddress & rPos)833 void                    ScRefListToken::CalcAbsIfRel( const ScAddress& rPos )
834 {
835     for (ScRefList::iterator it( aRefList.begin()); it != aRefList.end(); ++it)
836         (*it).CalcAbsIfRel( rPos);
837 }
CalcRelFromAbs(const ScAddress & rPos)838 void                    ScRefListToken::CalcRelFromAbs( const ScAddress& rPos )
839 {
840     for (ScRefList::iterator it( aRefList.begin()); it != aRefList.end(); ++it)
841         (*it).CalcRelFromAbs( rPos);
842 }
operator ==(const FormulaToken & r) const843 sal_Bool ScRefListToken::operator==( const FormulaToken& r ) const
844 {
845     return FormulaToken::operator==( r ) && &aRefList == static_cast<const ScToken&>(r).GetRefList();
846 }
847 
848 
GetMatrix() const849 const ScMatrix* ScMatrixToken::GetMatrix() const        { return pMatrix; }
GetMatrix()850 ScMatrix*       ScMatrixToken::GetMatrix()              { return pMatrix; }
operator ==(const FormulaToken & r) const851 sal_Bool ScMatrixToken::operator==( const FormulaToken& r ) const
852 {
853     return FormulaToken::operator==( r ) && pMatrix == static_cast<const ScToken&>(r).GetMatrix();
854 }
855 
856 // ============================================================================
857 
ScExternalSingleRefToken(sal_uInt16 nFileId,const String & rTabName,const ScSingleRefData & r)858 ScExternalSingleRefToken::ScExternalSingleRefToken( sal_uInt16 nFileId, const String& rTabName, const ScSingleRefData& r ) :
859     ScToken( svExternalSingleRef, ocExternalRef),
860     mnFileId(nFileId),
861     maTabName(rTabName),
862     maSingleRef(r)
863 {
864 }
865 
ScExternalSingleRefToken(const ScExternalSingleRefToken & r)866 ScExternalSingleRefToken::ScExternalSingleRefToken( const ScExternalSingleRefToken& r ) :
867     ScToken(r),
868     mnFileId(r.mnFileId),
869     maTabName(r.maTabName),
870     maSingleRef(r.maSingleRef)
871 {
872 }
873 
~ScExternalSingleRefToken()874 ScExternalSingleRefToken::~ScExternalSingleRefToken()
875 {
876 }
877 
GetIndex() const878 sal_uInt16 ScExternalSingleRefToken::GetIndex() const
879 {
880     return mnFileId;
881 }
882 
GetString() const883 const String& ScExternalSingleRefToken::GetString() const
884 {
885     return maTabName;
886 }
887 
GetSingleRef() const888 const ScSingleRefData& ScExternalSingleRefToken::GetSingleRef() const
889 {
890     return maSingleRef;
891 }
892 
GetSingleRef()893 ScSingleRefData& ScExternalSingleRefToken::GetSingleRef()
894 {
895     return maSingleRef;
896 }
897 
CalcAbsIfRel(const ScAddress & rPos)898 void ScExternalSingleRefToken::CalcAbsIfRel( const ScAddress& rPos )
899 {
900     maSingleRef.CalcAbsIfRel( rPos );
901 }
902 
CalcRelFromAbs(const ScAddress & rPos)903 void ScExternalSingleRefToken::CalcRelFromAbs( const ScAddress& rPos )
904 {
905     maSingleRef.CalcRelFromAbs( rPos );
906 }
907 
operator ==(const FormulaToken & r) const908 sal_Bool ScExternalSingleRefToken::operator ==( const FormulaToken& r ) const
909 {
910     if (!FormulaToken::operator==(r))
911         return false;
912 
913     if (mnFileId != r.GetIndex())
914         return false;
915 
916     if (maTabName != r.GetString())
917         return false;
918 
919     return maSingleRef == static_cast<const ScToken&>(r).GetSingleRef();
920 }
921 
922 // ============================================================================
923 
ScExternalDoubleRefToken(sal_uInt16 nFileId,const String & rTabName,const ScComplexRefData & r)924 ScExternalDoubleRefToken::ScExternalDoubleRefToken( sal_uInt16 nFileId, const String& rTabName, const ScComplexRefData& r ) :
925     ScToken( svExternalDoubleRef, ocExternalRef),
926     mnFileId(nFileId),
927     maTabName(rTabName),
928     maDoubleRef(r)
929 {
930 }
931 
ScExternalDoubleRefToken(const ScExternalDoubleRefToken & r)932 ScExternalDoubleRefToken::ScExternalDoubleRefToken( const ScExternalDoubleRefToken& r ) :
933     ScToken(r),
934     mnFileId(r.mnFileId),
935     maTabName(r.maTabName),
936     maDoubleRef(r.maDoubleRef)
937 {
938 }
939 
ScExternalDoubleRefToken(const ScExternalSingleRefToken & r)940 ScExternalDoubleRefToken::ScExternalDoubleRefToken( const ScExternalSingleRefToken& r ) :
941     ScToken( svExternalDoubleRef, ocExternalRef),
942     mnFileId( r.GetIndex()),
943     maTabName( r.GetString())
944 {
945     maDoubleRef.Ref1 = maDoubleRef.Ref2 = r.GetSingleRef();
946 }
947 
~ScExternalDoubleRefToken()948 ScExternalDoubleRefToken::~ScExternalDoubleRefToken()
949 {
950 }
951 
GetIndex() const952 sal_uInt16 ScExternalDoubleRefToken::GetIndex() const
953 {
954     return mnFileId;
955 }
956 
GetString() const957 const String& ScExternalDoubleRefToken::GetString() const
958 {
959     return maTabName;
960 }
961 
GetSingleRef() const962 const ScSingleRefData& ScExternalDoubleRefToken::GetSingleRef() const
963 {
964     return maDoubleRef.Ref1;
965 }
966 
GetSingleRef()967 ScSingleRefData& ScExternalDoubleRefToken::GetSingleRef()
968 {
969     return maDoubleRef.Ref1;
970 }
971 
GetSingleRef2() const972 const ScSingleRefData& ScExternalDoubleRefToken::GetSingleRef2() const
973 {
974     return maDoubleRef.Ref2;
975 }
976 
GetSingleRef2()977 ScSingleRefData& ScExternalDoubleRefToken::GetSingleRef2()
978 {
979     return maDoubleRef.Ref2;
980 }
981 
GetDoubleRef() const982 const ScComplexRefData& ScExternalDoubleRefToken::GetDoubleRef() const
983 {
984     return maDoubleRef;
985 }
986 
GetDoubleRef()987 ScComplexRefData& ScExternalDoubleRefToken::GetDoubleRef()
988 {
989     return maDoubleRef;
990 }
991 
CalcAbsIfRel(const ScAddress & rPos)992 void ScExternalDoubleRefToken::CalcAbsIfRel( const ScAddress& rPos )
993 {
994     maDoubleRef.CalcAbsIfRel( rPos );
995 }
996 
CalcRelFromAbs(const ScAddress & rPos)997 void ScExternalDoubleRefToken::CalcRelFromAbs( const ScAddress& rPos )
998 {
999     maDoubleRef.CalcRelFromAbs( rPos );
1000 }
1001 
operator ==(const FormulaToken & r) const1002 sal_Bool ScExternalDoubleRefToken::operator ==( const FormulaToken& r ) const
1003 {
1004     if (!ScToken::operator==(r))
1005         return false;
1006 
1007     if (mnFileId != r.GetIndex())
1008         return false;
1009 
1010     if (maTabName != r.GetString())
1011         return false;
1012 
1013     return maDoubleRef == static_cast<const ScToken&>(r).GetDoubleRef();
1014 }
1015 
1016 // ============================================================================
1017 
ScExternalNameToken(sal_uInt16 nFileId,const String & rName)1018 ScExternalNameToken::ScExternalNameToken( sal_uInt16 nFileId, const String& rName ) :
1019     ScToken( svExternalName, ocExternalRef),
1020     mnFileId(nFileId),
1021     maName(rName)
1022 {
1023 }
1024 
ScExternalNameToken(const ScExternalNameToken & r)1025 ScExternalNameToken::ScExternalNameToken( const ScExternalNameToken& r ) :
1026     ScToken(r),
1027     mnFileId(r.mnFileId),
1028     maName(r.maName)
1029 {
1030 }
1031 
~ScExternalNameToken()1032 ScExternalNameToken::~ScExternalNameToken() {}
1033 
GetIndex() const1034 sal_uInt16 ScExternalNameToken::GetIndex() const
1035 {
1036     return mnFileId;
1037 }
1038 
GetString() const1039 const String& ScExternalNameToken::GetString() const
1040 {
1041     return maName;
1042 }
1043 
operator ==(const FormulaToken & r) const1044 sal_Bool ScExternalNameToken::operator==( const FormulaToken& r ) const
1045 {
1046     if ( !FormulaToken::operator==(r) )
1047         return false;
1048 
1049     if (mnFileId != r.GetIndex())
1050         return false;
1051 
1052     xub_StrLen nLen = maName.Len();
1053     const String& rName = r.GetString();
1054     if (nLen != rName.Len())
1055         return false;
1056 
1057     const sal_Unicode* p1 = maName.GetBuffer();
1058     const sal_Unicode* p2 = rName.GetBuffer();
1059     for (xub_StrLen j = 0; j < nLen; ++j)
1060     {
1061         if (p1[j] != p2[j])
1062             return false;
1063     }
1064     return true;
1065 }
1066 
1067 // ============================================================================
1068 
GetJumpMatrix() const1069 ScJumpMatrix* ScJumpMatrixToken::GetJumpMatrix() const  { return pJumpMatrix; }
operator ==(const FormulaToken & r) const1070 sal_Bool ScJumpMatrixToken::operator==( const FormulaToken& r ) const
1071 {
1072     return FormulaToken::operator==( r ) && pJumpMatrix == static_cast<const ScToken&>(r).GetJumpMatrix();
1073 }
~ScJumpMatrixToken()1074 ScJumpMatrixToken::~ScJumpMatrixToken()
1075 {
1076     delete pJumpMatrix;
1077 }
1078 
GetDouble() const1079 double          ScEmptyCellToken::GetDouble() const     { return 0.0; }
GetString() const1080 const String &  ScEmptyCellToken::GetString() const
1081 {
1082     static  String              aDummyString;
1083     return aDummyString;
1084 }
operator ==(const FormulaToken & r) const1085 sal_Bool ScEmptyCellToken::operator==( const FormulaToken& r ) const
1086 {
1087     return FormulaToken::operator==( r ) &&
1088         bInherited == static_cast< const ScEmptyCellToken & >(r).IsInherited() &&
1089         bDisplayedAsString == static_cast< const ScEmptyCellToken & >(r).IsDisplayedAsString();
1090 }
1091 
1092 
GetDouble() const1093 double          ScMatrixCellResultToken::GetDouble() const  { return xUpperLeft->GetDouble(); }
GetString() const1094 const String &  ScMatrixCellResultToken::GetString() const  { return xUpperLeft->GetString(); }
GetMatrix() const1095 const ScMatrix* ScMatrixCellResultToken::GetMatrix() const  { return xMatrix; }
1096 // Non-const GetMatrix() is private and unused but must be implemented to
1097 // satisfy vtable linkage.
GetMatrix()1098 ScMatrix* ScMatrixCellResultToken::GetMatrix()
1099 {
1100     return const_cast<ScMatrix*>(xMatrix.operator->());
1101 }
operator ==(const FormulaToken & r) const1102 sal_Bool ScMatrixCellResultToken::operator==( const FormulaToken& r ) const
1103 {
1104     return FormulaToken::operator==( r ) &&
1105         xUpperLeft == static_cast<const ScMatrixCellResultToken &>(r).xUpperLeft &&
1106         xMatrix == static_cast<const ScMatrixCellResultToken &>(r).xMatrix;
1107 }
1108 
1109 
operator ==(const FormulaToken & r) const1110 sal_Bool ScMatrixFormulaCellToken::operator==( const FormulaToken& r ) const
1111 {
1112     const ScMatrixFormulaCellToken* p = dynamic_cast<const ScMatrixFormulaCellToken*>(&r);
1113     return p && ScMatrixCellResultToken::operator==( r ) &&
1114         nCols == p->nCols && nRows == p->nRows;
1115 }
Assign(const formula::FormulaToken & r)1116 void ScMatrixFormulaCellToken::Assign( const formula::FormulaToken& r )
1117 {
1118     if (this == &r)
1119         return;
1120     const ScMatrixCellResultToken* p = dynamic_cast<const ScMatrixCellResultToken*>(&r);
1121     if (p)
1122         ScMatrixCellResultToken::Assign( *p);
1123     else
1124     {
1125         DBG_ASSERT( r.GetType() != svMatrix, "ScMatrixFormulaCellToken::operator=: assigning ScMatrixToken to ScMatrixFormulaCellToken is not proper, use ScMatrixCellResultToken instead");
1126         if (r.GetType() == svMatrix)
1127         {
1128             xUpperLeft = NULL;
1129             xMatrix = static_cast<const ScToken&>(r).GetMatrix();
1130         }
1131         else
1132         {
1133             xUpperLeft = &r;
1134             xMatrix = NULL;
1135         }
1136     }
1137 }
SetUpperLeftDouble(double f)1138 void ScMatrixFormulaCellToken::SetUpperLeftDouble( double f )
1139 {
1140     switch (GetUpperLeftType())
1141     {
1142         case svDouble:
1143             const_cast<FormulaToken*>(xUpperLeft.get())->GetDoubleAsReference() = f;
1144             break;
1145         case svUnknown:
1146             if (!xUpperLeft)
1147             {
1148                 xUpperLeft = new FormulaDoubleToken( f);
1149                 break;
1150             }
1151             // fall thru
1152         default:
1153             {
1154                 DBG_ERRORFILE("ScMatrixFormulaCellToken::SetUpperLeftDouble: not modifying unhandled token type");
1155             }
1156     }
1157 }
1158 
1159 
GetDouble() const1160 double          ScHybridCellToken::GetDouble() const    { return fDouble; }
GetString() const1161 const String &  ScHybridCellToken::GetString() const    { return aString; }
operator ==(const FormulaToken & r) const1162 sal_Bool ScHybridCellToken::operator==( const FormulaToken& r ) const
1163 {
1164     return FormulaToken::operator==( r ) &&
1165         fDouble == r.GetDouble() && aString == r.GetString() &&
1166         aFormula == static_cast<const ScHybridCellToken &>(r).GetFormula();
1167 }
1168 
1169 
1170 
1171 
1172 //////////////////////////////////////////////////////////////////////////
1173 
AddFormulaToken(const com::sun::star::sheet::FormulaToken & _aToken,formula::ExternalReferenceHelper * _pRef)1174 bool ScTokenArray::AddFormulaToken(const com::sun::star::sheet::FormulaToken& _aToken,formula::ExternalReferenceHelper* _pRef)
1175 {
1176     bool bError = FormulaTokenArray::AddFormulaToken(_aToken,_pRef);
1177     if ( bError )
1178     {
1179         bError = false;
1180         const OpCode eOpCode = static_cast<OpCode>(_aToken.OpCode);      //! assuming equal values for the moment
1181 
1182         const uno::TypeClass eClass = _aToken.Data.getValueTypeClass();
1183         switch ( eClass )
1184         {
1185             case uno::TypeClass_STRUCT:
1186                 {
1187                     uno::Type aType = _aToken.Data.getValueType();
1188                     if ( aType.equals( cppu::UnoType<sheet::SingleReference>::get() ) )
1189                     {
1190                         ScSingleRefData aSingleRef;
1191                         sheet::SingleReference aApiRef;
1192                         _aToken.Data >>= aApiRef;
1193                         lcl_SingleRefToCalc( aSingleRef, aApiRef );
1194                         if ( eOpCode == ocPush )
1195                             AddSingleReference( aSingleRef );
1196                         else if ( eOpCode == ocColRowName )
1197                             AddColRowName( aSingleRef );
1198                         else
1199                             bError = true;
1200                     }
1201                     else if ( aType.equals( cppu::UnoType<sheet::ComplexReference>::get() ) )
1202                     {
1203                         ScComplexRefData aComplRef;
1204                         sheet::ComplexReference aApiRef;
1205                         _aToken.Data >>= aApiRef;
1206                         lcl_SingleRefToCalc( aComplRef.Ref1, aApiRef.Reference1 );
1207                         lcl_SingleRefToCalc( aComplRef.Ref2, aApiRef.Reference2 );
1208 
1209                         if ( eOpCode == ocPush )
1210                             AddDoubleReference( aComplRef );
1211                         else
1212                             bError = true;
1213                     }
1214                     else if ( aType.equals( cppu::UnoType<sheet::ExternalReference>::get() ) )
1215                     {
1216                         sheet::ExternalReference aApiExtRef;
1217                         if( (eOpCode == ocPush) && (_aToken.Data >>= aApiExtRef) && (0 <= aApiExtRef.Index) && (aApiExtRef.Index <= SAL_MAX_UINT16) )
1218                         {
1219                             sal_uInt16 nFileId = static_cast< sal_uInt16 >( aApiExtRef.Index );
1220                             sheet::SingleReference aApiSRef;
1221                             sheet::ComplexReference aApiCRef;
1222                             ::rtl::OUString aName;
1223                             if( aApiExtRef.Reference >>= aApiSRef )
1224                             {
1225                                 // try to resolve cache index to sheet name
1226                                 size_t nCacheId = static_cast< size_t >( aApiSRef.Sheet );
1227                                 String aTabName = _pRef->getCacheTableName( nFileId, nCacheId );
1228                                 if( aTabName.Len() > 0 )
1229                                 {
1230                                     ScSingleRefData aSingleRef;
1231                                     // convert column/row settings, set sheet index to absolute
1232                                     lcl_ExternalRefToCalc( aSingleRef, aApiSRef );
1233                                     AddExternalSingleReference( nFileId, aTabName, aSingleRef );
1234                                 }
1235                                 else
1236                                     bError = true;
1237                             }
1238                             else if( aApiExtRef.Reference >>= aApiCRef )
1239                             {
1240                                 // try to resolve cache index to sheet name.
1241                                 size_t nCacheId = static_cast< size_t >( aApiCRef.Reference1.Sheet );
1242                                 String aTabName = _pRef->getCacheTableName( nFileId, nCacheId );
1243                                 if( aTabName.Len() > 0 )
1244                                 {
1245                                     ScComplexRefData aComplRef;
1246                                     // convert column/row settings, set sheet index to absolute
1247                                     lcl_ExternalRefToCalc( aComplRef.Ref1, aApiCRef.Reference1 );
1248                                     lcl_ExternalRefToCalc( aComplRef.Ref2, aApiCRef.Reference2 );
1249                                     // NOTE: This assumes that cached sheets are in consecutive order!
1250                                     aComplRef.Ref2.nTab = aComplRef.Ref1.nTab + static_cast<SCsTAB>(aApiCRef.Reference2.Sheet - aApiCRef.Reference1.Sheet);
1251                                     AddExternalDoubleReference( nFileId, aTabName, aComplRef );
1252                                 }
1253                                 else
1254                                     bError = true;
1255                             }
1256                             else if( aApiExtRef.Reference >>= aName )
1257                             {
1258                                 if( aName.getLength() > 0 )
1259                                     AddExternalName( nFileId, aName );
1260                                 else
1261                                     bError = true;
1262                             }
1263                             else
1264                                 bError = true;
1265                         }
1266                         else
1267                             bError = true;
1268                     }
1269                     else
1270                         bError = true;      // unknown struct
1271                 }
1272                 break;
1273             case uno::TypeClass_SEQUENCE:
1274                 {
1275                     if ( eOpCode != ocPush )
1276                         bError = true;      // not an inline array
1277                     else if (!_aToken.Data.getValueType().equals( getCppuType(
1278                                     (uno::Sequence< uno::Sequence< uno::Any > > *)0)))
1279                         bError = true;      // unexpected sequence type
1280                     else
1281                     {
1282                         ScMatrixRef xMat = ScSequenceToMatrix::CreateMixedMatrix( _aToken.Data);
1283                         if (xMat)
1284                             AddMatrix( xMat);
1285                         else
1286                             bError = true;
1287                     }
1288                 }
1289                 break;
1290             default:
1291                 bError = true;
1292         }
1293     }
1294     return bError;
1295 }
ImplGetReference(ScRange & rRange,sal_Bool bValidOnly) const1296 sal_Bool ScTokenArray::ImplGetReference( ScRange& rRange, sal_Bool bValidOnly ) const
1297 {
1298     sal_Bool bIs = sal_False;
1299     if ( pCode && nLen == 1 )
1300     {
1301         const FormulaToken* pToken = pCode[0];
1302         if ( pToken )
1303         {
1304             if ( pToken->GetType() == svSingleRef )
1305             {
1306                 const ScSingleRefData& rRef = ((const ScSingleRefToken*)pToken)->GetSingleRef();
1307                 rRange.aStart = rRange.aEnd = ScAddress( rRef.nCol, rRef.nRow, rRef.nTab );
1308                 bIs = !bValidOnly || !rRef.IsDeleted();
1309             }
1310             else if ( pToken->GetType() == svDoubleRef )
1311             {
1312                 const ScComplexRefData& rCompl = ((const ScDoubleRefToken*)pToken)->GetDoubleRef();
1313                 const ScSingleRefData& rRef1 = rCompl.Ref1;
1314                 const ScSingleRefData& rRef2 = rCompl.Ref2;
1315                 rRange.aStart = ScAddress( rRef1.nCol, rRef1.nRow, rRef1.nTab );
1316                 rRange.aEnd   = ScAddress( rRef2.nCol, rRef2.nRow, rRef2.nTab );
1317                 bIs = !bValidOnly || (!rRef1.IsDeleted() && !rRef2.IsDeleted());
1318             }
1319         }
1320     }
1321     return bIs;
1322 }
1323 
IsReference(ScRange & rRange) const1324 sal_Bool ScTokenArray::IsReference( ScRange& rRange ) const
1325 {
1326     return ImplGetReference( rRange, sal_False );
1327 }
1328 
IsValidReference(ScRange & rRange) const1329 sal_Bool ScTokenArray::IsValidReference( ScRange& rRange ) const
1330 {
1331     return ImplGetReference( rRange, sal_True );
1332 }
1333 
1334 ////////////////////////////////////////////////////////////////////////////
1335 
ScTokenArray()1336 ScTokenArray::ScTokenArray()
1337 {
1338 }
1339 
ScTokenArray(const ScTokenArray & rArr)1340 ScTokenArray::ScTokenArray( const ScTokenArray& rArr ) : FormulaTokenArray(rArr)
1341 {
1342 }
1343 
~ScTokenArray()1344 ScTokenArray::~ScTokenArray()
1345 {
1346 }
1347 
1348 
1349 
operator =(const ScTokenArray & rArr)1350 ScTokenArray& ScTokenArray::operator=( const ScTokenArray& rArr )
1351 {
1352     Clear();
1353     Assign( rArr );
1354     return *this;
1355 }
1356 
Clone() const1357 ScTokenArray* ScTokenArray::Clone() const
1358 {
1359     ScTokenArray* p = new ScTokenArray();
1360     p->nLen = nLen;
1361     p->nRPN = nRPN;
1362     p->nRefs = nRefs;
1363     p->nMode = nMode;
1364     p->nError = nError;
1365     p->bHyperLink = bHyperLink;
1366     FormulaToken** pp;
1367     if( nLen )
1368     {
1369         pp = p->pCode = new FormulaToken*[ nLen ];
1370         memcpy( pp, pCode, nLen * sizeof( ScToken* ) );
1371         for( sal_uInt16 i = 0; i < nLen; i++, pp++ )
1372         {
1373             *pp = (*pp)->Clone();
1374             (*pp)->IncRef();
1375         }
1376     }
1377     if( nRPN )
1378     {
1379         pp = p->pRPN = new FormulaToken*[ nRPN ];
1380         memcpy( pp, pRPN, nRPN * sizeof( ScToken* ) );
1381         for( sal_uInt16 i = 0; i < nRPN; i++, pp++ )
1382         {
1383             FormulaToken* t = *pp;
1384             if( t->GetRef() > 1 )
1385             {
1386                 FormulaToken** p2 = pCode;
1387                 sal_uInt16 nIdx = 0xFFFF;
1388                 for( sal_uInt16 j = 0; j < nLen; j++, p2++ )
1389                 {
1390                     if( *p2 == t )
1391                     {
1392                         nIdx = j; break;
1393                     }
1394                 }
1395                 if( nIdx == 0xFFFF )
1396                     *pp = t->Clone();
1397                 else
1398                     *pp = p->pCode[ nIdx ];
1399             }
1400             else
1401                 *pp = t->Clone();
1402             (*pp)->IncRef();
1403         }
1404     }
1405     return p;
1406 }
1407 
AddRawToken(const ScRawToken & r)1408 FormulaToken* ScTokenArray::AddRawToken( const ScRawToken& r )
1409 {
1410     return Add( r.CreateToken() );
1411 }
1412 
1413 // Utility function to ensure that there is strict alternation of values and
1414 // seperators.
1415 static bool
checkArraySep(bool & bPrevWasSep,bool bNewVal)1416 checkArraySep( bool & bPrevWasSep, bool bNewVal )
1417 {
1418     bool bResult = (bPrevWasSep == bNewVal);
1419     bPrevWasSep = bNewVal;
1420     return bResult;
1421 }
1422 
MergeArray()1423 FormulaToken* ScTokenArray::MergeArray( )
1424 {
1425     int nCol = -1, nRow = 0;
1426     int i, nPrevRowSep = -1, nStart = 0;
1427     bool bPrevWasSep = false; // top of stack is ocArrayClose
1428     FormulaToken* t;
1429     bool bNumeric = false;  // numeric value encountered in current element
1430 
1431     // (1) Iterate from the end to the start to find matrix dims
1432     // and do basic validation.
1433     for ( i = nLen ; i-- > nStart ; )
1434     {
1435         t = pCode[i];
1436         switch ( t->GetOpCode() )
1437         {
1438             case ocPush :
1439                 if( checkArraySep( bPrevWasSep, false ) )
1440                 {
1441                     return NULL;
1442                 }
1443 
1444                 // no references or nested arrays
1445                 if ( t->GetType() != svDouble  && t->GetType() != svString )
1446                 {
1447                     return NULL;
1448                 }
1449                 bNumeric = (t->GetType() == svDouble);
1450             break;
1451 
1452             case ocMissing :
1453             case ocTrue :
1454             case ocFalse :
1455                 if( checkArraySep( bPrevWasSep, false ) )
1456                 {
1457                     return NULL;
1458                 }
1459                 bNumeric = false;
1460             break;
1461 
1462             case ocArrayColSep :
1463             case ocSep :
1464                 if( checkArraySep( bPrevWasSep, true ) )
1465                 {
1466                     return NULL;
1467                 }
1468                 bNumeric = false;
1469             break;
1470 
1471             case ocArrayClose :
1472                 // not possible with the , but check just in case
1473                 // something changes in the future
1474                 if( i != (nLen-1))
1475                 {
1476                     return NULL;
1477                 }
1478 
1479                 if( checkArraySep( bPrevWasSep, true ) )
1480                 {
1481                     return NULL;
1482                 }
1483 
1484                 nPrevRowSep = i;
1485                 bNumeric = false;
1486             break;
1487 
1488             case ocArrayOpen :
1489                 nStart = i; // stop iteration
1490                 // fall through to ArrayRowSep
1491 
1492             case ocArrayRowSep :
1493                 if( checkArraySep( bPrevWasSep, true ) )
1494                 {
1495                     return NULL;
1496                 }
1497 
1498                 if( nPrevRowSep < 0 ||              // missing ocArrayClose
1499                     ((nPrevRowSep - i) % 2) == 1)   // no complex elements
1500                 {
1501                     return NULL;
1502                 }
1503 
1504                 if( nCol < 0 )
1505                 {
1506                     nCol = (nPrevRowSep - i) / 2;
1507                 }
1508                 else if( (nPrevRowSep - i)/2 != nCol)   // irregular array
1509                 {
1510                     return NULL;
1511                 }
1512 
1513                 nPrevRowSep = i;
1514                 nRow++;
1515                 bNumeric = false;
1516             break;
1517 
1518             case ocNegSub :
1519             case ocAdd :
1520                 // negation or unary plus must precede numeric value
1521                 if( !bNumeric )
1522                 {
1523                     return NULL;
1524                 }
1525                 --nPrevRowSep;      // shorten this row by 1
1526                 bNumeric = false;   // one level only, no --42
1527             break;
1528 
1529             case ocSpaces :
1530                 // ignore spaces
1531                 --nPrevRowSep;      // shorten this row by 1
1532             break;
1533 
1534             default :
1535                 // no functions or operators
1536                 return NULL;
1537         }
1538     }
1539     if( nCol <= 0 || nRow <= 0 )
1540         return NULL;
1541 
1542     // fprintf (stderr, "Array (cols = %d, rows = %d)\n", nCol, nRow );
1543 
1544     int nSign = 1;
1545     ScMatrix* pArray = new ScMatrix( nCol, nRow );
1546     for ( i = nStart, nCol = 0, nRow = 0 ; i < nLen ; i++ )
1547     {
1548         t = pCode[i];
1549 
1550         switch ( t->GetOpCode() )
1551         {
1552             case ocPush :
1553                 if ( t->GetType() == svDouble )
1554                 {
1555                     pArray->PutDouble( t->GetDouble() * nSign, nCol, nRow );
1556                     nSign = 1;
1557                 }
1558                 else if ( t->GetType() == svString )
1559                 {
1560                     pArray->PutString( t->GetString(), nCol, nRow );
1561                 }
1562             break;
1563 
1564             case ocMissing :
1565                 pArray->PutEmpty( nCol, nRow );
1566             break;
1567 
1568             case ocTrue :
1569                 pArray->PutBoolean( true, nCol, nRow );
1570             break;
1571 
1572             case ocFalse :
1573                 pArray->PutBoolean( false, nCol, nRow );
1574             break;
1575 
1576             case ocArrayColSep :
1577             case ocSep :
1578                 nCol++;
1579             break;
1580 
1581             case ocArrayRowSep :
1582                 nRow++; nCol = 0;
1583             break;
1584 
1585             case ocNegSub :
1586                 nSign = -nSign;
1587             break;
1588 
1589             default :
1590                 break;
1591         }
1592         pCode[i] = NULL;
1593         t->DecRef();
1594     }
1595     nLen = sal_uInt16( nStart );
1596     return AddMatrix( pArray );
1597 }
1598 
1599 
MergeRangeReference(const ScAddress & rPos)1600 FormulaToken* ScTokenArray::MergeRangeReference( const ScAddress & rPos )
1601 {
1602     if (!pCode || !nLen)
1603         return NULL;
1604     sal_uInt16 nIdx = nLen;
1605     FormulaToken *p1, *p2, *p3;      // ref, ocRange, ref
1606     // The actual types are checked in ExtendRangeReference().
1607     if (((p3 = PeekPrev(nIdx)) != 0) &&
1608             (((p2 = PeekPrev(nIdx)) != 0) && p2->GetOpCode() == ocRange) &&
1609             ((p1 = PeekPrev(nIdx)) != 0))
1610     {
1611         FormulaTokenRef p = ScToken::ExtendRangeReference( *p1, *p3, rPos, true);
1612         if (p)
1613         {
1614             p->IncRef();
1615             p1->DecRef();
1616             p2->DecRef();
1617             p3->DecRef();
1618             nLen -= 2;
1619             pCode[ nLen-1 ] = p;
1620             nRefs--;
1621         }
1622     }
1623     return pCode[ nLen-1 ];
1624 }
1625 
AddOpCode(OpCode e)1626 FormulaToken* ScTokenArray::AddOpCode( OpCode e )
1627 {
1628     ScRawToken t;
1629     t.SetOpCode( e );
1630     return AddRawToken( t );
1631 }
1632 
AddSingleReference(const ScSingleRefData & rRef)1633 FormulaToken* ScTokenArray::AddSingleReference( const ScSingleRefData& rRef )
1634 {
1635     return Add( new ScSingleRefToken( rRef ) );
1636 }
1637 
AddMatrixSingleReference(const ScSingleRefData & rRef)1638 FormulaToken* ScTokenArray::AddMatrixSingleReference( const ScSingleRefData& rRef )
1639 {
1640     return Add( new ScSingleRefToken( rRef, ocMatRef ) );
1641 }
1642 
AddDoubleReference(const ScComplexRefData & rRef)1643 FormulaToken* ScTokenArray::AddDoubleReference( const ScComplexRefData& rRef )
1644 {
1645     return Add( new ScDoubleRefToken( rRef ) );
1646 }
1647 
AddMatrix(ScMatrix * p)1648 FormulaToken* ScTokenArray::AddMatrix( ScMatrix* p )
1649 {
1650     return Add( new ScMatrixToken( p ) );
1651 }
1652 
AddExternalName(sal_uInt16 nFileId,const String & rName)1653 FormulaToken* ScTokenArray::AddExternalName( sal_uInt16 nFileId, const String& rName )
1654 {
1655     return Add( new ScExternalNameToken(nFileId, rName) );
1656 }
1657 
AddExternalSingleReference(sal_uInt16 nFileId,const String & rTabName,const ScSingleRefData & rRef)1658 FormulaToken* ScTokenArray::AddExternalSingleReference( sal_uInt16 nFileId, const String& rTabName, const ScSingleRefData& rRef )
1659 {
1660     return Add( new ScExternalSingleRefToken(nFileId, rTabName, rRef) );
1661 }
1662 
AddExternalDoubleReference(sal_uInt16 nFileId,const String & rTabName,const ScComplexRefData & rRef)1663 FormulaToken* ScTokenArray::AddExternalDoubleReference( sal_uInt16 nFileId, const String& rTabName, const ScComplexRefData& rRef )
1664 {
1665     return Add( new ScExternalDoubleRefToken(nFileId, rTabName, rRef) );
1666 }
1667 
AddColRowName(const ScSingleRefData & rRef)1668 FormulaToken* ScTokenArray::AddColRowName( const ScSingleRefData& rRef )
1669 {
1670     return Add( new ScSingleRefToken( rRef, ocColRowName ) );
1671 }
1672 
GetAdjacentExtendOfOuterFuncRefs(SCCOLROW & nExtend,const ScAddress & rPos,ScDirection eDir)1673 sal_Bool ScTokenArray::GetAdjacentExtendOfOuterFuncRefs( SCCOLROW& nExtend,
1674         const ScAddress& rPos, ScDirection eDir )
1675 {
1676     SCCOL nCol = 0;
1677     SCROW nRow = 0;
1678     switch ( eDir )
1679     {
1680         case DIR_BOTTOM :
1681             if ( rPos.Row() < MAXROW )
1682                 nRow = (nExtend = rPos.Row()) + 1;
1683             else
1684                 return sal_False;
1685         break;
1686         case DIR_RIGHT :
1687             if ( rPos.Col() < MAXCOL )
1688                 nCol = static_cast<SCCOL>(nExtend = rPos.Col()) + 1;
1689             else
1690                 return sal_False;
1691         break;
1692         case DIR_TOP :
1693             if ( rPos.Row() > 0 )
1694                 nRow = (nExtend = rPos.Row()) - 1;
1695             else
1696                 return sal_False;
1697         break;
1698         case DIR_LEFT :
1699             if ( rPos.Col() > 0 )
1700                 nCol = static_cast<SCCOL>(nExtend = rPos.Col()) - 1;
1701             else
1702                 return sal_False;
1703         break;
1704         default:
1705             DBG_ERRORFILE( "unknown Direction" );
1706             return sal_False;
1707     }
1708     if ( pRPN && nRPN )
1709     {
1710         FormulaToken* t = pRPN[nRPN-1];
1711         if ( t->GetType() == svByte )
1712         {
1713             sal_uInt8 nParamCount = t->GetByte();
1714             if ( nParamCount && nRPN > nParamCount )
1715             {
1716                 sal_Bool bRet = sal_False;
1717                 sal_uInt16 nParam = nRPN - nParamCount - 1;
1718                 for ( ; nParam < nRPN-1; nParam++ )
1719                 {
1720                     FormulaToken* p = pRPN[nParam];
1721                     switch ( p->GetType() )
1722                     {
1723                         case svSingleRef :
1724                         {
1725                             ScSingleRefData& rRef = static_cast<ScToken*>(p)->GetSingleRef();
1726                             rRef.CalcAbsIfRel( rPos );
1727                             switch ( eDir )
1728                             {
1729                                 case DIR_BOTTOM :
1730                                     if ( rRef.nRow == nRow
1731                                             && rRef.nRow > nExtend )
1732                                     {
1733                                         nExtend = rRef.nRow;
1734                                         bRet = sal_True;
1735                                     }
1736                                 break;
1737                                 case DIR_RIGHT :
1738                                     if ( rRef.nCol == nCol
1739                                             && static_cast<SCCOLROW>(rRef.nCol)
1740                                             > nExtend )
1741                                     {
1742                                         nExtend = rRef.nCol;
1743                                         bRet = sal_True;
1744                                     }
1745                                 break;
1746                                 case DIR_TOP :
1747                                     if ( rRef.nRow == nRow
1748                                             && rRef.nRow < nExtend )
1749                                     {
1750                                         nExtend = rRef.nRow;
1751                                         bRet = sal_True;
1752                                     }
1753                                 break;
1754                                 case DIR_LEFT :
1755                                     if ( rRef.nCol == nCol
1756                                             && static_cast<SCCOLROW>(rRef.nCol)
1757                                             < nExtend )
1758                                     {
1759                                         nExtend = rRef.nCol;
1760                                         bRet = sal_True;
1761                                     }
1762                                 break;
1763                             }
1764                         }
1765                         break;
1766                         case svDoubleRef :
1767                         {
1768                             ScComplexRefData& rRef = static_cast<ScToken*>(p)->GetDoubleRef();
1769                             rRef.CalcAbsIfRel( rPos );
1770                             switch ( eDir )
1771                             {
1772                                 case DIR_BOTTOM :
1773                                     if ( rRef.Ref1.nRow == nRow
1774                                             && rRef.Ref2.nRow > nExtend )
1775                                     {
1776                                         nExtend = rRef.Ref2.nRow;
1777                                         bRet = sal_True;
1778                                     }
1779                                 break;
1780                                 case DIR_RIGHT :
1781                                     if ( rRef.Ref1.nCol == nCol &&
1782                                             static_cast<SCCOLROW>(rRef.Ref2.nCol)
1783                                             > nExtend )
1784                                     {
1785                                         nExtend = rRef.Ref2.nCol;
1786                                         bRet = sal_True;
1787                                     }
1788                                 break;
1789                                 case DIR_TOP :
1790                                     if ( rRef.Ref2.nRow == nRow
1791                                             && rRef.Ref1.nRow < nExtend )
1792                                     {
1793                                         nExtend = rRef.Ref1.nRow;
1794                                         bRet = sal_True;
1795                                     }
1796                                 break;
1797                                 case DIR_LEFT :
1798                                     if ( rRef.Ref2.nCol == nCol &&
1799                                             static_cast<SCCOLROW>(rRef.Ref1.nCol)
1800                                             < nExtend )
1801                                     {
1802                                         nExtend = rRef.Ref1.nCol;
1803                                         bRet = sal_True;
1804                                     }
1805                                 break;
1806                             }
1807                         }
1808                         break;
1809                         default:
1810                         {
1811                             // added to avoid warnings
1812                         }
1813                     } // switch
1814                 } // for
1815                 return bRet;
1816             }
1817         }
1818     }
1819     return sal_False;
1820 }
1821 
1822 
ReadjustRelative3DReferences(const ScAddress & rOldPos,const ScAddress & rNewPos)1823 void ScTokenArray::ReadjustRelative3DReferences( const ScAddress& rOldPos,
1824         const ScAddress& rNewPos )
1825 {
1826     for ( sal_uInt16 j=0; j<nLen; ++j )
1827     {
1828         switch ( pCode[j]->GetType() )
1829         {
1830             case svDoubleRef :
1831             {
1832                 ScSingleRefData& rRef2 = static_cast<ScToken*>(pCode[j])->GetSingleRef2();
1833                 // Also adjust if the reference is of the form Sheet1.A2:A3
1834                 if ( rRef2.IsFlag3D() || static_cast<ScToken*>(pCode[j])->GetSingleRef().IsFlag3D() )
1835                 {
1836                     rRef2.CalcAbsIfRel( rOldPos );
1837                     rRef2.CalcRelFromAbs( rNewPos );
1838                 }
1839             }
1840             //! fallthru
1841             case svSingleRef :
1842             {
1843                 ScSingleRefData& rRef1 = static_cast<ScToken*>(pCode[j])->GetSingleRef();
1844                 if ( rRef1.IsFlag3D() )
1845                 {
1846                     rRef1.CalcAbsIfRel( rOldPos );
1847                     rRef1.CalcRelFromAbs( rNewPos );
1848                 }
1849             }
1850             break;
1851             case svExternalDoubleRef:
1852             {
1853                 ScSingleRefData& rRef2 = static_cast<ScToken*>(pCode[j])->GetSingleRef2();
1854                 rRef2.CalcAbsIfRel( rOldPos );
1855                 rRef2.CalcRelFromAbs( rNewPos );
1856             }
1857             //! fallthru
1858             case svExternalSingleRef:
1859             {
1860                 ScSingleRefData& rRef1 = static_cast<ScToken*>(pCode[j])->GetSingleRef();
1861                 rRef1.CalcAbsIfRel( rOldPos );
1862                 rRef1.CalcRelFromAbs( rNewPos );
1863             }
1864             break;
1865             default:
1866             {
1867                 // added to avoid warnings
1868             }
1869         }
1870     }
1871 }
1872