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
|
//===--- ASTScriptEvaluator.cpp -------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 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
//
//===----------------------------------------------------------------------===//
///
/// AST script evaluation.
///
//===----------------------------------------------------------------------===//
#include "ASTScript.h"
#include "ASTScriptConfiguration.h"
#include "swift/AST/ASTMangler.h"
#include "swift/AST/ASTWalker.h"
#include "swift/AST/Decl.h"
#include "swift/AST/NameLookup.h"
#include "swift/AST/NameLookupRequests.h"
#include "swift/Frontend/Frontend.h"
using namespace swift;
using namespace scripting;
namespace {
class ASTScriptWalker : public ASTWalker {
const ASTScript &Script;
ProtocolDecl *ViewProtocol;
public:
ASTScriptWalker(const ASTScript &script, ProtocolDecl *viewProtocol)
: Script(script), ViewProtocol(viewProtocol) {}
MacroWalking getMacroWalkingBehavior() const override {
return MacroWalking::Expansion;
}
PreWalkAction walkToDeclPre(Decl *D) override {
visit(D);
return Action::Continue();
}
void visit(const Decl *D) {
auto fn = dyn_cast<AbstractFunctionDecl>(D);
if (!fn) return;
// Suppress warnings.
(void) Script;
for (auto param : *fn->getParameters()) {
// The parameter must have function type.
auto paramType = param->getInterfaceType();
auto paramFnType = paramType->getAs<FunctionType>();
if (!paramFnType) continue;
// The parameter function must return a type parameter that
// conforms to SwiftUI.View.
auto paramResultType = paramFnType->getResult();
if (!paramResultType->isTypeParameter()) continue;
auto sig = fn->getGenericSignature();
if (!sig->requiresProtocol(paramResultType, ViewProtocol)) continue;
// The parameter must not be a @ViewBuilder parameter.
if (param->getResultBuilderType()) continue;
// Print the function.
printDecl(fn);
}
}
void printDecl(const ValueDecl *decl) {
// FIXME: there's got to be some better way to print an exact reference
// to a declaration, including its context.
printDecl(llvm::outs(), decl);
llvm::outs() << "\n";
}
void printDecl(llvm::raw_ostream &out, const ValueDecl *decl) {
if (auto accessor = dyn_cast<AccessorDecl>(decl)) {
printDecl(out, accessor->getStorage());
out << ".(accessor)";
} else {
printDeclContext(out, decl->getDeclContext());
out << decl->getName();
}
}
void printDeclContext(llvm::raw_ostream &out, const DeclContext *dc) {
if (!dc) return;
if (auto module = dyn_cast<ModuleDecl>(dc)) {
out << module->getName() << ".";
} else if (auto extension = dyn_cast<ExtensionDecl>(dc)) {
auto *extended = extension->getExtendedNominal();
if (extended) {
printDecl(out, extended);
out << ".";
}
} else if (auto decl = dyn_cast_or_null<ValueDecl>(dc->getAsDecl())) {
printDecl(out, decl);
out << ".";
} else {
printDeclContext(out, dc->getParent());
}
}
};
}
bool ASTScript::execute() const {
// Hardcode the actual query we want to execute here.
auto &ctx = Config.Compiler.getASTContext();
auto swiftUI = ctx.getLoadedModule(ctx.getIdentifier("SwiftUI"));
if (!swiftUI) {
llvm::errs() << "error: SwiftUI module not loaded\n";
return true;
}
auto descriptor =
UnqualifiedLookupDescriptor(DeclNameRef(ctx.getIdentifier("View")),
swiftUI);
auto viewLookup = evaluateOrDefault(ctx.evaluator,
UnqualifiedLookupRequest{descriptor}, {});
auto viewProtocol =
dyn_cast_or_null<ProtocolDecl>(viewLookup.getSingleTypeResult());
if (!viewProtocol) {
llvm::errs() << "error: couldn't find SwiftUI.View protocol\n";
return true;
}
SmallVector<Decl*, 128> topLevelDecls;
swiftUI->getTopLevelDecls(topLevelDecls);
llvm::errs() << "found " << topLevelDecls.size() << " top-level declarations\n";
ASTScriptWalker walker(*this, viewProtocol);
for (auto decl : topLevelDecls) {
decl->walk(walker);
}
return false;
}
|