Bug 1874684 - Part 4: Prefer const references instead of copying Instant values....
[gecko.git] / dom / xslt / xslt / txStylesheetCompiler.h
blob759219ced64726ba81f7d07fbca21bdce3bfd485
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef TRANSFRMX_TXSTYLESHEETCOMPILER_H
7 #define TRANSFRMX_TXSTYLESHEETCOMPILER_H
9 #include "mozilla/Attributes.h"
10 #include "mozilla/UniquePtr.h"
11 #include "txStack.h"
12 #include "txXSLTPatterns.h"
13 #include "txExpr.h"
14 #include "txIXPathContext.h"
15 #include "txStylesheet.h"
16 #include "nsTArray.h"
18 extern bool TX_XSLTFunctionAvailable(nsAtom* aName, int32_t aNameSpaceID);
20 class txHandlerTable;
21 class txElementContext;
22 class txInstructionContainer;
23 class txInstruction;
24 class txNamespaceMap;
25 class txToplevelItem;
26 class txPushNewContext;
27 class txStylesheetCompiler;
29 class txElementContext : public txObject {
30 public:
31 explicit txElementContext(const nsAString& aBaseURI);
32 txElementContext(const txElementContext& aOther);
34 bool mPreserveWhitespace;
35 bool mForwardsCompatibleParsing;
36 nsString mBaseURI;
37 RefPtr<txNamespaceMap> mMappings;
38 nsTArray<int32_t> mInstructionNamespaces;
39 int32_t mDepth;
42 using mozilla::dom::ReferrerPolicy;
44 class txACompileObserver {
45 public:
46 NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
48 virtual nsresult loadURI(const nsAString& aUri, const nsAString& aReferrerUri,
49 ReferrerPolicy aReferrerPolicy,
50 txStylesheetCompiler* aCompiler) = 0;
51 virtual void onDoneCompiling(txStylesheetCompiler* aCompiler,
52 nsresult aResult,
53 const char16_t* aErrorText = nullptr,
54 const char16_t* aParam = nullptr) = 0;
57 #define TX_DECL_ACOMPILEOBSERVER \
58 nsresult loadURI(const nsAString& aUri, const nsAString& aReferrerUri, \
59 ReferrerPolicy aReferrerPolicy, \
60 txStylesheetCompiler* aCompiler) override; \
61 void onDoneCompiling(txStylesheetCompiler* aCompiler, nsresult aResult, \
62 const char16_t* aErrorText = nullptr, \
63 const char16_t* aParam = nullptr) override;
65 class txInScopeVariable {
66 public:
67 explicit txInScopeVariable(const txExpandedName& aName)
68 : mName(aName), mLevel(1) {}
69 txExpandedName mName;
70 int32_t mLevel;
73 class txStylesheetCompilerState : public txIParseContext {
74 public:
75 explicit txStylesheetCompilerState(txACompileObserver* aObserver);
76 ~txStylesheetCompilerState();
78 nsresult init(const nsAString& aStylesheetURI, ReferrerPolicy aReferrerPolicy,
79 txStylesheet* aStylesheet, txListIterator* aInsertPosition);
81 // Embedded stylesheets state
82 bool handleEmbeddedSheet() { return mEmbedStatus == eInEmbed; }
83 void doneEmbedding() { mEmbedStatus = eHasEmbed; }
85 // Stack functions
86 enum enumStackType {
87 eElementHandler,
88 eHandlerTable,
89 eVariableItem,
90 eCopy,
91 eInstruction,
92 ePushNewContext,
93 eConditionalGoto,
94 eCheckParam,
95 ePushNullTemplateRule
97 void pushHandlerTable(txHandlerTable* aTable);
98 void popHandlerTable();
99 void pushSorter(txPushNewContext* aSorter);
100 void popSorter();
101 void pushChooseGotoList();
102 void popChooseGotoList();
103 void pushObject(txObject* aObject);
104 txObject* popObject();
105 void pushPtr(void* aPtr, enumStackType aType);
106 void* popPtr(enumStackType aType);
108 // stylesheet functions
109 void addToplevelItem(txToplevelItem* aItem);
110 nsresult openInstructionContainer(txInstructionContainer* aContainer);
111 void closeInstructionContainer();
112 txInstruction* addInstruction(
113 mozilla::UniquePtr<txInstruction>&& aInstruction);
114 template <class T>
115 T* addInstruction(mozilla::UniquePtr<T> aInstruction) {
116 return static_cast<T*>(addInstruction(
117 mozilla::UniquePtr<txInstruction>(std::move(aInstruction))));
119 nsresult loadIncludedStylesheet(const nsAString& aURI);
120 nsresult loadImportedStylesheet(const nsAString& aURI,
121 txStylesheet::ImportFrame* aFrame);
123 // misc
124 void addGotoTarget(txInstruction** aTargetPointer);
125 void addVariable(const txExpandedName& aName);
127 // txIParseContext
128 nsresult resolveNamespacePrefix(nsAtom* aPrefix, int32_t& aID) override;
129 nsresult resolveFunctionCall(nsAtom* aName, int32_t aID,
130 FunctionCall** aFunction) override;
131 bool caseInsensitiveNameTests() override;
134 * Should the stylesheet be parsed in forwards compatible parsing mode.
136 bool fcp() { return mElementContext->mForwardsCompatibleParsing; }
138 void SetErrorOffset(uint32_t aOffset) override;
140 bool allowed(Allowed aAllowed) override { return !(mDisAllowed & aAllowed); }
142 bool ignoreError(nsresult aResult) {
143 // Some errors shouldn't be ignored even in forwards compatible parsing
144 // mode.
145 return aResult != NS_ERROR_XSLT_CALL_TO_KEY_NOT_ALLOWED && fcp();
148 RefPtr<txStylesheet> mStylesheet;
149 txHandlerTable* mHandlerTable;
150 mozilla::UniquePtr<txElementContext> mElementContext;
151 txPushNewContext* mSorter;
152 mozilla::UniquePtr<txList> mChooseGotoList;
153 bool mDOE;
154 bool mSearchingForFallback;
155 uint16_t mDisAllowed;
157 protected:
158 RefPtr<txACompileObserver> mObserver;
159 nsTArray<txInScopeVariable> mInScopeVariables;
160 nsTArray<txStylesheetCompiler*> mChildCompilerList;
161 // embed info, target information is the ID
162 nsString mTarget;
163 enum { eNoEmbed, eNeedEmbed, eInEmbed, eHasEmbed } mEmbedStatus;
164 nsString mStylesheetURI;
165 bool mIsTopCompiler;
166 bool mDoneWithThisStylesheet;
167 txStack mObjectStack;
168 txStack mOtherStack;
169 nsTArray<enumStackType> mTypeStack;
171 private:
172 mozilla::UniquePtr<txInstruction>* mNextInstrPtr;
173 txListIterator mToplevelIterator;
174 nsTArray<txInstruction**> mGotoTargetPointers;
175 ReferrerPolicy mReferrerPolicy;
178 struct txStylesheetAttr {
179 int32_t mNamespaceID;
180 RefPtr<nsAtom> mLocalName;
181 RefPtr<nsAtom> mPrefix;
182 nsString mValue;
185 class txStylesheetCompiler final : private txStylesheetCompilerState,
186 public txACompileObserver {
187 public:
188 friend class txStylesheetCompilerState;
189 friend bool TX_XSLTFunctionAvailable(nsAtom* aName, int32_t aNameSpaceID);
190 txStylesheetCompiler(const nsAString& aStylesheetURI,
191 ReferrerPolicy aReferrerPolicy,
192 txACompileObserver* aObserver);
193 txStylesheetCompiler(const nsAString& aStylesheetURI,
194 txStylesheet* aStylesheet,
195 txListIterator* aInsertPosition,
196 ReferrerPolicy aReferrerPolicy,
197 txACompileObserver* aObserver);
199 void setBaseURI(const nsString& aBaseURI);
201 nsresult startElement(int32_t aNamespaceID, nsAtom* aLocalName,
202 nsAtom* aPrefix, txStylesheetAttr* aAttributes,
203 int32_t aAttrCount);
204 nsresult startElement(const char16_t* aName, const char16_t** aAtts,
205 int32_t aAttrCount);
206 nsresult endElement();
207 nsresult characters(const nsAString& aStr);
208 nsresult doneLoading();
210 void cancel(nsresult aError, const char16_t* aErrorText = nullptr,
211 const char16_t* aParam = nullptr);
213 txStylesheet* getStylesheet();
215 TX_DECL_ACOMPILEOBSERVER
216 NS_INLINE_DECL_REFCOUNTING(txStylesheetCompiler, override)
218 private:
219 // Private destructor, to discourage deletion outside of Release():
220 ~txStylesheetCompiler() = default;
222 nsresult startElementInternal(int32_t aNamespaceID, nsAtom* aLocalName,
223 nsAtom* aPrefix, txStylesheetAttr* aAttributes,
224 int32_t aAttrCount);
226 nsresult flushCharacters();
227 nsresult ensureNewElementContext();
228 nsresult maybeDoneCompiling();
230 nsString mCharacters;
231 nsresult mStatus;
234 #endif