|
| 1 | +//===--- DeclarationFragmentPrinter.cpp - Declaration Fragment Printer ----===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2017 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 | +#include "DeclarationFragmentPrinter.h" |
| 14 | +#include "SymbolGraphASTWalker.h" |
| 15 | + |
| 16 | +using namespace swift; |
| 17 | +using namespace symbolgraphgen; |
| 18 | + |
| 19 | +void DeclarationFragmentPrinter::openFragment(FragmentKind Kind) { |
| 20 | + assert(Kind != FragmentKind::None); |
| 21 | + if (this->Kind != Kind) { |
| 22 | + closeFragment(); |
| 23 | + this->Kind = Kind, |
| 24 | + Spelling.clear(); |
| 25 | + USR.clear(); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +StringRef |
| 30 | +DeclarationFragmentPrinter::getKindSpelling(FragmentKind Kind) const { |
| 31 | + switch (Kind) { |
| 32 | + case FragmentKind::Keyword: |
| 33 | + return "keyword"; |
| 34 | + case FragmentKind::Attribute: |
| 35 | + return "attribute"; |
| 36 | + case FragmentKind::NumberLiteral: |
| 37 | + return "number"; |
| 38 | + case FragmentKind::StringLiteral: |
| 39 | + return "string"; |
| 40 | + case FragmentKind::Identifier: |
| 41 | + return "identifier"; |
| 42 | + case FragmentKind::TypeIdentifier: |
| 43 | + return "typeIdentifier"; |
| 44 | + case FragmentKind::GenericParameter: |
| 45 | + return "genericParameter"; |
| 46 | + case FragmentKind::Text: |
| 47 | + return "text"; |
| 48 | + case FragmentKind::None: |
| 49 | + llvm_unreachable("Fragment kind of 'None' has no spelling"); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +void DeclarationFragmentPrinter::closeFragment() { |
| 54 | + if (Kind == FragmentKind::None) { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + if (!Spelling.empty()) { |
| 59 | + OS.object([&](){ |
| 60 | + OS.attribute("kind", getKindSpelling(Kind)); |
| 61 | + OS.attribute("spelling", Spelling.str()); |
| 62 | + if (!USR.empty()) { |
| 63 | + OS.attribute("preciseIdentifier", USR.str()); |
| 64 | + } |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + Spelling.clear(); |
| 69 | + USR.clear(); |
| 70 | + Kind = FragmentKind::None; |
| 71 | +} |
| 72 | + |
| 73 | +void DeclarationFragmentPrinter::printDeclLoc(const Decl *D) { |
| 74 | + switch (D->getKind()) { |
| 75 | + case DeclKind::Constructor: |
| 76 | + case DeclKind::Destructor: |
| 77 | + case DeclKind::Subscript: |
| 78 | + openFragment(FragmentKind::Keyword); |
| 79 | + break; |
| 80 | + default: |
| 81 | + openFragment(FragmentKind::Identifier); |
| 82 | + break; |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +void |
| 87 | +DeclarationFragmentPrinter::printNamePre(PrintNameContext Context) { |
| 88 | + switch (Context) { |
| 89 | + case PrintNameContext::Keyword: |
| 90 | + openFragment(FragmentKind::Keyword); |
| 91 | + break; |
| 92 | + case PrintNameContext::GenericParameter: |
| 93 | + openFragment(FragmentKind::GenericParameter); |
| 94 | + break; |
| 95 | + case PrintNameContext::Attribute: |
| 96 | + openFragment(FragmentKind::Attribute); |
| 97 | + break; |
| 98 | + case PrintNameContext::ClassDynamicSelf: |
| 99 | + case PrintNameContext::FunctionParameterExternal: |
| 100 | + openFragment(FragmentKind::Identifier); |
| 101 | + break; |
| 102 | + case PrintNameContext::FunctionParameterLocal: |
| 103 | + openFragment(FragmentKind::Identifier); |
| 104 | + break; |
| 105 | + case PrintNameContext::TupleElement: |
| 106 | + case PrintNameContext::TypeMember: |
| 107 | + case PrintNameContext::Normal: |
| 108 | + break; |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +void DeclarationFragmentPrinter::printStructurePre(PrintStructureKind Kind, |
| 113 | + const Decl *D) { |
| 114 | + switch (Kind) { |
| 115 | + case PrintStructureKind::NumberLiteral: |
| 116 | + openFragment(FragmentKind::NumberLiteral); |
| 117 | + break; |
| 118 | + case PrintStructureKind::StringLiteral: |
| 119 | + openFragment(FragmentKind::StringLiteral); |
| 120 | + break; |
| 121 | + default: |
| 122 | + break; |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +void DeclarationFragmentPrinter::printTypeRef(Type T, const TypeDecl *RefTo, |
| 127 | + Identifier Name, |
| 128 | + PrintNameContext NameContext) { |
| 129 | + openFragment(FragmentKind::TypeIdentifier); |
| 130 | + printText(Name.str()); |
| 131 | + USR = Walker.getUSR(RefTo); |
| 132 | + closeFragment(); |
| 133 | +} |
| 134 | + |
| 135 | +void DeclarationFragmentPrinter::printText(StringRef Text) { |
| 136 | + if (Kind == FragmentKind::None) { |
| 137 | + openFragment(FragmentKind::Text); |
| 138 | + } |
| 139 | + Spelling.append(Text); |
| 140 | +} |
0 commit comments