Files
swift-mirror/lib/SIL/SILLocation.cpp
Anna Zaks 4d50044dd8 Add MandatoryInlinedLocation SIL location type.
This will allow us to differentiate from regular inlined code (as in optimization inlining). Some passes(AllocBocToStack) and debug info would be allowed to be more aggressive/preserve less debug info with mandatory inlined code.

Swift SVN r9339
2013-10-15 00:17:51 +00:00

165 lines
5.3 KiB
C++

//===--- SILLocation.cpp - Location information for SIL nodes -------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/SIL/SILLocation.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Expr.h"
#include "swift/AST/Pattern.h"
#include "swift/AST/Stmt.h"
#include "llvm/Support/raw_ostream.h"
using namespace swift;
SourceLoc SILLocation::getSourceLoc() const {
if (ASTNode.isNull())
return SILFileSourceLoc;
if (alwaysPointsToStart()) {
return getStartSourceLoc();
}
if (alwaysPointsToEnd()) {
return getEndSourceLoc();
}
// Use the end location for the CleanupKind.
if (getKind() == CleanupKind) {
return getEndSourceLoc();
}
// Use the end location for the ImplicitReturnKind.
if (getKind() == ImplicitReturnKind) {
return getEndSourceLoc();
}
// Use the start location for the ReturnKind.
if (getKind() == ReturnKind) {
return getStartSourceLoc();
}
if (auto decl = ASTNode.dyn_cast<Decl*>())
return decl->getLoc();
if (auto expr = ASTNode.dyn_cast<Expr*>())
return expr->getLoc();
if (auto stmt = ASTNode.dyn_cast<Stmt*>())
return stmt->getStartLoc();
if (auto patt = ASTNode.dyn_cast<Pattern*>())
return patt->getStartLoc();
llvm_unreachable("impossible SILLocation");
}
SourceLoc SILLocation::getStartSourceLoc() const {
if (ASTNode.isNull())
return SILFileSourceLoc;
if (auto decl = ASTNode.dyn_cast<Decl*>())
return decl->getStartLoc();
if (auto expr = ASTNode.dyn_cast<Expr*>())
return expr->getStartLoc();
if (auto stmt = ASTNode.dyn_cast<Stmt*>())
return stmt->getStartLoc();
if (auto patt = ASTNode.dyn_cast<Pattern*>())
return patt->getStartLoc();
llvm_unreachable("impossible SILLocation");
}
SourceLoc SILLocation::getEndSourceLoc() const {
if (ASTNode.isNull())
return SILFileSourceLoc;
if (auto decl = ASTNode.dyn_cast<Decl*>())
return decl->getEndLoc();
if (auto expr = ASTNode.dyn_cast<Expr*>())
return expr->getEndLoc();
if (auto stmt = ASTNode.dyn_cast<Stmt*>())
return stmt->getEndLoc();
if (auto patt = ASTNode.dyn_cast<Pattern*>())
return patt->getEndLoc();
llvm_unreachable("impossible SILLocation");
}
void SILLocation::dump(const SourceManager &SM) const {
if (auto D = ASTNode.dyn_cast<Decl *>())
llvm::errs() << Decl::getKindName(D->getKind()) << "Decl @ ";
if (auto E = ASTNode.dyn_cast<Expr *>())
llvm::errs() << Expr::getKindName(E->getKind()) << "Expr @ ";
if (auto S = ASTNode.dyn_cast<Stmt *>())
llvm::errs() << Stmt::getKindName(S->getKind()) << "Stmt @ ";
if (auto P = ASTNode.dyn_cast<Pattern *>())
llvm::errs() << Pattern::getKindName(P->getKind()) << "Pattern @ ";
print(llvm::errs(), SM);
}
void SILLocation::print(raw_ostream &OS, const SourceManager &SM) const {
if (isNull())
OS << "<no loc>";
getSourceLoc().print(OS, SM);
}
InlinedLocation InlinedLocation::getInlinedLocation(SILLocation L) {
if (Expr *E = L.getAsASTNode<Expr>())
return InlinedLocation(E);
if (Optional<SILFileLocation> FileLoc = L.getAs<SILFileLocation>())
return InlinedLocation(FileLoc.getValue().getFileLocation());
// Otherwise, it can be an inlined location wrapping a file location.
if (Optional<InlinedLocation> InlinedLoc = L.getAs<InlinedLocation>()) {
return InlinedLocation(InlinedLoc.getValue().getFileLocation());
}
// If the body is auto-generated, the location might be wrapping a Decl.
if (Decl *D = L.getAsASTNode<Decl>()) {
assert(L.isAutoGenerated());
InlinedLocation IL(D);
IL.markAutoGenerated();
return IL;
}
llvm_unreachable("Cannot construct Inlined loc from the given location.");
}
MandatoryInlinedLocation
MandatoryInlinedLocation::getMandatoryInlinedLocation(SILLocation L) {
if (Expr *E = L.getAsASTNode<Expr>())
return MandatoryInlinedLocation(E);
if (Optional<SILFileLocation> FileLoc = L.getAs<SILFileLocation>())
return MandatoryInlinedLocation(FileLoc.getValue().getFileLocation());
// Otherwise, it can be an inlined location wrapping a file location.
if (Optional<MandatoryInlinedLocation> InlinedLoc =
L.getAs<MandatoryInlinedLocation>()) {
return MandatoryInlinedLocation(InlinedLoc.getValue().getFileLocation());
}
// If the body is auto-generated, the location might be wrapping a Decl.
if (Decl *D = L.getAsASTNode<Decl>()) {
assert(L.isAutoGenerated());
MandatoryInlinedLocation IL(D);
IL.markAutoGenerated();
return IL;
}
llvm_unreachable("Cannot construct Inlined loc from the given location.");
}
CleanupLocation CleanupLocation::getCleanupLocation(SILLocation L) {
if (Expr *E = L.getAsASTNode<Expr>())
return CleanupLocation(E);
if (Stmt *S = L.getAsASTNode<Stmt>())
return CleanupLocation(S);
if (Pattern *P = L.getAsASTNode<Pattern>())
return CleanupLocation(P);
if (Decl *D = L.getAsASTNode<Decl>())
return CleanupLocation(D);
if (L.isNull())
return CleanupLocation();
llvm_unreachable("Cannot construct Cleanup loc from the "
"given location.");
}