|
| 1 | +//===--- ParseDeclName.h ----------------------------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2024 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_PARSE_PARSEDECLNAME_H |
| 14 | +#define SWIFT_PARSE_PARSEDECLNAME_H |
| 15 | + |
| 16 | +#include "swift/AST/Identifier.h" |
| 17 | +#include "swift/Basic/LLVM.h" |
| 18 | +#include "swift/Parse/Lexer.h" |
| 19 | + |
| 20 | +namespace swift { |
| 21 | + |
| 22 | +/// Describes a parsed declaration name. |
| 23 | +struct ParsedDeclName { |
| 24 | + /// The name of the context of which the corresponding entity should |
| 25 | + /// become a member. |
| 26 | + StringRef ContextName; |
| 27 | + |
| 28 | + /// The base name of the declaration. |
| 29 | + StringRef BaseName; |
| 30 | + |
| 31 | + /// The argument labels for a function declaration. |
| 32 | + SmallVector<StringRef, 4> ArgumentLabels; |
| 33 | + |
| 34 | + /// Whether this is a function name (vs. a value name). |
| 35 | + bool IsFunctionName = false; |
| 36 | + |
| 37 | + /// Whether this is a getter for the named property. |
| 38 | + bool IsGetter = false; |
| 39 | + |
| 40 | + /// Whether this is a setter for the named property. |
| 41 | + bool IsSetter = false; |
| 42 | + |
| 43 | + bool IsSubscript = false; |
| 44 | + |
| 45 | + /// For a declaration name that makes the declaration into an |
| 46 | + /// instance member, the index of the "Self" parameter. |
| 47 | + std::optional<unsigned> SelfIndex; |
| 48 | + |
| 49 | + /// Determine whether this is a valid name. |
| 50 | + explicit operator bool() const { return !BaseName.empty(); } |
| 51 | + |
| 52 | + /// Whether this declaration name turns the declaration into a |
| 53 | + /// member of some named context. |
| 54 | + bool isMember() const { return !ContextName.empty(); } |
| 55 | + |
| 56 | + /// Whether the result is translated into an instance member. |
| 57 | + bool isInstanceMember() const { |
| 58 | + return isMember() && static_cast<bool>(SelfIndex); |
| 59 | + } |
| 60 | + |
| 61 | + /// Whether the result is translated into a static/class member. |
| 62 | + bool isClassMember() const { |
| 63 | + return isMember() && !static_cast<bool>(SelfIndex); |
| 64 | + } |
| 65 | + |
| 66 | + /// Whether this is a property accessor. |
| 67 | + bool isPropertyAccessor() const { return IsGetter || IsSetter; } |
| 68 | + |
| 69 | + /// Whether this is an operator. |
| 70 | + bool isOperator() const { return Lexer::isOperator(BaseName); } |
| 71 | + |
| 72 | + /// Form a declaration name from this parsed declaration name. |
| 73 | + DeclName formDeclName(ASTContext &ctx, bool isSubscript = false, |
| 74 | + bool isCxxClassTemplateSpec = false) const; |
| 75 | + |
| 76 | + /// Form a declaration name from this parsed declaration name. |
| 77 | + DeclNameRef formDeclNameRef(ASTContext &ctx, bool isSubscript = false, |
| 78 | + bool isCxxClassTemplateSpec = false) const; |
| 79 | +}; |
| 80 | + |
| 81 | +/// Parse a stringified Swift declaration name, |
| 82 | +/// e.g. "Foo.translateBy(self:x:y:)". |
| 83 | +ParsedDeclName parseDeclName(StringRef name) LLVM_READONLY; |
| 84 | + |
| 85 | +/// Form a Swift declaration name from its constituent parts. |
| 86 | +DeclName formDeclName(ASTContext &ctx, StringRef baseName, |
| 87 | + ArrayRef<StringRef> argumentLabels, bool isFunctionName, |
| 88 | + bool isInitializer, bool isSubscript = false, |
| 89 | + bool isCxxClassTemplateSpec = false); |
| 90 | + |
| 91 | +/// Form a Swift declaration name reference from its constituent parts. |
| 92 | +DeclNameRef formDeclNameRef(ASTContext &ctx, StringRef baseName, |
| 93 | + ArrayRef<StringRef> argumentLabels, |
| 94 | + bool isFunctionName, bool isInitializer, |
| 95 | + bool isSubscript = false, |
| 96 | + bool isCxxClassTemplateSpec = false); |
| 97 | + |
| 98 | +} // namespace swift |
| 99 | + |
| 100 | +#endif // SWIFT_PARSE_PARSEDECLNAME_H |
0 commit comments