1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
//===--- ASTScopePrinting.cpp - Swift Object-Oriented AST Scope -----------===//
//
// 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 printing functions of the ASTScopeImpl ontology.
///
//===----------------------------------------------------------------------===//
#include "swift/AST/ASTScope.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/ASTWalker.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Expr.h"
#include "swift/AST/GenericParamList.h"
#include "swift/AST/Initializer.h"
#include "swift/AST/LazyResolver.h"
#include "swift/AST/Module.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/Pattern.h"
#include "swift/AST/SourceFile.h"
#include "swift/AST/Stmt.h"
#include "swift/AST/TypeRepr.h"
#include "swift/Basic/STLExtras.h"
#include "llvm/Support/Compiler.h"
#include <algorithm>
using namespace swift;
using namespace ast_scope;
#pragma mark dumping
void ASTScopeImpl::dump() const { print(llvm::errs(), 0, false); }
void ASTScopeImpl::dumpOneScopeMapLocation(
std::pair<unsigned, unsigned> lineColumn) {
auto bufferID = getSourceFile()->getBufferID();
if (!bufferID) {
llvm::errs() << "***No buffer, dumping all scopes***";
print(llvm::errs());
return;
}
SourceLoc loc = getSourceManager().getLocForLineCol(
*bufferID, lineColumn.first, lineColumn.second);
if (loc.isInvalid())
return;
llvm::errs() << "***Scope at " << lineColumn.first << ":" << lineColumn.second
<< "***\n";
auto *parentModule = getSourceFile()->getParentModule();
auto *locScope = findInnermostEnclosingScope(parentModule, loc, &llvm::errs());
locScope->print(llvm::errs(), 0, false, false);
namelookup::ASTScopeDeclGatherer gatherer;
// Print the local bindings introduced by this scope.
locScope->lookupLocalsOrMembers(gatherer);
if (!gatherer.getDecls().empty()) {
llvm::errs() << "Local bindings: ";
llvm::interleave(
gatherer.getDecls().begin(), gatherer.getDecls().end(),
[&](ValueDecl *value) { llvm::errs() << value->getName(); },
[&]() { llvm::errs() << " "; });
llvm::errs() << "\n";
}
}
llvm::raw_ostream &ASTScopeImpl::verificationError() const {
return llvm::errs() << "ASTScopeImpl verification error in source file '"
<< getSourceFile()->getFilename() << "': ";
}
#pragma mark printing
void ASTScopeImpl::print(llvm::raw_ostream &out, unsigned level, bool lastChild,
bool printChildren) const {
// Indent for levels 2+.
if (level > 1)
out.indent((level - 1) * 2);
// Print child marker and leading '-' for levels 1+.
if (level > 0)
out << (lastChild ? '`' : '|') << '-';
out << getClassName();
if (auto *a = addressForPrinting().getPtrOrNull())
out << " " << a;
out << ", ";
if (auto *d = getDeclIfAny().getPtrOrNull()) {
if (d->isImplicit())
out << "implicit ";
}
printRange(out);
out << " ";
printSpecifics(out);
out << "\n";
if (printChildren) {
for (unsigned i : indices(getChildren())) {
getChildren()[i]->print(out, level + 1,
/*lastChild=*/i == getChildren().size() - 1);
}
}
}
static void printSourceRange(llvm::raw_ostream &out, const SourceRange range,
SourceManager &SM) {
if (range.isInvalid()) {
out << "[invalid source range]";
return;
}
auto startLineAndCol = SM.getPresumedLineAndColumnForLoc(range.Start);
auto endLineAndCol = SM.getPresumedLineAndColumnForLoc(range.End);
out << "[" << startLineAndCol.first << ":" << startLineAndCol.second << " - "
<< endLineAndCol.first << ":" << endLineAndCol.second << "]";
}
void ASTScopeImpl::printRange(llvm::raw_ostream &out) const {
SourceRange range = getSourceRangeOfThisASTNode(/*omitAssertions=*/true);
printSourceRange(out, range, getSourceManager());
}
#pragma mark printSpecifics
void ASTSourceFileScope::printSpecifics(
llvm::raw_ostream &out) const {
out << "'" << SF->getFilename() << "'";
}
NullablePtr<const void> ASTScopeImpl::addressForPrinting() const {
if (auto *p = getDeclIfAny().getPtrOrNull())
return p;
if (auto *p = getStmtIfAny().getPtrOrNull())
return p;
if (auto *p = getExprIfAny().getPtrOrNull())
return p;
return nullptr;
}
void GenericTypeOrExtensionScope::printSpecifics(llvm::raw_ostream &out) const {
if (shouldHaveABody() && !doesDeclHaveABody())
out << "<no body>";
else if (auto *n = getCorrespondingNominalTypeDecl().getPtrOrNull())
out << "'" << n->getName() << "'";
else
out << "<no extended nominal?!>";
}
void GenericParamScope::printSpecifics(llvm::raw_ostream &out) const {
out << "param " << index;
auto *genericTypeParamDecl = paramList->getParams()[index];
out << " '";
genericTypeParamDecl->print(out);
out << "'";
}
void AbstractFunctionDeclScope::printSpecifics(llvm::raw_ostream &out) const {
out << "'" << decl->getName() << "'";
}
void AbstractPatternEntryScope::printSpecifics(llvm::raw_ostream &out) const {
out << "entry " << patternEntryIndex;
getPattern()->forEachVariable([&](VarDecl *vd) {
out << " '" << vd->getName() << "'";
});
}
void SubscriptDeclScope::printSpecifics(llvm::raw_ostream &out) const {
decl->dumpRef(out);
}
void MacroDeclScope::printSpecifics(llvm::raw_ostream &out) const {
decl->dumpRef(out);
}
void MacroExpansionDeclScope::printSpecifics(llvm::raw_ostream &out) const {
out << decl->getMacroName();
}
void ConditionalClausePatternUseScope::printSpecifics(
llvm::raw_ostream &out) const {
sec.getPattern()->print(out);
}
bool GenericTypeOrExtensionScope::doesDeclHaveABody() const { return false; }
bool IterableTypeScope::doesDeclHaveABody() const {
return getBraces().Start != getBraces().End;
}
void ast_scope::simple_display(llvm::raw_ostream &out,
const ASTScopeImpl *scope) {
// Cannot call scope->print(out) because printing an ASTFunctionBodyScope
// gets the source range which can cause a request to parse it.
// That in turn causes the request dependency printing code to blow up
// as the AnyRequest ends up with a null.
out << scope->getClassName() << "\n";
}
|