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 <formula/token.hxx>
23 #include <rtl/ref.hxx>
24 #include "document.hxx"
27 #include "calcmacros.hxx"
28 #include "address.hxx"
30 #include <formula/tokenarray.hxx>
34 struct RefUpdateContext
;
35 struct RefUpdateInsertTabContext
;
36 struct RefUpdateDeleteTabContext
;
37 struct RefUpdateMoveTabContext
;
38 struct RefUpdateResult
;
39 struct TokenStringContext
;
40 class ColRowReorderMapType
;
45 struct ScSingleRefData
;
46 struct ScComplexRefData
;
48 class SAL_WARN_UNUSED SAL_DLLPUBLIC_RTTI ScTokenArray final
: public formula::FormulaTokenArray
50 friend class ScCompiler
;
52 bool ImplGetReference( ScRange
& rRange
, const ScAddress
& rPos
, bool bValidOnly
) const;
54 // hold a reference to the limits because sometimes our lifetime exceeds the lifetime of the associated ScDocument
55 rtl::Reference
<ScSheetLimits
> mxSheetLimits
;
57 ScFormulaVectorState meVectorState
: 4; // Only 4 bits
58 bool mbOpenCLEnabled
: 1;
59 bool mbThreadingEnabled
: 1;
61 void CheckForThreading( const formula::FormulaToken
& r
);
64 SC_DLLPUBLIC
ScTokenArray(const ScDocument
& rDoc
);
65 ScTokenArray(ScSheetLimits
&);
66 /** Assignment with incrementing references of FormulaToken entries
68 ScTokenArray( const ScTokenArray
& ) = default;
69 ScTokenArray( ScTokenArray
&& ) = default;
70 SC_DLLPUBLIC
virtual ~ScTokenArray() override
;
72 bool EqualTokens( const ScTokenArray
* pArr2
) const;
74 SC_DLLPUBLIC
virtual void Clear() override
;
75 SC_DLLPUBLIC
std::unique_ptr
<ScTokenArray
> Clone() const; /// True copy!
76 SC_DLLPUBLIC ScTokenArray
CloneValue() const; /// True copy!
78 SC_DLLPUBLIC
void GenHash();
79 size_t GetHash() const { return mnHashValue
;}
81 ScFormulaVectorState
GetVectorState() const { return meVectorState
;}
82 void ResetVectorState();
83 bool IsFormulaVectorDisabled() const;
86 * If the array contains at least one relative row reference or named
87 * expression, it's variant. Otherwise invariant.
89 bool IsInvariant() const;
91 /// Exactly and only one range (valid or deleted)
92 SC_DLLPUBLIC
bool IsReference( ScRange
& rRange
, const ScAddress
& rPos
) const;
93 /// Exactly and only one valid range (no #REF!s)
94 SC_DLLPUBLIC
bool IsValidReference( ScRange
& rRange
, const ScAddress
& rPos
) const;
96 /** Determines the extent of direct adjacent
97 references. Only use with real functions, e.g.
98 GetOuterFuncOpCode() == ocSum ! */
99 bool GetAdjacentExtendOfOuterFuncRefs(
101 const ScAddress
& rPos
, ScDirection
);
103 formula::FormulaToken
* AddRawToken( const ScRawToken
& );
104 SC_DLLPUBLIC
virtual bool AddFormulaToken(
105 const css::sheet::FormulaToken
& rToken
,
106 svl::SharedStringPool
& rSPool
,
107 formula::ExternalReferenceHelper
* _pRef
) override
;
108 SC_DLLPUBLIC
virtual void CheckToken( const formula::FormulaToken
& r
) override
;
109 SC_DLLPUBLIC
virtual formula::FormulaToken
* AddOpCode( OpCode eCode
) override
;
110 /** ScSingleRefToken with ocPush. */
111 SC_DLLPUBLIC
formula::FormulaToken
* AddSingleReference( const ScSingleRefData
& rRef
);
112 /** ScSingleRefOpToken with ocMatRef. */
113 formula::FormulaToken
* AddMatrixSingleReference( const ScSingleRefData
& rRef
);
114 SC_DLLPUBLIC
formula::FormulaToken
* AddDoubleReference( const ScComplexRefData
& rRef
);
115 SC_DLLPUBLIC
void AddRangeName( sal_uInt16 n
, sal_Int16 nSheet
);
116 formula::FormulaToken
* AddDBRange( sal_uInt16 n
);
117 SC_DLLPUBLIC
formula::FormulaToken
* AddExternalName( sal_uInt16 nFileId
, const svl::SharedString
& rName
);
118 SC_DLLPUBLIC
void AddExternalSingleReference( sal_uInt16 nFileId
, const svl::SharedString
& rTabName
, const ScSingleRefData
& rRef
);
119 SC_DLLPUBLIC
formula::FormulaToken
* AddExternalDoubleReference( sal_uInt16 nFileId
, const svl::SharedString
& rTabName
, const ScComplexRefData
& rRef
);
120 SC_DLLPUBLIC
formula::FormulaToken
* AddMatrix( const ScMatrixRef
& p
);
121 /** ScSingleRefOpToken with ocColRowName. */
122 SC_DLLPUBLIC
formula::FormulaToken
* AddColRowName( const ScSingleRefData
& rRef
);
123 SC_DLLPUBLIC
virtual formula::FormulaToken
* MergeArray( ) override
;
125 /** Merge very last SingleRef+ocRange+SingleRef combination into DoubleRef
126 and adjust pCode array, or do nothing if conditions not met. */
127 void MergeRangeReference( const ScAddress
& rPos
);
129 /// Assign XML string placeholder to the array
130 void AssignXMLString( const OUString
&rText
, const OUString
&rFormulaNmsp
);
132 /** Assignment with incrementing references of FormulaToken entries
134 ScTokenArray
& operator=( const ScTokenArray
& );
135 ScTokenArray
& operator=( ScTokenArray
&& );
138 * Make all absolute references external references pointing to the old document
140 * @param rOldDoc old document
141 * @param rNewDoc new document
142 * @param rPos position of the cell to determine if the reference is in the copied area
143 * @param bRangeName set for range names, range names have special handling for absolute sheet ref + relative col/row ref
145 void ReadjustAbsolute3DReferences( const ScDocument
& rOldDoc
, ScDocument
& rNewDoc
, const ScAddress
& rPos
, bool bRangeName
= false );
148 * Make all absolute references pointing to the copied range if the range is copied too
149 * @param bCheckCopyArea should reference pointing into the copy area be adjusted independently from being absolute, should be true only for copy&paste between documents
151 void AdjustAbsoluteRefs( const ScDocument
& rOldDoc
, const ScAddress
& rOldPos
, const ScAddress
& rNewPos
, bool bCheckCopyArea
);
153 /** When copying a sheet-local named expression, move sheet references that
154 point to the originating sheet to point to the new sheet instead.
156 void AdjustSheetLocalNameReferences( SCTAB nOldTab
, SCTAB nNewTab
);
158 /** Returns true if the sheet nTab is referenced in code. Relative sheet
159 references are evaluated using nPosTab.
161 bool ReferencesSheet( SCTAB nTab
, SCTAB nPosTab
) const;
164 * Adjust all references in response to shifting of cells during cell
165 * insertion and deletion.
167 * @param rCxt context that stores details of shifted region.
168 * @param rOldPos old cell position prior to shifting.
170 sc::RefUpdateResult
AdjustReferenceOnShift( const sc::RefUpdateContext
& rCxt
, const ScAddress
& rOldPos
);
172 sc::RefUpdateResult
AdjustReferenceOnMove(
173 const sc::RefUpdateContext
& rCxt
, const ScAddress
& rOldPos
, const ScAddress
& rNewPos
);
176 * Move reference positions in response to column reordering. A range
177 * reference gets moved only when the whole range fits in a single column.
179 * @param rPos position of this formula cell
180 * @param nTab sheet where columns are reordered.
181 * @param nRow1 top row of reordered range.
182 * @param nRow2 bottom row of reordered range.
183 * @param rColMap old-to-new column mapping.
185 void MoveReferenceColReorder(
186 const ScAddress
& rPos
, SCTAB nTab
, SCROW nRow1
, SCROW nRow2
,
187 const sc::ColRowReorderMapType
& rColMap
);
189 void MoveReferenceRowReorder(
190 const ScAddress
& rPos
, SCTAB nTab
, SCCOL nCol1
, SCCOL nCol2
,
191 const sc::ColRowReorderMapType
& rRowMap
);
194 * Adjust all references in named expression. In named expression, we only
195 * update absolute positions, and leave relative positions intact.
197 * @param rCxt context that stores details of shifted region
199 * @return update result.
201 sc::RefUpdateResult
AdjustReferenceInName( const sc::RefUpdateContext
& rCxt
, const ScAddress
& rPos
);
203 sc::RefUpdateResult
AdjustReferenceInMovedName( const sc::RefUpdateContext
& rCxt
, const ScAddress
& rPos
);
206 * Adjust all references on sheet deletion.
208 * @param nDelPos position of sheet being deleted.
209 * @param nSheets number of sheets to delete.
210 * @param rOldPos position of formula cell prior to the deletion.
212 * @return true if at least one reference has changed its sheet reference.
214 sc::RefUpdateResult
AdjustReferenceOnDeletedTab( const sc::RefUpdateDeleteTabContext
& rCxt
, const ScAddress
& rOldPos
);
216 sc::RefUpdateResult
AdjustReferenceOnInsertedTab( const sc::RefUpdateInsertTabContext
& rCxt
, const ScAddress
& rOldPos
);
218 sc::RefUpdateResult
AdjustReferenceOnMovedTab( const sc::RefUpdateMoveTabContext
& rCxt
, const ScAddress
& rOldPos
);
221 * Adjust all internal references on base position change.
223 void AdjustReferenceOnMovedOrigin( const ScAddress
& rOldPos
, const ScAddress
& rNewPos
);
226 * Adjust all internal references on base position change if they point to
227 * a sheet other than the one of rOldPos.
229 void AdjustReferenceOnMovedOriginIfOtherSheet( const ScAddress
& rOldPos
, const ScAddress
& rNewPos
);
232 * Adjust internal range references on base position change to justify /
233 * put in order the relative references.
235 void AdjustReferenceOnCopy( const ScAddress
& rNewPos
);
238 * Clear sheet deleted flag from internal reference tokens if the sheet
239 * index falls within specified range. Note that when a reference is on a
240 * sheet that's been deleted, its referenced sheet index retains the
241 * original index of the deleted sheet.
243 * @param rPos position of formula cell
244 * @param nStartTab index of first sheet, inclusive.
245 * @param nEndTab index of last sheet, inclusive.
247 void ClearTabDeleted( const ScAddress
& rPos
, SCTAB nStartTab
, SCTAB nEndTab
);
249 void CheckRelativeReferenceBounds(
250 const sc::RefUpdateContext
& rCxt
, const ScAddress
& rPos
, SCROW nGroupLen
, std::vector
<SCROW
>& rBounds
) const;
252 void CheckRelativeReferenceBounds(
253 const ScAddress
& rPos
, SCROW nGroupLen
, const ScRange
& rRange
, std::vector
<SCROW
>& rBounds
) const;
255 void CheckExpandReferenceBounds(
256 const sc::RefUpdateContext
& rCxt
, const ScAddress
& rPos
, SCROW nGroupLen
, std::vector
<SCROW
>& rBounds
) const;
259 * Create a string representation of formula token array without modifying
260 * the internal state of the token array.
262 SC_DLLPUBLIC OUString
CreateString( sc::TokenStringContext
& rCxt
, const ScAddress
& rPos
) const;
264 SC_DLLPUBLIC
void WrapReference( const ScAddress
& rPos
, SCCOL nMaxCol
, SCROW nMaxRow
);
266 sal_Int32
GetWeight() const;
268 bool IsEnabledForOpenCL() const { return mbOpenCLEnabled
; }
269 bool IsEnabledForThreading() const { return mbThreadingEnabled
; }
271 #if DEBUG_FORMULA_COMPILER
276 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */