|
| 1 | +//===--- ASTScriptEvaluator.cpp -------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +/// |
| 13 | +/// AST script evaluation. |
| 14 | +/// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +#include "ASTScript.h" |
| 18 | +#include "ASTScriptConfiguration.h" |
| 19 | + |
| 20 | +#include "swift/AST/ASTMangler.h" |
| 21 | +#include "swift/AST/ASTWalker.h" |
| 22 | +#include "swift/AST/Decl.h" |
| 23 | +#include "swift/AST/NameLookup.h" |
| 24 | +#include "swift/Frontend/Frontend.h" |
| 25 | + |
| 26 | +using namespace swift; |
| 27 | +using namespace scripting; |
| 28 | + |
| 29 | +namespace { |
| 30 | + |
| 31 | +class ASTScriptWalker : public ASTWalker { |
| 32 | + const ASTScript &Script; |
| 33 | + ProtocolDecl *ViewProtocol; |
| 34 | + |
| 35 | +public: |
| 36 | + ASTScriptWalker(const ASTScript &script, ProtocolDecl *viewProtocol) |
| 37 | + : Script(script), ViewProtocol(viewProtocol) {} |
| 38 | + |
| 39 | + bool walkToDeclPre(Decl *D) override { |
| 40 | + visit(D); |
| 41 | + return true; |
| 42 | + } |
| 43 | + |
| 44 | + void visit(const Decl *D) { |
| 45 | + auto fn = dyn_cast<AbstractFunctionDecl>(D); |
| 46 | + if (!fn) return; |
| 47 | + |
| 48 | + // Suppress warnings. |
| 49 | + (void) Script; |
| 50 | + |
| 51 | + for (auto param : *fn->getParameters()) { |
| 52 | + // The parameter must have function type. |
| 53 | + auto paramType = param->getInterfaceType(); |
| 54 | + auto paramFnType = paramType->getAs<FunctionType>(); |
| 55 | + if (!paramFnType) continue; |
| 56 | + |
| 57 | + // The parameter function must return a type parameter that |
| 58 | + // conforms to SwiftUI.View. |
| 59 | + auto paramResultType = paramFnType->getResult(); |
| 60 | + if (!paramResultType->isTypeParameter()) continue; |
| 61 | + auto sig = fn->getGenericSignature(); |
| 62 | + if (!sig->conformsToProtocol(paramResultType, ViewProtocol)) continue; |
| 63 | + |
| 64 | + // The parameter must not be a @ViewBuilder parameter. |
| 65 | + if (param->getFunctionBuilderType()) continue; |
| 66 | + |
| 67 | + // Print the function. |
| 68 | + printDecl(fn); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + void printDecl(const ValueDecl *decl) { |
| 73 | + // FIXME: there's got to be some better way to print an exact reference |
| 74 | + // to a declaration, including its context. |
| 75 | + printDecl(llvm::outs(), decl); |
| 76 | + llvm::outs() << "\n"; |
| 77 | + } |
| 78 | + |
| 79 | + void printDecl(llvm::raw_ostream &out, const ValueDecl *decl) { |
| 80 | + if (auto accessor = dyn_cast<AccessorDecl>(decl)) { |
| 81 | + printDecl(out, accessor->getStorage()); |
| 82 | + out << ".(accessor)"; |
| 83 | + } else { |
| 84 | + printDeclContext(out, decl->getDeclContext()); |
| 85 | + out << decl->getFullName(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + void printDeclContext(llvm::raw_ostream &out, const DeclContext *dc) { |
| 90 | + if (!dc) return; |
| 91 | + if (auto module = dyn_cast<ModuleDecl>(dc)) { |
| 92 | + out << module->getName() << "."; |
| 93 | + } else if (auto extension = dyn_cast<ExtensionDecl>(dc)) { |
| 94 | + printDecl(out, extension->getExtendedNominal()); |
| 95 | + out << "."; |
| 96 | + } else if (auto decl = dyn_cast_or_null<ValueDecl>(dc->getAsDecl())) { |
| 97 | + printDecl(out, decl); |
| 98 | + out << "."; |
| 99 | + } else { |
| 100 | + printDeclContext(out, dc->getParent()); |
| 101 | + } |
| 102 | + } |
| 103 | +}; |
| 104 | + |
| 105 | +} |
| 106 | + |
| 107 | +bool ASTScript::execute() const { |
| 108 | + // Hardcode the actual query we want to execute here. |
| 109 | + |
| 110 | + auto &ctx = Config.Compiler.getASTContext(); |
| 111 | + auto swiftUI = ctx.getLoadedModule(ctx.getIdentifier("SwiftUI")); |
| 112 | + if (!swiftUI) { |
| 113 | + llvm::errs() << "error: SwiftUI module not loaded\n"; |
| 114 | + return true; |
| 115 | + } |
| 116 | + |
| 117 | + UnqualifiedLookup viewLookup(ctx.getIdentifier("View"), swiftUI, nullptr); |
| 118 | + auto viewProtocol = |
| 119 | + dyn_cast_or_null<ProtocolDecl>(viewLookup.getSingleTypeResult()); |
| 120 | + if (!viewProtocol) { |
| 121 | + llvm::errs() << "error: couldn't find SwiftUI.View protocol\n"; |
| 122 | + return true; |
| 123 | + } |
| 124 | + |
| 125 | + SmallVector<Decl*, 128> topLevelDecls; |
| 126 | + swiftUI->getTopLevelDecls(topLevelDecls); |
| 127 | + |
| 128 | + llvm::errs() << "found " << topLevelDecls.size() << " top-level declarations\n"; |
| 129 | + |
| 130 | + ASTScriptWalker walker(*this, viewProtocol); |
| 131 | + for (auto decl : topLevelDecls) { |
| 132 | + decl->walk(walker); |
| 133 | + } |
| 134 | + |
| 135 | + return false; |
| 136 | +} |
0 commit comments