Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -4903,12 +4903,12 @@ class EnumDecl final : public NominalTypeDecl {
return getAttrs().hasAttribute<IndirectAttr>();
}

/// True if the enum is marked with `@cdecl`.
/// True if the enum is marked with `@c`.
bool isCDeclEnum() const {
return getAttrs().hasAttribute<CDeclAttr>();
}

/// True if the enum is marked with `@cdecl` or `@objc`.
/// True if the enum is marked with `@c` or `@objc`.
bool isCCompatibleEnum() const {
return isCDeclEnum() || isObjC();
}
Expand Down Expand Up @@ -8236,8 +8236,8 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
/// instance method.
bool isObjCInstanceMethod() const;

/// Get the foreign language targeted by a @cdecl-style attribute, if any.
/// Used to abstract away the change in meaning of @cdecl vs @_cdecl while
/// Get the foreign language targeted by a @c-style attribute, if any.
/// Used to abstract away the change in meaning of @c vs @_cdecl while
/// formalizing the attribute.
std::optional<ForeignLanguage> getCDeclKind() const;

Expand Down
4 changes: 2 additions & 2 deletions include/swift/AST/DeclAttr.def
Original file line number Diff line number Diff line change
Expand Up @@ -368,11 +368,11 @@ SIMPLE_DECL_ATTR(_show_in_interface, ShowInInterface,
UserInaccessible | ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove | UnreachableInABIAttr,
62)

