|
| 1 | +//===--- DiagnosticArgument.h -----------------------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 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 | +#ifndef SWIFT_AST_DIAGNOSTIC_ARGUMENT_H |
| 14 | +#define SWIFT_AST_DIAGNOSTIC_ARGUMENT_H |
| 15 | + |
| 16 | +#include "swift/AST/ActorIsolation.h" |
| 17 | +#include "swift/AST/AvailabilityDomain.h" |
| 18 | +#include "swift/AST/DiagnosticConsumer.h" |
| 19 | +#include "swift/AST/Identifier.h" |
| 20 | +#include "swift/AST/LayoutConstraint.h" |
| 21 | +#include "swift/AST/TypeLoc.h" |
| 22 | +#include "swift/Basic/Version.h" |
| 23 | +#include "llvm/Support/VersionTuple.h" |
| 24 | + |
| 25 | +namespace clang { |
| 26 | +class NamedDecl; |
| 27 | +class Type; |
| 28 | +} // namespace clang |
| 29 | + |
| 30 | +namespace swift { |
| 31 | + |
| 32 | +class Decl; |
| 33 | +class DeclAttribute; |
| 34 | +class TypeRepr; |
| 35 | + |
| 36 | +enum class DescriptivePatternKind : uint8_t; |
| 37 | +enum class DescriptiveDeclKind : uint8_t; |
| 38 | +enum class ReferenceOwnership : uint8_t; |
| 39 | +enum class SelfAccessKind : uint8_t; |
| 40 | +enum class StaticSpellingKind : uint8_t; |
| 41 | +enum class StmtKind; |
| 42 | + |
| 43 | +/// A family of wrapper types for compiler data types that forces its |
| 44 | +/// underlying data to be formatted with full qualification. |
| 45 | +/// |
| 46 | +/// So far, this is only useful for \c Type, hence the SFINAE'ing. |
| 47 | +template <typename T, typename = void> |
| 48 | +struct FullyQualified {}; |
| 49 | + |
| 50 | +template <typename T> |
| 51 | +struct FullyQualified< |
| 52 | + T, typename std::enable_if<std::is_convertible<T, Type>::value>::type> { |
| 53 | + Type t; |
| 54 | + |
| 55 | +public: |
| 56 | + FullyQualified(T t) : t(t) {}; |
| 57 | + |
| 58 | + Type getType() const { return t; } |
| 59 | +}; |
| 60 | + |
| 61 | +struct WitnessType { |
| 62 | + Type t; |
| 63 | + |
| 64 | + WitnessType(Type t) : t(t) {} |
| 65 | + |
| 66 | + Type getType() { return t; } |
| 67 | +}; |
| 68 | + |
| 69 | +/// Describes the kind of diagnostic argument we're storing. |
| 70 | +enum class DiagnosticArgumentKind { |
| 71 | + String, |
| 72 | + Integer, |
| 73 | + Unsigned, |
| 74 | + Identifier, |
| 75 | + ObjCSelector, |
| 76 | + Decl, |
| 77 | + Type, |
| 78 | + TypeRepr, |
| 79 | + FullyQualifiedType, |
| 80 | + WitnessType, |
| 81 | + DescriptivePatternKind, |
| 82 | + SelfAccessKind, |
| 83 | + ReferenceOwnership, |
| 84 | + StaticSpellingKind, |
| 85 | + DescriptiveDeclKind, |
| 86 | + DescriptiveStmtKind, |
| 87 | + DeclAttribute, |
| 88 | + AvailabilityDomain, |
| 89 | + AvailabilityRange, |
| 90 | + VersionTuple, |
| 91 | + LayoutConstraint, |
| 92 | + ActorIsolation, |
| 93 | + IsolationSource, |
| 94 | + Diagnostic, |
| 95 | + ClangDecl, |
| 96 | + ClangType, |
| 97 | +}; |
| 98 | + |
| 99 | +/// Variant type that holds a single diagnostic argument of a known |
| 100 | +/// type. |
| 101 | +/// |
| 102 | +/// All diagnostic arguments are converted to an instance of this class. |
| 103 | +class DiagnosticArgument { |
| 104 | + DiagnosticArgumentKind Kind; |
| 105 | + union { |
| 106 | + int IntegerVal; |
| 107 | + unsigned UnsignedVal; |
| 108 | + StringRef StringVal; |
| 109 | + DeclNameRef IdentifierVal; |
| 110 | + ObjCSelector ObjCSelectorVal; |
| 111 | + const Decl *TheDecl; |
| 112 | + Type TypeVal; |
| 113 | + TypeRepr *TyR; |
| 114 | + FullyQualified<Type> FullyQualifiedTypeVal; |
| 115 | + WitnessType WitnessTypeVal; |
| 116 | + DescriptivePatternKind DescriptivePatternKindVal; |
| 117 | + SelfAccessKind SelfAccessKindVal; |
| 118 | + ReferenceOwnership ReferenceOwnershipVal; |
| 119 | + StaticSpellingKind StaticSpellingKindVal; |
| 120 | + DescriptiveDeclKind DescriptiveDeclKindVal; |
| 121 | + StmtKind DescriptiveStmtKindVal; |
| 122 | + const DeclAttribute *DeclAttributeVal; |
| 123 | + AvailabilityDomain AvailabilityDomainVal; |
| 124 | + AvailabilityRange AvailabilityRangeVal; |
| 125 | + llvm::VersionTuple VersionVal; |
| 126 | + LayoutConstraint LayoutConstraintVal; |
| 127 | + ActorIsolation ActorIsolationVal; |
| 128 | + IsolationSource IsolationSourceVal; |
| 129 | + DiagnosticInfo *DiagnosticVal; |
| 130 | + const clang::NamedDecl *ClangDecl; |
| 131 | + const clang::Type *ClangType; |
| 132 | + }; |
| 133 | + |
| 134 | +public: |
| 135 | + DiagnosticArgument(StringRef S); |
| 136 | + DiagnosticArgument(int I); |
| 137 | + DiagnosticArgument(unsigned I); |
| 138 | + DiagnosticArgument(DeclNameRef R); |
| 139 | + DiagnosticArgument(DeclName D); |
| 140 | + DiagnosticArgument(DeclBaseName D); |
| 141 | + DiagnosticArgument(Identifier I); |
| 142 | + DiagnosticArgument(ObjCSelector S); |
| 143 | + DiagnosticArgument(const Decl *VD); |
| 144 | + DiagnosticArgument(Type T); |
| 145 | + DiagnosticArgument(TypeRepr *T); |
| 146 | + DiagnosticArgument(FullyQualified<Type> FQT); |
| 147 | + DiagnosticArgument(WitnessType WT); |
| 148 | + DiagnosticArgument(const TypeLoc &TL); |
| 149 | + DiagnosticArgument(DescriptivePatternKind DPK); |
| 150 | + DiagnosticArgument(ReferenceOwnership RO); |
| 151 | + DiagnosticArgument(SelfAccessKind SAK); |
| 152 | + DiagnosticArgument(StaticSpellingKind SSK); |
| 153 | + DiagnosticArgument(DescriptiveDeclKind DDK); |
| 154 | + DiagnosticArgument(StmtKind SK); |
| 155 | + DiagnosticArgument(const DeclAttribute *attr); |
| 156 | + DiagnosticArgument(const AvailabilityDomain domain); |
| 157 | + DiagnosticArgument(const AvailabilityRange &range); |
| 158 | + DiagnosticArgument(llvm::VersionTuple version); |
| 159 | + DiagnosticArgument(LayoutConstraint L); |
| 160 | + DiagnosticArgument(ActorIsolation AI); |
| 161 | + DiagnosticArgument(IsolationSource IS); |
| 162 | + DiagnosticArgument(DiagnosticInfo *D); |
| 163 | + DiagnosticArgument(const clang::NamedDecl *ND); |
| 164 | + DiagnosticArgument(const clang::Type *Ty); |
| 165 | + |
| 166 | + /// Initializes a diagnostic argument using the underlying type of the |
| 167 | + /// given enum. |
| 168 | + template < |
| 169 | + typename EnumType, |
| 170 | + typename std::enable_if<std::is_enum<EnumType>::value>::type * = nullptr> |
| 171 | + DiagnosticArgument(EnumType value) |
| 172 | + : DiagnosticArgument( |
| 173 | + static_cast<typename std::underlying_type<EnumType>::type>(value)) { |
| 174 | + } |
| 175 | + |
| 176 | + DiagnosticArgumentKind getKind() const; |
| 177 | + |
| 178 | + StringRef getAsString() const; |
| 179 | + int getAsInteger() const; |
| 180 | + unsigned getAsUnsigned() const; |
| 181 | + DeclNameRef getAsIdentifier() const; |
| 182 | + ObjCSelector getAsObjCSelector() const; |
| 183 | + const Decl *getAsDecl() const; |
| 184 | + Type getAsType() const; |
| 185 | + TypeRepr *getAsTypeRepr() const; |
| 186 | + FullyQualified<Type> getAsFullyQualifiedType() const; |
| 187 | + WitnessType getAsWitnessType() const; |
| 188 | + DescriptivePatternKind getAsDescriptivePatternKind() const; |
| 189 | + ReferenceOwnership getAsReferenceOwnership() const; |
| 190 | + SelfAccessKind getAsSelfAccessKind() const; |
| 191 | + StaticSpellingKind getAsStaticSpellingKind() const; |
| 192 | + DescriptiveDeclKind getAsDescriptiveDeclKind() const; |
| 193 | + StmtKind getAsDescriptiveStmtKind() const; |
| 194 | + const DeclAttribute *getAsDeclAttribute() const; |
| 195 | + const AvailabilityDomain getAsAvailabilityDomain() const; |
| 196 | + const AvailabilityRange getAsAvailabilityRange() const; |
| 197 | + llvm::VersionTuple getAsVersionTuple() const; |
| 198 | + LayoutConstraint getAsLayoutConstraint() const; |
| 199 | + ActorIsolation getAsActorIsolation() const; |
| 200 | + IsolationSource getAsIsolationSource() const; |
| 201 | + DiagnosticInfo *getAsDiagnostic() const; |
| 202 | + const clang::NamedDecl *getAsClangDecl() const; |
| 203 | + const clang::Type *getAsClangType() const; |
| 204 | +}; |
| 205 | + |
| 206 | +} // namespace swift |
| 207 | + |
| 208 | +#endif /* SWIFT_AST_DIAGNOSTIC_ARGUMENT_H */ |
0 commit comments