Skip to content

Commit cae5d33

Browse files
committed
[AST] Move BridgedNominalTypeDecl + BridgedVarDecl to ASTBridging
And renaming OptionalBridgedVarDecl to BridgedNullableVarDecl for consistency with the existing nullable AST node wrappers.
1 parent fce1cb5 commit cae5d33

File tree

7 files changed

+138
-63
lines changed

7 files changed

+138
-63
lines changed

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,8 @@ final public class FixLifetimeInst : Instruction, UnaryInstruction {}
357357
public struct VarDecl {
358358
var bridged: BridgedVarDecl
359359

360-
public init?(bridged: OptionalBridgedVarDecl) {
361-
guard let decl = bridged.decl else { return nil }
360+
public init?(bridged: BridgedNullableVarDecl) {
361+
guard let decl = bridged.get() else { return nil }
362362
self.bridged = BridgedVarDecl(decl: decl)
363363
}
364364

include/swift/AST/ASTBridging.h

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
2828
namespace swift {
2929
class DiagnosticArgument;
3030
class DiagnosticEngine;
31+
class NominalTypeDecl;
32+
class VarDecl;
3133
}
3234

3335
//===----------------------------------------------------------------------===//
@@ -86,6 +88,69 @@ void DiagnosticEngine_diagnose(BridgedDiagnosticEngine, BridgedSourceLoc loc,
8688

8789
bool DiagnosticEngine_hadAnyError(BridgedDiagnosticEngine);
8890

91+
//===----------------------------------------------------------------------===//
92+
// NominalTypeDecl
93+
//===----------------------------------------------------------------------===//
94+
95+
class BridgedNominalTypeDecl {
96+
swift::NominalTypeDecl * _Nonnull Ptr;
97+
98+
public:
99+
#ifdef USED_IN_CPP_SOURCE
100+
BridgedNominalTypeDecl(swift::NominalTypeDecl * _Nonnull ptr) : Ptr(ptr) {}
101+
102+
swift::NominalTypeDecl * _Nonnull get() const { return Ptr; }
103+
#endif
104+
};
105+
106+
SWIFT_NAME("BridgedNominalTypeDecl.getName(self:)")
107+
BRIDGED_INLINE
108+
BridgedStringRef BridgedNominalTypeDecl_getName(BridgedNominalTypeDecl decl);
109+
110+
SWIFT_NAME("BridgedNominalTypeDecl.isStructWithUnreferenceableStorage(self:)")
111+
bool BridgedNominalTypeDecl_isStructWithUnreferenceableStorage(
112+
BridgedNominalTypeDecl decl);
113+
114+
SWIFT_NAME("BridgedNominalTypeDecl.isGlobalActor(self:)")
115+
BRIDGED_INLINE
116+
bool BridgedNominalTypeDecl_isGlobalActor(BridgedNominalTypeDecl decl);
117+
118+
//===----------------------------------------------------------------------===//
119+
// VarDecl
120+
//===----------------------------------------------------------------------===//
121+
122+
class BridgedVarDecl {
123+
swift::VarDecl * _Nonnull Ptr;
124+
125+
public:
126+
#ifdef USED_IN_CPP_SOURCE
127+
BridgedVarDecl(swift::VarDecl * _Nonnull ptr) : Ptr(ptr) {}
128+
129+
swift::VarDecl * _Nonnull get() const { return Ptr; }
130+
#endif
131+
};
132+
133+
SWIFT_NAME("BridgedVarDecl.getUserFacingName(self:)")
134+
BRIDGED_INLINE
135+
BridgedStringRef BridgedVarDecl_getUserFacingName(BridgedVarDecl decl);
136+
137+
class BridgedNullableVarDecl {
138+
swift::VarDecl * _Nullable Ptr;
139+
140+
public:
141+
#ifdef USED_IN_CPP_SOURCE
142+
BridgedNullableVarDecl(swift::VarDecl * _Nullable ptr) : Ptr(ptr) {}
143+
144+
swift::VarDecl * _Nullable get() const { return Ptr; }
145+
#endif
146+
};
147+
89148
SWIFT_END_NULLABILITY_ANNOTATIONS
90149

150+
#ifndef PURE_BRIDGING_MODE
151+
// In _not_ PURE_BRIDGING_MODE, briding functions are inlined and therefore
152+
// included in the header file.
153+
#include "ASTBridgingImpl.h"
154+
#endif
155+
91156
#endif // SWIFT_AST_ASTBRIDGING_H

include/swift/AST/ASTBridgingImpl.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//===--- ASTBridgingImpl.h - header for the swift ASTBridging module ------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2023 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_AST_ASTBRIDGINGIMPL_H
14+
#define SWIFT_AST_ASTBRIDGINGIMPL_H
15+
16+
#include "swift/AST/Decl.h"
17+
18+
SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
19+
20+
//===----------------------------------------------------------------------===//
21+
// BridgedNominalTypeDecl
22+
//===----------------------------------------------------------------------===//
23+
24+
BridgedStringRef BridgedNominalTypeDecl_getName(BridgedNominalTypeDecl decl) {
25+
return decl.get()->getName().str();
26+
}
27+
28+
bool BridgedNominalTypeDecl_isGlobalActor(BridgedNominalTypeDecl decl) {
29+
return decl.get()->isGlobalActor();
30+
}
31+
32+
//===----------------------------------------------------------------------===//
33+
// BridgedVarDecl
34+
//===----------------------------------------------------------------------===//
35+
36+
BridgedStringRef BridgedVarDecl_getUserFacingName(BridgedVarDecl decl) {
37+
return decl.get()->getBaseName().userFacingName();
38+
}
39+
40+
SWIFT_END_NULLABILITY_ANNOTATIONS
41+
42+
#endif // SWIFT_AST_ASTBRIDGINGIMPL_H

include/swift/SIL/SILBridging.h

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
// Function implementations should be placed into SILBridgingImpl.h or SILBridging.cpp and
1818
// required header files should be added there.
1919
//
20-
#include "swift/Basic/BasicBridging.h"
20+
#include "swift/AST/ASTBridging.h"
2121

2222
#ifdef USED_IN_CPP_SOURCE
2323
#include "llvm/ADT/ArrayRef.h"
@@ -39,7 +39,6 @@ struct BridgedFunction;
3939
struct BridgedBasicBlock;
4040
struct BridgedSuccessorArray;
4141
struct OptionalBridgedBasicBlock;
42-
struct BridgedNominalTypeDecl;
4342

4443
namespace swift {
4544
class ValueBase;
@@ -132,26 +131,6 @@ struct BridgedType {
132131
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedType getFunctionTypeWithNoEscape(bool withNoEscape) const;
133132
};
134133

135-
// AST bridging
136-
137-
struct BridgedNominalTypeDecl {
138-
swift::NominalTypeDecl * _Nonnull decl;
139-
140-
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef getName() const;
141-
bool isStructWithUnreferenceableStorage() const;
142-
BRIDGED_INLINE bool isGlobalActor() const;
143-
};
144-
145-
struct BridgedVarDecl {
146-
const swift::VarDecl * _Nonnull decl;
147-
148-
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef getUserFacingName() const;
149-
};
150-
151-
struct OptionalBridgedVarDecl {
152-
const swift::VarDecl * _Nullable decl;
153-
};
154-
155134
// SIL Bridging
156135

157136
struct BridgedValue {
@@ -654,19 +633,19 @@ struct BridgedInstruction {
654633
// VarDeclInst and DebugVariableInst
655634
// =========================================================================//
656635

657-
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedVarDecl
636+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedNullableVarDecl
658637
DebugValue_getDecl() const;
659638

660-
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedVarDecl
639+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedNullableVarDecl
661640
AllocStack_getDecl() const;
662641

663-
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedVarDecl
642+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedNullableVarDecl
664643
AllocBox_getDecl() const;
665644

666-
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedVarDecl
645+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedNullableVarDecl
667646
GlobalAddr_getDecl() const;
668647

669-
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedVarDecl
648+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedNullableVarDecl
670649
RefElementAddr_getDecl() const;
671650

672651
BRIDGED_INLINE OptionalBridgedSILDebugVariable DebugValue_getVarInfo() const;

include/swift/SIL/SILBridgingImpl.h

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -223,24 +223,6 @@ BridgedType BridgedType::getFunctionTypeWithNoEscape(bool withNoEscape) const {
223223
return swift::SILType::getPrimitiveObjectType(newTy);
224224
}
225225

226-
//===----------------------------------------------------------------------===//
227-
// BridgedNominalTypeDecl
228-
//===----------------------------------------------------------------------===//
229-
230-
BridgedStringRef BridgedNominalTypeDecl::getName() const {
231-
return decl->getName().str();
232-
}
233-
234-
bool BridgedNominalTypeDecl::isGlobalActor() const { return decl->isGlobalActor(); }
235-
236-
//===----------------------------------------------------------------------===//
237-
// BridgedVarDecl
238-
//===----------------------------------------------------------------------===//
239-
240-
BridgedStringRef BridgedVarDecl::getUserFacingName() const {
241-
return decl->getBaseName().userFacingName();
242-
}
243-
244226
//===----------------------------------------------------------------------===//
245227
// BridgedValue
246228
//===----------------------------------------------------------------------===//
@@ -1033,23 +1015,23 @@ SwiftInt BridgedInstruction::FullApplySite_numIndirectResultArguments() const {
10331015
// VarDeclInst and DebugVariableInst
10341016
//===----------------------------------------------------------------------===//
10351017

1036-
OptionalBridgedVarDecl BridgedInstruction::DebugValue_getDecl() const {
1018+
BridgedNullableVarDecl BridgedInstruction::DebugValue_getDecl() const {
10371019
return {getAs<swift::DebugValueInst>()->getDecl()};
10381020
}
10391021

1040-
OptionalBridgedVarDecl BridgedInstruction::AllocStack_getDecl() const {
1022+
BridgedNullableVarDecl BridgedInstruction::AllocStack_getDecl() const {
10411023
return {getAs<swift::AllocStackInst>()->getDecl()};
10421024
}
10431025

1044-
OptionalBridgedVarDecl BridgedInstruction::AllocBox_getDecl() const {
1026+
BridgedNullableVarDecl BridgedInstruction::AllocBox_getDecl() const {
10451027
return {getAs<swift::AllocBoxInst>()->getDecl()};
10461028
}
10471029

1048-
OptionalBridgedVarDecl BridgedInstruction::GlobalAddr_getDecl() const {
1030+
BridgedNullableVarDecl BridgedInstruction::GlobalAddr_getDecl() const {
10491031
return {getAs<swift::DebugValueInst>()->getDecl()};
10501032
}
10511033

1052-
OptionalBridgedVarDecl BridgedInstruction::RefElementAddr_getDecl() const {
1034+
BridgedNullableVarDecl BridgedInstruction::RefElementAddr_getDecl() const {
10531035
return {getAs<swift::DebugValueInst>()->getDecl()};
10541036
}
10551037

lib/AST/ASTBridging.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
#include "swift/AST/DiagnosticEngine.h"
1616
#include "swift/Basic/BridgingUtils.h"
1717

18+
#ifdef PURE_BRIDGING_MODE
19+
// In PURE_BRIDGING_MODE, bridging functions are not inlined and therefore
20+
// inluded in the cpp file.
21+
#include "swift/AST/ASTBridgingImpl.h"
22+
#endif
23+
1824
using namespace swift;
1925

2026
static_assert(sizeof(BridgedDiagnosticArgument) >= sizeof(DiagnosticArgument),
@@ -68,3 +74,15 @@ void DiagnosticEngine_diagnose(
6874
bool DiagnosticEngine_hadAnyError(BridgedDiagnosticEngine bridgedEngine) {
6975
return bridgedEngine.get()->hadAnyError();
7076
}
77+
78+
//===----------------------------------------------------------------------===//
79+
// BridgedNominalTypeDecl
80+
//===----------------------------------------------------------------------===//
81+
82+
bool BridgedNominalTypeDecl_isStructWithUnreferenceableStorage(
83+
BridgedNominalTypeDecl decl) {
84+
if (auto *structDecl = dyn_cast<swift::StructDecl>(decl.get())) {
85+
return structDecl->hasUnreferenceableStorage();
86+
}
87+
return false;
88+
}

lib/SIL/Utils/SILBridging.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -440,17 +440,6 @@ bool BridgedInstruction::mayBeDeinitBarrierNotConsideringSideEffects() const {
440440
return ::mayBeDeinitBarrierNotConsideringSideEffects(get());
441441
}
442442

443-
//===----------------------------------------------------------------------===//
444-
// BridgedNominalTypeDecl
445-
//===----------------------------------------------------------------------===//
446-
447-
bool BridgedNominalTypeDecl::isStructWithUnreferenceableStorage() const {
448-
if (auto *structDecl = dyn_cast<swift::StructDecl>(decl)) {
449-
return structDecl->hasUnreferenceableStorage();
450-
}
451-
return false;
452-
}
453-
454443
void writeCharToStderr(int c) {
455444
putc(c, stderr);
456445
}

0 commit comments

Comments
 (0)