DECL_ATTR(_cdecl, CDecl,
DECL_ATTR(c, CDecl,
OnFunc | OnAccessor | OnEnum,
LongAttribute | UserInaccessible | ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove | ForbiddenInABIAttr,
63)
DECL_ATTR_ALIAS(cdecl, CDecl)
DECL_ATTR_ALIAS(_cdecl, CDecl)

SIMPLE_DECL_ATTR(usableFromInline, UsableFromInline,
OnAbstractFunction | OnVar | OnSubscript | OnNominalType | OnTypeAlias,
Expand Down
8 changes: 4 additions & 4 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -2089,10 +2089,10 @@ ERROR(cdecl_empty_name,none,
ERROR(cdecl_throws,none,
"raising errors from %0 functions is not supported", (DeclAttribute))
ERROR(cdecl_incompatible_with_objc,none,
"cannot apply both '@cdecl' and '@objc' to %kindonly0",
"cannot apply both '@c' and '@objc' to %kindonly0",
(const Decl *))
ERROR(cdecl_feature_required,none,
"'@cdecl' requires '-enable-experimental-feature CDecl'",
"'@c' requires '-enable-experimental-feature CDecl'",
())

// @_used and @_section
Expand Down Expand Up @@ -6617,7 +6617,7 @@ ERROR(objc_cannot_infer_name_raw_identifier,none,
(const ValueDecl *))

// If you change this, also change enum ObjCReason
#define OBJC_ATTR_SELECT "select{marked '@cdecl'|marked '@_cdecl'|marked dynamic|marked '@objc'|marked '@objcMembers'|marked '@IBOutlet'|marked '@IBAction'|marked '@IBSegueAction'|marked '@NSManaged'|a member of an '@objc' protocol|implicitly '@objc'|an '@objc' override|an implementation of an '@objc' requirement|marked '@IBInspectable'|marked '@GKInspectable'|in an '@objc' extension of a class (without '@nonobjc')|in an '@objc @implementation' extension of a class (without final or '@nonobjc')|marked '@objc' by an access note}"
#define OBJC_ATTR_SELECT "select{marked '@c'|marked '@_cdecl'|marked dynamic|marked '@objc'|marked '@objcMembers'|marked '@IBOutlet'|marked '@IBAction'|marked '@IBSegueAction'|marked '@NSManaged'|a member of an '@objc' protocol|implicitly '@objc'|an '@objc' override|an implementation of an '@objc' requirement|marked '@IBInspectable'|marked '@GKInspectable'|in an '@objc' extension of a class (without '@nonobjc')|in an '@objc @implementation' extension of a class (without final or '@nonobjc')|marked '@objc' by an access note}"

ERROR(objc_invalid_on_var,none,
"property cannot be %" OBJC_ATTR_SELECT "0 "
Expand Down Expand Up @@ -6671,7 +6671,7 @@ NOTE(not_objc_swift_struct,none,
NOTE(not_objc_swift_enum,none,
"non-'@objc' enums cannot be represented in Objective-C", ())
NOTE(not_cdecl_or_objc_swift_enum,none,
"Swift enums not marked '@cdecl'%select{| or '@objc'}0 cannot be "
"Swift enums not marked '@c'%select{| or '@objc'}0 cannot be "
"represented in %" FOREIGN_LANG_SELECT "0",
(ForeignLanguage))
NOTE(not_objc_generic_type_param,none,
Expand Down
4 changes: 2 additions & 2 deletions include/swift/AST/TypeCheckRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -4948,7 +4948,7 @@ class TypeCheckObjCImplementationRequest
bool isCached() const { return true; }
};

/// Check @cdecl functions for compatibility with the foreign language.
/// Check @c functions for compatibility with the foreign language.
class TypeCheckCDeclFunctionRequest
: public SimpleRequest<TypeCheckCDeclFunctionRequest,
evaluator::SideEffect(FuncDecl *FD,
Expand All @@ -4967,7 +4967,7 @@ class TypeCheckCDeclFunctionRequest
bool isCached() const { return true; }
};

/// Check @cdecl enums for compatibility with C.
/// Check @c enums for compatibility with C.
class TypeCheckCDeclEnumRequest
: public SimpleRequest<TypeCheckCDeclEnumRequest,
evaluator::SideEffect(EnumDecl *ED,
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Basic/Features.def
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ EXPERIMENTAL_FEATURE(ClosureBodyMacro, true)
/// Allow declarations of Swift runtime symbols using @_silgen_name.
EXPERIMENTAL_FEATURE(AllowRuntimeSymbolDeclarations, true)

/// Allow use of `@cdecl`
/// Allow use of `@c`
EXPERIMENTAL_FEATURE(CDecl, false)

/// Allow use of `Module::name` syntax
Expand Down
14 changes: 11 additions & 3 deletions lib/AST/Attr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1246,9 +1246,17 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
break;
}

case DeclAttrKind::CDecl:
Printer << "@_cdecl(\"" << cast<CDeclAttr>(this)->Name << "\")";
case DeclAttrKind::CDecl: {
auto Attr = cast<CDeclAttr>(this);
if (Attr->Underscored)
Printer << "@_cdecl(\"" << cast<CDeclAttr>(this)->Name << "\")";
else {
Printer << "@c";
if (!Attr->Name.empty())
Printer << "(" << cast<CDeclAttr>(this)->Name << ")";
}
break;
}

case DeclAttrKind::Expose: {
Printer.printAttrName("@_expose");
Expand Down Expand Up @@ -1825,7 +1833,7 @@ StringRef DeclAttribute::getAttrName() const {
case DeclAttrKind::CDecl:
if (cast<CDeclAttr>(this)->Underscored)
return "_cdecl";
return "cdecl";
return "c";
case DeclAttrKind::SwiftNativeObjCRuntimeBase:
return "_swift_native_objc_runtime_base";
case DeclAttrKind::Semantics:
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/ClangTypeConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ clang::QualType ClangTypeConverter::visitEnumType(EnumType *type) {

auto ED = type->getDecl();
if (!ED->isCCompatibleEnum())
// Can't translate something not marked with @objc or @cdecl.
// Can't translate something not marked with @objc or @c.
return clang::QualType();

// @objc enums lower to their raw types.
Expand Down
4 changes: 2 additions & 2 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3166,7 +3166,7 @@ getForeignRepresentable(Type type, ForeignLanguage language,
if (isa<ClassDecl>(nominal) || isa<ProtocolDecl>(nominal))
return failure();

// @objc enums are not representable in C, @cdecl ones and imported ones
// @objc enums are not representable in C, @c ones and imported ones
// are ok.
if (!nominal->hasClangNode())
return failure();
Expand Down Expand Up @@ -3209,7 +3209,7 @@ getForeignRepresentable(Type type, ForeignLanguage language,
}
}

// @cdecl enums are representable in C and Objective-C.
// @c enums are representable in C and Objective-C.
if (nominal->getAttrs().getAttribute<CDeclAttr>()) {
return { ForeignRepresentableKind::Trivial, nullptr };
}
Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/GenEnum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6536,7 +6536,7 @@ EnumImplStrategy::get(TypeConverter &TC, SILType type, EnumDecl *theEnum) {
std::move(elementsWithPayload), std::move(elementsWithNoPayload)));
}

// Enums imported from Clang or marked with @objc or @cdecl use a
// Enums imported from Clang or marked with @objc or @c use a
// C-compatible layout.
if (theEnum->hasClangNode() || theEnum->isCCompatibleEnum()) {
assert(elementsWithPayload.empty() && "C enum with payload?!");
Expand Down
2 changes: 1 addition & 1 deletion lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3026,7 +3026,7 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
case DeclAttrKind::CDecl: {
if (!AttrName.starts_with("_") &&

// Backwards support for @cdecl("stringId"). Remove before enabling in
// Backwards support for @c("stringId"). Remove before enabling in
// production so we accept only the identifier format.
lookahead<bool>(1, [&](CancellableBacktrackingScope &) {
return Tok.isNot(tok::string_literal);
Expand Down
10 changes: 5 additions & 5 deletions lib/PrintAsClang/DeclAndTypePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2418,7 +2418,7 @@ class DeclAndTypePrinter::Implementation
if (ED && !NTD->hasClangNode()) {
if (ED->isCDeclEnum()) {
// We should be able to use the tag macro for all printed enums but
// for now restrict it to @cdecl to guard it behind the feature flag.
// for now restrict it to @c to guard it behind the feature flag.
os << "SWIFT_ENUM_TAG ";
} else {
os << "enum ";
Expand Down Expand Up @@ -3056,7 +3056,7 @@ bool DeclAndTypePrinter::shouldInclude(const ValueDecl *VD) {
return false;
}

// In C output mode print only the C variant `@cdecl` (no `@_cdecl`),
// In C output mode print only the C variant `@c` (no `@_cdecl`),
// while in other modes print only `@_cdecl`.
std::optional<ForeignLanguage> cdeclKind = std::nullopt;
if (auto *FD = dyn_cast<AbstractFunctionDecl>(VD))
Expand All @@ -3066,14 +3066,14 @@ bool DeclAndTypePrinter::shouldInclude(const ValueDecl *VD) {
(outputLang == OutputLanguageMode::C))
return false;

// C output mode only prints @cdecl functions and enums.
// C output mode only prints @c functions and enums.
if (outputLang == OutputLanguageMode::C &&
!cdeclKind && !isa<EnumDecl>(VD)) {
return false;
}

// The C mode prints @cdecl enums and reject other enums,
// while other modes accept other enums and reject @cdecl ones.
// The C mode prints @c enums and reject other enums,
// while other modes accept other enums and reject @c ones.
if (isa<EnumDecl>(VD) &&
VD->getAttrs().hasAttribute<CDeclAttr>() !=
(outputLang == OutputLanguageMode::C)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/PrintAsClang/ModuleContentsWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ class ModuleWriter {

if (isa<EnumDecl>(D) && !D->hasClangNode() &&
outputLangMode != OutputLanguageMode::Cxx) {
// We don't want to add an import for a @cdecl or @objc enum declared
// We don't want to add an import for a @c or @objc enum declared
// in Swift. We either do nothing for special enums like Optional as
// done in the prologue here, or we forward declare them.
return false;
Expand Down
2 changes: 1 addition & 1 deletion lib/PrintAsClang/PrintAsClang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ bool swift::printAsClangHeader(raw_ostream &os, ModuleDecl *M,
SwiftToClangInteropContext interopContext(*M, irGenOpts);
writePrologue(os, M->getASTContext(), computeMacroGuard(M));

// C content (@cdecl)
// C content (@c)
std::string moduleContentsScratch;
if (M->getASTContext().LangOpts.hasFeature(Feature::CDecl)) {
SmallPtrSet<ImportModuleTy, 8> imports;
Expand Down
2 changes: 1 addition & 1 deletion lib/SIL/IR/SILFunctionBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ void SILFunctionBuilder::addFunctionAttributes(
for (auto *EA : Attrs.getAttributes<ExposeAttr>()) {
bool shouldExportDecl = true;
if (Attrs.hasAttribute<CDeclAttr>()) {
// If the function is marked with @cdecl, expose only C compatible
// If the function is marked with @c, expose only C compatible
// thunk function.
shouldExportDecl = constant.isNativeToForeignThunk();
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2446,12 +2446,12 @@ void AttributeChecker::visitCDeclAttr(CDeclAttr *attr) {
attr, "func");
}

// Reject using both @cdecl and @objc on the same decl.
// Reject using both @c and @objc on the same decl.
if (D->getAttrs().getAttribute<ObjCAttr>()) {
diagnose(attr->getLocation(), diag::cdecl_incompatible_with_objc, D);
}

// @cdecl needs to be enabled via a feature flag.
// @c needs to be enabled via a feature flag.
if (!attr->Underscored &&
!Ctx.LangOpts.hasFeature(Feature::CDecl)) {
diagnose(attr->getLocation(), diag::cdecl_feature_required);
Expand Down
8 changes: 4 additions & 4 deletions lib/Sema/TypeCheckDeclObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ static void diagnoseTypeNotRepresentableInObjC(const DeclContext *DC,
// Special diagnostic for enums.
if (T->is<EnumType>()) {
if (DC->getASTContext().LangOpts.hasFeature(Feature::CDecl)) {
// New dialog mentioning @cdecl.
// New dialog mentioning @c.
diags.diagnose(TypeRange.Start, diag::not_cdecl_or_objc_swift_enum,
language)
.highlight(TypeRange)
Expand Down Expand Up @@ -1782,13 +1782,13 @@ static bool isCIntegerType(Type type) {
static bool isEnumObjC(EnumDecl *enumDecl, DeclAttribute *attr) {
// FIXME: Use shouldMarkAsObjC once it loses it's TypeChecker argument.

// If there is no @objc or @cdecl attribute, skip it.
// If there is no @objc or @c attribute, skip it.
if (!attr)
return false;

Type rawType = enumDecl->getRawType();

// @objc/@cdecl enums must have a raw type.
// @objc/@c enums must have a raw type.
if (!rawType) {
enumDecl->diagnose(diag::objc_enum_no_raw_type, attr);
return false;
Expand Down Expand Up @@ -4227,7 +4227,7 @@ TypeCheckCDeclFunctionRequest::evaluate(Evaluator &evaluator,
auto &ctx = FD->getASTContext();

auto lang = FD->getCDeclKind();
assert(lang && "missing @cdecl?");
assert(lang && "missing @c?");
auto kind = lang == ForeignLanguage::ObjectiveC
? ObjCReason::ExplicitlyUnderscoreCDecl
: ObjCReason::ExplicitlyCDecl;
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckObjC.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ObjCReason {
public:
// The kind of reason.
enum Kind {
/// Has the '@cdecl' attribute.
/// Has the '@c' attribute.
ExplicitlyCDecl,
/// Has the '@_cdecl' attribute.
ExplicitlyUnderscoreCDecl,
Expand Down
4 changes: 2 additions & 2 deletions test/ASTGen/attrs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ func fn(_: Int) {}
@_disallowFeatureSuppression(NoncopyableGenerics) public struct LoudlyNC<T: ~Copyable> {}

@_cdecl("c_function_name") func cdeclUnderscore(x: Int) {}
@cdecl(c_function_name_official) func cdecl(x: Int) {}
@cdecl func cdeclDefault() {}
@c(c_function_name_official) func cdecl(x: Int) {}
@c func cdeclDefault() {}

struct StaticProperties {
dynamic var property: Int { return 1 }
Expand Down
6 changes: 3 additions & 3 deletions test/IRGen/cdecl_implementation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
// REQUIRES: swift_feature_CImplementation
// REQUIRES: swift_feature_CDecl

@implementation @cdecl
@implementation @c
public func implFunc(_ param: Int32) {}

@implementation @cdecl
@implementation @c
public func implFuncCName(_ param: Int32) {}

@implementation @cdecl(implFuncRenamed_C)
@implementation @c(implFuncRenamed_C)
public func implFuncRenamed_Swift(param: Int32) {}

public func fn() {
Expand Down
18 changes: 9 additions & 9 deletions test/Interpreter/cdecl_enum_run.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
// REQUIRES: swift_feature_CDecl
// REQUIRES: executable_test

@cdecl
@c
enum CDecl8: UInt8 {
case a
case b
}

@cdecl
@c
enum CDecl16: UInt16 {
case a
case b
}

@cdecl(SomeName)
@c(SomeName)
enum CDecl32: UInt32 {
case a
case b
Expand All @@ -33,12 +33,12 @@ case a
case b
}

print("@cdecl enum 8 is \(MemoryLayout<CDecl8>.size) bytes")
// CHECK: @cdecl enum 8 is 1 bytes
print("@cdecl enum 16 is \(MemoryLayout<CDecl16>.size) bytes")
// CHECK: @cdecl enum 16 is 2 bytes
print("@cdecl enum 32 is \(MemoryLayout<CDecl32>.size) bytes")
// CHECK: @cdecl enum 32 is 4 bytes
print("@c enum 8 is \(MemoryLayout<CDecl8>.size) bytes")
// CHECK: @c enum 8 is 1 bytes
print("@c enum 16 is \(MemoryLayout<CDecl16>.size) bytes")
// CHECK: @c enum 16 is 2 bytes
print("@c enum 32 is \(MemoryLayout<CDecl32>.size) bytes")
// CHECK: @c enum 32 is 4 bytes
print("@objc enum is \(MemoryLayout<ObjCEnum>.size) bytes")
// CHECK: @objc enum is 4 bytes
print("Swift enum is \(MemoryLayout<SwiftEnum>.size) bytes")
Expand Down
8 changes: 4 additions & 4 deletions test/Interpreter/cdecl_implementation_run.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,23 @@ extern void renamed_clangSide(ptrdiff_t arg);

//--- Lib.swift

@cdecl @implementation
@c @implementation
public func simple(_ x: Int32, _ y: Int32) -> Int32 {
print(x, y)
return x
}

@implementation @cdecl
@implementation @c
public func primitiveTypes(_ i: Int, _ ci: CInt, _ l: CLong, _ c: CChar, _ f: Float, _ d: Double, _ b: Bool) {
print(i, ci, l, c, f, d, b)
}

@cdecl(sameName) @implementation
@c(sameName) @implementation
public func sameName() {
print("sameName")
}

@cdecl(renamed_clangSide) @implementation
@c(renamed_clangSide) @implementation
public func renamed_swiftSide(_ arg: Int) {
print(arg)
}
Expand Down
Loading