Skip to content

Commit cd48a11

Browse files
[NFC] Clean up GenClangType a little bit.
1 parent ea56a63 commit cd48a11

File tree

1 file changed

+52
-57
lines changed

1 file changed

+52
-57
lines changed

lib/IRGen/GenClangType.cpp

Lines changed: 52 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ static clang::CanQualType getClangVectorType(const clang::ASTContext &ctx,
191191
clang::VectorType::VectorKind vecKind,
192192
StringRef numEltsString) {
193193
unsigned numElts;
194-
bool failedParse = numEltsString.getAsInteger<unsigned>(10, numElts);
194+
bool failedParse = numEltsString.getAsInteger(10, numElts);
195195
assert(!failedParse && "vector type name didn't end in count?");
196196
(void) failedParse;
197197

@@ -366,36 +366,33 @@ clang::CanQualType GenClangType::visitTupleType(CanTupleType type) {
366366
return ctx.getCanonicalType(
367367
ctx.getConstantArrayType(clangEltTy, size,
368368
clang::ArrayType::Normal, 0));
369-
370-
llvm_unreachable("Unexpected tuple type in Clang type generation!");
371369
}
372370

373371
clang::CanQualType GenClangType::visitProtocolType(CanProtocolType type) {
374372
auto proto = type->getDecl();
373+
auto &clangCtx = getClangASTContext();
375374

376-
// Single protocol -> id<Proto>
377-
if (proto->isObjC()) {
378-
auto &clangCtx = getClangASTContext();
379-
clang::IdentifierInfo *name = &clangCtx.Idents.get(proto->getName().get());
380-
auto *PDecl = clang::ObjCProtocolDecl::Create(
381-
const_cast<clang::ASTContext &>(clangCtx),
382-
clangCtx.getTranslationUnitDecl(), name,
383-
clang::SourceLocation(), clang::SourceLocation(), nullptr);
384-
385-
// Attach an objc_runtime_name attribute with the Objective-C name to use
386-
// for this protocol.
387-
SmallString<64> runtimeNameBuffer;
388-
PDecl->addAttr(clang::ObjCRuntimeNameAttr::CreateImplicit(
389-
PDecl->getASTContext(),
390-
proto->getObjCRuntimeName(runtimeNameBuffer)));
391-
392-
auto clangType = clangCtx.getObjCObjectType(clangCtx.ObjCBuiltinIdTy,
393-
&PDecl, 1);
394-
auto ptrTy = clangCtx.getObjCObjectPointerType(clangType);
395-
return clangCtx.getCanonicalType(ptrTy);
396-
}
375+
if (!proto->isObjC())
376+
return getClangIdType(clangCtx);
397377

398-
return getClangIdType(getClangASTContext());
378+
// Single protocol -> id<Proto>
379+
clang::IdentifierInfo *name = &clangCtx.Idents.get(proto->getName().get());
380+
auto *PDecl = clang::ObjCProtocolDecl::Create(
381+
const_cast<clang::ASTContext &>(clangCtx),
382+
clangCtx.getTranslationUnitDecl(), name,
383+
clang::SourceLocation(), clang::SourceLocation(), nullptr);
384+
385+
// Attach an objc_runtime_name attribute with the Objective-C name to use
386+
// for this protocol.
387+
SmallString<64> runtimeNameBuffer;
388+
PDecl->addAttr(clang::ObjCRuntimeNameAttr::CreateImplicit(
389+
PDecl->getASTContext(),
390+
proto->getObjCRuntimeName(runtimeNameBuffer)));
391+
392+
auto clangType = clangCtx.getObjCObjectType(clangCtx.ObjCBuiltinIdTy,
393+
&PDecl, 1);
394+
auto ptrTy = clangCtx.getObjCObjectPointerType(clangType);
395+
return clangCtx.getCanonicalType(ptrTy);
399396
}
400397

401398
clang::CanQualType GenClangType::visitMetatypeType(CanMetatypeType type) {
@@ -409,29 +406,30 @@ GenClangType::visitExistentialMetatypeType(CanExistentialMetatypeType type) {
409406

410407
clang::CanQualType GenClangType::visitClassType(CanClassType type) {
411408
auto &clangCtx = getClangASTContext();
412-
// produce the clang type INTF * if it is imported ObjC object.
413409
auto swiftDecl = type->getDecl();
414-
if (swiftDecl->isObjC()) {
415-
clang::IdentifierInfo *ForwardClassId =
416-
&clangCtx.Idents.get(swiftDecl->getName().get());
417-
auto *CDecl = clang::ObjCInterfaceDecl::Create(
418-
clangCtx, clangCtx.getTranslationUnitDecl(),
419-
clang::SourceLocation(), ForwardClassId,
420-
/*typeParamList*/nullptr, /*PrevDecl=*/nullptr,
421-
clang::SourceLocation());
422-
423-
// Attach an objc_runtime_name attribute with the Objective-C name to use
424-
// for this class.
425-
SmallString<64> runtimeNameBuffer;
426-
CDecl->addAttr(clang::ObjCRuntimeNameAttr::CreateImplicit(
427-
CDecl->getASTContext(),
428-
swiftDecl->getObjCRuntimeName(runtimeNameBuffer)));
429-
430-
auto clangType = clangCtx.getObjCInterfaceType(CDecl);
431-
auto ptrTy = clangCtx.getObjCObjectPointerType(clangType);
432-
return clangCtx.getCanonicalType(ptrTy);
433-
}
434-
return getClangIdType(clangCtx);
410+
411+
if (!swiftDecl->isObjC())
412+
return getClangIdType(clangCtx);
413+
414+
// produce the clang type INTF * if it is imported ObjC object.
415+
clang::IdentifierInfo *ForwardClassId =
416+
&clangCtx.Idents.get(swiftDecl->getName().get());
417+
auto *CDecl = clang::ObjCInterfaceDecl::Create(
418+
clangCtx, clangCtx.getTranslationUnitDecl(),
419+
clang::SourceLocation(), ForwardClassId,
420+
/*typeParamList*/nullptr, /*PrevDecl=*/nullptr,
421+
clang::SourceLocation());
422+
423+
// Attach an objc_runtime_name attribute with the Objective-C name to use
424+
// for this class.
425+
SmallString<64> runtimeNameBuffer;
426+
CDecl->addAttr(clang::ObjCRuntimeNameAttr::CreateImplicit(
427+
CDecl->getASTContext(),
428+
swiftDecl->getObjCRuntimeName(runtimeNameBuffer)));
429+
430+
auto clangType = clangCtx.getObjCInterfaceType(CDecl);
431+
auto ptrTy = clangCtx.getObjCObjectPointerType(clangType);
432+
return clangCtx.getCanonicalType(ptrTy);
435433
}
436434

437435
clang::CanQualType GenClangType::visitBoundGenericClassType(
@@ -443,8 +441,7 @@ clang::CanQualType GenClangType::visitBoundGenericClassType(
443441

444442
clang::CanQualType
445443
GenClangType::visitBoundGenericType(CanBoundGenericType type) {
446-
// We only expect *Pointer<T>, ImplicitlyUnwrappedOptional<T>, and Optional<T>.
447-
// The first two are structs; the last is an enum.
444+
// We only expect *Pointer<T>, SIMD*<T> and Optional<T>.
448445
if (auto underlyingTy =
449446
SILType::getPrimitiveObjectType(type).getOptionalObjectType()) {
450447
// The underlying type could be a bridged type, which makes any
@@ -685,15 +682,13 @@ clang::CanQualType GenClangType::visitBuiltinRawPointerType(
685682
clang::CanQualType GenClangType::visitBuiltinIntegerType(
686683
CanBuiltinIntegerType type) {
687684
auto &ctx = getClangASTContext();
688-
if (type->getWidth().isPointerWidth()) {
685+
if (type->getWidth().isPointerWidth())
689686
return ctx.getCanonicalType(ctx.getUIntPtrType());
690-
}
691-
if (type->getWidth().isFixedWidth()) {
692-
auto width = type->getWidth().getFixedWidth();
693-
if (width == 1) return ctx.BoolTy;
694-
return ctx.getCanonicalType(ctx.getIntTypeForBitwidth(width, /*signed*/ 0));
695-
}
696-
llvm_unreachable("");
687+
assert(type->getWidth().isFixedWidth());
688+
auto width = type->getWidth().getFixedWidth();
689+
if (width == 1)
690+
return ctx.BoolTy;
691+
return ctx.getCanonicalType(ctx.getIntTypeForBitwidth(width, /*signed*/ 0));
697692
}
698693

699694
clang::CanQualType GenClangType::visitBuiltinFloatType(

0 commit comments

Comments
 (0)