Skip to content

Commit 05363cd

Browse files
committed
Regex literal runtime plumbing.
- Frontend: Implicitly import `_StringProcessing` when frontend flag `-enable-experimental-string-processing` is set. - Type checker: Set a regex literal expression's type as `_StringProcessing.Regex<(Substring, DynamicCaptures)>`. `(Substring, DynamicCaptures)` is a temporary `Match` type that will help get us to an end-to-end working system. This will be replaced by actual type inference based a regex's pattern in a follow-up patch (soon). - SILGen: Lower a regex literal expression to a call to `_StringProcessing.Regex.init(_regexString:)`. - String processing runtime: Add `Regex`, `DynamicCaptures` (matching actual APIs in apple/swift-experimental-string-processing), and `Regex(_regexString:)`. Upcoming: - Build `_MatchingEngine` and `_StringProcessing` modules with sources from apple/swift-experimental-string-processing. - Replace `DynamicCaptures` with inferred capture types.
1 parent 2d12fab commit 05363cd

25 files changed

+164
-102
lines changed

include/swift/AST/ASTContext.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,9 @@ class ASTContext final {
620620
ConcreteDeclRef getBuiltinInitDecl(NominalTypeDecl *decl,
621621
KnownProtocolKind builtinProtocol,
622622
llvm::function_ref<DeclName (ASTContext &ctx)> initName) const;
623+
624+
/// Retrieve _StringProcessing.Regex.init(_regexString: String).
625+
ConcreteDeclRef getRegexInitDecl(Type regexType) const;
623626

624627
/// Retrieve the declaration of Swift.<(Int, Int) -> Bool.
625628
FuncDecl *getLessThanIntDecl() const;

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ WARNING(emit_reference_dependencies_without_primary_file,none,
145145

146146
WARNING(warn_implicit_concurrency_import_failed,none,
147147
"unable to perform implicit import of \"_Concurrency\" module: no such module found", ())
148+
WARNING(warn_implicit_string_processing_import_failed,none,
149+
"unable to perform implicit import of \"_StringProcessing\" module: no such module found", ())
148150

149151
ERROR(error_module_name_required,none, "-module-name is required", ())
150152
ERROR(error_bad_module_name,none,

include/swift/AST/DiagnosticsSema.def

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3668,9 +3668,6 @@ ERROR(builtin_string_literal_broken_proto,none,
36683668
ERROR(string_literal_broken_proto,none,
36693669
"protocol 'ExpressibleByStringLiteral' is broken", ())
36703670

3671-
ERROR(regex_decl_broken,none,
3672-
"cannot find 'Regex' type in scope", ())
3673-
36743671
// Array literals
36753672
ERROR(should_use_dictionary_literal,none,
36763673
"dictionary of type %0 cannot be %select{used|initialized}1 "
@@ -4722,6 +4719,14 @@ ERROR(async_unavailable_decl,none,
47224719
"%0 %1 is unavailable from asynchronous contexts%select{|; %3}2",
47234720
(DescriptiveDeclKind, DeclBaseName, bool, StringRef))
47244721

4722+
//------------------------------------------------------------------------------
4723+
// MARK: String Processing
4724+
//------------------------------------------------------------------------------
4725+
4726+
ERROR(string_processing_lib_missing,none,
4727+
"missing '%0' declaration, probably because the '_StringProcessing' "
4728+
"module was not imported properly", (StringRef))
4729+
47254730
//------------------------------------------------------------------------------
47264731
// MARK: Type Check Types
47274732
//------------------------------------------------------------------------------

include/swift/AST/Expr.h

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -966,26 +966,18 @@ class InterpolatedStringLiteralExpr : public LiteralExpr {
966966
class RegexLiteralExpr : public LiteralExpr {
967967
SourceLoc Loc;
968968
StringRef RegexText;
969-
Expr *SemanticExpr;
970969

971-
RegexLiteralExpr(SourceLoc loc, StringRef regexText, Expr *semanticExpr,
972-
bool isImplicit)
970+
RegexLiteralExpr(SourceLoc loc, StringRef regexText, bool isImplicit)
973971
: LiteralExpr(ExprKind::RegexLiteral, isImplicit), Loc(loc),
974-
RegexText(regexText), SemanticExpr(semanticExpr) {}
972+
RegexText(regexText) {}
975973

976974
public:
977975
static RegexLiteralExpr *createParsed(ASTContext &ctx, SourceLoc loc,
978-
StringRef regexText,
979-
Expr *semanticExpr);
976+
StringRef regexText);
980977

981978
/// Retrieve the raw regex text.
982979
StringRef getRegexText() const { return RegexText; }
983980

984-
/// Retrieve the semantic expression that the regex will be type-checked and
985-
/// emitted as.
986-
Expr *getSemanticExpr() const { return SemanticExpr; }
987-
void setSemanticExpr(Expr *expr) { SemanticExpr = expr; }
988-
989981
SourceRange getSourceRange() const { return Loc; }
990982

991983
static bool classof(const Expr *E) {

include/swift/AST/KnownIdentifiers.def

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,10 @@ IDENTIFIER(pullback)
250250
IDENTIFIER(TangentVector)
251251
IDENTIFIER(zero)
252252

253-
// Regex literals
253+
// String processing
254254
IDENTIFIER(Regex)
255-
IDENTIFIER(_regexString)
255+
IDENTIFIER_(regexString)
256+
IDENTIFIER_(StringProcessing)
256257

257258
// Distributed actors
258259
IDENTIFIER(transport)

include/swift/AST/KnownSDKTypes.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,8 @@ KNOWN_SDK_TYPE_DECL(Distributed, DistributedActor, ProtocolDecl, 0)
4848
KNOWN_SDK_TYPE_DECL(Distributed, ActorIdentity, ProtocolDecl, 0)
4949
KNOWN_SDK_TYPE_DECL(Distributed, AnyActorIdentity, StructDecl, 0)
5050

51+
// String processing
52+
KNOWN_SDK_TYPE_DECL(StringProcessing, Regex, StructDecl, 1)
53+
KNOWN_SDK_TYPE_DECL(StringProcessing, DynamicCaptures, EnumDecl, 0)
54+
5155
#undef KNOWN_SDK_TYPE_DECL

include/swift/Frontend/Frontend.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,10 @@ class CompilerInvocation {
354354
/// imported.
355355
bool shouldImportSwiftConcurrency() const;
356356

357+
/// Whether the Swift String Processing support library should be implicitly
358+
/// imported.
359+
bool shouldImportSwiftStringProcessing() const;
360+
357361
/// Performs input setup common to these tools:
358362
/// sil-opt, sil-func-extractor, sil-llvm-gen, and sil-nm.
359363
/// Return value includes the buffer so caller can keep it alive.
@@ -533,6 +537,14 @@ class CompilerInstance {
533537
/// i.e. if it can be found.
534538
bool canImportSwiftConcurrency() const;
535539

540+
/// Verify that if an implicit import of the `StringProcessing` module if
541+
/// expected, it can actually be imported. Emit a warning, otherwise.
542+
void verifyImplicitStringProcessingImport();
543+
544+
/// Whether the Swift String Processing support library can be imported
545+
/// i.e. if it can be found.
546+
bool canImportSwiftStringProcessing() const;
547+
536548
/// Gets the SourceFile which is the primary input for this CompilerInstance.
537549
/// \returns the primary SourceFile, or nullptr if there is no primary input;
538550
/// if there are _multiple_ primary inputs, fails with an assertion.

include/swift/Strings.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ constexpr static const StringLiteral SWIFT_ONONE_SUPPORT = "SwiftOnoneSupport";
2626
constexpr static const StringLiteral SWIFT_CONCURRENCY_NAME = "_Concurrency";
2727
/// The name of the Distributed module, which supports that extension.
2828
constexpr static const StringLiteral SWIFT_DISTRIBUTED_NAME = "_Distributed";
29+
/// The name of the StringProcessing module, which supports that extension.
30+
constexpr static const StringLiteral SWIFT_STRING_PROCESSING_NAME = "_StringProcessing";
2931
/// The name of the SwiftShims module, which contains private stdlib decls.
3032
constexpr static const StringLiteral SWIFT_SHIMS_NAME = "SwiftShims";
3133
/// The name of the Builtin module, which contains Builtin functions.

lib/AST/ASTContext.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1216,8 +1216,23 @@ ASTContext::getBuiltinInitDecl(NominalTypeDecl *decl,
12161216
return witness;
12171217
}
12181218

1219+
ConcreteDeclRef ASTContext::getRegexInitDecl(Type regexType) const {
1220+
auto *spModule = getLoadedModule(Id_StringProcessing);
1221+
DeclName name(*const_cast<ASTContext *>(this),
1222+
DeclBaseName::createConstructor(),
1223+
{Id_regexString});
1224+
SmallVector<ValueDecl *, 1> results;
1225+
spModule->lookupQualified(getRegexType(), DeclNameRef(name),
1226+
NL_IncludeUsableFromInline, results);
1227+
assert(results.size() == 1);
1228+
auto *foundDecl = cast<ConstructorDecl>(results[0]);
1229+
auto subs = regexType->getMemberSubstitutionMap(spModule, foundDecl);
1230+
return ConcreteDeclRef(foundDecl, subs);
1231+
}
1232+
12191233
static
1220-
FuncDecl *getBinaryComparisonOperatorIntDecl(const ASTContext &C, StringRef op, FuncDecl *&cached) {
1234+
FuncDecl *getBinaryComparisonOperatorIntDecl(const ASTContext &C, StringRef op,
1235+
FuncDecl *&cached) {
12211236
if (cached)
12221237
return cached;
12231238

lib/AST/ASTDumper.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1909,7 +1909,10 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
19091909
}
19101910
void visitRegexLiteralExpr(RegexLiteralExpr *E) {
19111911
printCommon(E, "regex_literal_expr");
1912-
printRec(E->getSemanticExpr());
1912+
PrintWithColorRAII(OS, LiteralValueColor)
1913+
<< " text=" << QuotedString(E->getRegexText())
1914+
<< " initializer=";
1915+
E->getInitializer().dump(PrintWithColorRAII(OS, LiteralValueColor).getOS());
19131916
PrintWithColorRAII(OS, ParenthesisColor) << ')';
19141917
}
19151918

0 commit comments

Comments
 (0)