Skip to content

Commit 72d3323

Browse files
committed
[sil-serialization] Create SILSerializationFunctionBuilder and use it when deserializing.
This allowed me to fold all of the weird direct calls to createFunction into a singular SILSerializationFunctionBuilder::createDeclaration. This is the only API that is needed by the SILParser so only providing that gives us a significantly cleaner API. rdar://42301529
1 parent d71ebcd commit 72d3323

File tree

2 files changed

+61
-26
lines changed

2 files changed

+61
-26
lines changed

lib/Serialization/DeserializeSIL.cpp

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "DeserializationErrors.h"
1717
#include "SILFormat.h"
1818

19+
#include "SILSerializationFunctionBuilder.h"
1920
#include "swift/AST/GenericSignature.h"
2021
#include "swift/AST/PrettyStackTrace.h"
2122
#include "swift/AST/ProtocolConformance.h"
@@ -24,7 +25,6 @@
2425
#include "swift/SIL/SILArgument.h"
2526
#include "swift/SIL/SILBuilder.h"
2627
#include "swift/SIL/SILDebugScope.h"
27-
#include "swift/SIL/SILFunctionBuilder.h"
2828
#include "swift/SIL/SILModule.h"
2929
#include "swift/SIL/SILUndef.h"
3030
#include "swift/Serialization/BCReadingExtras.h"
@@ -326,18 +326,6 @@ static SILType getSILType(Type Ty, SILValueCategory Category) {
326326
Category);
327327
}
328328

329-
/// Helper function to create a bogus SILFunction to appease error paths.
330-
static SILFunction *createBogusSILFunction(SILModule &M,
331-
StringRef name,
332-
SILType type) {
333-
SourceLoc loc;
334-
SILFunctionBuilder builder(M);
335-
return builder.createFunction(
336-
SILLinkage::Private, name, type.castTo<SILFunctionType>(), nullptr,
337-
RegularLocation(loc), IsNotBare, IsNotTransparent, IsNotSerialized,
338-
ProfileCounter(), IsNotThunk, SubclassScope::NotApplicable);
339-
}
340-
341329
/// Helper function to find a SILFunction, given its name and type.
342330
SILFunction *SILDeserializer::getFuncForReference(StringRef name,
343331
SILType type) {
@@ -360,9 +348,16 @@ SILFunction *SILDeserializer::getFuncForReference(StringRef name,
360348

361349
// FIXME: check for matching types.
362350

363-
// Always return something of the right type.
364-
if (!fn) fn = createBogusSILFunction(SILMod, name, type);
365-
return fn;
351+
// At this point, if fn is set, we know that we have a good function to use.
352+
if (fn)
353+
return fn;
354+
355+
// Otherwise, create a function declaration with the right type and a bogus
356+
// source location. This ensures that we can at least parse the rest of the
357+
// SIL.
358+
SourceLoc sourceLoc;
359+
SILSerializationFunctionBuilder builder(SILMod);
360+
return builder.createDeclaration(name, type, RegularLocation(sourceLoc));
366361
}
367362

368363
/// Helper function to find a SILFunction, given its name and type.
@@ -550,18 +545,18 @@ SILDeserializer::readSILFunctionChecked(DeclID FID, SILFunction *existingFn,
550545
linkage == SILLinkage::PublicNonABI) {
551546
fn->setLinkage(SILLinkage::SharedExternal);
552547
}
553-
554-
// Otherwise, create a new function.
555548
} else {
556-
SILFunctionBuilder builder(SILMod);
557-
fn = builder.createFunction(
558-
linkage.getValue(), name, ty.castTo<SILFunctionType>(), nullptr, loc,
559-
IsNotBare, IsTransparent_t(isTransparent == 1),
560-
IsSerialized_t(isSerialized), ProfileCounter(), IsThunk_t(isThunk),
561-
SubclassScope::NotApplicable, (Inline_t)inlineStrategy);
549+
// Otherwise, create a new function.
550+
SILSerializationFunctionBuilder builder(SILMod);
551+
fn = builder.createDeclaration(name, ty, loc);
552+
fn->setLinkage(linkage.getValue());
553+
fn->setTransparent(IsTransparent_t(isTransparent == 1));
554+
fn->setSerialized(IsSerialized_t(isSerialized));
555+
fn->setThunk(IsThunk_t(isThunk));
556+
fn->setInlineStrategy(Inline_t(inlineStrategy));
562557
fn->setGlobalInit(isGlobal == 1);
563-
fn->setEffectsKind((EffectsKind)effect);
564-
fn->setOptimizationMode((OptimizationMode)optimizationMode);
558+
fn->setEffectsKind(EffectsKind(effect));
559+
fn->setOptimizationMode(OptimizationMode(optimizationMode));
565560
fn->setWeakLinked(isWeakLinked);
566561
if (clangNodeOwner)
567562
fn->setClangNodeOwner(clangNodeOwner);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===--- SerializationFunctionBuilder.h -----------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_SERIALIZATION_SERIALIZATIONFUNCTIONBUILDER_H
14+
#define SWIFT_SERIALIZATION_SERIALIZATIONFUNCTIONBUILDER_H
15+
16+
#include "swift/SIL/SILFunctionBuilder.h"
17+
18+
namespace swift {
19+
20+
class LLVM_LIBRARY_VISIBILITY SILSerializationFunctionBuilder {
21+
SILFunctionBuilder builder;
22+
23+
public:
24+
SILSerializationFunctionBuilder(SILModule &mod) : builder(mod) {}
25+
26+
/// Create a SILFunction declaration for use either as a forward reference or
27+
/// for the eventual deserialization of a function body.
28+
SILFunction *createDeclaration(StringRef name, SILType type,
29+
SILLocation loc) {
30+
return builder.createFunction(
31+
SILLinkage::Private, name, type.getAs<SILFunctionType>(), nullptr,
32+
loc, IsNotBare, IsNotTransparent,
33+
IsNotSerialized, ProfileCounter(), IsNotThunk,
34+
SubclassScope::NotApplicable);
35+
}
36+
};
37+
38+
} // namespace swift
39+
40+
#endif

0 commit comments

Comments
 (0)