Skip to content

Commit cac49ea

Browse files
Merge pull request swiftlang#4518 from swiftwasm/katei/merge-5.7-2022-05-06
Merge 5.7 2022-05-06
2 parents ea514a5 + 5e25b3d commit cac49ea

File tree

98 files changed

+2279
-490
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+2279
-490
lines changed

SwiftCompilerSources/Sources/Parse/Regex.swift

Lines changed: 33 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ import AST
1515
import Basic
1616

1717
#if canImport(_CompilerRegexParser)
18-
import _CompilerRegexParser
18+
@_spi(CompilerInterface) import _CompilerRegexParser
1919

2020
public func registerRegexParser() {
2121
Parser_registerRegexLiteralParsingFn(_RegexLiteralParsingFn)
2222
Parser_registerRegexLiteralLexingFn(_RegexLiteralLexingFn)
2323
}
2424

25-
/// Bridging between C++ lexer and _CompilerRegexParser.lexRegex()
25+
/// Bridging between C++ lexer and swiftCompilerLexRegexLiteral.
2626
///
2727
/// Attempt to lex a regex literal string.
2828
///
@@ -50,47 +50,29 @@ private func _RegexLiteralLexingFn(
5050
) -> /*CompletelyErroneous*/ CBool {
5151
let inputPtr = curPtrPtr.pointee
5252

53-
do {
54-
let (_, _, endPtr) = try lexRegex(start: inputPtr, end: bufferEndPtr)
55-
curPtrPtr.pointee = endPtr.assumingMemoryBound(to: CChar.self)
53+
guard let (resumePtr, error) = swiftCompilerLexRegexLiteral(
54+
start: inputPtr, bufferEnd: bufferEndPtr, mustBeRegex: mustBeRegex
55+
) else {
56+
// Not a regex literal, fallback without advancing the pointer.
5657
return false
57-
} catch let error as DelimiterLexError {
58-
if !mustBeRegex {
59-
// This token can be something else. Let the client fallback.
60-
return false;
61-
}
62-
if error.kind == .unknownDelimiter {
63-
// An unknown delimiter should be recovered from, as we may want to try
64-
// lex something else.
65-
return false
66-
}
58+
}
6759

60+
// Advance the current pointer.
61+
curPtrPtr.pointee = resumePtr.assumingMemoryBound(to: CChar.self)
62+
63+
if let error = error {
64+
// Emit diagnostic if diagnostics are enabled.
6865
if let diagEngine = DiagnosticEngine(bridged: bridgedDiagnosticEngine) {
69-
// Emit diagnostic.
7066
let startLoc = SourceLoc(
71-
locationInFile: UnsafeRawPointer(inputPtr).assumingMemoryBound(to: UInt8.self))!
72-
diagEngine.diagnose(startLoc, .regex_literal_parsing_error, "\(error)")
73-
}
74-
75-
// Advance the current pointer.
76-
curPtrPtr.pointee = error.resumePtr.assumingMemoryBound(to: CChar.self)
77-
78-
switch error.kind {
79-
case .unterminated, .multilineClosingNotOnNewline:
80-
// These can be recovered from.
81-
return false
82-
case .unprintableASCII, .invalidUTF8:
83-
// We don't currently have good recovery behavior for these.
84-
return true
85-
case .unknownDelimiter:
86-
fatalError("Already handled")
67+
locationInFile: error.location.assumingMemoryBound(to: UInt8.self))!
68+
diagEngine.diagnose(startLoc, .regex_literal_parsing_error, error.message)
8769
}
88-
} catch {
89-
fatalError("Should be a DelimiterLexError")
70+
return error.completelyErroneous
9071
}
72+
return false
9173
}
9274

