Skip to content

Commit 513507e

Browse files
committed
[interop] refactor PrimitiveTypeMapping to use one struct regardless of language
1 parent f7902a6 commit 513507e

File tree

2 files changed

+14
-24
lines changed

2 files changed

+14
-24
lines changed

lib/PrintAsClang/PrimitiveTypeMapping.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ void PrimitiveTypeMapping::initialize(ASTContext &ctx) {
118118
"SIMD elements is changed");
119119
}
120120

121-
PrimitiveTypeMapping::ClangTypeInfo *
121+
PrimitiveTypeMapping::FullClangTypeInfo *
122122
PrimitiveTypeMapping::getMappedTypeInfoOrNull(const TypeDecl *typeDecl) {
123123
if (mappedTypeNames.empty())
124124
initialize(typeDecl->getASTContext());
@@ -131,27 +131,27 @@ PrimitiveTypeMapping::getMappedTypeInfoOrNull(const TypeDecl *typeDecl) {
131131
return &iter->second;
132132
}
133133

134-
Optional<PrimitiveTypeMapping::ObjCClangTypeInfo>
134+
Optional<PrimitiveTypeMapping::ClangTypeInfo>
135135
PrimitiveTypeMapping::getKnownObjCTypeInfo(const TypeDecl *typeDecl) {
136136
if (auto *typeInfo = getMappedTypeInfoOrNull(typeDecl))
137-
return ObjCClangTypeInfo{typeInfo->objcName, typeInfo->canBeNullable};
137+
return ClangTypeInfo{typeInfo->objcName, typeInfo->canBeNullable};
138138
return None;
139139
}
140140

141-
Optional<PrimitiveTypeMapping::CClangTypeInfo>
141+
Optional<PrimitiveTypeMapping::ClangTypeInfo>
142142
PrimitiveTypeMapping::getKnownCTypeInfo(const TypeDecl *typeDecl) {
143143
if (auto *typeInfo = getMappedTypeInfoOrNull(typeDecl)) {
144144
if (typeInfo->cName)
145-
return CClangTypeInfo{*typeInfo->cName, typeInfo->canBeNullable};
145+
return ClangTypeInfo{*typeInfo->cName, typeInfo->canBeNullable};
146146
}
147147
return None;
148148
}
149149

150-
Optional<PrimitiveTypeMapping::CxxClangTypeInfo>
150+
Optional<PrimitiveTypeMapping::ClangTypeInfo>
151151
PrimitiveTypeMapping::getKnownCxxTypeInfo(const TypeDecl *typeDecl) {
152152
if (auto *typeInfo = getMappedTypeInfoOrNull(typeDecl)) {
153153
if (typeInfo->cxxName)
154-
return CxxClangTypeInfo{*typeInfo->cxxName, typeInfo->canBeNullable};
154+
return ClangTypeInfo{*typeInfo->cxxName, typeInfo->canBeNullable};
155155
}
156156
return None;
157157
}

lib/PrintAsClang/PrimitiveTypeMapping.h

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,37 +30,27 @@ class TypeDecl;
3030
/// but to something like `intptr_t` or `swift::Int` for C and C++ declarations.
3131
class PrimitiveTypeMapping {
3232
public:
33-
struct ObjCClangTypeInfo {
33+
struct ClangTypeInfo {
3434
StringRef name;
3535
bool canBeNullable;
3636
};
3737

3838
/// Returns the Objective-C type name and nullability for the given Swift
3939
/// primitive type declaration, or \c None if no such type name exists.
40-
Optional<ObjCClangTypeInfo> getKnownObjCTypeInfo(const TypeDecl *typeDecl);
41-
42-
struct CClangTypeInfo {
43-
StringRef name;
44-
bool canBeNullable;
45-
};
40+
Optional<ClangTypeInfo> getKnownObjCTypeInfo(const TypeDecl *typeDecl);
4641

4742
/// Returns the C type name and nullability for the given Swift
4843
/// primitive type declaration, or \c None if no such type name exists.
49-
Optional<CClangTypeInfo> getKnownCTypeInfo(const TypeDecl *typeDecl);
50-
51-
struct CxxClangTypeInfo {
52-
StringRef name;
53-
bool canBeNullable;
54-
};
44+
Optional<ClangTypeInfo> getKnownCTypeInfo(const TypeDecl *typeDecl);
5545

5646
/// Returns the C++ type name and nullability for the given Swift
5747
/// primitive type declaration, or \c None if no such type name exists.
58-
Optional<CxxClangTypeInfo> getKnownCxxTypeInfo(const TypeDecl *typeDecl);
48+
Optional<ClangTypeInfo> getKnownCxxTypeInfo(const TypeDecl *typeDecl);
5949

6050
private:
6151
void initialize(ASTContext &ctx);
6252

63-
struct ClangTypeInfo {
53+
struct FullClangTypeInfo {
6454
// The Objective-C name of the Swift type.
6555
StringRef objcName;
6656
// The C name of the Swift type.
@@ -70,15 +60,15 @@ class PrimitiveTypeMapping {
7060
bool canBeNullable;
7161
};
7262

73-
ClangTypeInfo *getMappedTypeInfoOrNull(const TypeDecl *typeDecl);
63+
FullClangTypeInfo *getMappedTypeInfoOrNull(const TypeDecl *typeDecl);
7464

7565
/// A map from {Module, TypeName} pairs to {C name, C nullability} pairs.
7666
///
7767
/// This is populated on first use with a list of known Swift types that are
7868
/// translated directly by the ObjC printer instead of structurally, allowing
7969
/// it to do things like map 'Int' to 'NSInteger' and 'Float' to 'float'.
8070
/// In some sense it's the reverse of the ClangImporter's MappedTypes.def.
81-
llvm::DenseMap<std::pair<Identifier, Identifier>, ClangTypeInfo>
71+
llvm::DenseMap<std::pair<Identifier, Identifier>, FullClangTypeInfo>
8272
mappedTypeNames;
8373
};
8474

0 commit comments

Comments
 (0)