Skip to content

Commit 8e6071e

Browse files
committed
[ClangImporter] Skip value importing on partial/dependent C++ decls, adjust C++ lit tests
1 parent dc65773 commit 8e6071e

10 files changed

+49
-17
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4593,10 +4593,23 @@ namespace {
45934593

45944594
ValueDecl *result = nullptr;
45954595

4596+
bool initIsEvaluatable = false;
4597+
if (auto init = decl->getInit()) {
4598+
// Don't import values for partial specializations. TODO: Should we stop
4599+
// importing partially specialized variables completely?
4600+
bool partial = isa<clang::VarTemplatePartialSpecializationDecl>(decl);
4601+
4602+
// Don't import values when type-dependent or value-dependent.
4603+
bool typeDependent = decl->getType()->isDependentType();
4604+
bool valueDependent = init->isValueDependent();
4605+
4606+
initIsEvaluatable = !partial && !typeDependent && !valueDependent;
4607+
}
4608+
45964609
// If the variable is const (we're importing it as a let), and has an
45974610
// initializer, then ask Clang for its constant value and synthesize a
45984611
// getter with that value.
4599-
if (introducer == VarDecl::Introducer::Let && decl->hasInit()) {
4612+
if (introducer == VarDecl::Introducer::Let && initIsEvaluatable) {
46004613
auto val = decl->evaluateValue();
46014614
// For now, only import integer and float constants. If in the future
46024615
// SwiftDeclSynthesizer::createConstant becomes able to import more

test/ClangImporter/const_values_cxx.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ class OtherClass {
3333
static constexpr int class_static_constexpr_int = 42;
3434
};
3535

36+
template <int N, int M>
37+
inline const int template_gcd = template_gcd<M, N % M>;
38+
39+
template <int N>
40+
inline const int template_gcd<N, 0> = N;
41+
3642

3743
//--- main.swift
3844
func foo() {
@@ -49,6 +55,9 @@ func foo() {
4955

5056
print(MyClass().class_const_int)
5157
print(MyClass.class_static_const_int)
58+
59+
// TODO: This seems to be incorrectly imported, this test here is just to check that the compiler doesn't crash.
60+
print(template_gcd)
5261
}
5362

5463
// Only imported as external declarations:

test/Interop/Cxx/class/access/access-inversion-module-interface.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// CHECK: public init()
1010
// CHECK: private init()
1111
// CHECK: public func privateRecMethod()
12-
// CHECK: public static let PRIVATE_REC_CONST: Bool
12+
// CHECK: public static var PRIVATE_REC_CONST: Bool { get }
1313
// CHECK: }
1414

1515
// CHECK: private struct PrivateEnum : Hashable, Equatable, RawRepresentable {
@@ -26,7 +26,7 @@
2626
// CHECK: case privateEnumClassMember
2727
// CHECK: }
2828

29-
// CHECK: private static let PRIVATE_CONST: Bool
29+
// CHECK: private static var PRIVATE_CONST: Bool { get }
3030

3131
// CHECK: private static var privateAliasVal: Leaky.PrivateAlias
3232
// CHECK: private static var privateRecVal: Leaky.PrivateRec

test/Interop/Cxx/namespace/templates-with-forward-decl-module-interface.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// CHECK-NEXT: init(fwd: NS1.ForwardDeclared<CInt>)
1313
// CHECK-NEXT: typealias MyInt = Int32
1414
// CHECK-NEXT: var fwd: NS1.ForwardDeclared<CInt>
15-
// CHECK-NEXT: static let intValue: NS1.Decl<CInt>.MyInt
15+
// CHECK-NEXT: static var intValue: NS1.Decl<CInt>.MyInt { get }
1616
// CHECK-NEXT: }
1717
// CHECK-NEXT: @available(*, unavailable, message: "Un-specialized class templates are not currently supported. Please use a specialization of this type.")
1818
// CHECK-NEXT: struct Decl<T> {

test/Interop/Cxx/static/static-member-var-irgen.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public func readDefinedConstMember() -> CInt {
4545
}
4646

4747
// CHECK: define {{(protected |dllexport )?}}swiftcc i32 @"$s4main22readDefinedConstMembers5Int32VyF"() #0
48-
// CHECK: [[VALUE:%.*]] = load i32, ptr @{{_ZN21WithConstStaticMember7definedE|"\?defined@WithConstStaticMember@@2HB"}}, align 4
49-
// CHECK: ret i32 [[VALUE]]
48+
// CHECK: ret i32 48
49+
5050
public func readDefinedOutOfLineConstMember() -> CInt {
5151
return WithConstStaticMember.definedOutOfLine
5252
}

test/Interop/Cxx/static/static-member-var-silgen.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ func readDefinedConstMember() -> CInt {
5656
}
5757

5858
// CHECK: sil hidden @$s4main22readDefinedConstMembers5Int32VyF : $@convention(thin) () -> Int32
59-
// CHECK: [[ADDR:%.*]] = global_addr @{{_ZN21WithConstStaticMember7definedE|\?defined@WithConstStaticMember@@2HB}} : $*Int32
60-
// CHECK: [[VALUE:%.*]] = load [[ADDR]] : $*Int32
61-
// CHECK: return [[VALUE]] : $Int32
59+
// CHECK: [[VALUE:%.*]] = integer_literal $Builtin.Int32, 48
60+
// CHECK: [[STRUCT:%.*]] = struct $Int32 ([[VALUE]] : $Builtin.Int32)
61+
// CHECK: return [[STRUCT]] : $Int32
62+
6263
func readDefinedOutOfLineConstMember() -> CInt {
6364
return WithConstStaticMember.definedOutOfLine
6465
}

test/Interop/Cxx/static/static-var-irgen.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ public func initStaticVars() -> CInt {
1010
// CHECK: @{{_ZL9staticVar|staticVar}} = internal global i32 2, align 4
1111
// CHECK: @{{_ZL13staticVarInit|staticVarInit}} = internal global i32 0, align 4
1212
// CHECK: @{{_ZL19staticVarInlineInit|staticVarInlineInit}} = internal global i32 0, align 4
13-
// CHECK: @{{_ZL11staticConst|staticConst}} = internal constant i32 4, align 4
1413
// CHECK: @{{_ZL15staticConstInit|staticConstInit}} = internal global i32 0, align 4
1514
// CHECK: @{{_ZL21staticConstInlineInit|staticConstInlineInit}} = internal global i32 0, align 4
1615
// CHECK: @{{_ZL16staticNonTrivial|staticNonTrivial}} = internal global %class.NonTrivial zeroinitializer, align 4

test/Interop/Cxx/static/static-var-silgen.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ func initStaticVars() -> CInt {
1313
// CHECK: sil_global public_external @staticVarInit : $Int32
1414
// CHECK: // clang name: staticVarInlineInit
1515
// CHECK: sil_global public_external @staticVarInlineInit : $Int32
16-
// CHECK: // clang name: staticConst
17-
// CHECK: sil_global public_external [let] @staticConst : $Int32
1816
// CHECK: // clang name: staticConstInit
1917
// CHECK: sil_global public_external [let] @staticConstInit : $Int32
2018
// CHECK: // clang name: staticConstInlineInit
@@ -24,6 +22,14 @@ func initStaticVars() -> CInt {
2422
// CHECK: // clang name: staticConstNonTrivial
2523
// CHECK: sil_global public_external [let] @staticConstNonTrivial : $NonTrivial
2624

25+
// CHECK: // staticConst.getter
26+
// CHECK: sil shared [transparent] @$sSo11staticConsts5Int32Vvg : $@convention(thin) () -> Int32 {
27+
// CHECK: bb0:
28+
// CHECK: %0 = integer_literal $Builtin.Int32, 4
29+
// CHECK: %1 = struct $Int32 (%0 : $Builtin.Int32)
30+
// CHECK: return %1 : $Int32
31+
// CHECK: }
32+
2733
func readStaticVar() -> CInt {
2834
return staticVar
2935
}

test/Interop/Cxx/templates/explicit-class-specialization-module-interface.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
// CHECK: }
66

77
// CHECK: struct HasEmptySpecializationAndStaticDateMember<CChar> {
8-
// CHECK: static let value: Bool
8+
// CHECK: static var value: Bool { get }
99
// CHECK: }

test/SILOptimizer/access_marker_verify.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -618,11 +618,15 @@ func testShims() -> UInt32 {
618618
}
619619
// CHECK-LABEL: sil hidden [ossa] @$s20access_marker_verify9testShimss6UInt32VyF : $@convention(thin) () -> UInt32 {
620620
// CHECK: bb0:
621-
// CHECK: [[GA:%.*]] = global_addr @_SwiftKeyPathBufferHeader_SizeMask : $*UInt32
622-
// CHECK-NOT: begin_access
623-
// CHECK: load [trivial] [[GA]] : $*UInt32
624-
// CHECK: return
621+
// CHECK: [[FR:%.*]] = function_ref @$sSo34_SwiftKeyPathBufferHeader_SizeMasks6UInt32Vvg : $@convention(thin) () -> UInt32
622+
// CHECK: [[AP:%.*]] = apply [[FR]]() : $@convention(thin) () -> UInt32
623+
// CHECK: return [[AP]]
625624
// CHECK-LABEL: } // end sil function '$s20access_marker_verify9testShimss6UInt32VyF'
625+
// CHECK: sil shared [transparent] [serialized] [ossa] @$sSo34_SwiftKeyPathBufferHeader_SizeMasks6UInt32Vvg : $@convention(thin) () -> UInt32 {
626+
// CHECK: bb0:
627+
// CHECK: integer_literal $Builtin.IntLiteral, 16777215
628+
// CHECK: } // end sil function '$sSo34_SwiftKeyPathBufferHeader_SizeMasks6UInt32Vvg'
629+
626630

627631
// --- global variable initialization.
628632
var globalString1 = "" // start non-empty

0 commit comments

Comments
 (0)