Skip to content

Commit

Permalink
Introduce CGFloat as a distinct struct type.
Browse files Browse the repository at this point in the history
CGFloat is 32-bit on 32-bit architectures and 64-bit on 64-bit
architectures for historical reasons. Rather than having it alias
either Float (32-bit) or Double (64-bit), introduce a distinct struct
type for CGFloat. CGFloat provides a complete set of comparisons and
arithmetic operators (including tgmath functions), initializers allows
explicit conversion between it an Int, UInt, Float, and Double, as
well as conforming to all of the protocols that Float/Double do.

This formulation of CGFloat makes use of CGFloat
architecture-independent, although it still requires a number of casts.
Fixes <rdar://problem/17224725>

Swift SVN r19689
  • Loading branch information
DougGregor committed Jul 8, 2014
1 parent c86df0b commit 9e2b68c
Show file tree
Hide file tree
Showing 17 changed files with 906 additions and 18 deletions.
5 changes: 5 additions & 0 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,11 @@ getSwiftStdlibType(const clang::TypedefNameDecl *D,
ClangCtx.getObjCClassRedefinitionType()))
return std::make_pair(Type(), "");
break;

case MappedCTypeKind::CGFloat:
if (!ClangType->isFloatingType())
return std::make_pair(Type(), "");
break;
}

Module *M;
Expand Down
15 changes: 9 additions & 6 deletions lib/ClangImporter/ImportType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1113,12 +1113,15 @@ Type ClangImporter::Implementation::getNamedSwiftType(Module *module,
return Type();

// Look for the type.
UnqualifiedLookup lookup(SwiftContext.getIdentifier(name), module,
getTypeResolver());
if (auto type = lookup.getSingleTypeResult()) {
if (typeResolver)
typeResolver->resolveDeclSignature(type);
return type->getDeclaredType();
SmallVector<ValueDecl *, 2> results;
module->lookupValue({ }, SwiftContext.getIdentifier(name),
NLKind::UnqualifiedLookup, results);
if (results.size() == 1) {
if (auto type = dyn_cast<TypeDecl>(results.front())) {
if (typeResolver)
typeResolver->resolveDeclSignature(type);
return type->getDeclaredType();
}
}

return Type();
Expand Down
1 change: 1 addition & 0 deletions lib/ClangImporter/ImporterImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ enum class MappedCTypeKind {
ObjCSel,
ObjCId,
ObjCClass,
CGFloat,
};

/// \brief Describes what to do with the C name of a type that can be mapped to
Expand Down
3 changes: 3 additions & 0 deletions lib/ClangImporter/MappedTypes.def
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ MAP_STDLIB_TYPE("NSInteger", SignedWord, 0, "Int", ObjC1, false, DefineOnly)
// confusing.
MAP_STDLIB_TYPE("NSUInteger", UnsignedWord, 0, "Int", ObjC1, false, DoNothing)

// CoreGraphics types.
MAP_TYPE("CGFloat", CGFloat, 0, "CoreGraphics", "CGFloat", All, false,
DoNothing)

// CoreFoundation types.
// Note that we're preserving the typealias for CFIndex.
Expand Down
12 changes: 12 additions & 0 deletions lib/IRGen/GenClangType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ class GenClangType : public CanTypeVisitor<GenClangType, clang::CanQualType> {
}

clang::CanQualType GenClangType::visitStructType(CanStructType type) {
auto swiftDecl = type->getDecl();
auto &swiftCtx = type->getASTContext();
if (swiftDecl->getName().str() == "CGFloat") {
// Dig out the underlying type.
auto underlyingTypeDecl
= cast<TypeDecl>(
swiftDecl->lookupDirect(swiftCtx.getIdentifier("NativeType"))[0]);
return Converter.convert(IGM,
underlyingTypeDecl->getDeclaredType()
->getCanonicalType());
}

// Everything else should have been handled as an imported type
// or an importer-primitive type.
llvm_unreachable("Unhandled struct type in Clang type generation");
Expand Down
9 changes: 9 additions & 0 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2446,6 +2446,15 @@ void TypeChecker::fillObjCRepresentableTypeCache(const DeclContext *DC) {
lookupLibraryTypes(*this, ObjCModule, StdlibTypeNames, ObjCMappedTypes);
}

Identifier ID_CoreGraphics = Context.getIdentifier("CoreGraphics");
if (auto CoreGraphicsModule = Context.getLoadedModule(ID_CoreGraphics)) {
StdlibTypeNames.clear();
StdlibTypeNames.push_back(Context.getIdentifier("CGFloat"));
lookupLibraryTypes(*this, CoreGraphicsModule, StdlibTypeNames,
ObjCMappedTypes);
}


Identifier ID_Foundation = Context.getIdentifier(FOUNDATION_MODULE_NAME);
if (auto FoundationModule = Context.getLoadedModule(ID_Foundation)) {
StdlibTypeNames.clear();
Expand Down

0 comments on commit 9e2b68c

Please sign in to comment.