Skip to content

Commit 7d0ba28

Browse files
committed
Make default arg functions private static functions instead of public global functions
1 parent 5ad0a8a commit 7d0ba28

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

lib/ClangImporter/ImportType.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2660,6 +2660,7 @@ bool ClangImporter::Implementation::isDefaultArgSafeToImport(
26602660
}
26612661

26622662
static ParamDecl *getParameterInfo(ClangImporter::Implementation *impl,
2663+
DeclContext *dc,
26632664
const clang::ParmVarDecl *param,
26642665
const Identifier &name,
26652666
const swift::Type &swiftParamTy,
@@ -2709,7 +2710,7 @@ static ParamDecl *getParameterInfo(ClangImporter::Implementation *impl,
27092710
!param->isTemplated()) {
27102711
SwiftDeclSynthesizer synthesizer(*impl);
27112712
if (CallExpr *defaultArgExpr = synthesizer.makeDefaultArgument(
2712-
param, swiftParamTy, paramInfo->getParameterNameLoc())) {
2713+
dc, param, swiftParamTy, paramInfo->getParameterNameLoc())) {
27132714
paramInfo->setDefaultArgumentKind(DefaultArgumentKind::Normal);
27142715
paramInfo->setTypeCheckedDefaultExpr(defaultArgExpr);
27152716
paramInfo->setDefaultValueStringRepresentation("cxxDefaultArg");
@@ -2774,7 +2775,7 @@ ParameterList *ClangImporter::Implementation::importFunctionParameterList(
27742775
name = argNames[index];
27752776

27762777
auto paramInfo =
2777-
getParameterInfo(this, param, name, swiftParamTy, isInOut, isConsuming,
2778+
getParameterInfo(this, dc, param, name, swiftParamTy, isInOut, isConsuming,
27782779
isParamTypeImplicitlyUnwrapped);
27792780
parameters.push_back(paramInfo);
27802781
++index;
@@ -3411,8 +3412,8 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
34113412

34123413
// Set up the parameter info
34133414
auto paramInfo =
3414-
getParameterInfo(this, param, name, swiftParamTy, isInOut, isConsuming,
3415-
isParamTypeImplicitlyUnwrapped);
3415+
getParameterInfo(this, origDC, param, name, swiftParamTy, isInOut,
3416+
isConsuming, isParamTypeImplicitlyUnwrapped);
34163417

34173418
// Determine whether we have a default argument.
34183419
if (kind == SpecialMethodKind::Regular ||

lib/ClangImporter/SwiftDeclSynthesizer.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2478,7 +2478,8 @@ synthesizeDefaultArgumentBody(AbstractFunctionDecl *afd, void *context) {
24782478
}
24792479

24802480
CallExpr *
2481-
SwiftDeclSynthesizer::makeDefaultArgument(const clang::ParmVarDecl *param,
2481+
SwiftDeclSynthesizer::makeDefaultArgument(DeclContext *dc,
2482+
const clang::ParmVarDecl *param,
24822483
const swift::Type &swiftParamTy,
24832484
SourceLoc paramLoc) {
24842485
assert(param->hasDefaultArg() && "must have a C++ default argument");
@@ -2503,15 +2504,18 @@ SwiftDeclSynthesizer::makeDefaultArgument(const clang::ParmVarDecl *param,
25032504
os << "__defaultArg_" << param->getFunctionScopeIndex() << "_";
25042505
ImporterImpl.getMangledName(mangler.get(), clangFunc, os);
25052506

2506-
// Synthesize `func __defaultArg_XYZ() -> ParamTy { ... }`.
2507+
// Synthesize `static private func __defaultArg_XYZ() -> ParamTy { ... }`
2508+
// in the context of the function where the param is declared.
25072509
DeclName funcName(ctx, DeclBaseName(ctx.getIdentifier(s)),
25082510
ParameterList::createEmpty(ctx));
25092511
auto funcDecl = FuncDecl::createImplicit(
2510-
ctx, StaticSpellingKind::None, funcName, paramLoc, false, false, Type(),
2511-
{}, ParameterList::createEmpty(ctx), swiftParamTy,
2512-
ImporterImpl.ImportedHeaderUnit);
2512+
ctx, StaticSpellingKind::KeywordStatic, funcName, paramLoc, false, false,
2513+
Type(), {}, ParameterList::createEmpty(ctx), swiftParamTy, dc);
25132514
funcDecl->setBodySynthesizer(synthesizeDefaultArgumentBody, (void *)param);
2514-
funcDecl->setAccess(AccessLevel::Public);
2515+
funcDecl->setAccess(AccessLevel::Private); // This is necessary in case the
2516+
// default parameter involves any
2517+
// private types, which are
2518+
// forbidden in public functions.
25152519

25162520
ImporterImpl.defaultArgGenerators[param] = funcDecl;
25172521

lib/ClangImporter/SwiftDeclSynthesizer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,8 @@ class SwiftDeclSynthesizer {
330330
VarDecl *makeComputedPropertyFromCXXMethods(FuncDecl *getter,
331331
FuncDecl *setter);
332332

333-
CallExpr *makeDefaultArgument(const clang::ParmVarDecl *param,
333+
CallExpr *makeDefaultArgument(DeclContext *dc,
334+
const clang::ParmVarDecl *param,
334335
const swift::Type &swiftParamTy,
335336
SourceLoc paramLoc);
336337

0 commit comments

Comments
 (0)