1 | // Copyright (C) 2023 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
3 | |
4 | #include "qqmlsasourcelocation.h" |
5 | #include "qqmlsasourcelocation_p.h" |
6 | |
7 | QT_BEGIN_NAMESPACE |
8 | |
9 | using namespace Qt::StringLiterals; |
10 | |
11 | namespace QQmlSA { |
12 | |
13 | static_assert(SourceLocationPrivate::sizeOfSourceLocation() == sizeof(SourceLocation)); |
14 | |
15 | /*! |
16 | \class QQmlSA::SourceLocation |
17 | \inmodule QtQmlCompiler |
18 | |
19 | \brief Represents a location or region in the source code. |
20 | */ |
21 | |
22 | /*! |
23 | Constructs a new SourceLocation with values given by \a offset, \a length, |
24 | \a line, and \a column. |
25 | */ |
26 | QQmlSA::SourceLocation::SourceLocation(quint32 offset, quint32 length, quint32 line, quint32 column) |
27 | { |
28 | new (m_data) QQmlJS::SourceLocation{ offset, length, line, column }; |
29 | } |
30 | |
31 | // explicitly defaulted out-of-line for PIMPL |
32 | /*! |
33 | Creates a copy of \a other. |
34 | */ |
35 | QQmlSA::SourceLocation::SourceLocation(const SourceLocation &other) = default; |
36 | |
37 | /*! |
38 | \fn SourceLocation::SourceLocation(SourceLocation &&other) noexcept |
39 | Move-Constructs a SourceLocation from \a other. |
40 | */ |
41 | |
42 | /*! |
43 | Assigns \a other to this SourceLocation. |
44 | */ |
45 | QQmlSA::SourceLocation & QQmlSA::SourceLocation::operator=(const QQmlSA::SourceLocation &other) = default; |
46 | |
47 | /*! |
48 | \fn SourceLocation &SourceLocation::operator=(SourceLocation &&other) noexcept |
49 | Move-assigns \a other to this SourceLocation. |
50 | */ |
51 | |
52 | /*! |
53 | Destructs this SourceLocation instance. |
54 | */ |
55 | SourceLocation::~SourceLocation() = default; |
56 | |
57 | /*! |
58 | Returns \c true is this SourceLocation is valid, \c false otherwise. |
59 | */ |
60 | bool QQmlSA::SourceLocation::isValid() const |
61 | { |
62 | return QQmlSA::SourceLocationPrivate::sourceLocation(sourceLocation: *this).isValid(); |
63 | } |
64 | |
65 | /*! |
66 | Returns the offset of the beginning of this source location. |
67 | */ |
68 | quint32 QQmlSA::SourceLocation::begin() const |
69 | { |
70 | return QQmlSA::SourceLocationPrivate::sourceLocation(sourceLocation: *this).begin(); |
71 | } |
72 | |
73 | /*! |
74 | Returns the offset of the end of this source location. |
75 | */ |
76 | quint32 QQmlSA::SourceLocation::end() const |
77 | { |
78 | return QQmlSA::SourceLocationPrivate::sourceLocation(sourceLocation: *this).end(); |
79 | } |
80 | |
81 | /*! |
82 | Returns the offset of the beginning of this source location. |
83 | */ |
84 | quint32 QQmlSA::SourceLocation::offset() const |
85 | { |
86 | return QQmlSA::SourceLocationPrivate::sourceLocation(sourceLocation: *this).offset; |
87 | } |
88 | |
89 | /*! |
90 | Returns the length of this source location. |
91 | */ |
92 | quint32 QQmlSA::SourceLocation::length() const |
93 | { |
94 | return QQmlSA::SourceLocationPrivate::sourceLocation(sourceLocation: *this).length; |
95 | } |
96 | |
97 | /*! |
98 | Returns the line number containing the beginning of this source location. |
99 | */ |
100 | quint32 QQmlSA::SourceLocation::startLine() const |
101 | { |
102 | return QQmlSA::SourceLocationPrivate::sourceLocation(sourceLocation: *this).startLine; |
103 | } |
104 | |
105 | /*! |
106 | Returns the column number containing the beginning of this source location. |
107 | */ |
108 | quint32 QQmlSA::SourceLocation::startColumn() const |
109 | { |
110 | return QQmlSA::SourceLocationPrivate::sourceLocation(sourceLocation: *this).startColumn; |
111 | } |
112 | |
113 | /*! |
114 | Returns a source location of lenth zero pointing to the beginning of this |
115 | source location. |
116 | */ |
117 | QQmlSA::SourceLocation QQmlSA::SourceLocation::startZeroLengthLocation() const |
118 | { |
119 | QQmlSA::SourceLocation saLocation; |
120 | auto &wrappedLocation = reinterpret_cast<QQmlJS::SourceLocation &>(saLocation.m_data); |
121 | wrappedLocation = |
122 | QQmlSA::SourceLocationPrivate::sourceLocation(sourceLocation: *this).startZeroLengthLocation(); |
123 | |
124 | return saLocation; |
125 | } |
126 | |
127 | /*! |
128 | Returns a source location of lenth zero pointing to the end of this source |
129 | location pointing to \a text. |
130 | */ |
131 | QQmlSA::SourceLocation QQmlSA::SourceLocation::endZeroLengthLocation(QStringView text) const |
132 | { |
133 | QQmlSA::SourceLocation saLocation; |
134 | auto &wrappedLocation = reinterpret_cast<QQmlJS::SourceLocation &>(saLocation.m_data); |
135 | wrappedLocation = wrappedLocation.endZeroLengthLocation(text); |
136 | |
137 | return saLocation; |
138 | } |
139 | |
140 | /*! |
141 | \fn friend qsizetype SourceLocation::qHash(const SourceLocation &location, qsizetype seed) |
142 | Returns the hash value for \a location, using \a seed to seed the calculation. |
143 | */ |
144 | |
145 | /*! |
146 | \fn friend bool SourceLocation::operator==(const SourceLocation &lhs, const SourceLocation &rhs) |
147 | Returns true if \a lhs equals \a rhs, and \c false otherwise. |
148 | Two SourceLocations are considered equal if they have the same values for |
149 | their offset, length, line, and column members. |
150 | */ |
151 | /*! |
152 | \fn friend bool SourceLocation::operator!=(const SourceLocation &lhs, const SourceLocation &rhs) |
153 | Returns true if \a lhs does not equal \a rhs, and \c false otherwise. |
154 | See \l {SourceLocation::operator==} for when two source locations are considered equal. |
155 | */ |
156 | |
157 | qsizetype QQmlSA::SourceLocation::qHashImpl(const SourceLocation &location, qsizetype seed) |
158 | { |
159 | return qHash(location: QQmlSA::SourceLocationPrivate::sourceLocation(sourceLocation: location), seed); |
160 | } |
161 | |
162 | bool QQmlSA::SourceLocation::operatorEqualsImpl(const SourceLocation &lhs, |
163 | const SourceLocation &rhs) |
164 | { |
165 | return QQmlSA::SourceLocationPrivate::sourceLocation(sourceLocation: lhs) |
166 | == QQmlSA::SourceLocationPrivate::sourceLocation(sourceLocation: rhs); |
167 | } |
168 | |
169 | } // namespace QQmlSA |
170 | |
171 | QT_END_NAMESPACE |
172 | |