aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/parser/qqmljsast.cpp
diff options
context:
space:
mode:
authorFabian Kosmale <fabian.kosmale@qt.io>2021-11-15 16:16:16 +0100
committerFabian Kosmale <fabian.kosmale@qt.io>2021-11-17 20:59:23 +0100
commitc9380aa42805cf55736dc87c87149d912282c0ae (patch)
treea1b824fcdf3768ed4e6d4477ef180649e239bd0d /src/qml/parser/qqmljsast.cpp
parentf9bddfa12d233d9ef01299869f4d491d62a0828e (diff)
Clean up property attributes (2/2)
This commit introduces a new AST node for property attributes (currently required, default and readonly). That node is not integrated into our visitors, as it is only meant as an internal implementaion detail of UiPublicMember. All information stored in it is meant to be accessed by UiPublicMember's methods (see also the note below). We use an union of SourceLocation and a node pointer to only pay the overhead of the new node for properties; signals simply store the signal token. The grammar is rewritten with a new UiPropertyAttributes rule, which avoids quite a bit of duplication in various rules, which had to deal with the combinatorial explosion of attributes. Some parse errors are now turned into semantic errors instead (readonly without initializer, required with initializer). By centralizing the handling of attributes in the grammar, we now can easily support e.g. default properties with a list initializer. As part of this restructuring, UiPublicMember's firstSourceLocation is fixed to ensure that we actually return the first source location, independent of the order in which the attributes are written. Note: In theory, we would not need to make UiPropertyAttributes an AST node. It could be a simple data class. However, the parser currently assumes that every pointer in its parser stack is an AST node, and touching that part would be a larger undertaking. As we use a pool allocator, the cost is not that high (though we use more memory from the pool then we would strictly need to). Change-Id: Ia1d9fd7a6553d443cc57bc3d773f5be0aebe0e0e Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'src/qml/parser/qqmljsast.cpp')
-rw-r--r--src/qml/parser/qqmljsast.cpp17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/qml/parser/qqmljsast.cpp b/src/qml/parser/qqmljsast.cpp
index a068a412df..279ed62956 100644
--- a/src/qml/parser/qqmljsast.cpp
+++ b/src/qml/parser/qqmljsast.cpp
@@ -37,12 +37,15 @@
**
****************************************************************************/
+#include <QString>
#include <QLocale>
#include "qqmljsast_p.h"
#include "qqmljsastvisitor_p.h"
#include <qlocale.h>
+#include <algorithm>
+
QT_BEGIN_NAMESPACE
namespace QQmlJS { namespace AST {
@@ -1648,6 +1651,20 @@ void UiAnnotation::accept0(BaseVisitor *visitor)
visitor->endVisit(this);
}
+SourceLocation UiPropertyAttributes::firstSourceLocation() const
+{
+ std::array<const SourceLocation *, 4> tokens {&m_propertyToken, &m_defaultToken, &m_readonlyToken, &m_requiredToken};
+ const auto it = std::min_element(tokens.begin(), tokens.end(), compareLocationsByBegin<true>);
+ return **it;
+}
+
+SourceLocation UiPropertyAttributes::lastSourceLocation() const
+{
+ std::array<const SourceLocation *, 4> tokens {&m_propertyToken, &m_defaultToken, &m_readonlyToken, &m_requiredToken};
+ const auto it = std::max_element(tokens.begin(), tokens.end(), compareLocationsByBegin<false>);
+ return **it;
+}
+
} } // namespace QQmlJS::AST
QT_END_NAMESPACE