Skip to content

Commit c8530f4

Browse files
authored
Merge pull request #18441 from gottesmm/pr-cc95c2cfd69d487247a59137995ae90b647073ea
2 parents a061077 + cc72706 commit c8530f4

File tree

3 files changed

+92
-64
lines changed

3 files changed

+92
-64
lines changed

include/swift/SIL/SILFunctionBuilder.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
namespace swift {
1919

20+
class SILParserFunctionBuilder;
21+
2022
/// A class for creating SILFunctions in a specific SILModule.
2123
///
2224
/// The intention is that this class is not used directly, but rather that each
@@ -36,6 +38,8 @@ namespace swift {
3638
class SILFunctionBuilder {
3739
SILModule &mod;
3840

41+
friend class SILParserFunctionBuilder;
42+
3943
public:
4044
SILFunctionBuilder(SILModule &mod) : mod(mod) {}
4145

lib/ParseSIL/ParseSIL.cpp

Lines changed: 49 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#include "SILParserFunctionBuilder.h"
1314
#include "swift/AST/ASTWalker.h"
1415
#include "swift/AST/ExistentialLayout.h"
1516
#include "swift/AST/GenericEnvironment.h"
@@ -26,7 +27,6 @@
2627
#include "swift/SIL/SILArgument.h"
2728
#include "swift/SIL/SILBuilder.h"
2829
#include "swift/SIL/SILDebugScope.h"
29-
#include "swift/SIL/SILFunctionBuilder.h"
3030
#include "swift/SIL/SILModule.h"
3131
#include "swift/SIL/SILUndef.h"
3232
#include "swift/SIL/TypeLowering.h"
@@ -517,93 +517,78 @@ bool SILParser::diagnoseProblems() {
517517

518518
/// getGlobalNameForDefinition - Given a definition of a global name, look
519519
/// it up and return an appropriate SIL function.
520-
SILFunction *SILParser::getGlobalNameForDefinition(Identifier Name,
521-
CanSILFunctionType Ty,
522-
SourceLoc Loc) {
520+
SILFunction *SILParser::getGlobalNameForDefinition(Identifier name,
521+
CanSILFunctionType ty,
522+
SourceLoc sourceLoc) {
523+
SILParserFunctionBuilder builder(SILMod);
524+
auto silLoc = RegularLocation(sourceLoc);
525+
523526
// Check to see if a function of this name has been forward referenced. If so
524527
// complete the forward reference.
525-
auto It = TUState.ForwardRefFns.find(Name);
526-
if (It != TUState.ForwardRefFns.end()) {
527-
SILFunction *Fn = It->second.first;
528-
528+
auto iter = TUState.ForwardRefFns.find(name);
529+
if (iter != TUState.ForwardRefFns.end()) {
530+
SILFunction *fn = iter->second.first;
531+
529532
// Verify that the types match up.
530-
if (Fn->getLoweredFunctionType() != Ty) {
531-
P.diagnose(Loc, diag::sil_value_use_type_mismatch, Name.str(),
532-
Fn->getLoweredFunctionType(), Ty);
533-
P.diagnose(It->second.second, diag::sil_prior_reference);
534-
auto loc = RegularLocation(Loc);
535-
SILFunctionBuilder builder(SILMod);
536-
Fn = builder.createFunction(SILLinkage::Private, "", Ty, nullptr, loc,
537-
IsNotBare, IsNotTransparent, IsNotSerialized);
538-
Fn->setDebugScope(new (SILMod) SILDebugScope(loc, Fn));
533+
if (fn->getLoweredFunctionType() != ty) {
534+
P.diagnose(sourceLoc, diag::sil_value_use_type_mismatch, name.str(),
535+
fn->getLoweredFunctionType(), ty);
536+
P.diagnose(iter->second.second, diag::sil_prior_reference);
537+
fn = builder.createFunctionForForwardReference("" /*name*/, ty, silLoc);
539538
}
540-
541-
assert(Fn->isExternalDeclaration() && "Forward defns cannot have bodies!");
542-
TUState.ForwardRefFns.erase(It);
539+
540+
assert(fn->isExternalDeclaration() && "Forward defns cannot have bodies!");
541+
TUState.ForwardRefFns.erase(iter);
543542

544543
// Move the function to this position in the module.
545-
SILMod.getFunctionList().remove(Fn);
546-
SILMod.getFunctionList().push_back(Fn);
544+
//
545+
// FIXME: Should we move this functionality into SILParserFunctionBuilder?
546+
SILMod.getFunctionList().remove(fn);
547+
SILMod.getFunctionList().push_back(fn);
547548

548-
return Fn;
549+
return fn;
549550
}
550-
551-
auto loc = RegularLocation(Loc);
551+
552552
// If we don't have a forward reference, make sure the function hasn't been
553553
// defined already.
554-
if (SILMod.lookUpFunction(Name.str()) != nullptr) {
555-
P.diagnose(Loc, diag::sil_value_redefinition, Name.str());
556-
SILFunctionBuilder builder(SILMod);
557-
auto *fn =
558-
builder.createFunction(SILLinkage::Private, "", Ty, nullptr, loc,
559-
IsNotBare, IsNotTransparent, IsNotSerialized);
560-
fn->setDebugScope(new (SILMod) SILDebugScope(loc, fn));
561-
return fn;
554+
if (SILMod.lookUpFunction(name.str()) != nullptr) {
555+
P.diagnose(sourceLoc, diag::sil_value_redefinition, name.str());
556+
return builder.createFunctionForForwardReference("" /*name*/, ty, silLoc);
562557
}
563558

564559
// Otherwise, this definition is the first use of this name.
565-
SILFunctionBuilder builder(SILMod);
566-
auto *fn =
567-
builder.createFunction(SILLinkage::Private, Name.str(), Ty, nullptr, loc,
568-
IsNotBare, IsNotTransparent, IsNotSerialized);
569-
fn->setDebugScope(new (SILMod) SILDebugScope(loc, fn));
570-
return fn;
560+
return builder.createFunctionForForwardReference(name.str(), ty, silLoc);
571561
}
572562

573-
574-
575563
/// getGlobalNameForReference - Given a reference to a global name, look it
576564
/// up and return an appropriate SIL function.
577-
SILFunction *SILParser::getGlobalNameForReference(Identifier Name,
578-
CanSILFunctionType Ty,
579-
SourceLoc Loc,
580-
bool IgnoreFwdRef) {
581-
auto loc = RegularLocation(Loc);
582-
565+
SILFunction *SILParser::getGlobalNameForReference(Identifier name,
566+
CanSILFunctionType funcTy,
567+
SourceLoc sourceLoc,
568+
bool ignoreFwdRef) {
569+
SILParserFunctionBuilder builder(SILMod);
570+
auto silLoc = RegularLocation(sourceLoc);
571+
583572
// Check to see if we have a function by this name already.
584-
if (SILFunction *FnRef = SILMod.lookUpFunction(Name.str())) {
573+
if (SILFunction *fn = SILMod.lookUpFunction(name.str())) {
585574
// If so, check for matching types.
586-
if (FnRef->getLoweredFunctionType() != Ty) {
587-
P.diagnose(Loc, diag::sil_value_use_type_mismatch,
588-
Name.str(), FnRef->getLoweredFunctionType(), Ty);
589-
SILFunctionBuilder builder(SILMod);
590-
FnRef =
591-
builder.createFunction(SILLinkage::Private, "", Ty, nullptr, loc,
592-
IsNotBare, IsNotTransparent, IsNotSerialized);
593-
FnRef->setDebugScope(new (SILMod) SILDebugScope(loc, FnRef));
575+
if (fn->getLoweredFunctionType() == funcTy) {
576+
return fn;
594577
}
595-
return FnRef;
578+
579+
P.diagnose(sourceLoc, diag::sil_value_use_type_mismatch, name.str(),
580+
fn->getLoweredFunctionType(), funcTy);
581+
582+
return builder.createFunctionForForwardReference("" /*name*/, funcTy,
583+
silLoc);
596584
}
597585

598586
// If we didn't find a function, create a new one - it must be a forward
599587
// reference.
600-
SILFunctionBuilder builder(SILMod);
601-
auto *Fn =
602-
builder.createFunction(SILLinkage::Private, Name.str(), Ty, nullptr, loc,
603-
IsNotBare, IsNotTransparent, IsNotSerialized);
604-
Fn->setDebugScope(new (SILMod) SILDebugScope(loc, Fn));
605-
TUState.ForwardRefFns[Name] = { Fn, IgnoreFwdRef ? SourceLoc() : Loc };
606-
return Fn;
588+
auto *fn =
589+
builder.createFunctionForForwardReference(name.str(), funcTy, silLoc);
590+
TUState.ForwardRefFns[name] = {fn, ignoreFwdRef ? SourceLoc() : sourceLoc};
591+
return fn;
607592
}
608593

609594

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===--- SILParserFunctionBuilder.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_PARSESIL_SILPARSERFUNCTIONBUILDER_H
14+
#define SWIFT_PARSESIL_SILPARSERFUNCTIONBUILDER_H
15+
16+
#include "swift/SIL/SILFunctionBuilder.h"
17+
18+
namespace swift {
19+
20+
class LLVM_LIBRARY_VISIBILITY SILParserFunctionBuilder {
21+
SILFunctionBuilder builder;
22+
23+
public:
24+
SILParserFunctionBuilder(SILModule &mod) : builder(mod) {}
25+
26+
SILFunction *createFunctionForForwardReference(StringRef name,
27+
CanSILFunctionType ty,
28+
SILLocation loc) {
29+
auto *result =
30+
builder.createFunction(SILLinkage::Private, name, ty, nullptr, loc,
31+
IsNotBare, IsNotTransparent, IsNotSerialized);
32+
result->setDebugScope(new (builder.mod) SILDebugScope(loc, result));
33+
return result;
34+
}
35+
};
36+
37+
} // namespace swift
38+
39+
#endif

0 commit comments

Comments
 (0)