Skip to content

Commit 6c73952

Browse files
committed
Correctly handle Field decls
1 parent 54e220f commit 6c73952

8 files changed

+102
-5
lines changed

lib/IRGen/GenClangDecl.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,19 @@ class ClangDeclFinder
3333
explicit ClangDeclFinder(Fn fn) : callback(fn) {}
3434

3535
bool VisitDeclRefExpr(clang::DeclRefExpr *DRE) {
36-
callback(DRE->getDecl());
36+
if (isa<clang::FunctionDecl>(DRE->getDecl()) ||
37+
isa<clang::VarDecl>(DRE->getDecl())) {
38+
callback(DRE->getDecl());
39+
}
3740
return true;
3841
}
3942

4043
bool VisitMemberExpr(clang::MemberExpr *ME) {
41-
callback(ME->getMemberDecl());
44+
if (isa<clang::FunctionDecl>(ME->getMemberDecl()) ||
45+
isa<clang::VarDecl>(ME->getMemberDecl()) ||
46+
isa<clang::FieldDecl>(ME->getMemberDecl())) {
47+
callback(ME->getMemberDecl());
48+
}
4249
return true;
4350
}
4451

@@ -64,6 +71,10 @@ clang::Decl *getDeclWithExecutableCode(clang::Decl *decl) {
6471
if (initializingDecl) {
6572
return initializingDecl;
6673
}
74+
} else if (auto fd = dyn_cast<clang::FieldDecl>(decl)) {
75+
if(fd->hasInClassInitializer()) {
76+
return fd;
77+
}
6778
}
6879
return nullptr;
6980
}
@@ -116,7 +127,7 @@ void IRGenModule::emitClangDecl(const clang::Decl *decl) {
116127
if (auto var = dyn_cast<clang::VarDecl>(next))
117128
if (!var->isFileVarDecl())
118129
continue;
119-
if (auto fieldDecl = dyn_cast<clang::FieldDecl>(next)) {
130+
if (isa<clang::FieldDecl>(next)) {
120131
continue;
121132
}
122133
ClangCodeGen->HandleTopLevelDecl(clang::DeclGroupRef(next));
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef TEST_INTEROP_CXX_MEMBERS_INPUTS_FIELD_INIT_CALLS_FUNCTION_H
2+
#define TEST_INTEROP_CXX_MEMBERS_INPUTS_FIELD_INIT_CALLS_FUNCTION_H
3+
4+
inline int increment(int t) { return t + 1; }
5+
6+
struct Incrementor {
7+
int incrementee = increment(41);
8+
};
9+
10+
inline int initializeField() { return Incrementor().incrementee; }
11+
12+
#endif // TEST_INTEROP_CXX_MEMBERS_INPUTS_FIELD_INIT_CALLS_FUNCTION_H

test/Interop/Cxx/members/Inputs/module.modulemap

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@ module ConstructorCallsFunction {
22
header "constructor-calls-function.h"
33
}
44

5+
module ConstructorCallsFunctionFromNestedStruct {
6+
header "constructor-calls-function-from-nested-struct.h"
7+
}
8+
59
module ConstructorCallsMethod {
610
header "constructor-calls-method.h"
711
}
812

13+
module FieldInitCallsFunction {
14+
header "field-init-calls-function.h"
15+
}
16+
917
module MethodCallsFunction {
1018
header "method-calls-function.h"
1119
}
@@ -18,6 +26,6 @@ module MethodCallsMethodFromNestedStruct {
1826
header "method-calls-method-from-nested-struct.h"
1927
}
2028

21-
module ConstructorCallsFunctionFromNestedStruct {
22-
header "constructor-calls-function-from-nested-struct.h"
29+
module StaticVarInitCallsFunction {
30+
header "static-var-init-calls-function.h"
2331
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef TEST_INTEROP_CXX_MEMBERS_INPUTS_STATIC_VAR_INIT_CALLS_FUNCTION_H
2+
#define TEST_INTEROP_CXX_MEMBERS_INPUTS_STATIC_VAR_INIT_CALLS_FUNCTION_H
3+
4+
inline int increment(int t) { return t + 1; }
5+
6+
struct Incrementor {
7+
static int incrementee;
8+
};
9+
10+
int Incrementor::incrementee = increment(41);
11+
12+
inline int initializeStaticVar() {
13+
return Incrementor::incrementee;
14+
}
15+
16+
#endif // TEST_INTEROP_CXX_MEMBERS_INPUTS_STATIC_VAR_INIT_CALLS_FUNCTION_H
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %target-swift-emit-ir %s -I %S/Inputs -enable-cxx-interop | %FileCheck %s
2+
3+
import FieldInitCallsFunction
4+
5+
public func getInitializedField() -> CInt {
6+
return initializeField()
7+
}
8+
9+
// CHECK: define linkonce_odr{{( dso_local)?}} i32 @{{_Z9incrementi|"\?increment@@YAHH@Z"}}(i32 %t)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-cxx-interop)
2+
//
3+
// REQUIRES: executable_test
4+
5+
import FieldInitCallsFunction
6+
import StdlibUnittest
7+
8+
var MembersTestSuite = TestSuite("MembersTestSuite")
9+
10+
MembersTestSuite.test("emit-function-from-field-init") {
11+
expectEqual(42, initializeField())
12+
}
13+
14+
runAllTests()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %target-swift-emit-ir %s -I %S/Inputs -enable-cxx-interop -validate-tbd-against-ir=none | %FileCheck %s
2+
3+
// TODO: See why -validate-tbd-against-ir=none is needed here
4+
5+
import StaticVarInitCallsFunction
6+
7+
public func getInitializedStaticVar() -> CInt {
8+
return initializeStaticVar()
9+
}
10+
11+
// CHECK: define linkonce_odr{{( dso_local)?}} i32 @{{_Z9incrementi|"\?increment@@YAHH@Z"}}(i32 %t)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-cxx-interop -Xfrontend -validate-tbd-against-ir=none)
2+
//
3+
// REQUIRES: executable_test
4+
5+
// TODO: See why -validate-tbd-against-ir=none is needed here
6+
7+
import StaticVarInitCallsFunction
8+
import StdlibUnittest
9+
10+
var MembersTestSuite = TestSuite("MembersTestSuite")
11+
12+
MembersTestSuite.test("emit-function-from-static-var-init") {
13+
expectEqual(42, initializeStaticVar())
14+
}
15+
16+
runAllTests()

0 commit comments

Comments
 (0)