Skip to content

[Macros] Skeletal implementation of accessor macros #63023

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/swift/Basic/SourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace swift {

class CustomAttr;
class DeclContext;

/// Augments a buffer that was created specifically to hold generated source
Expand Down Expand Up @@ -59,6 +60,9 @@ class GeneratedSourceInfo {

/// The declaration context in which this buffer logically resides.
DeclContext *declContext;

/// The custom attribute for an attached macro.
CustomAttr *attachedMacroCustomAttr = nullptr;
};

/// This class manages and owns source buffers.
Expand Down
14 changes: 14 additions & 0 deletions include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,14 @@ class Parser {
bool HasLetOrVarKeyword = true);

struct ParsedAccessors;

bool parseAccessorAfterIntroducer(
SourceLoc Loc, AccessorKind Kind, ParsedAccessors &accessors,
bool &hasEffectfulGet, ParameterList *Indices, bool &parsingLimitedSyntax,
DeclAttributes &Attributes, ParseDeclOptions Flags,
AbstractStorageDecl *storage, SourceLoc StaticLoc, ParserStatus &Status
);

ParserStatus parseGetSet(ParseDeclOptions Flags, ParameterList *Indices,
TypeRepr *ResultType, ParsedAccessors &accessors,
AbstractStorageDecl *storage, SourceLoc StaticLoc);
Expand All @@ -1207,6 +1215,12 @@ class Parser {
AccessorKind currentKind,
SourceLoc const& currentLoc);

/// Parse accessors provided as a separate list, for use in macro
/// expansions.
void parseTopLevelAccessors(
AbstractStorageDecl *storage, SmallVectorImpl<ASTNode> &items
);

ParserResult<FuncDecl> parseDeclFunc(SourceLoc StaticLoc,
StaticSpellingKind StaticSpelling,
ParseDeclOptions Flags,
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/ASTScopeCreation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ void ASTScopeImpl::addChild(ASTScopeImpl *child, ASTContext &ctx) {
child->parentAndWasExpanded.setPointer(this);

#ifndef NDEBUG
checkSourceRangeBeforeAddingChild(child, ctx);
// checkSourceRangeBeforeAddingChild(child, ctx);
#endif

// If this is the first time we've added children, notify the ASTContext
Expand Down
29 changes: 23 additions & 6 deletions lib/AST/ASTScopeSourceRange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ void ASTScopeImpl::checkSourceRangeBeforeAddingChild(ASTScopeImpl *child,

auto range = getCharSourceRangeOfScope(sourceMgr);

auto childCharRange = child->getCharSourceRangeOfScope(sourceMgr);

bool childContainedInParent = [&]() {
std::function<bool(CharSourceRange)> containedInParent;
containedInParent = [&](CharSourceRange childCharRange) {
// HACK: For code completion. Handle replaced range.
for (const auto &pair : sourceMgr.getReplacedRanges()) {
auto originalRange =
Expand All @@ -65,10 +64,28 @@ void ASTScopeImpl::checkSourceRangeBeforeAddingChild(ASTScopeImpl *child,
return true;
}

return range.contains(childCharRange);
}();
if (range.contains(childCharRange))
return true;

// If this is from a macro expansion, look at the where the expansion
// occurred.
auto childBufferID =
sourceMgr.findBufferContainingLoc(childCharRange.getStart());
auto generatedInfo = sourceMgr.getGeneratedSourceInfo(childBufferID);
if (!generatedInfo)
return false;

SourceRange expansionRange = generatedInfo->originalSourceRange;
if (expansionRange.isInvalid())
return false;

return containedInParent(
Lexer::getCharSourceRangeFromSourceRange(sourceMgr, expansionRange));
};

auto childCharRange = child->getCharSourceRangeOfScope(sourceMgr);

if (!childContainedInParent) {
if (!containedInParent(childCharRange)) {
auto &out = verificationError() << "child not contained in its parent:\n";
child->print(out);
out << "\n***Parent node***\n";
Expand Down
6 changes: 5 additions & 1 deletion lib/AST/DiagnosticEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1314,7 +1314,11 @@ std::vector<Diagnostic> DiagnosticEngine::getGeneratedSourceBufferNotes(
case GeneratedSourceInfo::MacroExpansion: {
SourceRange origRange = expansionNode.getSourceRange();
DeclName macroName;
if (auto expansionExpr = dyn_cast_or_null<MacroExpansionExpr>(
if (auto customAttr = generatedInfo->attachedMacroCustomAttr) {
// FIXME: How will we handle deserialized custom attributes like this?
auto declRefType = dyn_cast<DeclRefTypeRepr>(customAttr->getTypeRepr());
macroName = declRefType->getNameRef().getFullName();
} else if (auto expansionExpr = dyn_cast_or_null<MacroExpansionExpr>(
expansionNode.dyn_cast<Expr *>())) {
macroName = expansionExpr->getMacroName().getFullName();
} else {
Expand Down
135 changes: 135 additions & 0 deletions lib/ASTGen/Sources/ASTGen/Macros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,138 @@ func evaluateMacro(

return 0
}

/// Retrieve a syntax node in the given source file, with the given type.
private func findSyntaxNodeInSourceFile<Node: SyntaxProtocol>(
sourceFilePtr: UnsafeRawPointer,
sourceLocationPtr: UnsafePointer<UInt8>?,
type: Node.Type
) -> Node? {
guard let sourceLocationPtr = sourceLocationPtr else {
return nil
}

let sourceFilePtr = sourceFilePtr.bindMemory(
to: ExportedSourceFile.self, capacity: 1
)

// Find the offset.
let buffer = sourceFilePtr.pointee.buffer
let offset = sourceLocationPtr - buffer.baseAddress!
if offset < 0 || offset >= buffer.count {
print("source location isn't inside this buffer")
return nil
}

// Find the token at that offset.
let sf = sourceFilePtr.pointee.syntax
guard let token = sf.token(at: AbsolutePosition(utf8Offset: offset)) else {
print("couldn't find token at offset \(offset)")
return nil
}

// Dig out its parent.
guard let parentSyntax = token.parent else {
print("not on a macro expansion node: \(token.recursiveDescription)")
return nil
}

return parentSyntax.as(type)
}

@_cdecl("swift_ASTGen_expandAttachedMacro")
@usableFromInline
func expandAttachedMacro(
diagEnginePtr: UnsafeMutablePointer<UInt8>,
macroPtr: UnsafeRawPointer,
customAttrSourceFilePtr: UnsafeRawPointer,
customAttrSourceLocPointer: UnsafePointer<UInt8>?,
declarationSourceFilePtr: UnsafeRawPointer,
attachedTo declarationSourceLocPointer: UnsafePointer<UInt8>?,
expandedSourcePointer: UnsafeMutablePointer<UnsafePointer<UInt8>?>,
expandedSourceLength: UnsafeMutablePointer<Int>
) -> Int {
// We didn't expand anything so far.
expandedSourcePointer.pointee = nil
expandedSourceLength.pointee = 0

// Dig out the custom attribute for the attached macro declarations.
guard let customAttrNode = findSyntaxNodeInSourceFile(
sourceFilePtr: customAttrSourceFilePtr,
sourceLocationPtr: customAttrSourceLocPointer,
type: AttributeSyntax.self
) else {
return 1
}

// Dig out the node for the declaration to which the custom attribute is
// attached.
guard let declarationNode = findSyntaxNodeInSourceFile(
sourceFilePtr: declarationSourceFilePtr,
sourceLocationPtr: declarationSourceLocPointer,
type: DeclSyntax.self
) else {
return 1
}

// Get the macro.
let macroPtr = macroPtr.bindMemory(to: ExportedMacro.self, capacity: 1)
let macro = macroPtr.pointee.macro

// FIXME: Which source file? I don't know! This should go.
let declarationSourceFilePtr = declarationSourceFilePtr.bindMemory(
to: ExportedSourceFile.self, capacity: 1
)

var context = MacroExpansionContext(
moduleName: declarationSourceFilePtr.pointee.moduleName,
fileName: declarationSourceFilePtr.pointee.fileName.withoutPath()
)

var evaluatedSyntaxStr: String
do {
switch macro {
case let attachedMacro as AccessorDeclarationMacro.Type:
let accessors = try attachedMacro.expansion(
of: customAttrNode, attachedTo: declarationNode, in: &context
)

// Form a buffer of accessor declarations to return to the caller.
evaluatedSyntaxStr = accessors.map {
$0.withoutTrivia().description
}.joined(separator: "\n\n")

default:
print("\(macroPtr) does not conform to any known attached macro protocol")
return 1
}
} catch {
// Record the error
// FIXME: Need to decide where to diagnose the error:
context.diagnose(
Diagnostic(
node: Syntax(declarationNode),
message: ThrownErrorDiagnostic(message: String(describing: error))
)
)

return 1
}

// FIXME: Emit diagnostics, but how do we figure out which source file to
// use?

// Form the result buffer for our caller.
evaluatedSyntaxStr.withUTF8 { utf8 in
let evaluatedResultPtr = UnsafeMutablePointer<UInt8>.allocate(capacity: utf8.count + 1)
if let baseAddress = utf8.baseAddress {
evaluatedResultPtr.initialize(from: baseAddress, count: utf8.count)
}
evaluatedResultPtr[utf8.count] = 0

expandedSourcePointer.pointee = UnsafePointer(evaluatedResultPtr)
expandedSourceLength.pointee = utf8.count
}

return 0
}
Loading