Skip to content

Commit cfa09f4

Browse files
committed
safe pointer -> bounds annotated
1 parent 6dd2d6a commit cfa09f4

File tree

3 files changed

+43
-40
lines changed

3 files changed

+43
-40
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ createFuncOrAccessor(ClangImporter::Implementation &impl, SourceLoc funcLoc,
109109
SourceLoc nameLoc, GenericParamList *genericParams,
110110
ParameterList *bodyParams, Type resultTy, bool async,
111111
bool throws, DeclContext *dc, ClangNode clangNode,
112-
bool hasSafePointer) {
112+
bool hasBoundsAnnotation) {
113113
FuncDecl *decl;
114114
if (accessorInfo) {
115115
decl = AccessorDecl::create(
@@ -125,7 +125,7 @@ createFuncOrAccessor(ClangImporter::Implementation &impl, SourceLoc funcLoc,
125125
genericParams, dc, clangNode);
126126
}
127127
impl.importSwiftAttrAttributes(decl);
128-
if (hasSafePointer)
128+
if (hasBoundsAnnotation)
129129
impl.importBoundsAttributes(decl);
130130

131131
return decl;
@@ -3277,7 +3277,7 @@ namespace {
32773277
return Impl.importFunctionParameterList(
32783278
dc, decl, nonSelfParams, decl->isVariadic(), allowNSUIntegerAsInt,
32793279
argNames, genericParams, /*resultType=*/nullptr,
3280-
/*hasSafePointerParam=*/nullptr);
3280+
/*hasBoundsAnnotatedParam=*/nullptr);
32813281
}
32823282

32833283
Decl *
@@ -3695,7 +3695,7 @@ namespace {
36953695

36963696
bool importFuncWithoutSignature =
36973697
isa<clang::CXXMethodDecl>(decl) && Impl.importSymbolicCXXDecls;
3698-
bool hasSafePointer = false;
3698+
bool hasBoundsAnnotation = false;
36993699
if (!dc->isModuleScopeContext() && !isa<clang::CXXMethodDecl>(decl)) {
37003700
// Handle initializers.
37013701
if (name.getBaseName().isConstructor()) {
@@ -3792,7 +3792,7 @@ namespace {
37923792
importedType = Impl.importFunctionParamsAndReturnType(
37933793
dc, decl, {decl->param_begin(), decl->param_size()},
37943794
decl->isVariadic(), isInSystemModule(dc), name, bodyParams,
3795-
templateParams, &hasSafePointer);
3795+
templateParams, &hasBoundsAnnotation);
37963796
}
37973797

37983798
if (auto *mdecl = dyn_cast<clang::CXXMethodDecl>(decl)) {
@@ -3858,10 +3858,11 @@ namespace {
38583858
} else {
38593859
auto resultTy = importedType.getType();
38603860

3861-
FuncDecl *func = createFuncOrAccessor(
3862-
Impl, loc, accessorInfo, name, nameLoc, genericParams, bodyParams,
3863-
resultTy,
3864-
/*async=*/false, /*throws=*/false, dc, clangNode, hasSafePointer);
3861+
FuncDecl *func =
3862+
createFuncOrAccessor(Impl, loc, accessorInfo, name, nameLoc,
3863+
genericParams, bodyParams, resultTy,
3864+
/*async=*/false, /*throws=*/false, dc,
3865+
clangNode, hasBoundsAnnotation);
38653866
result = func;
38663867

38673868
if (!dc->isModuleScopeContext()) {
@@ -4904,13 +4905,14 @@ namespace {
49044905
}
49054906
}
49064907

4907-
bool hasSafePointer = false; // currently only implemented for functions
4908+
bool hasBoundsAnnotation =
4909+
false; // currently only implemented for functions
49084910
auto result = createFuncOrAccessor(
49094911
Impl,
49104912
/*funcLoc*/ SourceLoc(), accessorInfo, importedName.getDeclName(),
49114913
/*nameLoc*/ SourceLoc(),
49124914
/*genericParams=*/nullptr, bodyParams, resultTy, async, throws, dc,
4913-
decl, hasSafePointer);
4915+
decl, hasBoundsAnnotation);
49144916

49154917
result->setAccess(decl->isDirectMethod() ? AccessLevel::Public
49164918
: getOverridableAccessLevel(dc));
@@ -6551,7 +6553,7 @@ Decl *SwiftDeclConverter::importGlobalAsInitializer(
65516553
parameterList = Impl.importFunctionParameterList(
65526554
dc, decl, {decl->param_begin(), decl->param_end()}, decl->isVariadic(),
65536555
allowNSUIntegerAsInt, argNames, /*genericParams=*/{},
6554-
/*resultType=*/nullptr, /*hasSafePointerParam=*/nullptr);
6556+
/*resultType=*/nullptr, /*hasBoundsAnnotatedParam=*/nullptr);
65556557
}
65566558
if (!parameterList)
65576559
return nullptr;

lib/ClangImporter/ImportType.cpp

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ namespace {
223223
Bridgeability Bridging;
224224
const clang::FunctionType *CompletionHandlerType;
225225
std::optional<unsigned> CompletionHandlerErrorParamIndex;
226-
bool isSafePointer = false;
226+
bool isBoundsAnnotated = false;
227227

228228
public:
229229
SwiftTypeConverter(ClangImporter::Implementation &impl,
@@ -246,7 +246,7 @@ namespace {
246246
return IR;
247247
}
248248

249-
bool hasSafePointer() { return isSafePointer; }
249+
bool hasBoundsAnnotation() { return isBoundsAnnotated; }
250250

251251
ImportResult VisitType(const Type*) = delete;
252252

@@ -422,7 +422,7 @@ namespace {
422422

423423
ImportResult VisitCountAttributedType(
424424
const clang::CountAttributedType *type) {
425-
isSafePointer = true;
425+
isBoundsAnnotated = true;
426426
return Visit(type->desugar());
427427
}
428428

@@ -493,7 +493,7 @@ namespace {
493493
pointeeQualType, ImportTypeKind::Value, addImportDiagnostic,
494494
AllowNSUIntegerAsInt, Bridgeability::None, ImportTypeAttrs(),
495495
OTK_ImplicitlyUnwrappedOptional, /*resugarNSErrorPointer=*/true,
496-
&isSafePointer);
496+
&isBoundsAnnotated);
497497

498498
// If this is imported as a reference type, ignore the innermost pointer.
499499
// (`T *` becomes `T`, but `T **` becomes `UnsafeMutablePointer<T>`.)
@@ -1723,7 +1723,7 @@ ImportedType ClangImporter::Implementation::importType(
17231723
bool allowNSUIntegerAsInt, Bridgeability bridging, ImportTypeAttrs attrs,
17241724
OptionalTypeKind optionality, bool resugarNSErrorPointer,
17251725
std::optional<unsigned> completionHandlerErrorParamIndex,
1726-
bool *isSafePointer) {
1726+
bool *isBoundsAnnotated) {
17271727
if (type.isNull())
17281728
return {Type(), false};
17291729

@@ -1785,8 +1785,8 @@ ImportedType ClangImporter::Implementation::importType(
17851785
*this, addImportDiagnosticFn, allowNSUIntegerAsInt, bridging,
17861786
completionHandlerType, completionHandlerErrorParamIndex);
17871787
auto importResult = converter.Visit(type);
1788-
if (isSafePointer)
1789-
*isSafePointer |= converter.hasSafePointer();
1788+
if (isBoundsAnnotated)
1789+
*isBoundsAnnotated |= converter.hasBoundsAnnotation();
17901790

17911791
// Now fix up the type based on how we're concretely using it.
17921792
auto adjustedType = adjustTypeForConcreteImport(
@@ -1802,11 +1802,12 @@ Type ClangImporter::Implementation::importTypeIgnoreIUO(
18021802
llvm::function_ref<void(Diagnostic &&)> addImportDiagnosticFn,
18031803
bool allowNSUIntegerAsInt, Bridgeability bridging, ImportTypeAttrs attrs,
18041804
OptionalTypeKind optionality, bool resugarNSErrorPointer,
1805-
bool *isSafePointer) {
1805+
bool *isBoundsAnnotated) {
18061806

1807-
auto importedType = importType(
1808-
type, importKind, addImportDiagnosticFn, allowNSUIntegerAsInt, bridging,
1809-
attrs, optionality, resugarNSErrorPointer, std::nullopt, isSafePointer);
1807+
auto importedType =
1808+
importType(type, importKind, addImportDiagnosticFn, allowNSUIntegerAsInt,
1809+
bridging, attrs, optionality, resugarNSErrorPointer,
1810+
std::nullopt, isBoundsAnnotated);
18101811

18111812
return importedType.getType();
18121813
}
@@ -2191,7 +2192,7 @@ applyImportTypeAttrs(ImportTypeAttrs attrs, Type type,
21912192

21922193
ImportedType ClangImporter::Implementation::importFunctionReturnType(
21932194
DeclContext *dc, const clang::FunctionDecl *clangDecl,
2194-
bool allowNSUIntegerAsInt, bool *isSafePointer) {
2195+
bool allowNSUIntegerAsInt, bool *isBoundsAnnotated) {
21952196

21962197
// Hardcode handling of certain result types for builtins.
21972198
if (auto builtinID = clangDecl->getBuiltinID()) {
@@ -2306,7 +2307,7 @@ ImportedType ClangImporter::Implementation::importFunctionReturnType(
23062307
: ImportTypeKind::Result),
23072308
ImportDiagnosticAdder(*this, clangDecl, clangDecl->getLocation()),
23082309
allowNSUIntegerAsInt, Bridgeability::Full, getImportTypeAttrs(clangDecl),
2309-
OptionalityOfReturn, isSafePointer);
2310+
OptionalityOfReturn, isBoundsAnnotated);
23102311
}
23112312

23122313
static Type
@@ -2341,7 +2342,7 @@ ImportedType ClangImporter::Implementation::importFunctionParamsAndReturnType(
23412342
DeclContext *dc, const clang::FunctionDecl *clangDecl,
23422343
ArrayRef<const clang::ParmVarDecl *> params, bool isVariadic,
23432344
bool isFromSystemModule, DeclName name, ParameterList *&parameterList,
2344-
ArrayRef<GenericTypeParamDecl *> genericParams, bool *hasSafePointer) {
2345+
ArrayRef<GenericTypeParamDecl *> genericParams, bool *hasBoundsAnnotation) {
23452346

23462347
bool allowNSUIntegerAsInt =
23472348
shouldAllowNSUIntegerAsInt(isFromSystemModule, clangDecl);
@@ -2399,7 +2400,7 @@ ImportedType ClangImporter::Implementation::importFunctionParamsAndReturnType(
23992400
// was supposed to be used (instead of the typedef type).
24002401
if (!importedType) {
24012402
importedType = importFunctionReturnType(
2402-
dc, clangDecl, allowNSUIntegerAsInt, hasSafePointer);
2403+
dc, clangDecl, allowNSUIntegerAsInt, hasBoundsAnnotation);
24032404
if (!importedType) {
24042405
addDiag(Diagnostic(diag::return_type_not_imported));
24052406
return {Type(), false};
@@ -2411,7 +2412,7 @@ ImportedType ClangImporter::Implementation::importFunctionParamsAndReturnType(
24112412
ArrayRef<Identifier> argNames = name.getArgumentNames();
24122413
parameterList = importFunctionParameterList(
24132414
dc, clangDecl, params, isVariadic, allowNSUIntegerAsInt, argNames,
2414-
genericParams, swiftResultTy, hasSafePointer);
2415+
genericParams, swiftResultTy, hasBoundsAnnotation);
24152416
if (!parameterList)
24162417
return {Type(), false};
24172418

@@ -2548,7 +2549,7 @@ ClangImporter::Implementation::importParameterType(
25482549
}
25492550
}
25502551

2551-
bool isSafePointer = false;
2552+
bool isBoundsAnnotated = false;
25522553
if (!swiftParamTy) {
25532554
// If this is the throws error parameter, we don't need to convert any
25542555
// NSError** arguments to the sugared NSErrorPointer typealias form,
@@ -2562,7 +2563,7 @@ ClangImporter::Implementation::importParameterType(
25622563
paramTy, importKind, addImportDiagnosticFn, allowNSUIntegerAsInt,
25632564
Bridgeability::Full, attrs, optionalityOfParam,
25642565
/*resugarNSErrorPointer=*/!paramIsError,
2565-
completionHandlerErrorParamIndex, &isSafePointer);
2566+
completionHandlerErrorParamIndex, &isBoundsAnnotated);
25662567
if (!importedType)
25672568
return std::nullopt;
25682569

@@ -2580,7 +2581,7 @@ ClangImporter::Implementation::importParameterType(
25802581
isInOut = false;
25812582

25822583
return ImportParameterTypeResult{swiftParamTy, isInOut, isConsuming,
2583-
isParamTypeImplicitlyUnwrapped, isSafePointer};
2584+
isParamTypeImplicitlyUnwrapped, isBoundsAnnotated};
25842585
}
25852586

25862587
bool ClangImporter::Implementation::isDefaultArgSafeToImport(
@@ -2694,7 +2695,7 @@ ParameterList *ClangImporter::Implementation::importFunctionParameterList(
26942695
ArrayRef<const clang::ParmVarDecl *> params, bool isVariadic,
26952696
bool allowNSUIntegerAsInt, ArrayRef<Identifier> argNames,
26962697
ArrayRef<GenericTypeParamDecl *> genericParams, Type resultType,
2697-
bool *hasSafePointerParam) {
2698+
bool *hasBoundsAnnotatedParam) {
26982699
// Import the parameters.
26992700
SmallVector<ParamDecl *, 4> parameters;
27002701
unsigned index = 0;
@@ -2735,8 +2736,8 @@ ParameterList *ClangImporter::Implementation::importFunctionParameterList(
27352736
bool isConsuming = swiftParamTyOpt->isConsuming;
27362737
bool isParamTypeImplicitlyUnwrapped =
27372738
swiftParamTyOpt->isParamTypeImplicitlyUnwrapped;
2738-
if (swiftParamTyOpt->isSafePointer && hasSafePointerParam)
2739-
*hasSafePointerParam = true;
2739+
if (swiftParamTyOpt->isBoundsAnnotated && hasBoundsAnnotatedParam)
2740+
*hasBoundsAnnotatedParam = true;
27402741

27412742
// Retrieve the argument name.
27422743
Identifier name;

lib/ClangImporter/ImporterImpl.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
13591359
OptionalTypeKind optional = OTK_ImplicitlyUnwrappedOptional,
13601360
bool resugarNSErrorPointer = true,
13611361
std::optional<unsigned> completionHandlerErrorParamIndex = std::nullopt,
1362-
bool *isSafePointer = nullptr);
1362+
bool *isBoundsAnnotated = nullptr);
13631363

13641364
/// Import the given Clang type into Swift.
13651365
///
@@ -1376,7 +1376,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
13761376
bool allowNSUIntegerAsInt, Bridgeability topLevelBridgeability,
13771377
ImportTypeAttrs attrs,
13781378
OptionalTypeKind optional = OTK_ImplicitlyUnwrappedOptional,
1379-
bool resugarNSErrorPointer = true, bool *isSafePointer = nullptr);
1379+
bool resugarNSErrorPointer = true, bool *isBoundsAnnotated = nullptr);
13801380

13811381
/// Import the given Clang type into Swift, returning the
13821382
/// Swift parameters and result type and whether we should treat it
@@ -1410,7 +1410,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
14101410
ArrayRef<const clang::ParmVarDecl *> params, bool isVariadic,
14111411
bool isFromSystemModule, DeclName name, ParameterList *&parameterList,
14121412
ArrayRef<GenericTypeParamDecl *> genericParams,
1413-
bool *hasSafePointerParam);
1413+
bool *hasBoundsAnnotatedParam);
14141414

14151415
/// Import the given function return type.
14161416
///
@@ -1425,7 +1425,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
14251425
ImportedType importFunctionReturnType(DeclContext *dc,
14261426
const clang::FunctionDecl *clangDecl,
14271427
bool allowNSUIntegerAsInt,
1428-
bool *isSafePointer = nullptr);
1428+
bool *isBoundsAnnotated = nullptr);
14291429

14301430
/// Import the parameter list for a function
14311431
///
@@ -1443,7 +1443,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
14431443
ArrayRef<const clang::ParmVarDecl *> params, bool isVariadic,
14441444
bool allowNSUIntegerAsInt, ArrayRef<Identifier> argNames,
14451445
ArrayRef<GenericTypeParamDecl *> genericParams, Type resultType,
1446-
bool *hasSafePointerParam);
1446+
bool *hasBoundsAnnotatedParam);
14471447

14481448
struct ImportParameterTypeResult {
14491449
/// The imported parameter Swift type.
@@ -1455,7 +1455,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
14551455
/// If the parameter is implicitly unwrapped or not.
14561456
bool isParamTypeImplicitlyUnwrapped;
14571457
/// If the parameter has (potentially nested) safe pointer types
1458-
bool isSafePointer;
1458+
bool isBoundsAnnotated;
14591459
};
14601460

14611461
/// Import a parameter type

0 commit comments

Comments
 (0)