Skip to content

Commit d057429

Browse files
committed
[AST] Add new declaration - using
Initially this declaration is going to be used to determine per-file default actor isolation i.e. `using @MainActor` and `using nonisolated` but it could be extended to support other file-global settings in the future. (cherry picked from commit aabfebe)
1 parent 3e79e5e commit d057429

21 files changed

+122
-1
lines changed

include/swift/AST/Decl.h

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,8 @@ enum class DescriptiveDeclKind : uint8_t {
213213
OpaqueResultType,
214214
OpaqueVarType,
215215
Macro,
216-
MacroExpansion
216+
MacroExpansion,
217+
Using
217218
};
218219

219220
/// Describes which spelling was used in the source for the 'static' or 'class'
@@ -267,6 +268,20 @@ static_assert(uint8_t(SelfAccessKind::LastSelfAccessKind) <
267268
"Self Access Kind is too small to fit in SelfAccess kind bits. "
268269
"Please expand ");
269270

271+
enum class UsingSpecifier : uint8_t {
272+
MainActor,
273+
nonisolated,
274+
LastSpecifier = nonisolated,
275+
};
276+
enum : unsigned {
277+
NumUsingSpecifierBits =
278+
countBitsUsed(static_cast<unsigned>(UsingSpecifier::LastSpecifier))
279+
};
280+
static_assert(uint8_t(UsingSpecifier::LastSpecifier) <
281+
(NumUsingSpecifierBits << 1),
282+
"UsingSpecifier is too small to fit in UsingDecl specifier bits. "
283+
"Please expand ");
284+
270285
/// Diagnostic printing of \c SelfAccessKind.
271286
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, SelfAccessKind SAK);
272287

@@ -830,6 +845,10 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl>, public Swi
830845
NumPathElements : 8
831846
);
832847

848+
SWIFT_INLINE_BITFIELD(UsingDecl, Decl, NumUsingSpecifierBits,
849+
Specifier : NumUsingSpecifierBits
850+
);
851+
833852
SWIFT_INLINE_BITFIELD(ExtensionDecl, Decl, 4+1,
834853
/// An encoding of the default and maximum access level for this extension.
835854
/// The value 4 corresponds to AccessLevel::Public
@@ -9738,6 +9757,34 @@ class MacroExpansionDecl : public Decl, public FreestandingMacroExpansion {
97389757
}
97399758
};
97409759