93-
/// Bridging between C++ parser and _CompilerRegexParser.parseWithDelimiters()
75+
/// Bridging between C++ parser and swiftCompilerParseRegexLiteral.
9476
///
9577
/// - Parameters:
9678
/// - inputPtr: A null-terminated C string.
@@ -103,6 +85,8 @@ private func _RegexLiteralLexingFn(
10385
/// greater than or equal to `strlen(inputPtr)`.
10486
/// - bridgedDiagnosticBaseLoc: Source location of the start of the literal
10587
/// - bridgedDiagnosticEngine: Diagnostic engine to emit diagnostics.
88+
///
89+
/// - Returns: `true` if there was a parse error, `false` otherwise.
10690
public func _RegexLiteralParsingFn(
10791
_ inputPtr: UnsafePointer<CChar>,
10892
_ versionOut: UnsafeMutablePointer<CUnsignedInt>,
@@ -111,30 +95,27 @@ public func _RegexLiteralParsingFn(
11195
_ bridgedDiagnosticBaseLoc: BridgedSourceLoc,
11296
_ bridgedDiagnosticEngine: BridgedDiagnosticEngine
11397
) -> Bool {
114-
versionOut.pointee = currentRegexLiteralFormatVersion
115-
11698
let str = String(cString: inputPtr)
99+
let captureBuffer = UnsafeMutableRawBufferPointer(
100+
start: captureStructureOut, count: Int(captureStructureSize))
117101
do {
118-
let ast = try parseWithDelimiters(str)
119-
// Serialize the capture structure for later type inference.
120-
assert(captureStructureSize >= str.utf8.count)
121-
let buffer = UnsafeMutableRawBufferPointer(
122-
start: captureStructureOut, count: Int(captureStructureSize))
123-
ast.captureStructure.encode(to: buffer)
124-
return false;
125-
} catch {
102+
// FIXME: We need to plumb through the 'regexToEmit' result to the caller.
103+
// For now, it is the same as the input.
104+
let (_, version) = try swiftCompilerParseRegexLiteral(
105+
str, captureBufferOut: captureBuffer)
106+
versionOut.pointee = CUnsignedInt(version)
107+
return false
108+
} catch let error as CompilerParseError {
126109
var diagLoc = SourceLoc(bridged: bridgedDiagnosticBaseLoc)
127110
let diagEngine = DiagnosticEngine(bridged: bridgedDiagnosticEngine)
128-
if let _diagLoc = diagLoc,
129-
let locatedError = error as? LocatedErrorProtocol {
130-
let offset = str.utf8.distance(from: str.startIndex,
131-
to: locatedError.location.start)
111+
if let _diagLoc = diagLoc, let errorLoc = error.location {
112+
let offset = str.utf8.distance(from: str.startIndex, to: errorLoc)
132113
diagLoc = _diagLoc.advanced(by: offset)
133114
}
134-
diagEngine.diagnose(
135-
diagLoc, .regex_literal_parsing_error,
136-
"cannot parse regular expression: \(String(describing: error))")
115+
diagEngine.diagnose(diagLoc, .regex_literal_parsing_error, error.message)
137116
return true
117+
} catch {
118+
fatalError("Expected CompilerParseError")
138119
}
139120
}
140121

include/swift/ABI/GenericContext.h

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ struct TargetGenericContextDescriptorHeader {
5858
/// Key arguments include generic parameters and conformance
5959
/// requirements which are part of the identity of the context.
6060
///
61-
/// The key area of the argument layout considers of a sequence
61+
/// The key area of the argument layout consists of a sequence
6262
/// of type metadata pointers (in the same order as the parameter
6363
/// descriptors, for those parameters which satisfy hasKeyArgument())
6464
/// followed by a sequence of witness table pointers (in the same
@@ -191,25 +191,48 @@ using GenericRequirementDescriptor =
191191
extern const GenericParamDescriptor
192192
ImplicitGenericParamDescriptors[MaxNumImplicitGenericParamDescriptors];
193193

194+
inline const GenericParamDescriptor *
195+
externalTargetImplicitGenericParamDescriptors() {
196+
static const GenericParamDescriptor
197+
buffer[MaxNumImplicitGenericParamDescriptors] = {
198+
#define D GenericParamDescriptor::implicit()
199+
D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D,
200+
D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D,
201+
D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D
202+
#undef D
203+
};
204+
return buffer;
205+
}
206+
207+
template <class Runtime>
208+
const GenericParamDescriptor *targetImplicitGenericParamDescriptors() {
209+
return externalTargetImplicitGenericParamDescriptors();
210+
}
211+
template <>
212+
inline const GenericParamDescriptor *targetImplicitGenericParamDescriptors<InProcess>() {
213+
return ImplicitGenericParamDescriptors;
214+
}
215+
194216
/// A runtime description of a generic signature.
217+
template<typename Runtime>
195218
class RuntimeGenericSignature {
196-
GenericContextDescriptorHeader Header;
219+
TargetGenericContextDescriptorHeader<Runtime> Header;
197220
const GenericParamDescriptor *Params;
198-
const GenericRequirementDescriptor *Requirements;
221+
const TargetGenericRequirementDescriptor<Runtime> *Requirements;
199222
public:
200223
RuntimeGenericSignature()
201224
: Header{0, 0, 0, 0}, Params(nullptr), Requirements(nullptr) {}
202225

203-
RuntimeGenericSignature(const GenericContextDescriptorHeader &header,
226+
RuntimeGenericSignature(const TargetGenericContextDescriptorHeader<Runtime> &header,
204227
const GenericParamDescriptor *params,
205-
const GenericRequirementDescriptor *requirements)
228+
const TargetGenericRequirementDescriptor<Runtime> *requirements)
206229
: Header(header), Params(params), Requirements(requirements) {}
207230

208231
llvm::ArrayRef<GenericParamDescriptor> getParams() const {
209232
return llvm::makeArrayRef(Params, Header.NumParams);
210233
}
211234

212-
llvm::ArrayRef<GenericRequirementDescriptor> getRequirements() const {
235+
llvm::ArrayRef<TargetGenericRequirementDescriptor<Runtime>> getRequirements() const {
213236
return llvm::makeArrayRef(Requirements, Header.NumRequirements);
214237
}
215238

@@ -375,8 +398,8 @@ class TrailingGenericContextObjects<TargetSelf<Runtime>,
375398
* sizeof(StoredPointer);
376399
}
377400

378-
RuntimeGenericSignature getGenericSignature() const {
379-
if (!asSelf()->isGeneric()) return RuntimeGenericSignature();
401+
RuntimeGenericSignature<Runtime> getGenericSignature() const {
402+
if (!asSelf()->isGeneric()) return RuntimeGenericSignature<Runtime>();
380403
return {getGenericContextHeader(),
381404
getGenericParams().data(),
382405
getGenericRequirements().data()};

include/swift/ABI/Metadata.h

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,6 +1735,12 @@ struct TargetExistentialTypeMetadata
17351735
using ExistentialTypeMetadata
17361736
= TargetExistentialTypeMetadata<InProcess>;
17371737

1738+
template<typename Runtime>
1739+
struct TargetExistentialTypeExpression {
1740+
/// The type expression.
1741+
TargetRelativeDirectPointer<Runtime, const char, /*nullable*/ false> name;
1742+
};
1743+
17381744
/// A description of the shape of an existential type.
17391745
///
17401746
/// An existential type has the general form:
@@ -1791,7 +1797,7 @@ struct TargetExtendedExistentialTypeShape
17911797
// Optional generalization signature header
17921798
TargetGenericContextDescriptorHeader<Runtime>,
17931799
// Optional type subexpression
1794-
TargetRelativeDirectPointer<Runtime, const char, /*nullable*/ false>,
1800+
TargetExistentialTypeExpression<Runtime>,
17951801
// Optional suggested value witnesses
17961802
TargetRelativeIndirectablePointer<Runtime, const TargetValueWitnessTable<Runtime>,
17971803
/*nullable*/ false>,
@@ -1802,8 +1808,6 @@ struct TargetExtendedExistentialTypeShape
18021808
// for generalization signature
18031809
TargetGenericRequirementDescriptor<Runtime>> {
18041810
private:
1805-
using RelativeStringPointer =
1806-
TargetRelativeDirectPointer<Runtime, const char, /*nullable*/ false>;
18071811
using RelativeValueWitnessTablePointer =
18081812
TargetRelativeIndirectablePointer<Runtime,
18091813
const TargetValueWitnessTable<Runtime>,
@@ -1812,7 +1816,7 @@ struct TargetExtendedExistentialTypeShape
18121816
swift::ABI::TrailingObjects<
18131817
TargetExtendedExistentialTypeShape<Runtime>,
18141818
TargetGenericContextDescriptorHeader<Runtime>,
1815-
RelativeStringPointer,
1819+
TargetExistentialTypeExpression<Runtime>,
18161820
RelativeValueWitnessTablePointer,
18171821
GenericParamDescriptor,
18181822
TargetGenericRequirementDescriptor<Runtime>>;
@@ -1825,7 +1829,7 @@ struct TargetExtendedExistentialTypeShape
18251829
return Flags.hasGeneralizationSignature();
18261830
}
18271831

1828-
size_t numTrailingObjects(OverloadToken<RelativeStringPointer>) const {
1832+
size_t numTrailingObjects(OverloadToken<TargetExistentialTypeExpression<Runtime>>) const {
18291833
return Flags.hasTypeExpression();
18301834
}
18311835

@@ -1879,12 +1883,13 @@ struct TargetExtendedExistentialTypeShape
18791883
/// we nonetheless distinguish at compile time. Storing this also
18801884
/// allows us to far more easily produce a formal type from this
18811885
/// shape reflectively.
1882-
RelativeStringPointer ExistentialType;
1886+
TargetRelativeDirectPointer<Runtime, const char, /*nullable*/ false>
1887+
ExistentialType;
18831888

18841889
/// The header describing the requirement signature of the existential.
18851890
TargetGenericContextDescriptorHeader<Runtime> ReqSigHeader;
18861891

1887-
RuntimeGenericSignature getRequirementSignature() const {
1892+
RuntimeGenericSignature<Runtime> getRequirementSignature() const {
18881893
return {ReqSigHeader, getReqSigParams(), getReqSigRequirements()};
18891894
}
18901895

@@ -1894,8 +1899,8 @@ struct TargetExtendedExistentialTypeShape
18941899

18951900
const GenericParamDescriptor *getReqSigParams() const {
18961901
return Flags.hasImplicitReqSigParams()
1897-
? ImplicitGenericParamDescriptors
1898-
: this->template getTrailingObjects<GenericParamDescriptor>();
1902+
? swift::targetImplicitGenericParamDescriptors<Runtime>()
1903+
: this->template getTrailingObjects<GenericParamDescriptor>();
18991904
}
19001905

19011906
unsigned getNumReqSigRequirements() const {
@@ -1911,10 +1916,11 @@ struct TargetExtendedExistentialTypeShape
19111916
/// The type expression of the existential, as a symbolic mangled type
19121917
/// string. Must be null if the header is just the (single)
19131918
/// requirement type parameter.
1914-
TargetPointer<Runtime, const char> getTypeExpression() const {
1919+
const TargetExistentialTypeExpression<Runtime> *getTypeExpression() const {
19151920
return Flags.hasTypeExpression()
1916-
? this->template getTrailingObjects<RelativeStringPointer>()->get()
1917-
: nullptr;
1921+
? this->template getTrailingObjects<
1922+
TargetExistentialTypeExpression<Runtime>>()
1923+
: nullptr;
19181924
}
19191925

19201926
bool isTypeExpressionOpaque() const {
@@ -1961,8 +1967,8 @@ struct TargetExtendedExistentialTypeShape
19611967
return Flags.hasGeneralizationSignature();
19621968
}
19631969

1964-
RuntimeGenericSignature getGeneralizationSignature() const {
1965-
if (!hasGeneralizationSignature()) return RuntimeGenericSignature();
1970+
RuntimeGenericSignature<Runtime> getGeneralizationSignature() const {
1971+
if (!hasGeneralizationSignature()) return RuntimeGenericSignature<Runtime>();
19661972
return {*getGenSigHeader(), getGenSigParams(), getGenSigRequirements()};
19671973
}
19681974

@@ -1974,7 +1980,7 @@ struct TargetExtendedExistentialTypeShape
19741980
const GenericParamDescriptor *getGenSigParams() const {
19751981
assert(hasGeneralizationSignature());
19761982
if (Flags.hasImplicitGenSigParams())
1977-
return ImplicitGenericParamDescriptors;
1983+
return swift::targetImplicitGenericParamDescriptors<Runtime>();
19781984
auto base = this->template getTrailingObjects<GenericParamDescriptor>();
19791985
if (!Flags.hasImplicitReqSigParams())
19801986
base += getNumReqSigParams();
@@ -2086,6 +2092,8 @@ struct TargetExtendedExistentialTypeMetadata
20862092
swift::ABI::TrailingObjects<
20872093
TargetExtendedExistentialTypeMetadata<Runtime>,
20882094
ConstTargetPointer<Runtime, void>> {
2095+
using StoredSize = typename Runtime::StoredSize;
2096+
20892097
private:
20902098
using TrailingObjects =
20912099
swift::ABI::TrailingObjects<
@@ -2097,9 +2105,12 @@ struct TargetExtendedExistentialTypeMetadata
20972105
using OverloadToken = typename TrailingObjects::template OverloadToken<T>;
20982106

20992107
size_t numTrailingObjects(OverloadToken<ConstTargetPointer<Runtime, void>>) const {
2100-
return Shape->getGenSigLayoutSizeInWords();
2108+
return Shape->getGenSigArgumentLayoutSizeInWords();
21012109
}
21022110

2111+
public:
2112+
static constexpr StoredSize OffsetToArguments = sizeof(TargetMetadata<Runtime>);
2113+
21032114
public:
21042115
explicit constexpr
21052116
TargetExtendedExistentialTypeMetadata(const ExtendedExistentialTypeShape *shape)
@@ -2113,6 +2124,11 @@ struct TargetExtendedExistentialTypeMetadata
21132124
ConstTargetPointer<Runtime, void> const *getGeneralizationArguments() const {
21142125
return this->template getTrailingObjects<ConstTargetPointer<Runtime, void>>();
21152126
}
2127+
2128+
public:
2129+
static bool classof(const TargetMetadata<Runtime> *metadata) {
2130+
return metadata->getKind() == MetadataKind::ExtendedExistential;
2131+
}
21162132
};
21172133
using ExtendedExistentialTypeMetadata
21182134
= TargetExtendedExistentialTypeMetadata<InProcess>;

include/swift/AST/ASTDemangler.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ class ASTBuilder {
5959
using BuiltType = swift::Type;
6060
using BuiltTypeDecl = swift::GenericTypeDecl *; // nominal or type alias
6161
using BuiltProtocolDecl = swift::ProtocolDecl *;
62+
using BuiltGenericSignature = swift::GenericSignature;
63+
using BuiltGenericTypeParam = swift::GenericTypeParamType *;
64+
using BuiltRequirement = swift::Requirement;
65+
using BuiltSubstitutionMap = swift::SubstitutionMap;
66+
6267
explicit ASTBuilder(ASTContext &ctx) : Ctx(ctx) {}
6368

6469
ASTContext &getASTContext() { return Ctx; }
@@ -112,6 +117,8 @@ class ASTBuilder {
112117
bool isClassBound,
113118
bool forRequirement = true);
114119

120+
Type createProtocolTypeFromDecl(ProtocolDecl *protocol);
121+
115122
Type createParameterizedProtocolType(Type base, ArrayRef<Type> args);
116123

117124
Type createExistentialMetatypeType(Type instance,
@@ -134,7 +141,6 @@ class ASTBuilder {
134141
Type createSILBoxType(Type base);
135142
using BuiltSILBoxField = llvm::PointerIntPair<Type, 1>;
136143
using BuiltSubstitution = std::pair<Type, Type>;
137-
using BuiltRequirement = swift::Requirement;
138144
using BuiltLayoutConstraint = swift::LayoutConstraint;
139145
Type createSILBoxTypeWithLayout(ArrayRef<BuiltSILBoxField> Fields,
140146
ArrayRef<BuiltSubstitution> Substitutions,
@@ -167,6 +173,15 @@ class ASTBuilder {
167173

168174
Type createParenType(Type base);
169175

176+
BuiltGenericSignature
177+
createGenericSignature(ArrayRef<BuiltType> params,
178+
ArrayRef<BuiltRequirement> requirements);
179+
180+
BuiltSubstitutionMap createSubstitutionMap(BuiltGenericSignature sig,
181+
ArrayRef<BuiltType> replacements);
182+
183+
Type subst(Type subject, const BuiltSubstitutionMap &Subs) const;
184+
170185
LayoutConstraint getLayoutConstraint(LayoutConstraintKind kind);
171186
LayoutConstraint getLayoutConstraintWithSizeAlign(LayoutConstraintKind kind,
172187
unsigned size,

0 commit comments

Comments
 (0)