Skip to content

Commit aabfebe

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.
1 parent 38d89f1 commit aabfebe

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

@@ -827,6 +842,10 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl>, public Swi
827842
NumPathElements : 8
828843
);
829844

845+
SWIFT_INLINE_BITFIELD(UsingDecl, Decl, NumUsingSpecifierBits,
846+
Specifier : NumUsingSpecifierBits
847+
);
848+
830849
SWIFT_INLINE_BITFIELD(ExtensionDecl, Decl, 4+1,
831850
/// An encoding of the default and maximum access level for this extension.
832851
/// The value 4 corresponds to AccessLevel::Public
@@ -9737,6 +9756,34 @@ class MacroExpansionDecl : public Decl, public FreestandingMacroExpansion {
97379756
}
97389757
};
97399758

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

2117+
void visitUsingDecl(UsingDecl *UD, Label label) {
2118+
printCommon(UD, "using_decl", label);
2119+
printFieldQuoted(UD->getSpecifierName(), Label::always("specifier"));
2120+
}
2121+
21172122
void visitExtensionDecl(ExtensionDecl *ED, Label label) {
21182123
printCommon(ED, "extension_decl", label, ExtensionColor);
21192124
printFlag(!ED->hasBeenBound(), "unbound");

lib/AST/ASTMangler.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5426,6 +5426,7 @@ ASTMangler::BaseEntitySignature::BaseEntitySignature(const Decl *decl)
54265426
case DeclKind::PrefixOperator:
54275427
case DeclKind::PostfixOperator:
54285428
case DeclKind::MacroExpansion:
5429+
case DeclKind::Using:
54295430
break;
54305431
};
54315432
}

lib/AST/ASTPrinter.cpp

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

3055+
void PrintAST::visitUsingDecl(UsingDecl *decl) {
3056+
Printer.printIntroducerKeyword("using", Options, " ");
3057+
Printer << decl->getSpecifierName();
3058+
}
3059+
30553060
void PrintAST::printExtendedTypeName(TypeLoc ExtendedTypeLoc) {
30563061
bool OldFullyQualifiedTypesIfAmbiguous =
30573062
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
@@ -189,6 +189,7 @@ DescriptiveDeclKind Decl::getDescriptiveKind() const {
189189
TRIVIAL_KIND(MissingMember);
190190
TRIVIAL_KIND(Macro);
191191
TRIVIAL_KIND(MacroExpansion);
192+
TRIVIAL_KIND(Using);
192193

193194
case DeclKind::TypeAlias:
194195
return cast<TypeAliasDecl>(this)->getGenericParams()
@@ -400,6 +401,7 @@ StringRef Decl::getDescriptiveKindName(DescriptiveDeclKind K) {
400401
ENTRY(OpaqueVarType, "type");
401402
ENTRY(Macro, "macro");
402403
ENTRY(MacroExpansion, "pound literal");
404+
ENTRY(Using, "using");
403405
}
404406
#undef ENTRY
405407
llvm_unreachable("bad DescriptiveDeclKind");
@@ -1711,6 +1713,7 @@ ImportKind ImportDecl::getBestImportKind(const ValueDecl *VD) {
17111713
case DeclKind::Missing:
17121714
case DeclKind::MissingMember:
17131715
case DeclKind::MacroExpansion:
1716+
case DeclKind::Using:
17141717
llvm_unreachable("not a ValueDecl");
17151718

17161719
case DeclKind::AssociatedType:
@@ -1838,6 +1841,30 @@ bool ImportDecl::isAccessLevelImplicit() const {
18381841
return true;
18391842
}
18401843

1844+
UsingDecl::UsingDecl(SourceLoc usingLoc, SourceLoc specifierLoc,
1845+
UsingSpecifier specifier, DeclContext *parent)
1846+
: Decl(DeclKind::Using, parent), UsingLoc(usingLoc),
1847+
SpecifierLoc(specifierLoc) {
1848+
Bits.UsingDecl.Specifier = static_cast<unsigned>(specifier);
1849+
assert(getSpecifier() == specifier &&
1850+
"not enough bits in UsingDecl flags for specifier");
1851+
}
1852+
1853+
std::string UsingDecl::getSpecifierName() const {
1854+
switch (getSpecifier()) {
1855+
case UsingSpecifier::MainActor:
1856+
return "@MainActor";
1857+
case UsingSpecifier::nonisolated:
1858+
return "nonisolated";
1859+
}
1860+
}
1861+
1862+
UsingDecl *UsingDecl::create(ASTContext &ctx, SourceLoc usingLoc,
1863+
SourceLoc specifierLoc, UsingSpecifier specifier,
1864+
DeclContext *parent) {
1865+
return new (ctx) UsingDecl(usingLoc, specifierLoc, specifier, parent);
1866+
}
1867+
18411868
void NominalTypeDecl::setConformanceLoader(LazyMemberLoader *lazyLoader,
18421869
uint64_t contextData) {
18431870
assert(!Bits.NominalTypeDecl.HasLazyConformances &&
@@ -3652,6 +3679,7 @@ bool ValueDecl::isInstanceMember() const {
36523679
case DeclKind::Missing:
36533680
case DeclKind::MissingMember:
36543681
case DeclKind::MacroExpansion:
3682+
case DeclKind::Using:
36553683
llvm_unreachable("Not a ValueDecl");
36563684

36573685
case DeclKind::Class:
@@ -4765,6 +4793,7 @@ SourceLoc Decl::getAttributeInsertionLoc(bool forModifier) const {
47654793
case DeclKind::MissingMember:
47664794
case DeclKind::MacroExpansion:
47674795
case DeclKind::BuiltinTuple:
4796+
case DeclKind::Using:
47684797
// These don't take attributes.
47694798
return SourceLoc();
47704799

0 commit comments

Comments
 (0)