Skip to content

[NFC] Light refactoring of ParamDecl #10767

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
merged 3 commits into from
Jul 5, 2017
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
17 changes: 4 additions & 13 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -4333,7 +4333,7 @@ class VarDecl : public AbstractStorageDecl {

// For Param Decls

None = Let,
Owned = Let,
InOut = 2,
};

Expand Down Expand Up @@ -4464,6 +4464,9 @@ class VarDecl : public AbstractStorageDecl {
Specifier getSpecifier() const {
return static_cast<Specifier>(VarDeclBits.Specifier);
}
void setSpecifier(Specifier Spec) {
VarDeclBits.Specifier = static_cast<unsigned>(Spec);
}

/// Is this a type ('static') variable?
bool isStatic() const { return VarDeclBits.IsStatic; }
Expand All @@ -4474,18 +4477,6 @@ class VarDecl : public AbstractStorageDecl {

/// Is this an immutable 'let' property?
bool isLet() const { return getSpecifier() == Specifier::Let; }
// FIXME: Remove this setter.
void setLet(bool immutable) {
auto specifier = VarDecl::Specifier::Let;
if (!immutable) {
if (getKind() == DeclKind::Param) {
specifier = VarDecl::Specifier::InOut;
} else {
specifier = VarDecl::Specifier::Var;
}
}
VarDeclBits.Specifier = static_cast<unsigned>(specifier);
}

/// Is this an element in a capture list?
bool isCaptureList() const { return VarDeclBits.IsCaptureList; }
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@ class Parser {
SourceLoc SpecifierLoc;

/// The parsed specifier kind, if present.
VarDecl::Specifier SpecifierKind = VarDecl::Specifier::None;
VarDecl::Specifier SpecifierKind = VarDecl::Specifier::Owned;

/// The location of the first name.
///
Expand Down
4 changes: 2 additions & 2 deletions lib/AST/Builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ getBuiltinFunction(Identifier Id, ArrayRef<Type> argTypes, Type ResType,

SmallVector<ParamDecl*, 4> params;
for (Type argType : argTypes) {
auto PD = new (Context) ParamDecl(VarDecl::Specifier::None, SourceLoc(), SourceLoc(),
auto PD = new (Context) ParamDecl(VarDecl::Specifier::Owned, SourceLoc(), SourceLoc(),
Identifier(), SourceLoc(),
Identifier(), argType,
DC);
Expand Down Expand Up @@ -217,7 +217,7 @@ getBuiltinGenericFunction(Identifier Id,
for (unsigned i = 0, e = ArgParamTypes.size(); i < e; i++) {
auto paramType = ArgBodyTypes[i];
auto paramIfaceType = ArgParamTypes[i].getType();
auto PD = new (Context) ParamDecl(VarDecl::Specifier::None, SourceLoc(), SourceLoc(),
auto PD = new (Context) ParamDecl(VarDecl::Specifier::Owned, SourceLoc(), SourceLoc(),
Identifier(), SourceLoc(),
Identifier(), paramType, DC);
PD->setInterfaceType(paramIfaceType);
Expand Down
4 changes: 2 additions & 2 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4191,7 +4191,7 @@ Type DeclContext::getSelfInterfaceType() const {
/// generic parameters.
ParamDecl *ParamDecl::createUnboundSelf(SourceLoc loc, DeclContext *DC) {
ASTContext &C = DC->getASTContext();
auto *selfDecl = new (C) ParamDecl(VarDecl::Specifier::None, SourceLoc(), SourceLoc(),
auto *selfDecl = new (C) ParamDecl(VarDecl::Specifier::Owned, SourceLoc(), SourceLoc(),
Identifier(), loc, C.Id_self, Type(), DC);
selfDecl->setImplicit();
return selfDecl;
Expand All @@ -4211,7 +4211,7 @@ ParamDecl *ParamDecl::createSelf(SourceLoc loc, DeclContext *DC,
ASTContext &C = DC->getASTContext();
auto selfType = DC->getSelfTypeInContext();
auto selfInterfaceType = DC->getSelfInterfaceType();
auto specifier = VarDecl::Specifier::None;
auto specifier = VarDecl::Specifier::Owned;
assert(selfType && selfInterfaceType);

if (isStaticMethod) {
Expand Down
6 changes: 3 additions & 3 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ makeEnumRawValueConstructor(ClangImporter::Implementation &Impl,
auto selfDecl = ParamDecl::createSelf(SourceLoc(), enumDecl,
/*static*/false, /*inout*/true);

auto param = new (C) ParamDecl(VarDecl::Specifier::None, SourceLoc(),
auto param = new (C) ParamDecl(VarDecl::Specifier::Owned, SourceLoc(),
SourceLoc(), C.Id_rawValue,
SourceLoc(), C.Id_rawValue,
enumDecl->getRawType(),
Expand Down Expand Up @@ -593,7 +593,7 @@ static FuncDecl *makeFieldSetterDecl(ClangImporter::Implementation &Impl,
auto &C = Impl.SwiftContext;
auto selfDecl = ParamDecl::createSelf(SourceLoc(), importedDecl,
/*isStatic*/false, /*isInOut*/true);
auto newValueDecl = new (C) ParamDecl(VarDecl::Specifier::None,
auto newValueDecl = new (C) ParamDecl(VarDecl::Specifier::Owned,
SourceLoc(), SourceLoc(),
Identifier(), SourceLoc(), C.Id_value,
importedFieldDecl->getType(),
Expand Down Expand Up @@ -1128,7 +1128,7 @@ createValueConstructor(ClangImporter::Implementation &Impl,

Identifier argName = generateParamName ? var->getName() : Identifier();
auto param = new (context)
ParamDecl(VarDecl::Specifier::None, SourceLoc(), SourceLoc(), argName,
ParamDecl(VarDecl::Specifier::Owned, SourceLoc(), SourceLoc(), argName,
SourceLoc(), var->getName(), var->getType(), structDecl);
param->setInterfaceType(var->getInterfaceType());
valueParameters.push_back(param);
Expand Down
10 changes: 5 additions & 5 deletions lib/ClangImporter/ImportType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1675,7 +1675,7 @@ ParameterList *ClangImporter::Implementation::importFunctionParameterList(
// imported header unit.
auto paramInfo = createDeclWithClangNode<ParamDecl>(
param, Accessibility::Private,
VarDecl::Specifier::None, SourceLoc(), SourceLoc(), name,
VarDecl::Specifier::Owned, SourceLoc(), SourceLoc(), name,
importSourceLoc(param->getLocation()), bodyName,
dc->mapTypeIntoContext(swiftParamTy),
ImportedHeaderUnit);
Expand All @@ -1696,7 +1696,7 @@ ParameterList *ClangImporter::Implementation::importFunctionParameterList(
BoundGenericType::get(SwiftContext.getArrayDecl(), Type(),
{SwiftContext.TheAnyType});
auto name = SwiftContext.getIdentifier("varargs");
auto param = new (SwiftContext) ParamDecl(VarDecl::Specifier::None,
auto param = new (SwiftContext) ParamDecl(VarDecl::Specifier::Owned,
SourceLoc(), SourceLoc(),
Identifier(), SourceLoc(),
name, paramTy,
Expand Down Expand Up @@ -1997,7 +1997,7 @@ Type ClangImporter::Implementation::importMethodType(
// It doesn't actually matter which DeclContext we use, so just
// use the imported header unit.
auto type = TupleType::getEmpty(SwiftContext);
auto var = new (SwiftContext) ParamDecl(VarDecl::Specifier::None, SourceLoc(),
auto var = new (SwiftContext) ParamDecl(VarDecl::Specifier::Owned, SourceLoc(),
SourceLoc(), argName,
SourceLoc(), argName, type,
ImportedHeaderUnit);
Expand Down Expand Up @@ -2115,7 +2115,7 @@ Type ClangImporter::Implementation::importMethodType(
// Set up the parameter info.
auto paramInfo
= createDeclWithClangNode<ParamDecl>(param, Accessibility::Private,
VarDecl::Specifier::None,
VarDecl::Specifier::Owned,
SourceLoc(), SourceLoc(), name,
importSourceLoc(param->getLocation()),
bodyName,
Expand Down Expand Up @@ -2239,7 +2239,7 @@ Type ClangImporter::Implementation::importAccessorMethodType(
Identifier argLabel = functionName.getDeclName().getArgumentNames().front();
auto paramInfo
= createDeclWithClangNode<ParamDecl>(param, Accessibility::Private,
VarDecl::Specifier::None,
VarDecl::Specifier::Owned,
/*let loc*/SourceLoc(),
/*label loc*/SourceLoc(),
argLabel, nameLoc, bodyName,
Expand Down
4 changes: 2 additions & 2 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3503,7 +3503,7 @@ createSetterAccessorArgument(SourceLoc nameLoc, Identifier name,
name = P.Context.getIdentifier(implName);
}

auto result = new (P.Context) ParamDecl(VarDecl::Specifier::None,
auto result = new (P.Context) ParamDecl(VarDecl::Specifier::Owned,
SourceLoc(),SourceLoc(),
Identifier(), nameLoc, name,
Type(), P.CurDeclContext);
Expand Down Expand Up @@ -4086,7 +4086,7 @@ VarDecl *Parser::parseDeclVarGetSet(Pattern *pattern,
diagnose(accessors.LBLoc, diag::let_cannot_be_addressed_property);
else
diagnose(accessors.LBLoc, diag::let_cannot_be_computed_property);
PrimaryVar->setLet(false);
PrimaryVar->setSpecifier(VarDecl::Specifier::Var);
Invalid = true;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2373,7 +2373,7 @@ parseClosureSignatureIfPresent(SmallVectorImpl<CaptureListEntry> &captureList,

Identifier name = Tok.is(tok::identifier) ?
Context.getIdentifier(Tok.getText()) : Identifier();
auto var = new (Context) ParamDecl(VarDecl::Specifier::None, SourceLoc(),
auto var = new (Context) ParamDecl(VarDecl::Specifier::Owned, SourceLoc(),
SourceLoc(), Identifier(),
Tok.getLoc(), name, Type(), nullptr);
elements.push_back(var);
Expand Down Expand Up @@ -2672,7 +2672,7 @@ Expr *Parser::parseExprAnonClosureArg() {
StringRef varName = ("$" + Twine(nextIdx)).toStringRef(StrBuf);
Identifier ident = Context.getIdentifier(varName);
SourceLoc varLoc = leftBraceLoc;
auto *var = new (Context) ParamDecl(VarDecl::Specifier::None, SourceLoc(),SourceLoc(),
auto *var = new (Context) ParamDecl(VarDecl::Specifier::Owned, SourceLoc(),SourceLoc(),
Identifier(), varLoc, ident, Type(),
closure);
var->setImplicit();
Expand Down
2 changes: 1 addition & 1 deletion lib/Parse/ParsePattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ mapParsedParameters(Parser &parser,
} else if (paramInfo.SpecifierKind == VarDecl::Specifier::InOut) {
parser.diagnose(paramInfo.SpecifierLoc, diag::inout_must_have_type);
paramInfo.SpecifierLoc = SourceLoc();
paramInfo.SpecifierKind = VarDecl::Specifier::None;
paramInfo.SpecifierKind = VarDecl::Specifier::Owned;
}
return param;
};
Expand Down
4 changes: 2 additions & 2 deletions lib/SILGen/SILGenConstructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static SILValue emitConstructorMetatypeArg(SILGenFunction &gen,
Type metatype = ctor->getInterfaceType()->castTo<AnyFunctionType>()->getInput();
auto *DC = ctor->getInnermostDeclContext();
auto &AC = gen.getASTContext();
auto VD = new (AC) ParamDecl(VarDecl::Specifier::None, SourceLoc(), SourceLoc(),
auto VD = new (AC) ParamDecl(VarDecl::Specifier::Owned, SourceLoc(), SourceLoc(),
AC.getIdentifier("$metatype"), SourceLoc(),
AC.getIdentifier("$metatype"), Type(),
DC);
Expand All @@ -61,7 +61,7 @@ static RValue emitImplicitValueConstructorArg(SILGenFunction &gen,
return tuple;
} else {
auto &AC = gen.getASTContext();
auto VD = new (AC) ParamDecl(VarDecl::Specifier::None, SourceLoc(), SourceLoc(),
auto VD = new (AC) ParamDecl(VarDecl::Specifier::Owned, SourceLoc(), SourceLoc(),
AC.getIdentifier("$implicit_value"),
SourceLoc(),
AC.getIdentifier("$implicit_value"), Type(),
Expand Down
46 changes: 15 additions & 31 deletions lib/Sema/CodeSynthesis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,6 @@ static ParamDecl *buildArgument(SourceLoc loc, DeclContext *DC,
return param;
}

static ParamDecl *buildLetArgument(SourceLoc loc, DeclContext *DC,
StringRef name,
Type type,
Type interfaceType) {
return buildArgument(loc, DC, name, type, interfaceType,
VarDecl::Specifier::None);
}

static ParamDecl *buildInOutArgument(SourceLoc loc, DeclContext *DC,
StringRef name,
Type type,
Type interfaceType) {
return buildArgument(
loc, DC, name,
InOutType::get(type),
InOutType::get(interfaceType),
VarDecl::Specifier::InOut);
}

static Type getTypeOfStorage(AbstractStorageDecl *storage,
bool wantInterfaceType) {
if (auto var = dyn_cast<VarDecl>(storage)) {
Expand Down Expand Up @@ -215,10 +196,11 @@ static FuncDecl *createSetterPrototype(AbstractStorageDecl *storage,
// Add a "(value : T, indices...)" argument list.
auto storageType = getTypeOfStorage(storage, false);
auto storageInterfaceType = getTypeOfStorage(storage, true);
valueDecl = buildLetArgument(storage->getLoc(),
storage->getDeclContext(), "value",
storageType,
storageInterfaceType);
valueDecl = buildArgument(storage->getLoc(),
storage->getDeclContext(), "value",
storageType,
storageInterfaceType,
VarDecl::Specifier::Owned);
params.push_back(buildIndexForwardingParamList(storage, valueDecl));

Type setterRetTy = TupleType::getEmpty(TC.Context);
Expand Down Expand Up @@ -317,12 +299,14 @@ static FuncDecl *createMaterializeForSetPrototype(AbstractStorageDecl *storage,
// inout storage: Builtin.UnsafeValueBuffer,
// indices...).
ParamDecl *bufferElements[] = {
buildLetArgument(loc, DC, "buffer",
ctx.TheRawPointerType,
ctx.TheRawPointerType),
buildInOutArgument(loc, DC, "callbackStorage",
ctx.TheUnsafeValueBufferType,
ctx.TheUnsafeValueBufferType)
buildArgument(loc, DC, "buffer",
ctx.TheRawPointerType,
ctx.TheRawPointerType,
VarDecl::Specifier::Owned),
buildArgument(loc, DC, "callbackStorage",
InOutType::get(ctx.TheUnsafeValueBufferType),
InOutType::get(ctx.TheUnsafeValueBufferType),
VarDecl::Specifier::InOut)
};
params.push_back(buildIndexForwardingParamList(storage, bufferElements));

Expand Down Expand Up @@ -1391,7 +1375,7 @@ void TypeChecker::completePropertyBehaviorParameter(VarDecl *VD,
llvm::raw_svector_ostream names(ParamNameBuf);
names << "%arg." << i;
}
auto param = new (Context) ParamDecl(VarDecl::Specifier::None,
auto param = new (Context) ParamDecl(VarDecl::Specifier::Owned,
SourceLoc(), SourceLoc(),
Identifier(),
SourceLoc(),
Expand Down Expand Up @@ -1946,7 +1930,7 @@ ConstructorDecl *swift::createImplicitConstructor(TypeChecker &tc,
}

// Create the parameter.
auto *arg = new (context) ParamDecl(VarDecl::Specifier::None, SourceLoc(),
auto *arg = new (context) ParamDecl(VarDecl::Specifier::Owned, SourceLoc(),
Loc, var->getName(),
Loc, var->getName(), varType, decl);
arg->setInterfaceType(varInterfaceType);
Expand Down
6 changes: 3 additions & 3 deletions lib/Sema/DerivedConformanceCodable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ static CallExpr *createContainerKeyedByCall(ASTContext &C, DeclContext *DC,
Expr *base, Type returnType,
NominalTypeDecl *param) {
// (keyedBy:)
auto *keyedByDecl = new (C) ParamDecl(VarDecl::Specifier::None, SourceLoc(),
auto *keyedByDecl = new (C) ParamDecl(VarDecl::Specifier::Owned, SourceLoc(),
SourceLoc(), C.Id_keyedBy, SourceLoc(),
C.Id_keyedBy, returnType, DC);
keyedByDecl->setImplicit();
Expand Down Expand Up @@ -737,7 +737,7 @@ static FuncDecl *deriveEncodable_encode(TypeChecker &tc, Decl *parentDecl,

// Params: (self [implicit], Encoder)
auto *selfDecl = ParamDecl::createSelf(SourceLoc(), target);
auto *encoderParam = new (C) ParamDecl(VarDecl::Specifier::None, SourceLoc(),
auto *encoderParam = new (C) ParamDecl(VarDecl::Specifier::Owned, SourceLoc(),
SourceLoc(), C.Id_to, SourceLoc(),
C.Id_encoder, encoderType, target);
encoderParam->setInterfaceType(encoderType);
Expand Down Expand Up @@ -1038,7 +1038,7 @@ static ValueDecl *deriveDecodable_init(TypeChecker &tc, Decl *parentDecl,
auto *selfDecl = ParamDecl::createSelf(SourceLoc(), target,
/*isStatic=*/false,
/*isInOut=*/inOut);
auto *decoderParamDecl = new (C) ParamDecl(VarDecl::Specifier::None,
auto *decoderParamDecl = new (C) ParamDecl(VarDecl::Specifier::Owned,
SourceLoc(),
SourceLoc(), C.Id_from,
SourceLoc(), C.Id_decoder,
Expand Down
4 changes: 2 additions & 2 deletions lib/Sema/DerivedConformanceCodingKey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ static void deriveRawValueInit(AbstractFunctionDecl *initDecl) {
DeclNameLoc(), /*Implicit=*/true);

// rawValue param to init(rawValue:)
auto *rawValueDecl = new (C) ParamDecl(VarDecl::Specifier::None, SourceLoc(),
auto *rawValueDecl = new (C) ParamDecl(VarDecl::Specifier::Owned, SourceLoc(),
SourceLoc(), C.Id_rawValue,
SourceLoc(), C.Id_rawValue,
valueParam->getType(), parentDC);
Expand Down Expand Up @@ -124,7 +124,7 @@ static ValueDecl *deriveInitDecl(TypeChecker &tc, Decl *parentDecl,
auto *parentDC = cast<DeclContext>(parentDecl);

// rawValue
auto *rawDecl = new (C) ParamDecl(VarDecl::Specifier::None, SourceLoc(), SourceLoc(),
auto *rawDecl = new (C) ParamDecl(VarDecl::Specifier::Owned, SourceLoc(), SourceLoc(),
paramName, SourceLoc(), paramName,
paramType, parentDC);
rawDecl->setInterfaceType(paramType);
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/DerivedConformanceEquatableHashable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ deriveEquatable_enum_eq(TypeChecker &tc, Decl *parentDecl, EnumDecl *enumDecl) {
auto enumIfaceTy = parentDC->getDeclaredInterfaceType();

auto getParamDecl = [&](StringRef s) -> ParamDecl* {
auto *param = new (C) ParamDecl(VarDecl::Specifier::None, SourceLoc(), SourceLoc(),
auto *param = new (C) ParamDecl(VarDecl::Specifier::Owned, SourceLoc(), SourceLoc(),
Identifier(), SourceLoc(), C.getIdentifier(s),
enumTy, parentDC);
param->setInterfaceType(enumIfaceTy);
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/DerivedConformanceRawRepresentable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ static ConstructorDecl *deriveRawRepresentable_init(TypeChecker &tc,
auto *selfDecl = ParamDecl::createSelf(SourceLoc(), parentDC,
/*static*/false, /*inout*/true);

auto *rawDecl = new (C) ParamDecl(VarDecl::Specifier::None, SourceLoc(), SourceLoc(),
auto *rawDecl = new (C) ParamDecl(VarDecl::Specifier::Owned, SourceLoc(), SourceLoc(),
C.Id_rawValue, SourceLoc(),
C.Id_rawValue, rawType, parentDC);
rawDecl->setInterfaceType(rawInterfaceType);
Expand Down
5 changes: 4 additions & 1 deletion lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1272,7 +1272,10 @@ static void configureImplicitSelf(TypeChecker &tc,

// 'self' is 'let' for reference types (i.e., classes) or when 'self' is
// neither inout.
selfDecl->setLet(!selfIfaceTy->is<InOutType>());
auto specifier = selfIfaceTy->is<InOutType>()
? VarDecl::Specifier::InOut
: VarDecl::Specifier::Owned;
selfDecl->setSpecifier(specifier);

selfDecl->setInterfaceType(selfIfaceTy);
}
Expand Down
Loading