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 .
20 #ifndef INCLUDED_SC_INC_TOKENARRAY_HXX
21 #define INCLUDED_SC_INC_TOKENARRAY_HXX
23 #include <formula/token.hxx>
24 #include <rtl/ref.hxx>
25 #include "document.hxx"
28 #include "calcmacros.hxx"
29 #include "address.hxx"
31 #include <formula/tokenarray.hxx>
35 struct RefUpdateContext
;
36 struct RefUpdateInsertTabContext
;
37 struct RefUpdateDeleteTabContext
;
38 struct RefUpdateMoveTabContext
;
39 struct RefUpdateResult
;
40 struct TokenStringContext
;
41 class ColRowReorderMapType
;
46 struct ScSingleRefData
;
47 struct ScComplexRefData
;
49 class SAL_WARN_UNUSED SC_DLLPUBLIC ScTokenArray final
: public formula::FormulaTokenArray
51 friend class ScCompiler
;
53 bool ImplGetReference( ScRange
& rRange
, const ScAddress
& rPos
, bool bValidOnly
) const;
55 // hold a reference to the limits because sometimes our lifetime exceeds the lifetime of the associated ScDocument
56 rtl::Reference
<ScSheetLimits
> mxSheetLimits
;
58 ScFormulaVectorState meVectorState
: 4; // Only 4 bits
59 bool mbOpenCLEnabled
: 1;
60 bool mbThreadingEnabled
: 1;
62 void CheckForThreading( const formula::FormulaToken
& r
);
65 ScTokenArray(const ScDocument
& rDoc
);
66 ScTokenArray(ScSheetLimits
&);
67 /** Assignment with incrementing references of FormulaToken entries
69 ScTokenArray( const ScTokenArray
& ) = default;
70 virtual ~ScTokenArray() override
;
72 bool EqualTokens( const ScTokenArray
* pArr2
) const;
74 virtual void Clear() override
;
75 std::unique_ptr
<ScTokenArray
> Clone() const; /// True copy!
78 size_t GetHash() const { return mnHashValue
;}
80 ScFormulaVectorState
GetVectorState() const { return meVectorState
;}
81 void ResetVectorState();
82 bool IsFormulaVectorDisabled() const;
85 * If the array contains at least one relative row reference or named
86 * expression, it's variant. Otherwise invariant.
88 bool IsInvariant() const;
90 /// Exactly and only one range (valid or deleted)
91 bool IsReference( ScRange
& rRange
, const ScAddress
& rPos
) const;
92 /// Exactly and only one valid range (no #REF!s)
93 bool IsValidReference( ScRange
& rRange
, const ScAddress
& rPos
) const;
95 /** Determines the extent of direct adjacent
96 references. Only use with real functions, e.g.
97 GetOuterFuncOpCode() == ocSum ! */
98 bool GetAdjacentExtendOfOuterFuncRefs(
100 const ScAddress
& rPos
, ScDirection
);
102 formula::FormulaToken
* AddRawToken( const ScRawToken
& );
103 virtual bool AddFormulaToken(
104 const css::sheet::FormulaToken
& rToken
,
105 svl::SharedStringPool
& rSPool
,
106 formula::ExternalReferenceHelper
* _pRef
) override
;
107 virtual void CheckToken( const formula::FormulaToken
& r
) override
;
108 virtual formula::FormulaToken
* AddOpCode( OpCode eCode
) override
;
109 /** ScSingleRefToken with ocPush. */
110 formula::FormulaToken
* AddSingleReference( const ScSingleRefData
& rRef
);
111 /** ScSingleRefOpToken with ocMatRef. */
112 formula::FormulaToken
* AddMatrixSingleReference( const ScSingleRefData
& rRef
);
113 formula::FormulaToken
* AddDoubleReference( const ScComplexRefData
& rRef
);
114 void AddRangeName( sal_uInt16 n
, sal_Int16 nSheet
);
115 formula::FormulaToken
* AddDBRange( sal_uInt16 n
);
116 formula::FormulaToken
* AddExternalName( sal_uInt16 nFileId
, const svl::SharedString
& rName
);
117 void AddExternalSingleReference( sal_uInt16 nFileId
, const svl::SharedString
& rTabName
, const ScSingleRefData
& rRef
);
118 formula::FormulaToken
* AddExternalDoubleReference( sal_uInt16 nFileId
, const svl::SharedString
& rTabName
, const ScComplexRefData
& rRef
);
119 formula::FormulaToken
* AddMatrix( const ScMatrixRef
& p
);
120 /** ScSingleRefOpToken with ocColRowName. */
121 formula::FormulaToken
* AddColRowName( const ScSingleRefData
& rRef
);
122 virtual formula::FormulaToken
* MergeArray( ) override
;
124 /** Merge very last SingleRef+ocRange+SingleRef combination into DoubleRef
125 and adjust pCode array, or do nothing if conditions not met. */
126 void MergeRangeReference( const ScAddress
& rPos
);
128 /// Assign XML string placeholder to the array
129 void AssignXMLString( const OUString
&rText
, const OUString
&rFormulaNmsp
);
131 /** Assignment with incrementing references of FormulaToken entries
133 ScTokenArray
& operator=( const ScTokenArray
& );
136 * Make all absolute references external references pointing to the old document
138 * @param rOldDoc old document
139 * @param rNewDoc new document
140 * @param rPos position of the cell to determine if the reference is in the copied area
141 * @param bRangeName set for range names, range names have special handling for absolute sheet ref + relative col/row ref
143 void ReadjustAbsolute3DReferences( const ScDocument
& rOldDoc
, ScDocument
& rNewDoc
, const ScAddress
& rPos
, bool bRangeName
= false );
146 * Make all absolute references pointing to the copied range if the range is copied too
147 * @param bCheckCopyArea should reference pointing into the copy area be adjusted independently from being absolute, should be true only for copy&paste between documents
149 void AdjustAbsoluteRefs( const ScDocument
& rOldDoc
, const ScAddress
& rOldPos
, const ScAddress
& rNewPos
, bool bCheckCopyArea
);
151 /** When copying a sheet-local named expression, move sheet references that
152 point to the originating sheet to point to the new sheet instead.
154 void AdjustSheetLocalNameReferences( SCTAB nOldTab
, SCTAB nNewTab
);
156 /** Returns true if the sheet nTab is referenced in code. Relative sheet
157 references are evaluated using nPosTab.
159 bool ReferencesSheet( SCTAB nTab
, SCTAB nPosTab
) const;
162 * Adjust all references in response to shifting of cells during cell
163 * insertion and deletion.
165 * @param rCxt context that stores details of shifted region.
166 * @param rOldPos old cell position prior to shifting.
168 sc::RefUpdateResult
AdjustReferenceOnShift( const sc::RefUpdateContext
& rCxt
, const ScAddress
& rOldPos
);
170 sc::RefUpdateResult
AdjustReferenceOnMove(
171 const sc::RefUpdateContext
& rCxt
, const ScAddress
& rOldPos
, const ScAddress
& rNewPos
);
174 * Move reference positions in response to column reordering. A range
175 * reference gets moved only when the whole range fits in a single column.
177 * @param rPos position of this formula cell
178 * @param nTab sheet where columns are reordered.
179 * @param nRow1 top row of reordered range.
180 * @param nRow2 bottom row of reordered range.
181 * @param rColMap old-to-new column mapping.
183 void MoveReferenceColReorder(
184 const ScAddress
& rPos
, SCTAB nTab
, SCROW nRow1
, SCROW nRow2
,
185 const sc::ColRowReorderMapType
& rColMap
);
187 void MoveReferenceRowReorder(
188 const ScAddress
& rPos
, SCTAB nTab
, SCCOL nCol1
, SCCOL nCol2
,
189 const sc::ColRowReorderMapType
& rRowMap
);
192 * Adjust all references in named expression. In named expression, we only
193 * update absolute positions, and leave relative positions intact.
195 * @param rCxt context that stores details of shifted region
197 * @return update result.
199 sc::RefUpdateResult
AdjustReferenceInName( const sc::RefUpdateContext
& rCxt
, const ScAddress
& rPos
);
201 sc::RefUpdateResult
AdjustReferenceInMovedName( const sc::RefUpdateContext
& rCxt
, const ScAddress
& rPos
);
204 * Adjust all references on sheet deletion.
206 * @param nDelPos position of sheet being deleted.
207 * @param nSheets number of sheets to delete.
208 * @param rOldPos position of formula cell prior to the deletion.
210 * @return true if at least one reference has changed its sheet reference.
212 sc::RefUpdateResult
AdjustReferenceOnDeletedTab( const sc::RefUpdateDeleteTabContext
& rCxt
, const ScAddress
& rOldPos
);
214 sc::RefUpdateResult
AdjustReferenceOnInsertedTab( const sc::RefUpdateInsertTabContext
& rCxt
, const ScAddress
& rOldPos
);
216 sc::RefUpdateResult
AdjustReferenceOnMovedTab( const sc::RefUpdateMoveTabContext
& rCxt
, const ScAddress
& rOldPos
);
219 * Adjust all internal references on base position change.
221 void AdjustReferenceOnMovedOrigin( const ScAddress
& rOldPos
, const ScAddress
& rNewPos
);
224 * Adjust all internal references on base position change if they point to
225 * a sheet other than the one of rOldPos.
227 void AdjustReferenceOnMovedOriginIfOtherSheet( const ScAddress
& rOldPos
, const ScAddress
& rNewPos
);
230 * Adjust internal range references on base position change to justify /
231 * put in order the relative references.
233 void AdjustReferenceOnCopy( const ScAddress
& rNewPos
);
236 * Clear sheet deleted flag from internal reference tokens if the sheet
237 * index falls within specified range. Note that when a reference is on a
238 * sheet that's been deleted, its referenced sheet index retains the
239 * original index of the deleted sheet.
241 * @param rPos position of formula cell
242 * @param nStartTab index of first sheet, inclusive.
243 * @param nEndTab index of last sheet, inclusive.
245 void ClearTabDeleted( const ScAddress
& rPos
, SCTAB nStartTab
, SCTAB nEndTab
);
247 void CheckRelativeReferenceBounds(
248 const sc::RefUpdateContext
& rCxt
, const ScAddress
& rPos
, SCROW nGroupLen
, std::vector
<SCROW
>& rBounds
) const;
250 void CheckRelativeReferenceBounds(
251 const ScAddress
& rPos
, SCROW nGroupLen
, const ScRange
& rRange
, std::vector
<SCROW
>& rBounds
) const;
253 void CheckExpandReferenceBounds(
254 const sc::RefUpdateContext
& rCxt
, const ScAddress
& rPos
, SCROW nGroupLen
, std::vector
<SCROW
>& rBounds
) const;
257 * Create a string representation of formula token array without modifying
258 * the internal state of the token array.
260 OUString
CreateString( sc::TokenStringContext
& rCxt
, const ScAddress
& rPos
) const;
262 void WrapReference( const ScAddress
& rPos
, SCCOL nMaxCol
, SCROW nMaxRow
);
263 bool NeedsWrapReference( const ScAddress
& rPos
, SCCOL nMaxCol
, SCROW nMaxRow
) const;
265 sal_Int32
GetWeight() const;
267 bool IsEnabledForOpenCL() const { return mbOpenCLEnabled
; }
268 bool IsEnabledForThreading() const { return mbThreadingEnabled
; }
270 #if DEBUG_FORMULA_COMPILER
275 #endif // INCLUDED_SC_INC_TOKENARRAY_HXX
277 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */