-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Initial language support for lifetime dependence #71069
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d37b0cf
0cb805b
52def21
79248a5
1894b11
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2166,5 +2166,19 @@ ERROR(requires_experimental_feature, none, | |||||
"%2 is enabled", | ||||||
(StringRef, bool, StringRef)) | ||||||
|
||||||
ERROR(expected_lparen_after_lifetime_dependence, PointsToFirstBadToken, | ||||||
"expected '(' after lifetime dependence specifier", ()) | ||||||
|
||||||
ERROR(expected_identifier_or_index_or_self_after_lifetime_dependence, | ||||||
PointsToFirstBadToken, | ||||||
"expected identifier or index or self in lifetime dependence specifier", | ||||||
()) | ||||||
|
||||||
ERROR(expected_rparen_after_lifetime_dependence, PointsToFirstBadToken, | ||||||
"expected ')' after param list in lifetime dependence specifier", ()) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Also: I don’ t think we have a test case for this diagnostic. |
||||||
|
||||||
ERROR(expected_param_index_lifetime_dependence, PointsToFirstBadToken, | ||||||
"expected unsigned parameter index in lifetime dependence specifier", ()) | ||||||
meg-gupta marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
#define UNDEFINE_DIAGNOSTIC_MACROS | ||||||
#include "DefineDiagnosticMacros.h" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
//===--- LifetimeDependenceSpecifiers.h ------------------------*- C++ -*-===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file defines types and utilities related to Lifetime Dependence | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef SWIFT_AST_LIFETIMEDEPENDENCE_H | ||
#define SWIFT_AST_LIFETIMEDEPENDENCE_H | ||
|
||
#include "swift/AST/Decl.h" | ||
#include "swift/Basic/Debug.h" | ||
#include "swift/Basic/OptionSet.h" | ||
#include "llvm/Support/TrailingObjects.h" | ||
|
||
namespace swift { | ||
|
||
enum class LifetimeDependenceKind : uint8_t { | ||
Copy = 0, | ||
Consume, | ||
Borrow, | ||
Mutate | ||
}; | ||
|
||
class LifetimeDependenceSpecifier { | ||
public: | ||
enum class SpecifierKind { Named, Ordered, Self }; | ||
|
||
private: | ||
SourceLoc loc; | ||
SpecifierKind specifierKind; | ||
LifetimeDependenceKind lifetimeDependenceKind; | ||
union Value { | ||
struct { | ||
Identifier name; | ||
} Named; | ||
struct { | ||
unsigned index; | ||
} Ordered; | ||
struct { | ||
} self; | ||
Value(Identifier name) : Named({name}) {} | ||
Value(unsigned index) : Ordered({index}) {} | ||
Value() {} | ||
} value; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of doing this could we take advantage of inline bit fields here? We'll get a NamedSpecifier, OrderedSpecifier or SelfSpecifier sublcasses and share trailing storage to store name/id similar to #70998? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can I do this as a follow on commit ? |
||
|
||
LifetimeDependenceSpecifier(SourceLoc loc, SpecifierKind specifierKind, | ||
LifetimeDependenceKind lifetimeDependenceKind, | ||
Value value) | ||
: loc(loc), specifierKind(specifierKind), | ||
lifetimeDependenceKind(lifetimeDependenceKind), value(value) {} | ||
|
||
public: | ||
static LifetimeDependenceSpecifier getNamedLifetimeDependenceSpecifier( | ||
SourceLoc loc, LifetimeDependenceKind kind, Identifier name) { | ||
return {loc, SpecifierKind::Named, kind, name}; | ||
} | ||
|
||
static LifetimeDependenceSpecifier getOrderedLifetimeDependenceSpecifier( | ||
SourceLoc loc, LifetimeDependenceKind kind, unsigned index) { | ||
return {loc, SpecifierKind::Ordered, kind, index}; | ||
} | ||
|
||
static LifetimeDependenceSpecifier | ||
getSelfLifetimeDependenceSpecifier(SourceLoc loc, | ||
LifetimeDependenceKind kind) { | ||
return {loc, SpecifierKind::Self, kind, {}}; | ||
} | ||
|
||
SourceLoc getLoc() const { return loc; } | ||
|
||
SpecifierKind getSpecifierKind() const { return specifierKind; } | ||
|
||
LifetimeDependenceKind getLifetimeDependenceKind() const { | ||
return lifetimeDependenceKind; | ||
} | ||
|
||
Identifier getName() const { | ||
assert(specifierKind == SpecifierKind::Named); | ||
return value.Named.name; | ||
} | ||
|
||
unsigned getIndex() const { | ||
assert(specifierKind == SpecifierKind::Ordered); | ||
return value.Ordered.index; | ||
} | ||
std::string getParamString() const { | ||
switch (specifierKind) { | ||
case SpecifierKind::Named: | ||
return value.Named.name.str().str(); | ||
case SpecifierKind::Self: | ||
return "self"; | ||
case SpecifierKind::Ordered: | ||
return std::to_string(value.Ordered.index); | ||
} | ||
llvm_unreachable("Invalid LifetimeDependenceSpecifier::SpecifierKind"); | ||
} | ||
}; | ||
} // namespace swift | ||
|
||
#endif |
Uh oh!
There was an error while loading. Please reload this page.