|
| 1 | +//===--- HiddenLibSyntaxAction.h ------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#ifndef SWIFT_PARSE_HIDDENLIBSYNTAXACTION_H |
| 14 | +#define SWIFT_PARSE_HIDDENLIBSYNTAXACTION_H |
| 15 | + |
| 16 | +#include "swift/Parse/SyntaxParseActions.h" |
| 17 | +#include "swift/SyntaxParse/SyntaxTreeCreator.h" |
| 18 | +#include "llvm/Support/Allocator.h" |
| 19 | + |
| 20 | +namespace swift { |
| 21 | +namespace syntax { |
| 22 | +class RawSyntax; |
| 23 | +} |
| 24 | + |
| 25 | +/// Holds an explicitly provided action and uses it to handle all function |
| 26 | +/// calls. Also hides an implicit SyntaxTreeCreator and ensures libSyntax nodes |
| 27 | +/// are always created. Provides an interface to map results of the explicitly |
| 28 | +/// provided action to the hidden libSyntax action. |
| 29 | +// todo [gsoc]: remove when possible |
| 30 | +class HiddenLibSyntaxAction : public SyntaxParseActions { |
| 31 | + |
| 32 | + struct Node { |
| 33 | + OpaqueSyntaxNode ExplicitActionNode; |
| 34 | + OpaqueSyntaxNode LibSyntaxNode; |
| 35 | + |
| 36 | + Node(OpaqueSyntaxNode ExplicitActionNode, OpaqueSyntaxNode LibSyntaxNode) |
| 37 | + : ExplicitActionNode(ExplicitActionNode), LibSyntaxNode(LibSyntaxNode) { |
| 38 | + } |
| 39 | + }; |
| 40 | + |
| 41 | + std::shared_ptr<SyntaxParseActions> ExplicitAction; |
| 42 | + std::shared_ptr<SyntaxTreeCreator> LibSyntaxAction; |
| 43 | + llvm::SpecificBumpPtrAllocator<Node> NodeAllocator; |
| 44 | + |
| 45 | + bool areBothLibSyntax() { |
| 46 | + return ExplicitAction->getOpaqueKind() == OpaqueSyntaxNodeKind::LibSyntax; |
| 47 | + } |
| 48 | + |
| 49 | + OpaqueSyntaxNode makeHiddenNode(OpaqueSyntaxNode explicitActionNode, |
| 50 | + OpaqueSyntaxNode libSyntaxNode); |
| 51 | + |
| 52 | +public: |
| 53 | + HiddenLibSyntaxAction( |
| 54 | + const std::shared_ptr<SyntaxParseActions> &SPActions, |
| 55 | + const std::shared_ptr<SyntaxTreeCreator> &LibSyntaxAction) |
| 56 | + : ExplicitAction(SPActions != nullptr ? SPActions : LibSyntaxAction), |
| 57 | + LibSyntaxAction(LibSyntaxAction){}; |
| 58 | + |
| 59 | + OpaqueSyntaxNode recordToken(tok tokenKind, |
| 60 | + ArrayRef<ParsedTriviaPiece> leadingTrivia, |
| 61 | + ArrayRef<ParsedTriviaPiece> trailingTrivia, |
| 62 | + CharSourceRange range) override; |
| 63 | + |
| 64 | + OpaqueSyntaxNode recordMissingToken(tok tokenKind, SourceLoc loc) override; |
| 65 | + |
| 66 | + OpaqueSyntaxNode recordRawSyntax(syntax::SyntaxKind kind, |
| 67 | + ArrayRef<OpaqueSyntaxNode> elements, |
| 68 | + CharSourceRange range) override; |
| 69 | + |
| 70 | + std::pair<size_t, OpaqueSyntaxNode> |
| 71 | + lookupNode(size_t lexerOffset, syntax::SyntaxKind kind) override; |
| 72 | + |
| 73 | + OpaqueSyntaxNodeKind getOpaqueKind() override { |
| 74 | + return ExplicitAction->getOpaqueKind(); |
| 75 | + } |
| 76 | + |
| 77 | + /// Returns the libSyntax node from the specified node that has been created |
| 78 | + /// by this action. |
| 79 | + syntax::RawSyntax *getLibSyntaxNodeFor(OpaqueSyntaxNode node); |
| 80 | + |
| 81 | + /// Returns the node created by explicit syntax action from the specified |
| 82 | + /// node that has been created by this action. |
| 83 | + OpaqueSyntaxNode getExplicitNodeFor(OpaqueSyntaxNode node); |
| 84 | + |
| 85 | + bool isReleaseNeeded() { |
| 86 | + return ExplicitAction == LibSyntaxAction || !areBothLibSyntax(); |
| 87 | + } |
| 88 | + |
| 89 | + /// Returns the underlying libSyntax SyntaxTreeCreator. |
| 90 | + std::shared_ptr<SyntaxTreeCreator> getLibSyntaxAction() { |
| 91 | + return LibSyntaxAction; |
| 92 | + } |
| 93 | +}; |
| 94 | +} // namespace swift |
| 95 | + |
| 96 | +#endif // SWIFT_PARSE_HIDDENLIBSYNTAXACTION_H |
0 commit comments