Skip to content

Commit c929995

Browse files
committed
[sil] Add the ability to grab an ASTNode out of a SILLocation.
Previously if one wanted to get an ASTNode, one needed to use the getAsASTNode<T>() type. If one just wants to get out the type and use it in a generic way using ASTNode there wasn't any way to do this... so I did it. We actually have to do the marshalling here since ASTNodeTy and ASTNode have different layouts despite them both being PointerUnions. So one can't just cast in between them =---(.
1 parent ca663b1 commit c929995

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

include/swift/SIL/SILLocation.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define SWIFT_SIL_LOCATION_H
1515

1616
#include "llvm/ADT/PointerUnion.h"
17+
#include "swift/AST/ASTNode.h"
1718
#include "swift/Basic/SourceLoc.h"
1819
#include "swift/SIL/SILAllocated.h"
1920
#include "swift/AST/TypeAlignments.h"
@@ -414,6 +415,26 @@ class SILLocation {
414415
return castNodeTo<T>(getPrimaryASTNode());
415416
}
416417

418+
/// If this SILLocation contains an ASTNode, return that node.
419+
ASTNode getASTNode() const {
420+
if (!isASTNode())
421+
return ASTNode();
422+
// ASTNode is a superset of PrimaryASTNode so we can just cast it, once we
423+
// remove the bit stolen by ASTNodeTy from the underlying ASTNode pointer.
424+
auto primaryNode = getPrimaryASTNode().getPointer();
425+
426+
if (auto *stmt = primaryNode.dyn_cast<Stmt *>())
427+
return {stmt};
428+
if (auto *expr = primaryNode.dyn_cast<Expr *>())
429+
return {expr};
430+
if (auto *decl = primaryNode.dyn_cast<Decl *>())
431+
return {decl};
432+
if (auto *pattern = primaryNode.dyn_cast<Pattern *>())
433+
return {pattern};
434+
435+
return ASTNode();
436+
}
437+
417438
/// Return the location as a DeclContext or null.
418439
DeclContext *getAsDeclContext() const;
419440

0 commit comments

Comments
 (0)