mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
Merge pull request #36230 from ahoppen/pr/ref-count-syntaxdata
[libSyntax] Reference count SyntaxData
This commit is contained in:
@@ -54,10 +54,10 @@ const auto NoParent = llvm::None;
|
||||
/// convenience methods based on the node's kind.
|
||||
class Syntax {
|
||||
protected:
|
||||
SyntaxData Data;
|
||||
RC<const SyntaxData> Data;
|
||||
|
||||
public:
|
||||
explicit Syntax(const SyntaxData Data) : Data(Data) {}
|
||||
explicit Syntax(const RC<const SyntaxData> &Data) : Data(Data) {}
|
||||
|
||||
virtual ~Syntax() {}
|
||||
|
||||
@@ -84,9 +84,7 @@ public:
|
||||
}
|
||||
|
||||
/// Get the Data for this Syntax node.
|
||||
const SyntaxData &getData() const {
|
||||
return Data;
|
||||
}
|
||||
const RC<const SyntaxData> &getData() const { return Data; }
|
||||
|
||||
/// Cast this Syntax node to a more specific type, asserting it's of the
|
||||
/// right kind.
|
||||
@@ -111,7 +109,7 @@ public:
|
||||
|
||||
/// Returns the child index of this node in its parent,
|
||||
/// if it has one, otherwise 0.
|
||||
CursorIndex getIndexInParent() const { return getData().getIndexInParent(); }
|
||||
CursorIndex getIndexInParent() const { return getData()->getIndexInParent(); }
|
||||
|
||||
/// Return the number of bytes this node takes when spelled out in the source
|
||||
size_t getTextLength() const { return getRaw()->getTextLength(); }
|
||||
@@ -155,8 +153,8 @@ public:
|
||||
SWIFT_DEBUG_DUMP;
|
||||
|
||||
bool hasSameIdentityAs(const Syntax &Other) const {
|
||||
return Data.getAbsoluteRaw().getNodeId() ==
|
||||
Other.Data.getAbsoluteRaw().getNodeId();
|
||||
return Data->getAbsoluteRaw().getNodeId() ==
|
||||
Other.Data->getAbsoluteRaw().getNodeId();
|
||||
}
|
||||
|
||||
static bool kindof(SyntaxKind Kind) {
|
||||
@@ -178,23 +176,23 @@ public:
|
||||
|
||||
/// Get the offset at which the leading trivia of this node starts.
|
||||
AbsoluteOffsetPosition getAbsolutePositionBeforeLeadingTrivia() const {
|
||||
return Data.getAbsolutePositionBeforeLeadingTrivia();
|
||||
return Data->getAbsolutePositionBeforeLeadingTrivia();
|
||||
}
|
||||
|
||||
/// Get the offset at which the actual content (i.e. non-triva) of this node
|
||||
/// starts.
|
||||
AbsoluteOffsetPosition getAbsolutePositionAfterLeadingTrivia() const {
|
||||
return Data.getAbsolutePositionAfterLeadingTrivia();
|
||||
return Data->getAbsolutePositionAfterLeadingTrivia();
|
||||
}
|
||||
|
||||
/// Get the offset at which the trailing trivia of this node starts.
|
||||
AbsoluteOffsetPosition getAbsoluteEndPositionBeforeTrailingTrivia() const {
|
||||
return Data.getAbsoluteEndPositionBeforeTrailingTrivia();
|
||||
return Data->getAbsoluteEndPositionBeforeTrailingTrivia();
|
||||
}
|
||||
|
||||
/// Get the offset at which the trailing trivia of this node starts.
|
||||
AbsoluteOffsetPosition getAbsoluteEndPositionAfterTrailingTrivia() const {
|
||||
return Data.getAbsoluteEndPositionAfterTrailingTrivia();
|
||||
return Data->getAbsoluteEndPositionAfterTrailingTrivia();
|
||||
}
|
||||
|
||||
// TODO: hasSameStructureAs ?
|
||||
|
||||
@@ -56,8 +56,8 @@ class SyntaxCollection : public Syntax {
|
||||
friend class Syntax;
|
||||
|
||||
private:
|
||||
static SyntaxData makeData(std::initializer_list<Element> &Elements,
|
||||
const RC<SyntaxArena> &Arena) {
|
||||
static RC<const SyntaxData> makeData(std::initializer_list<Element> &Elements,
|
||||
const RC<SyntaxArena> &Arena) {
|
||||
std::vector<const RawSyntax *> List;
|
||||
List.reserve(Elements.size());
|
||||
for (auto &Elt : Elements)
|
||||
@@ -68,7 +68,7 @@ private:
|
||||
}
|
||||
|
||||
public:
|
||||
SyntaxCollection(const SyntaxData Data) : Syntax(Data) {}
|
||||
SyntaxCollection(const RC<const SyntaxData> &Data) : Syntax(Data) {}
|
||||
|
||||
SyntaxCollection(std::initializer_list<Element> list):
|
||||
SyntaxCollection(SyntaxCollection::makeData(list)) {}
|
||||
@@ -104,7 +104,7 @@ public:
|
||||
Element operator[](const size_t Index) const {
|
||||
assert(Index < size());
|
||||
assert(!empty());
|
||||
return Element(*Data.getChild(Index));
|
||||
return Element(Data->getChild(Index));
|
||||
}
|
||||
|
||||
/// Return a new collection with the given element added to the end.
|
||||
@@ -118,7 +118,7 @@ public:
|
||||
auto Raw = RawSyntax::makeAndCalcLength(CollectionKind, NewLayout,
|
||||
getRaw()->getPresence(),
|
||||
getRaw()->getArena());
|
||||
return SyntaxCollection<CollectionKind, Element>(Data.replacingSelf(Raw));
|
||||
return SyntaxCollection<CollectionKind, Element>(Data->replacingSelf(Raw));
|
||||
}
|
||||
|
||||
/// Return a new collection with an element removed from the end.
|
||||
@@ -130,7 +130,7 @@ public:
|
||||
auto Raw = RawSyntax::makeAndCalcLength(CollectionKind, NewLayout,
|
||||
getRaw()->getPresence(),
|
||||
getRaw()->getArena());
|
||||
return SyntaxCollection<CollectionKind, Element>(Data.replacingSelf(Raw));
|
||||
return SyntaxCollection<CollectionKind, Element>(Data->replacingSelf(Raw));
|
||||
}
|
||||
|
||||
/// Return a new collection with the given element appended to the front.
|
||||
@@ -143,7 +143,7 @@ public:
|
||||
auto Raw = RawSyntax::makeAndCalcLength(CollectionKind, NewLayout,
|
||||
getRaw()->getPresence(),
|
||||
getRaw()->getArena());
|
||||
return SyntaxCollection<CollectionKind, Element>(Data.replacingSelf(Raw));
|
||||
return SyntaxCollection<CollectionKind, Element>(Data->replacingSelf(Raw));
|
||||
}
|
||||
|
||||
/// Return a new collection with an element removed from the end.
|
||||
@@ -155,7 +155,7 @@ public:
|
||||
auto Raw = RawSyntax::makeAndCalcLength(CollectionKind, NewLayout,
|
||||
getRaw()->getPresence(),
|
||||
getRaw()->getArena());
|
||||
return SyntaxCollection<CollectionKind, Element>(Data.replacingSelf(Raw));
|
||||
return SyntaxCollection<CollectionKind, Element>(Data->replacingSelf(Raw));
|
||||
}
|
||||
|
||||
/// Return a new collection with the Element inserted at index i.
|
||||
@@ -176,7 +176,7 @@ public:
|
||||
auto Raw = RawSyntax::makeAndCalcLength(CollectionKind, NewLayout,
|
||||
getRaw()->getPresence(),
|
||||
getRaw()->getArena());
|
||||
return SyntaxCollection<CollectionKind, Element>(Data.replacingSelf(Raw));
|
||||
return SyntaxCollection<CollectionKind, Element>(Data->replacingSelf(Raw));
|
||||
}
|
||||
|
||||
/// Return a new collection with the element removed at index i.
|
||||
@@ -189,14 +189,14 @@ public:
|
||||
auto Raw = RawSyntax::makeAndCalcLength(CollectionKind, NewLayout,
|
||||
getRaw()->getPresence(),
|
||||
getRaw()->getArena());
|
||||
return SyntaxCollection<CollectionKind, Element>(Data.replacingSelf(Raw));
|
||||
return SyntaxCollection<CollectionKind, Element>(Data->replacingSelf(Raw));
|
||||
}
|
||||
|
||||
/// Return an empty syntax collection of this type.
|
||||
SyntaxCollection<CollectionKind, Element> cleared() const {
|
||||
auto Raw = RawSyntax::makeAndCalcLength(
|
||||
CollectionKind, {}, getRaw()->getPresence(), getRaw()->getArena());
|
||||
return SyntaxCollection<CollectionKind, Element>(Data.replacingSelf(Raw));
|
||||
return SyntaxCollection<CollectionKind, Element>(Data->replacingSelf(Raw));
|
||||
}
|
||||
|
||||
static bool kindof(SyntaxKind Kind) {
|
||||
|
||||
@@ -51,20 +51,6 @@
|
||||
namespace swift {
|
||||
namespace syntax {
|
||||
|
||||
/// A reference counted box that can contain any type.
|
||||
template <typename T>
|
||||
class RefCountedBox final
|
||||
: public llvm::ThreadSafeRefCountedBase<RefCountedBox<T>> {
|
||||
public:
|
||||
const T Data;
|
||||
|
||||
RefCountedBox(const T Data) : Data(Data) {}
|
||||
|
||||
static RC<RefCountedBox<T>> make(const T &Data) {
|
||||
return RC<RefCountedBox<T>>{new RefCountedBox(Data)};
|
||||
}
|
||||
};
|
||||
|
||||
/// The class for holding parented syntax.
|
||||
///
|
||||
/// This structure should not contain significant public
|
||||
@@ -72,9 +58,9 @@ public:
|
||||
///
|
||||
/// It is essentially a wrapper around \c AbsoluteRawSyntax that also keeps
|
||||
/// track of the parent.
|
||||
class SyntaxData {
|
||||
class SyntaxData : public llvm::ThreadSafeRefCountedBase<SyntaxData> {
|
||||
const AbsoluteRawSyntax AbsoluteRaw;
|
||||
RC<RefCountedBox<SyntaxData>> Parent;
|
||||
RC<const SyntaxData> Parent;
|
||||
|
||||
/// If this node is the root of a Syntax tree (i.e. \c Parent is \c nullptr ),
|
||||
/// the arena in which this node's \c RawSyntax node has been allocated.
|
||||
@@ -82,8 +68,7 @@ class SyntaxData {
|
||||
RC<SyntaxArena> Arena;
|
||||
|
||||
/// Create a intermediate node with a parent.
|
||||
SyntaxData(AbsoluteRawSyntax AbsoluteRaw,
|
||||
const RC<RefCountedBox<SyntaxData>> &Parent)
|
||||
SyntaxData(AbsoluteRawSyntax AbsoluteRaw, const RC<const SyntaxData> &Parent)
|
||||
: AbsoluteRaw(AbsoluteRaw), Parent(Parent), Arena(nullptr) {}
|
||||
|
||||
/// Create a new root node
|
||||
@@ -94,36 +79,36 @@ class SyntaxData {
|
||||
public:
|
||||
/// With a new \c RawSyntax node, create a new node from this one and
|
||||
/// recursively rebuild the parental chain up to the root.
|
||||
SyntaxData replacingSelf(const RawSyntax *NewRaw) const;
|
||||
RC<const SyntaxData> replacingSelf(const RawSyntax *NewRaw) const;
|
||||
|
||||
/// Replace a child in the raw syntax and recursively rebuild the
|
||||
/// parental chain up to the root.
|
||||
template <typename CursorType>
|
||||
SyntaxData replacingChild(const RawSyntax *RawChild,
|
||||
CursorType ChildCursor) const {
|
||||
RC<const SyntaxData> replacingChild(const RawSyntax *RawChild,
|
||||
CursorType ChildCursor) const {
|
||||
auto NewRaw = AbsoluteRaw.getRaw()->replacingChild(ChildCursor, RawChild);
|
||||
return replacingSelf(NewRaw);
|
||||
}
|
||||
|
||||
/// Get the node immediately before this current node that does contain a
|
||||
/// non-missing token. Return \c None if we cannot find such node.
|
||||
Optional<SyntaxData> getPreviousNode() const;
|
||||
/// non-missing token. Return \c nullptr if we cannot find such node.
|
||||
RC<const SyntaxData> getPreviousNode() const;
|
||||
|
||||
/// Get the node immediately after this current node that does contain a
|
||||
/// non-missing token. Return \c None if we cannot find such node.
|
||||
Optional<SyntaxData> getNextNode() const;
|
||||
/// non-missing token. Return \c nullptr if we cannot find such node.
|
||||
RC<const SyntaxData> getNextNode() const;
|
||||
|
||||
/// Get the first non-missing token node in this tree. Return \c None if
|
||||
/// Get the first non-missing token node in this tree. Return \c nullptr if
|
||||
/// this node does not contain non-missing tokens.
|
||||
Optional<SyntaxData> getFirstToken() const;
|
||||
RC<const SyntaxData> getFirstToken() const;
|
||||
|
||||
/// Get the last non-missing token node in this tree. Return \c None if
|
||||
/// Get the last non-missing token node in this tree. Return \c nullptr if
|
||||
/// this node does not contain non-missing tokens.
|
||||
Optional<SyntaxData> getLastToken() const;
|
||||
RC<const SyntaxData> getLastToken() const;
|
||||
|
||||
/// Make a new \c SyntaxData node for the tree's root.
|
||||
static SyntaxData makeRoot(AbsoluteRawSyntax AbsoluteRaw) {
|
||||
return SyntaxData(AbsoluteRaw);
|
||||
static RC<const SyntaxData> makeRoot(AbsoluteRawSyntax AbsoluteRaw) {
|
||||
return RC<const SyntaxData>(new SyntaxData(AbsoluteRaw));
|
||||
}
|
||||
|
||||
const AbsoluteRawSyntax &getAbsoluteRaw() const { return AbsoluteRaw; }
|
||||
@@ -135,13 +120,7 @@ public:
|
||||
SyntaxKind getKind() const { return AbsoluteRaw.getRaw()->getKind(); }
|
||||
|
||||
/// Return the parent syntax if there is one.
|
||||
Optional<SyntaxData> getParent() const {
|
||||
if (Parent) {
|
||||
return Parent->Data;
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
RC<const SyntaxData> getParent() const { return Parent; }
|
||||
|
||||
/// Returns true if this syntax node has a parent.
|
||||
bool hasParent() const {
|
||||
@@ -161,13 +140,13 @@ public:
|
||||
|
||||
/// Gets the child at the index specified by the provided cursor.
|
||||
template <typename CursorType>
|
||||
Optional<SyntaxData> getChild(CursorType Cursor) const {
|
||||
RC<const SyntaxData> getChild(CursorType Cursor) const {
|
||||
return getChild(
|
||||
(AbsoluteSyntaxPosition::IndexInParentType)cursorIndex(Cursor));
|
||||
}
|
||||
|
||||
/// Gets the child at the specified \p Index.
|
||||
Optional<SyntaxData>
|
||||
RC<const SyntaxData>
|
||||
getChild(AbsoluteSyntaxPosition::IndexInParentType Index) const;
|
||||
|
||||
/// Get the offset at which the leading trivia of this node starts.
|
||||
|
||||
@@ -69,7 +69,7 @@ public:
|
||||
};
|
||||
% end
|
||||
|
||||
${node.name}(const SyntaxData Data) : ${node.base_type}(Data) {
|
||||
${node.name}(const RC<const SyntaxData> &Data) : ${node.base_type}(Data) {
|
||||
% if node.requires_validation():
|
||||
this->validate();
|
||||
% end
|
||||
|
||||
@@ -33,7 +33,7 @@ protected:
|
||||
assert(getRaw()->isToken());
|
||||
}
|
||||
public:
|
||||
TokenSyntax(const SyntaxData Data) : Syntax(Data) {}
|
||||
TokenSyntax(const RC<const SyntaxData> &Data) : Syntax(Data) {}
|
||||
|
||||
static TokenSyntax missingToken(const tok Kind, StringRef Text,
|
||||
const RC<SyntaxArena> &Arena) {
|
||||
@@ -50,12 +50,12 @@ public:
|
||||
|
||||
TokenSyntax withLeadingTrivia(StringRef Trivia) const {
|
||||
auto NewRaw = getRaw()->withLeadingTrivia(Trivia);
|
||||
return TokenSyntax(getData().replacingSelf(NewRaw));
|
||||
return TokenSyntax(getData()->replacingSelf(NewRaw));
|
||||
}
|
||||
|
||||
TokenSyntax withTrailingTrivia(StringRef Trivia) const {
|
||||
auto NewRaw = getRaw()->withTrailingTrivia(Trivia);
|
||||
return TokenSyntax(getData().replacingSelf(NewRaw));
|
||||
return TokenSyntax(getData()->replacingSelf(NewRaw));
|
||||
}
|
||||
|
||||
/* TODO: If we really need them.
|
||||
|
||||
@@ -29,7 +29,7 @@ namespace syntax {
|
||||
class UnknownSyntax : public Syntax {
|
||||
void validate() const;
|
||||
public:
|
||||
UnknownSyntax(const SyntaxData Data) : Syntax(Data) {}
|
||||
UnknownSyntax(const RC<const SyntaxData> &Data) : Syntax(Data) {}
|
||||
|
||||
static bool classof(const Syntax *S) {
|
||||
return S->isUnknown();
|
||||
|
||||
Reference in New Issue
Block a user