mirror of
https://github.com/apple/swift.git
synced 2026-06-20 15:42:51 +02:00
425cd0ebd7
**Overview**: This PR introduces the basic infrastructure needed to eventually migrate derived macros generation from hand-crafted AST nodes to macros. No conformance have been migrated yet. **Motivation**: Derived conformances (e.g. `Equatable`, `Hashable`, `Codable`, ...) are currently implemented as a special case in the compiler, producing synthetic AST nodes directly. Migrating this to macros will hopefully unify the code path with the existing macro expansion infrastructure, make conformance synthesis easier to extend and test as well as reducing the amount of special cases in the compiler. **Changes**: - New experimental feature flag `DeriveConformancesViaMacros`: Introduces the flag that will eventually gate the new derived conformance code paths. It does not control any behaviour for the moment as none have been migrated yet but this enables future changes to be built incrementally. - New GeneratedSourceInfo and SourceFile kinds `SyntheticMacro`: Introduces new GSI and SourceFile kinds named `SyntheticMacro` to represent macros synthesized by the compiler. Since macros need a real buffer to expand, this is the kind of source file and GSI associated with those buffers. - Conformance derivation via macros API: Introduces the `deriveRequirementViaMacro` function that produces the required witness via macro expansion. See https://github.com/swiftlang/llvm-project/pull/13124 for llvm-related changes. **Next steps**: - Macros do not contain any semantic information, especially regarding types. Therefore it is necessary to provide them with type information as an argument so they can eventually derive the conformances. A separate PR is being created to generate this type information as strings containing swift-parsable code for easy parsing on the macro end. - Implement derived conformance synthesis for individual protocols using the new infrastructure, like `Equatable` or `Hashable` for starters. - Wire the experimental flag to gate the new path once an implementation exists --------- Co-authored-by: Hamish Knight <hamish_knight@apple.com>
187 lines
6.1 KiB
C++
187 lines
6.1 KiB
C++
//===--- ASTNode.cpp - Swift Language ASTs --------------------------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file implements the ASTNode, which is a union of Stmt, Expr, and Decl.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "swift/AST/ASTNode.h"
|
|
#include "swift/AST/Decl.h"
|
|
#include "swift/AST/Expr.h"
|
|
#include "swift/AST/Stmt.h"
|
|
#include "swift/AST/Pattern.h"
|
|
#include "swift/AST/TypeRepr.h"
|
|
#include "swift/Basic/Assertions.h"
|
|
#include "swift/Basic/SourceLoc.h"
|
|
#include "swift/Parse/Token.h"
|
|
|
|
using namespace swift;
|
|
|
|
SourceRange ASTNode::getSourceRange() const {
|
|
if (const auto *E = this->dyn_cast<Expr*>())
|
|
return E->getSourceRange();
|
|
if (const auto *S = this->dyn_cast<Stmt*>())
|
|
return S->getSourceRange();
|
|
if (const auto *D = this->dyn_cast<Decl*>())
|
|
return D->getSourceRange();
|
|
if (const auto *P = this->dyn_cast<Pattern*>())
|
|
return P->getSourceRange();
|
|
if (const auto *T = this->dyn_cast<TypeRepr *>())
|
|
return T->getSourceRange();
|
|
if (const auto *C = this->dyn_cast<StmtConditionElement *>())
|
|
return C->getSourceRange();
|
|
if (const auto *I = this->dyn_cast<CaseLabelItem *>()) {
|
|
return I->getSourceRange();
|
|
}
|
|
assert(!isNull() && "Null ASTNode doesn't have a source range");
|
|
llvm_unreachable("unsupported AST node");
|
|
}
|
|
|
|
/// Return the location of the start of the statement.
|
|
SourceLoc ASTNode::getStartLoc() const {
|
|
return getSourceRange().Start;
|
|
}
|
|
|
|
/// Return the location of the end of the statement.
|
|
SourceLoc ASTNode::getEndLoc() const {
|
|
return getSourceRange().End;
|
|
}
|
|
|
|
DeclContext *ASTNode::getAsDeclContext() const {
|
|
if (auto *E = this->dyn_cast<Expr*>()) {
|
|
if (isa<AbstractClosureExpr>(E))
|
|
return static_cast<AbstractClosureExpr*>(E);
|
|
} else if (isa<Stmt *>(*this)) {
|
|
return nullptr;
|
|
} else if (auto *D = this->dyn_cast<Decl *>()) {
|
|
if (isa<DeclContext>(D))
|
|
return cast<DeclContext>(D);
|
|
} else if (getOpaqueValue())
|
|
llvm_unreachable("unsupported AST node");
|
|
return nullptr;
|
|
}
|
|
|
|
bool ASTNode::isImplicit() const {
|
|
if (const auto *E = this->dyn_cast<Expr*>())
|
|
return E->isImplicit();
|
|
if (const auto *S = this->dyn_cast<Stmt*>())
|
|
return S->isImplicit();
|
|
if (const auto *D = this->dyn_cast<Decl*>())
|
|
return D->isImplicit();
|
|
if (const auto *P = this->dyn_cast<Pattern*>())
|
|
return P->isImplicit();
|
|
if (isa<TypeRepr *>(*this))
|
|
return false;
|
|
if (isa<StmtConditionElement *>(*this))
|
|
return false;
|
|
if (isa<CaseLabelItem *>(*this))
|
|
return false;
|
|
llvm_unreachable("unsupported AST node");
|
|
}
|
|
|
|
void ASTNode::walk(ASTWalker &Walker) {
|
|
if (auto *E = this->dyn_cast<Expr*>())
|
|
E->walk(Walker);
|
|
else if (auto *S = this->dyn_cast<Stmt*>())
|
|
S->walk(Walker);
|
|
else if (auto *D = this->dyn_cast<Decl*>())
|
|
D->walk(Walker);
|
|
else if (auto *P = this->dyn_cast<Pattern*>())
|
|
P->walk(Walker);
|
|
else if (auto *T = this->dyn_cast<TypeRepr*>())
|
|
T->walk(Walker);
|
|
else if (auto *C = this->dyn_cast<StmtConditionElement *>())
|
|
C->walk(Walker);
|
|
else if (auto *I = this->dyn_cast<CaseLabelItem *>()) {
|
|
if (auto *P = I->getPattern())
|
|
P->walk(Walker);
|
|
|
|
if (auto *G = I->getGuardExpr())
|
|
G->walk(Walker);
|
|
} else
|
|
llvm_unreachable("unsupported AST node");
|
|
}
|
|
|
|
void ASTNode::dump(raw_ostream &OS, unsigned Indent) const {
|
|
if (isNull())
|
|
OS << "(null)";
|
|
else if (auto S = dyn_cast<Stmt*>())
|
|
S->dump(OS, /*context=*/nullptr, Indent);
|
|
else if (auto E = dyn_cast<Expr*>())
|
|
E->dump(OS, Indent);
|
|
else if (auto D = dyn_cast<Decl*>())
|
|
D->dump(OS, Indent);
|
|
else if (auto P = dyn_cast<Pattern*>())
|
|
P->dump(OS, Indent);
|
|
else if (auto T = dyn_cast<TypeRepr*>())
|
|
T->print(OS);
|
|
else if (isa<StmtConditionElement *>(*this))
|
|
OS.indent(Indent) << "(statement condition)";
|
|
else if (isa<CaseLabelItem *>(*this)) {
|
|
OS.indent(Indent) << "(case label item)";
|
|
} else
|
|
llvm_unreachable("unsupported AST node");
|
|
}
|
|
|
|
void ASTNode::dump() const {
|
|
dump(llvm::errs());
|
|
}
|
|
|
|
StringRef swift::getTokenText(tok kind) {
|
|
switch(kind) {
|
|
#define KEYWORD(KW) case tok::kw_##KW: return #KW;
|
|
#define POUND_KEYWORD(KW) case tok::pound_##KW: return "#"#KW;
|
|
#define PUNCTUATOR(PUN, TEXT) case tok::PUN: return TEXT;
|
|
#include "swift/AST/TokenKinds.def"
|
|
default: return StringRef();
|
|
}
|
|
}
|
|
|
|
#define FUNC(T) \
|
|
bool ASTNode::is##T(T##Kind Kind) const { \
|
|
if (!isa<T *>(*this)) \
|
|
return false; \
|
|
return cast<T *>(*this)->getKind() == Kind; \
|
|
}
|
|
FUNC(Stmt)
|
|
FUNC(Expr)
|
|
FUNC(Decl)
|
|
FUNC(Pattern)
|
|
#undef FUNC
|
|
|
|
SourceRange swift::getUnexpandedMacroRange(const SourceManager &SM,
|
|
SourceRange range) {
|
|
unsigned bufferID = SM.findBufferContainingLoc(range.Start);
|
|
SourceRange outerRange;
|
|
while (const auto *info = SM.getGeneratedSourceInfo(bufferID)) {
|
|
switch (info->kind) {
|
|
#define MACRO_ROLE(Name, Description) \
|
|
case GeneratedSourceInfo::Name##MacroExpansion:
|
|
#include "swift/Basic/MacroRoles.def"
|
|
if (auto *customAttr = info->attachedMacroCustomAttr)
|
|
outerRange = customAttr->getRange();
|
|
else
|
|
outerRange =
|
|
ASTNode::getFromOpaqueValue(info->astNode).getSourceRange();
|
|
bufferID = SM.findBufferContainingLoc(outerRange.Start);
|
|
continue;
|
|
case GeneratedSourceInfo::ReplacedFunctionBody:
|
|
case GeneratedSourceInfo::PrettyPrinted:
|
|
case GeneratedSourceInfo::DefaultArgument:
|
|
case GeneratedSourceInfo::AttributeFromClang:
|
|
case GeneratedSourceInfo::SyntheticMacro:
|
|
return SourceRange();
|
|
}
|
|
}
|
|
return outerRange;
|
|
}
|