Skip to content

[sil-parse] Create SILParserFunctionBuilder and use it when parsing. #18441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/swift/SIL/SILFunctionBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

namespace swift {

class SILParserFunctionBuilder;

/// A class for creating SILFunctions in a specific SILModule.
///
/// The intention is that this class is not used directly, but rather that each
Expand All @@ -36,6 +38,8 @@ namespace swift {
class SILFunctionBuilder {
SILModule &mod;

friend class SILParserFunctionBuilder;

public:
SILFunctionBuilder(SILModule &mod) : mod(mod) {}

Expand Down
113 changes: 49 additions & 64 deletions lib/ParseSIL/ParseSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//

#include "SILParserFunctionBuilder.h"
#include "swift/AST/ASTWalker.h"
#include "swift/AST/ExistentialLayout.h"
#include "swift/AST/GenericEnvironment.h"
Expand All @@ -26,7 +27,6 @@
#include "swift/SIL/SILArgument.h"
#include "swift/SIL/SILBuilder.h"
#include "swift/SIL/SILDebugScope.h"
#include "swift/SIL/SILFunctionBuilder.h"
#include "swift/SIL/SILModule.h"
#include "swift/SIL/SILUndef.h"
#include "swift/SIL/TypeLowering.h"
Expand Down Expand Up @@ -517,93 +517,78 @@ bool SILParser::diagnoseProblems() {

/// getGlobalNameForDefinition - Given a definition of a global name, look
/// it up and return an appropriate SIL function.
SILFunction *SILParser::getGlobalNameForDefinition(Identifier Name,
CanSILFunctionType Ty,
SourceLoc Loc) {
SILFunction *SILParser::getGlobalNameForDefinition(Identifier name,
CanSILFunctionType ty,
SourceLoc sourceLoc) {
SILParserFunctionBuilder builder(SILMod);
auto silLoc = RegularLocation(sourceLoc);

// Check to see if a function of this name has been forward referenced. If so
// complete the forward reference.
auto It = TUState.ForwardRefFns.find(Name);
if (It != TUState.ForwardRefFns.end()) {
SILFunction *Fn = It->second.first;
auto iter = TUState.ForwardRefFns.find(name);
if (iter != TUState.ForwardRefFns.end()) {
SILFunction *fn = iter->second.first;

// Verify that the types match up.
if (Fn->getLoweredFunctionType() != Ty) {
P.diagnose(Loc, diag::sil_value_use_type_mismatch, Name.str(),
Fn->getLoweredFunctionType(), Ty);
P.diagnose(It->second.second, diag::sil_prior_reference);
auto loc = RegularLocation(Loc);
SILFunctionBuilder builder(SILMod);
Fn = builder.createFunction(SILLinkage::Private, "", Ty, nullptr, loc,
IsNotBare, IsNotTransparent, IsNotSerialized);
Fn->setDebugScope(new (SILMod) SILDebugScope(loc, Fn));
if (fn->getLoweredFunctionType() != ty) {
P.diagnose(sourceLoc, diag::sil_value_use_type_mismatch, name.str(),
fn->getLoweredFunctionType(), ty);
P.diagnose(iter->second.second, diag::sil_prior_reference);
fn = builder.createFunctionForForwardReference("" /*name*/, ty, silLoc);
}
assert(Fn->isExternalDeclaration() && "Forward defns cannot have bodies!");
TUState.ForwardRefFns.erase(It);

assert(fn->isExternalDeclaration() && "Forward defns cannot have bodies!");
TUState.ForwardRefFns.erase(iter);

// Move the function to this position in the module.
SILMod.getFunctionList().remove(Fn);
SILMod.getFunctionList().push_back(Fn);
//
// FIXME: Should we move this functionality into SILParserFunctionBuilder?
SILMod.getFunctionList().remove(fn);
SILMod.getFunctionList().push_back(fn);

return Fn;
return fn;
}

auto loc = RegularLocation(Loc);

// If we don't have a forward reference, make sure the function hasn't been
// defined already.
if (SILMod.lookUpFunction(Name.str()) != nullptr) {
P.diagnose(Loc, diag::sil_value_redefinition, Name.str());
SILFunctionBuilder builder(SILMod);
auto *fn =
builder.createFunction(SILLinkage::Private, "", Ty, nullptr, loc,
IsNotBare, IsNotTransparent, IsNotSerialized);
fn->setDebugScope(new (SILMod) SILDebugScope(loc, fn));
return fn;
if (SILMod.lookUpFunction(name.str()) != nullptr) {
P.diagnose(sourceLoc, diag::sil_value_redefinition, name.str());
return builder.createFunctionForForwardReference("" /*name*/, ty, silLoc);
}

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



/// getGlobalNameForReference - Given a reference to a global name, look it
/// up and return an appropriate SIL function.
SILFunction *SILParser::getGlobalNameForReference(Identifier Name,
CanSILFunctionType Ty,
SourceLoc Loc,
bool IgnoreFwdRef) {
auto loc = RegularLocation(Loc);

SILFunction *SILParser::getGlobalNameForReference(Identifier name,
CanSILFunctionType funcTy,
SourceLoc sourceLoc,
bool ignoreFwdRef) {
SILParserFunctionBuilder builder(SILMod);
auto silLoc = RegularLocation(sourceLoc);

// Check to see if we have a function by this name already.
if (SILFunction *FnRef = SILMod.lookUpFunction(Name.str())) {
if (SILFunction *fn = SILMod.lookUpFunction(name.str())) {
// If so, check for matching types.
if (FnRef->getLoweredFunctionType() != Ty) {
P.diagnose(Loc, diag::sil_value_use_type_mismatch,
Name.str(), FnRef->getLoweredFunctionType(), Ty);
SILFunctionBuilder builder(SILMod);
FnRef =
builder.createFunction(SILLinkage::Private, "", Ty, nullptr, loc,
IsNotBare, IsNotTransparent, IsNotSerialized);
FnRef->setDebugScope(new (SILMod) SILDebugScope(loc, FnRef));
if (fn->getLoweredFunctionType() == funcTy) {
return fn;
}
return FnRef;

P.diagnose(sourceLoc, diag::sil_value_use_type_mismatch, name.str(),
fn->getLoweredFunctionType(), funcTy);

return builder.createFunctionForForwardReference("" /*name*/, funcTy,
silLoc);
}

// If we didn't find a function, create a new one - it must be a forward
// reference.
SILFunctionBuilder builder(SILMod);
auto *Fn =
builder.createFunction(SILLinkage::Private, Name.str(), Ty, nullptr, loc,
IsNotBare, IsNotTransparent, IsNotSerialized);
Fn->setDebugScope(new (SILMod) SILDebugScope(loc, Fn));
TUState.ForwardRefFns[Name] = { Fn, IgnoreFwdRef ? SourceLoc() : Loc };
return Fn;
auto *fn =
builder.createFunctionForForwardReference(name.str(), funcTy, silLoc);
TUState.ForwardRefFns[name] = {fn, ignoreFwdRef ? SourceLoc() : sourceLoc};
return fn;
}


Expand Down
39 changes: 39 additions & 0 deletions lib/ParseSIL/SILParserFunctionBuilder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//===--- SILParserFunctionBuilder.h ---------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_PARSESIL_SILPARSERFUNCTIONBUILDER_H
#define SWIFT_PARSESIL_SILPARSERFUNCTIONBUILDER_H

#include "swift/SIL/SILFunctionBuilder.h"

namespace swift {

class LLVM_LIBRARY_VISIBILITY SILParserFunctionBuilder {
SILFunctionBuilder builder;

public:
SILParserFunctionBuilder(SILModule &mod) : builder(mod) {}

SILFunction *createFunctionForForwardReference(StringRef name,
CanSILFunctionType ty,
SILLocation loc) {
auto *result =
builder.createFunction(SILLinkage::Private, name, ty, nullptr, loc,
IsNotBare, IsNotTransparent, IsNotSerialized);
result->setDebugScope(new (builder.mod) SILDebugScope(loc, result));
return result;
}
};

} // namespace swift

#endif