9760+
/// UsingDecl - This represents a single `using` declaration, e.g.:
9761+
/// using @MainActor
9762+
class UsingDecl : public Decl {
9763+
friend class Decl;
9764+
9765+
private:
9766+
SourceLoc UsingLoc, SpecifierLoc;
9767+
9768+
UsingDecl(SourceLoc usingLoc, SourceLoc specifierLoc,
9769+
UsingSpecifier specifier, DeclContext *parent);
9770+
9771+
public:
9772+
UsingSpecifier getSpecifier() const {
9773+
return static_cast<UsingSpecifier>(Bits.UsingDecl.Specifier);
9774+
}
9775+
9776+
std::string getSpecifierName() const;
9777+
9778+
SourceLoc getLocFromSource() const { return UsingLoc; }
9779+
SourceRange getSourceRange() const { return {UsingLoc, SpecifierLoc}; }
9780+
9781+
static UsingDecl *create(ASTContext &ctx, SourceLoc usingLoc,
9782+
SourceLoc specifierLoc, UsingSpecifier specifier,
9783+
DeclContext *parent);
9784+
9785+
static bool classof(const Decl *D) { return D->getKind() == DeclKind::Using; }
9786+
};
9787+
97419788
inline void
97429789
AbstractStorageDecl::overwriteSetterAccess(AccessLevel accessLevel) {
97439790
Accessors.setInt(accessLevel);

include/swift/AST/DeclExportabilityVisitor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ class DeclExportabilityVisitor
158158
UNREACHABLE(MissingMember);
159159
UNREACHABLE(GenericTypeParam);
160160
UNREACHABLE(Param);
161+
UNREACHABLE(Using);
161162

162163
#undef UNREACHABLE
163164

include/swift/AST/DeclNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ DECL(Missing, Decl)
190190
DECL(MissingMember, Decl)
191191
DECL(PatternBinding, Decl)
192192
DECL(EnumCase, Decl)
193+
DECL(Using, Decl)
193194

194195
ABSTRACT_DECL(Operator, Decl)
195196
OPERATOR_DECL(InfixOperator, OperatorDecl)

include/swift/AST/TypeMemberVisitor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class TypeMemberVisitor : public DeclVisitor<ImplClass, RetTy> {
4141
BAD_MEMBER(Operator)
4242
BAD_MEMBER(PrecedenceGroup)
4343
BAD_MEMBER(Macro)
44+
BAD_MEMBER(Using)
4445

4546
RetTy visitMacroExpansionDecl(MacroExpansionDecl *D) {
4647
// Expansion already visited as auxiliary decls.

lib/AST/ASTDumper.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2126,6 +2126,11 @@ namespace {
21262126
printFoot();
21272127
}
21282128

2129+
void visitUsingDecl(UsingDecl *UD, Label label) {
2130+
printCommon(UD, "using_decl", label);
2131+
printFieldQuoted(UD->getSpecifierName(), Label::always("specifier"));
2132+
}
2133+
21292134
void visitExtensionDecl(ExtensionDecl *ED, Label label) {
21302135
printCommon(ED, "extension_decl", label, ExtensionColor);
21312136
printFlag(!ED->hasBeenBound(), "unbound");

lib/AST/ASTMangler.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5429,6 +5429,7 @@ ASTMangler::BaseEntitySignature::BaseEntitySignature(const Decl *decl)
54295429
case DeclKind::PrefixOperator:
54305430
case DeclKind::PostfixOperator:
54315431
case DeclKind::MacroExpansion:
5432+
case DeclKind::Using:
54325433
break;
54335434
};
54345435
}

lib/AST/ASTPrinter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3058,6 +3058,11 @@ void PrintAST::visitImportDecl(ImportDecl *decl) {
30583058
[&] { Printer << "."; });
30593059
}
30603060

3061+
void PrintAST::visitUsingDecl(UsingDecl *decl) {
3062+
Printer.printIntroducerKeyword("using", Options, " ");
3063+
Printer << decl->getSpecifierName();
3064+
}
3065+
30613066
void PrintAST::printExtendedTypeName(TypeLoc ExtendedTypeLoc) {
30623067
bool OldFullyQualifiedTypesIfAmbiguous =
30633068
Options.FullyQualifiedTypesIfAmbiguous;

lib/AST/ASTScopeCreation.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ class NodeAdder
404404
VISIT_AND_IGNORE(ParamDecl)
405405
VISIT_AND_IGNORE(MissingDecl)
406406
VISIT_AND_IGNORE(MissingMemberDecl)
407+
VISIT_AND_IGNORE(UsingDecl)
407408

408409
// This declaration is handled from the PatternBindingDecl
409410
VISIT_AND_IGNORE(VarDecl)

lib/AST/ASTWalker.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
201201
return false;
202202
}
203203

204+
bool visitUsingDecl(UsingDecl *UD) {
205+
return false;
206+
}
207+
204208
bool visitExtensionDecl(ExtensionDecl *ED) {
205209
if (auto *typeRepr = ED->getExtendedTypeRepr())
206210
if (doIt(typeRepr))

lib/AST/Decl.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ DescriptiveDeclKind Decl::getDescriptiveKind() const {
190190
TRIVIAL_KIND(MissingMember);
191191
TRIVIAL_KIND(Macro);
192192
TRIVIAL_KIND(MacroExpansion);
193+
TRIVIAL_KIND(Using);
193194

194195
case DeclKind::Enum:
195196
return cast<EnumDecl>(this)->getGenericParams()
@@ -396,6 +397,7 @@ StringRef Decl::getDescriptiveKindName(DescriptiveDeclKind K) {
396397
ENTRY(OpaqueVarType, "type");
397398
ENTRY(Macro, "macro");
398399
ENTRY(MacroExpansion, "pound literal");
400+
ENTRY(Using, "using");
399401
}
400402
#undef ENTRY
401403
llvm_unreachable("bad DescriptiveDeclKind");
@@ -1707,6 +1709,7 @@ ImportKind ImportDecl::getBestImportKind(const ValueDecl *VD) {
17071709
case DeclKind::Missing:
17081710
case DeclKind::MissingMember:
17091711
case DeclKind::MacroExpansion:
1712+
case DeclKind::Using:
17101713
llvm_unreachable("not a ValueDecl");
17111714

17121715
case DeclKind::AssociatedType:
@@ -1834,6 +1837,30 @@ bool ImportDecl::isAccessLevelImplicit() const {
18341837
return true;
18351838
}
18361839

1840+
UsingDecl::UsingDecl(SourceLoc usingLoc, SourceLoc specifierLoc,
1841+
UsingSpecifier specifier, DeclContext *parent)
1842+
: Decl(DeclKind::Using, parent), UsingLoc(usingLoc),
1843+
SpecifierLoc(specifierLoc) {
1844+
Bits.UsingDecl.Specifier = static_cast<unsigned>(specifier);
1845+
assert(getSpecifier() == specifier &&
1846+
"not enough bits in UsingDecl flags for specifier");
1847+
}
1848+
1849+
std::string UsingDecl::getSpecifierName() const {
1850+
switch (getSpecifier()) {
1851+
case UsingSpecifier::MainActor:
1852+
return "@MainActor";
1853+
case UsingSpecifier::nonisolated:
1854+
return "nonisolated";
1855+
}
1856+
}
1857+
1858+
UsingDecl *UsingDecl::create(ASTContext &ctx, SourceLoc usingLoc,
1859+
SourceLoc specifierLoc, UsingSpecifier specifier,
1860+
DeclContext *parent) {
1861+
return new (ctx) UsingDecl(usingLoc, specifierLoc, specifier, parent);
1862+
}
1863+
18371864
void NominalTypeDecl::setConformanceLoader(LazyMemberLoader *lazyLoader,
18381865
uint64_t contextData) {
18391866
assert(!Bits.NominalTypeDecl.HasLazyConformances &&
@@ -3635,6 +3662,7 @@ bool ValueDecl::isInstanceMember() const {
36353662
case DeclKind::Missing:
36363663
case DeclKind::MissingMember:
36373664
case DeclKind::MacroExpansion:
3665+
case DeclKind::Using:
36383666
llvm_unreachable("Not a ValueDecl");
36393667

36403668
case DeclKind::Class:
@@ -4748,6 +4776,7 @@ SourceLoc Decl::getAttributeInsertionLoc(bool forModifier) const {
47484776
case DeclKind::MissingMember:
47494777
case DeclKind::MacroExpansion:
47504778
case DeclKind::BuiltinTuple:
4779+
case DeclKind::Using:
47514780
// These don't take attributes.
47524781
return SourceLoc();
47534782

0 commit comments

Comments
 (0)