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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
//===--- JSON.cpp - Symbol Graph JSON Helpers -----------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// Adds Symbol Graph JSON serialization to other types.
//===----------------------------------------------------------------------===//
#include "JSON.h"
#include "Symbol.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/Decl.h"
#include "swift/AST/FileUnit.h"
#include "swift/AST/GenericParamList.h"
#include "swift/AST/Module.h"
#include "swift/AST/Type.h"
#include "swift/AST/USRGeneration.h"
#include "swift/ClangImporter/ClangModule.h"
#include "swift/Serialization/SerializedModuleLoader.h"
void swift::symbolgraphgen::serialize(const llvm::VersionTuple &VT,
llvm::json::OStream &OS) {
OS.object([&](){
OS.attribute("major", VT.getMajor());
if (VT.getMinor()) {
OS.attribute("minor", *VT.getMinor());
}
if (VT.getSubminor()) {
OS.attribute("patch", *VT.getSubminor());
}
// Despite the name,
// this is not Semantic Versioning "build metadata"
if (VT.getBuild()) {
OS.attribute("prerelease", *VT.getBuild());
}
});
}
void swift::symbolgraphgen::serialize(const llvm::Triple &T,
llvm::json::OStream &OS) {
OS.object([&](){
OS.attribute("architecture", T.getArchName());
if (!T.getEnvironmentName().empty()) {
OS.attribute("environment", T.getEnvironmentName());
}
OS.attribute("vendor", T.getVendorName());
OS.attributeObject("operatingSystem", [&](){
OS.attribute("name", T.getOSTypeName(T.getOS()));
llvm::VersionTuple OSVersion = T.getOSVersion();
if (!OSVersion.empty()) {
OS.attributeBegin("minimumVersion");
serialize(OSVersion, OS);
OS.attributeEnd();
}
});
});
}
void swift::symbolgraphgen::serialize(const ExtensionDecl *Extension,
llvm::json::OStream &OS) {
OS.attributeObject("swiftExtension", [&](){
if (const auto *ExtendedNominal = Extension->getExtendedNominal()) {
if (const auto *ExtendedModule = ExtendedNominal->getModuleContext()) {
OS.attribute("extendedModule", ExtendedModule->getNameStr());
}
OS.attribute("typeKind", Symbol::getKind(ExtendedNominal).first);
}
SmallVector<Requirement, 4> FilteredRequirements;
filterGenericRequirements(Extension,
FilteredRequirements);
if (!FilteredRequirements.empty()) {
OS.attributeArray("constraints", [&](){
for (const auto &Requirement : FilteredRequirements) {
serialize(Requirement, OS);
}
}); // end constraints:
}
}); // end swiftExtension:
}
void swift::symbolgraphgen::serialize(const Requirement &Req,
llvm::json::OStream &OS) {
StringRef Kind;
switch (Req.getKind()) {
case RequirementKind::SameShape:
Kind = "sameShape";
break;
case RequirementKind::Conformance:
Kind = "conformance";
break;
case RequirementKind::Superclass:
Kind = "superclass";
break;
case RequirementKind::SameType:
Kind = "sameType";
break;
case RequirementKind::Layout:
return;
}
OS.object([&](){
OS.attribute("kind", Kind);
OS.attribute("lhs", Req.getFirstType()->getString());
OS.attribute("rhs", Req.getSecondType()->getString());
// If the RHS type has a USR we can link to, add it to the output
if (auto *TyDecl = Req.getSecondType()->getAnyNominal()) {
SmallString<256> USR;
{
llvm::raw_svector_ostream SOS(USR);
ide::printDeclUSR(TyDecl, SOS);
}
OS.attribute("rhsPrecise", USR.str());
}
});
}
void swift::symbolgraphgen::serialize(const swift::GenericTypeParamType *Param,
llvm::json::OStream &OS) {
OS.object([&](){
OS.attribute("name", Param->getName().str());
OS.attribute("index", Param->getIndex());
OS.attribute("depth", Param->getDepth());
});
}
void swift::symbolgraphgen::serialize(const ModuleDecl &Module,
llvm::json::OStream &OS,
llvm::Triple Target) {
auto *MainFile = Module.getFiles().front();
switch (MainFile->getKind()) {
case FileUnitKind::Builtin:
llvm_unreachable("Unexpected module kind: Builtin");
case FileUnitKind::DWARFModule:
llvm_unreachable("Unexpected module kind: DWARFModule");
case FileUnitKind::Synthesized:
llvm_unreachable("Unexpected module kind: Synthesized");
break;
case FileUnitKind::Source:
serialize(Module.getASTContext().LangOpts.Target, OS);
break;
case FileUnitKind::SerializedAST: {
auto SerializedAST = cast<SerializedASTFile>(MainFile);
auto Target = llvm::Triple(SerializedAST->getTargetTriple());
serialize(Target, OS);
break;
}
case FileUnitKind::ClangModule: {
auto ClangModule = cast<ClangModuleUnit>(MainFile);
if (const auto *Overlay = ClangModule->getOverlayModule()) {
serialize(*Overlay, OS, Target);
} else {
serialize(Target, OS);
}
break;
}
}
}
void
swift::symbolgraphgen::filterGenericParams(
ArrayRef<GenericTypeParamType *> GenericParams,
SmallVectorImpl<const GenericTypeParamType*> &FilteredParams,
SubstitutionMap SubMap) {
for (auto Param : GenericParams) {
if (const auto *GPD = Param->getDecl()) {
// Ignore the implicit Self param
if (GPD->isImplicit()) {
if (!isa<ExtensionDecl>(GPD->getDeclContext()))
continue;
// Extension decls (and their children) refer to implicit copies of the
// explicit params of the nominal they extend. Don't filter those out.
auto *ED = cast<ExtensionDecl>(GPD->getDeclContext());
if (auto *NTD = ED->getExtendedNominal()) {
if (auto *GPL = NTD->getGenericParams()) {
auto ImplicitAndSameName = [&](GenericTypeParamDecl *NominalGPD) {
return NominalGPD->isImplicit() &&
GPD->getName() == NominalGPD->getName();
};
if (llvm::any_of(GPL->getParams(), ImplicitAndSameName))
continue;
}
}
}
// Ignore parameters that have been substituted.
if (!SubMap.empty()) {
Type SubTy = Type(Param).subst(SubMap);
if (!SubTy->hasError() && SubTy.getPointer() != Param) {
if (!SubTy->is<ArchetypeType>()) {
continue;
}
auto AT = SubTy->castTo<ArchetypeType>();
if (!AT->getInterfaceType()->isEqual(Param)) {
continue;
}
}
}
FilteredParams.push_back(Param);
}
}
}
// FIXME: This is wrong. We should instead compute the new requirements of a
// member declaration by comparing against the generic signature of its
// parent, with getRequirementsNotSatisfiedBy().
static bool containsParams(swift::Type Ty, llvm::ArrayRef<const swift::GenericTypeParamType*> Others) {
return Ty.findIf([&](swift::Type T) -> bool {
if (auto AT = T->getAs<swift::ArchetypeType>()) {
T = AT->getInterfaceType();
}
for (auto *Param: Others) {
if (T->isEqual(const_cast<swift::GenericTypeParamType*>(Param)))
return true;
}
return false;
});
}
void swift::symbolgraphgen::filterGenericRequirements(
ArrayRef<Requirement> Requirements,
const ProtocolDecl *Self,
SmallVectorImpl<Requirement> &FilteredRequirements,
SubstitutionMap SubMap,
ArrayRef<const GenericTypeParamType *> FilteredParams) {
for (const auto &Req : Requirements) {
// FIXME: We're just dropping "Self: AnyObject", etc.
if (Req.getKind() == RequirementKind::Layout) {
continue;
}
// extension /* protocol */ Q {
// func foo() {}
// }
// ignore Self : Q, obvious
if (Self &&
Req.getKind() == RequirementKind::Conformance &&
Req.getProtocolDecl() == Self) {
continue;
}
// Ignore requirements that don't involve the filtered set of generic
// parameters after substitution.
if (!SubMap.empty()) {
Type SubFirst = Req.getFirstType().subst(SubMap);
if (SubFirst->hasError())
SubFirst = Req.getFirstType();
Type SubSecond = Req.getSecondType().subst(SubMap);
if (SubSecond->hasError())
SubSecond = Req.getSecondType();
if (!containsParams(SubFirst, FilteredParams) &&
!containsParams(SubSecond, FilteredParams))
continue;
// Use the same requirement kind with the substituted types.
FilteredRequirements.emplace_back(Req.getKind(), SubFirst, SubSecond);
} else {
// Use the original requirement.
FilteredRequirements.push_back(Req);
}
}
}
void
swift::symbolgraphgen::filterGenericRequirements(const ExtensionDecl *Extension,
SmallVectorImpl<Requirement> &FilteredRequirements) {
auto Sig = Extension->getGenericSignature();
if (!Sig)
return;
SmallVector<Requirement, 2> Reqs;
SmallVector<InverseRequirement, 2> InverseReqs;
Sig->getRequirementsWithInverses(Reqs, InverseReqs);
// FIXME(noncopyable_generics): Do something with InverseReqs, or just use
// getRequirements() above and update the tests.
for (const auto &Req : Reqs) {
if (Req.getKind() == RequirementKind::Layout) {
continue;
}
// extension /* protocol */ Q
// ignore Self : Q, obvious
if (auto *Proto = Extension->getExtendedProtocolDecl()) {
if (Req.getKind() == RequirementKind::Conformance &&
Req.getFirstType()->isEqual(Extension->getSelfInterfaceType()) &&
Req.getProtocolDecl() == Proto) {
continue;
}
}
FilteredRequirements.push_back(Req);
}
}
|