@@ -459,36 +459,6 @@ static bool shouldSerializeAsLocalContext(const DeclContext *DC) {
459
459
!isa<SubscriptDecl>(DC);
460
460
}
461
461
462
- static const Decl *getDeclForContext (const DeclContext *DC) {
463
- switch (DC->getContextKind ()) {
464
- case DeclContextKind::Module:
465
- // Use a null decl to represent the module.
466
- return nullptr ;
467
- case DeclContextKind::FileUnit:
468
- return getDeclForContext (DC->getParent ());
469
- case DeclContextKind::SerializedLocal:
470
- llvm_unreachable (" Serialized local contexts should only come from deserialization" );
471
- case DeclContextKind::Initializer:
472
- case DeclContextKind::AbstractClosureExpr:
473
- // FIXME: What about default functions?
474
- llvm_unreachable (" shouldn't serialize decls from anonymous closures" );
475
- case DeclContextKind::GenericTypeDecl:
476
- return cast<GenericTypeDecl>(DC);
477
- case DeclContextKind::ExtensionDecl:
478
- return cast<ExtensionDecl>(DC);
479
- case DeclContextKind::TopLevelCodeDecl:
480
- llvm_unreachable (" shouldn't serialize the main module" );
481
- case DeclContextKind::AbstractFunctionDecl:
482
- return cast<AbstractFunctionDecl>(DC);
483
- case DeclContextKind::SubscriptDecl:
484
- return cast<SubscriptDecl>(DC);
485
- case DeclContextKind::EnumElementDecl:
486
- return cast<EnumElementDecl>(DC);
487
- }
488
-
489
- llvm_unreachable (" Unhandled DeclContextKind in switch." );
490
- }
491
-
492
462
namespace {
493
463
struct Accessors {
494
464
uint8_t OpaqueReadOwnership;
@@ -634,7 +604,7 @@ DeclContextID Serializer::addDeclContextRef(const DeclContext *DC) {
634
604
if (shouldSerializeAsLocalContext (DC))
635
605
addLocalDeclContextRef (DC);
636
606
else
637
- addDeclRef (getDeclForContext (DC ));
607
+ addDeclRef (DC-> getAsDecl ( ));
638
608
639
609
auto &id = DeclContextIDs[DC];
640
610
if (id)
@@ -1023,23 +993,19 @@ void Serializer::writeHeader(const SerializationOptions &options) {
1023
993
}
1024
994
}
1025
995
1026
- using ImportPathBlob = llvm::SmallString<64 >;
1027
996
static void flattenImportPath (const ModuleDecl::ImportedModule &import ,
1028
- ImportPathBlob &out) {
1029
- SmallVector<StringRef, 4 > reverseSubmoduleNames (
1030
- import .second ->getReverseFullModuleName (), {});
1031
-
1032
- interleave (reverseSubmoduleNames.rbegin (), reverseSubmoduleNames.rend (),
1033
- [&out](StringRef next) { out.append (next); },
1034
- [&out] { out.push_back (' \0 ' ); });
997
+ SmallVectorImpl<char > &out) {
998
+ llvm::raw_svector_ostream outStream (out);
999
+ import .second ->getReverseFullModuleName ().printForward (outStream,
1000
+ StringRef (" \0 " , 1 ));
1035
1001
1036
1002
if (import .first .empty ())
1037
1003
return ;
1038
1004
1039
- out. push_back ( ' \0 ' ) ;
1005
+ outStream << ' \0 ' ;
1040
1006
assert (import .first .size () == 1 && " can only handle top-level decl imports" );
1041
1007
auto accessPathElem = import .first .front ();
1042
- out. append ( accessPathElem.first .str () );
1008
+ outStream << accessPathElem.first .str ();
1043
1009
}
1044
1010
1045
1011
uint64_t getRawModTimeOrHash (const SerializationOptions::FileDependency &dep) {
@@ -1152,7 +1118,7 @@ void Serializer::writeInputBlock(const SerializationOptions &options) {
1152
1118
continue ;
1153
1119
}
1154
1120
1155
- ImportPathBlob importPath;
1121
+ SmallString< 64 > importPath;
1156
1122
flattenImportPath (import , importPath);
1157
1123
1158
1124
serialization::ImportControl stableImportControl;
@@ -1538,7 +1504,7 @@ Serializer::writeConformance(ProtocolConformanceRef conformanceRef,
1538
1504
switch (conformance->getKind ()) {
1539
1505
case ProtocolConformanceKind::Normal: {
1540
1506
auto normal = cast<NormalProtocolConformance>(conformance);
1541
- if (!isDeclXRef (getDeclForContext ( normal->getDeclContext ()))
1507
+ if (!isDeclXRef (normal->getDeclContext ()-> getAsDecl ( ))
1542
1508
&& !isa<ClangModuleUnit>(normal->getDeclContext ()
1543
1509
->getModuleScopeContext ())) {
1544
1510
// A normal conformance in this module file.
@@ -2006,12 +1972,6 @@ static void verifyAttrSerializable(const KIND ## Decl *D) {\
2006
1972
static void verifyAttrSerializable (const Decl *D) {}
2007
1973
#endif
2008
1974
2009
- static inline unsigned getOptionalOrZero (const llvm::Optional<unsigned > &X) {
2010
- if (X.hasValue ())
2011
- return X.getValue ();
2012
- return 0 ;
2013
- }
2014
-
2015
1975
bool Serializer::isDeclXRef (const Decl *D) const {
2016
1976
const DeclContext *topLevel = D->getDeclContext ()->getModuleScopeContext ();
2017
1977
if (topLevel->getParentModule () != M)
@@ -2046,7 +2006,7 @@ void Serializer::writeDeclContext(const DeclContext *DC) {
2046
2006
case DeclContextKind::GenericTypeDecl:
2047
2007
case DeclContextKind::ExtensionDecl:
2048
2008
case DeclContextKind::EnumElementDecl:
2049
- declOrDeclContextID = addDeclRef (getDeclForContext (DC ));
2009
+ declOrDeclContextID = addDeclRef (DC-> getAsDecl ( ));
2050
2010
isDecl = true ;
2051
2011
break ;
2052
2012
@@ -2416,8 +2376,8 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
2416
2376
if (X##_Val.hasValue ()) {\
2417
2377
const auto &Y = X##_Val.getValue ();\
2418
2378
X##_Major = Y.getMajor ();\
2419
- X##_Minor = getOptionalOrZero ( Y.getMinor ());\
2420
- X##_Subminor = getOptionalOrZero ( Y.getSubminor ());\
2379
+ X##_Minor = Y.getMinor (). getValueOr ( 0 );\
2380
+ X##_Subminor = Y.getSubminor (). getValueOr ( 0 );\
2421
2381
X##_HasMinor = Y.getMinor ().hasValue ();\
2422
2382
X##_HasSubminor = Y.getSubminor ().hasValue ();\
2423
2383
}
@@ -2848,13 +2808,6 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
2848
2808
for (auto Attr : D->getAttrs ())
2849
2809
writeDeclAttribute (Attr);
2850
2810
2851
- if (auto VD = dyn_cast<ValueDecl>(D)) {
2852
- // Hack: synthesize a 'final' attribute if finality was inferred.
2853
- if (VD->isFinal () && !D->getAttrs ().hasAttribute <FinalAttr>())
2854
- writeDeclAttribute (
2855
- new (D->getASTContext ()) FinalAttr (/* Implicit=*/ false ));
2856
- }
2857
-
2858
2811
if (auto *value = dyn_cast<ValueDecl>(D))
2859
2812
writeDiscriminatorsIfNeeded (value);
2860
2813
@@ -2935,8 +2888,7 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
2935
2888
2936
2889
// Reverse the list, and write the parameter lists, from outermost
2937
2890
// to innermost.
2938
- std::reverse (allGenericParams.begin (), allGenericParams.end ());
2939
- for (auto *genericParams : allGenericParams)
2891
+ for (auto *genericParams : swift::reversed (allGenericParams))
2940
2892
writeGenericParams (genericParams);
2941
2893
2942
2894
writeMembers (id, extension->getMembers (), isClassExtension);
@@ -5035,7 +4987,7 @@ void swift::serializeToBuffers(
5035
4987
std::unique_ptr<llvm::MemoryBuffer> *moduleDocBuffer,
5036
4988
const SILModule *M) {
5037
4989
5038
- assert (options.OutputPath && options. OutputPath [ 0 ] != ' \0 ' );
4990
+ assert (! StringRef::withNullAsEmpty ( options.OutputPath ). empty () );
5039
4991
{
5040
4992
SharedTimer timer (" Serialization, swiftmodule, to buffer" );
5041
4993
llvm::SmallString<1024 > buf;
@@ -5054,7 +5006,7 @@ void swift::serializeToBuffers(
5054
5006
std::move (buf), options.OutputPath );
5055
5007
}
5056
5008
5057
- if (options.DocOutputPath && options. DocOutputPath [ 0 ] != ' \0 ' ) {
5009
+ if (! StringRef::withNullAsEmpty ( options.DocOutputPath ). empty () ) {
5058
5010
SharedTimer timer (" Serialization, swiftdoc, to buffer" );
5059
5011
llvm::SmallString<1024 > buf;
5060
5012
llvm::raw_svector_ostream stream (buf);
@@ -5074,12 +5026,12 @@ void swift::serializeToBuffers(
5074
5026
void swift::serialize (ModuleOrSourceFile DC,
5075
5027
const SerializationOptions &options,
5076
5028
const SILModule *M) {
5077
- assert (options.OutputPath && options. OutputPath [ 0 ] != ' \0 ' );
5029
+ assert (! StringRef::withNullAsEmpty ( options.OutputPath ). empty () );
5078
5030
5079
- if (strcmp ( " - " , options.OutputPath ) == 0 ) {
5031
+ if (StringRef ( options.OutputPath ) == " - " ) {
5080
5032
// Special-case writing to stdout.
5081
5033
Serializer::writeToStream (llvm::outs (), DC, M, options);
5082
- assert (! options.DocOutputPath || options. DocOutputPath [ 0 ] == ' \0 ' );
5034
+ assert (StringRef::withNullAsEmpty ( options.DocOutputPath ). empty () );
5083
5035
return ;
5084
5036
}
5085
5037
@@ -5093,7 +5045,7 @@ void swift::serialize(ModuleOrSourceFile DC,
5093
5045
if (hadError)
5094
5046
return ;
5095
5047
5096
- if (options.DocOutputPath && options. DocOutputPath [ 0 ] != ' \0 ' ) {
5048
+ if (! StringRef::withNullAsEmpty ( options.DocOutputPath ). empty () ) {
5097
5049
(void )withOutputFile (getContext (DC).Diags ,
5098
5050
options.DocOutputPath ,
5099
5051
[&](raw_ostream &out) {
0 commit comments