Skip to content

Commit 3054c47

Browse files
Merge pull request #5255 from swiftwasm/main
[pull] swiftwasm from main
2 parents 47a23ff + 5427191 commit 3054c47

File tree

140 files changed

+8926
-944
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

140 files changed

+8926
-944
lines changed

docs/ABI/Mangling.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ Entities
348348
entity-spec ::= type 'fU' INDEX // explicit anonymous closure expression
349349
entity-spec ::= type 'fu' INDEX // implicit anonymous closure
350350
entity-spec ::= 'fA' INDEX // default argument N+1 generator
351-
entity-spec ::= 'fa' // runtime discoverable attribute generator
351+
entity-spec ::= entity 'fa' // runtime discoverable attribute generator
352352
entity-spec ::= 'fi' // non-local variable initializer
353353
entity-spec ::= 'fP' // property wrapper backing initializer
354354
entity-spec ::= 'fW' // property wrapper init from projected value
@@ -396,7 +396,10 @@ Entities
396396

397397
macro-discriminator-list ::= macro-discriminator-list? 'fM' macro-expansion-operator INDEX
398398

399+
macro-expansion-operator ::= identifier 'a' // accessor attached macro
400+
macro-expansion-operator ::= identifier 'A' // member-attribute attached macro
399401
macro-expansion-operator ::= identifier 'f' // freestanding macro
402+
macro-expansion-operator ::= identifier 'm' // member attached macro
400403
macro-expansion-operator ::= identifier 'u' // uniquely-named entity
401404

402405
file-discriminator ::= identifier 'Ll' // anonymous file-discriminated declaration

include/swift/AST/ASTMangler.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ class ASTMangler : public Mangler {
343343

344344
std::string mangleTypeAsContextUSR(const NominalTypeDecl *type);
345345

346+
void appendAnyDecl(const ValueDecl *Decl);
346347
std::string mangleAnyDecl(const ValueDecl *Decl, bool prefix,
347348
bool respectOriginallyDefinedIn = false);
348349
std::string mangleDeclAsUSR(const ValueDecl *Decl, StringRef USRPrefix);
@@ -368,9 +369,12 @@ class ASTMangler : public Mangler {
368369

369370
std::string mangleMacroExpansion(const MacroExpansionExpr *expansion);
370371
std::string mangleMacroExpansion(const MacroExpansionDecl *expansion);
372+
std::string mangleAttachedMacroExpansion(
373+
const Decl *decl, CustomAttr *attr, MacroRole role);
374+
371375
void appendMacroExpansionContext(SourceLoc loc, DeclContext *origDC);
372376
void appendMacroExpansionOperator(
373-
StringRef macroName, unsigned discriminator);
377+
StringRef macroName, MacroRole role, unsigned discriminator);
374378

375379
enum SpecialContext {
376380
ObjCContext,

include/swift/AST/ASTScope.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,7 @@ class PatternEntryInitializerScope final : public AbstractPatternEntryScope {
955955

956956
protected:
957957
bool lookupLocalsOrMembers(DeclConsumer) const override;
958+
bool isLabeledStmtLookupTerminator() const override;
958959
};
959960

960961
/// The scope introduced by a conditional clause initializer in an

include/swift/AST/ASTTypeIDZone.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ SWIFT_TYPEID(Fingerprint)
2626
SWIFT_TYPEID(GenericSignature)
2727
SWIFT_TYPEID(ImplicitImportList)
2828
SWIFT_TYPEID(ImplicitMemberAction)
29+
SWIFT_TYPEID(IsSingleValueStmtResult)
2930
SWIFT_TYPEID(ParamSpecifier)
3031
SWIFT_TYPEID(PropertyWrapperAuxiliaryVariables)
3132
SWIFT_TYPEID(PropertyWrapperInitializerInfo)

include/swift/AST/ASTTypeIDs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class GenericParamList;
4141
class GenericSignature;
4242
class GenericTypeParamType;
4343
class InfixOperatorDecl;
44+
class IsSingleValueStmtResult;
4445
class IterableDeclContext;
4546
class ModuleDecl;
4647
struct ImplicitImportList;

include/swift/AST/AccessScope.h

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ class AccessScope {
8585
return !Value.getPointer() && Value.getInt() == AccessLimitKind::Package;
8686
}
8787

88-
/// Returns true if this scope is more restrictive than the argument scope.
89-
/// It's often used to compute the min access scope. The order of restrictiveness
90-
/// is: private (most restrictive), fileprivate, internal, package, public (least restrictive).
91-
/// \see DeclContext::isChildContextOf
88+
/// Returns true if the context of this (use site) is more restrictive than
89+
/// the argument context (decl site). This function does _not_ check the
90+
/// restrictiveness of the access level between this and the argument. \see
91+
/// AccessScope::isInContext
9292
bool isChildOf(AccessScope AS) const {
93-
if (isInternalOrLess()) {
94-
if (AS.isInternalOrLess())
93+
if (isInContext()) {
94+
if (AS.isInContext())
9595
return allowsPrivateAccess(getDeclContext(), AS.getDeclContext());
9696
else
9797
return AS.isPackage() || AS.isPublic();
@@ -103,7 +103,27 @@ class AccessScope {
103103
return false;
104104
}
105105

106-
bool isInternalOrLess() const { return getDeclContext() != nullptr; }
106+
/// Result depends on whether it's called at a use site or a decl site:
107+
///
108+
/// For example,
109+
///
110+
/// ```
111+
/// public func foo(_ arg: bar) {} // `bar` is a `package` decl in another
112+
/// module
113+
/// ```
114+
///
115+
/// The meaning of \c isInContext changes whether it's at the use site or the
116+
/// decl site.
117+
///
118+
/// The use site of \c bar, i.e. \c foo, is "in context" (decl context is
119+
/// non-null), regardless of the access level of \c foo (\c public in this
120+
/// case).
121+
///
122+
/// The decl site of \c bar is only "in context" if the access level of the
123+
/// decl is \c internal or more restrictive. The context at the decl site is\c
124+
/// FileUnit if the decl is \c fileprivate or \c private; \c ModuleDecl if \c
125+
/// internal, and null if \c package or \c public.
126+
bool isInContext() const { return getDeclContext() != nullptr; }
107127

108128
/// Returns the associated access level for diagnostic purposes.
109129
AccessLevel accessLevelForDiagnostics() const;

include/swift/AST/CASTBridging.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ void *SwiftVarDecl_create(void *ctx, BridgedIdentifier _Nullable name,
169169
void *initExpr, void *loc, _Bool isStatic,
170170
_Bool isLet, void *dc);
171171

172+
void *SingleValueStmtExpr_createWithWrappedBranches(void *ctx, void *S,
173+
void *DC, _Bool mustBeExpr);
174+
172175
void *IfStmt_create(void *ctx, void *ifLoc, void *cond, void *_Nullable then,
173176
void *_Nullable elseLoc, void *_Nullable elseStmt);
174177

include/swift/AST/Decl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,11 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
875875
/// declaration.
876876
void forEachAttachedMacro(MacroRole role, MacroCallback) const;
877877

878+
/// Retrieve the discriminator for the given custom attribute that names
879+
/// an attached macro.
880+
unsigned getAttachedMacroDiscriminator(
881+
MacroRole role, const CustomAttr *attr) const;
882+
878883
/// Returns the innermost enclosing decl with an availability annotation.
879884
const Decl *getInnermostDeclWithAvailability() const;
880885

include/swift/AST/DiagnosticsSema.def

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,29 @@ ERROR(ternary_expr_cases_mismatch,none,
11081108
"result values in '? :' expression have mismatching types %0 and %1",
11091109
(Type, Type))
11101110

1111+
// Statements as expressions
1112+
ERROR(single_value_stmt_branches_mismatch,none,
1113+
"branches have mismatching types %0 and %1",
1114+
(Type, Type))
1115+
ERROR(single_value_stmt_out_of_place,none,
1116+
"'%0' may only be used as expression in return, throw, or as the source "
1117+
"of an assignment",
1118+
(StmtKind))
1119+
ERROR(single_value_stmt_must_be_unlabeled,none,
1120+
"'%0' cannot have a jump label when used as expression",
1121+
(StmtKind))
1122+
ERROR(if_expr_must_be_syntactically_exhaustive,none,
1123+
"'if' must have an unconditional 'else' to be used as expression",
1124+
())
1125+
ERROR(single_value_stmt_branch_must_end_in_throw,none,
1126+
"non-expression branch of '%0' expression may only end with a 'throw'",
1127+
(StmtKind))
1128+
ERROR(cannot_jump_in_single_value_stmt,none,
1129+
"cannot '%0' in '%1' when used as expression",
1130+
(StmtKind, StmtKind))
1131+
ERROR(effect_marker_on_single_value_stmt,none,
1132+
"'%0' may not be used on '%1' expression", (StringRef, StmtKind))
1133+
11111134
ERROR(did_not_call_function_value,none,
11121135
"function value was used as a property; add () to call it",
11131136
())

include/swift/AST/Expr.h

Lines changed: 95 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4360,6 +4360,10 @@ class OpaqueValueExpr : public Expr {
43604360
Bits.OpaqueValueExpr.IsPlaceholder = isPlaceholder;
43614361
}
43624362

4363+
static OpaqueValueExpr *
4364+
createImplicit(ASTContext &ctx, Type Ty, bool isPlaceholder = false,
4365+
AllocationArena arena = AllocationArena::Permanent);
4366+
43634367
/// Whether this opaque value expression represents a placeholder that
43644368
/// is injected before type checking to act as a placeholder for some
43654369
/// value to be specified later.
@@ -5975,6 +5979,63 @@ class KeyPathDotExpr : public Expr {
59755979
}
59765980
};
59775981

5982+
/// An expression that may wrap a statement which produces a single value.
5983+
class SingleValueStmtExpr : public Expr {
5984+
public:
5985+
enum class Kind {
5986+
If, Switch
5987+
};
5988+
5989+
private:
5990+
Stmt *S;
5991+
DeclContext *DC;
5992+
5993+
SingleValueStmtExpr(Stmt *S, DeclContext *DC)
5994+
: Expr(ExprKind::SingleValueStmt, /*isImplicit*/ true), S(S), DC(DC) {}
5995+
5996+
public:
5997+
/// Creates a new SingleValueStmtExpr wrapping a statement.
5998+
static SingleValueStmtExpr *create(ASTContext &ctx, Stmt *S, DeclContext *DC);
5999+
6000+
/// Creates a new SingleValueStmtExpr wrapping a statement, and recursively
6001+
/// attempts to wrap any branches of that statement that can become single
6002+
/// value statement expressions.
6003+
///
6004+
/// If \p mustBeExpr is true, branches will be eagerly wrapped even if they
6005+
/// may not be valid SingleValueStmtExprs (which Sema will later diagnose).
6006+
static SingleValueStmtExpr *createWithWrappedBranches(ASTContext &ctx,
6007+
Stmt *S,
6008+
DeclContext *DC,
6009+
bool mustBeExpr);
6010+
6011+
/// Attempt to look through valid parent expressions to a child
6012+
/// SingleValueStmtExpr.
6013+
static SingleValueStmtExpr *tryDigOutSingleValueStmtExpr(Expr *E);
6014+
6015+
/// Retrieve the wrapped statement.
6016+
Stmt *getStmt() const { return S; }
6017+
void setStmt(Stmt *newS) { S = newS; }
6018+
6019+
/// Retrieve the kind of statement being wrapped.
6020+
Kind getStmtKind() const;
6021+
6022+
/// Retrieve the complete set of branches for the underlying statement.
6023+
ArrayRef<Stmt *> getBranches(SmallVectorImpl<Stmt *> &scratch) const;
6024+
6025+
/// Retrieve the single expression branches of the statement, excluding
6026+
/// branches that either have multiple expressions, or have statements.
6027+
ArrayRef<Expr *>
6028+
getSingleExprBranches(SmallVectorImpl<Expr *> &scratch) const;
6029+
6030+
DeclContext *getDeclContext() const { return DC; }
6031+
6032+
SourceRange getSourceRange() const;
6033+
6034+
static bool classof(const Expr *E) {
6035+
return E->getKind() == ExprKind::SingleValueStmt;
6036+
}
6037+
};
6038+
59786039
/// Expression node that effects a "one-way" constraint in
59796040
/// the constraint system, allowing type information to flow from the
59806041
/// subexpression outward but not the other way.
@@ -6008,6 +6069,10 @@ class TypeJoinExpr final : public Expr,
60086069

60096070
DeclRefExpr *Var;
60106071

6072+
/// If this is joining the expression branches for a SingleValueStmtExpr,
6073+
/// this holds the expr node. Otherwise, it is \c nullptr.
6074+
SingleValueStmtExpr *SVE;
6075+
60116076
size_t numTrailingObjects() const {
60126077
return getNumElements();
60136078
}
@@ -6016,11 +6081,34 @@ class TypeJoinExpr final : public Expr,
60166081
return { getTrailingObjects<Expr *>(), getNumElements() };
60176082
}
60186083

6019-
TypeJoinExpr(DeclRefExpr *var, ArrayRef<Expr *> elements);
6084+
TypeJoinExpr(llvm::PointerUnion<DeclRefExpr *, TypeBase *> result,
6085+
ArrayRef<Expr *> elements, SingleValueStmtExpr *SVE);
6086+
6087+
static TypeJoinExpr *
6088+
createImpl(ASTContext &ctx,
6089+
llvm::PointerUnion<DeclRefExpr *, TypeBase *> varOrType,
6090+
ArrayRef<Expr *> elements,
6091+
AllocationArena arena = AllocationArena::Permanent,
6092+
SingleValueStmtExpr *SVE = nullptr);
60206093

60216094
public:
6022-
static TypeJoinExpr *create(ASTContext &ctx, DeclRefExpr *var,
6023-
ArrayRef<Expr *> exprs);
6095+
static TypeJoinExpr *
6096+
create(ASTContext &ctx, DeclRefExpr *var, ArrayRef<Expr *> exprs,
6097+
AllocationArena arena = AllocationArena::Permanent) {
6098+
return createImpl(ctx, var, exprs, arena);
6099+
}
6100+
6101+
static TypeJoinExpr *
6102+
create(ASTContext &ctx, Type joinType, ArrayRef<Expr *> exprs,
6103+
AllocationArena arena = AllocationArena::Permanent) {
6104+
return createImpl(ctx, joinType.getPointer(), exprs, arena);
6105+
}
6106+
6107+
/// Create a join for the branch types of a SingleValueStmtExpr.
6108+
static TypeJoinExpr *
6109+
forBranchesOfSingleValueStmtExpr(ASTContext &ctx, Type joinType,
6110+
SingleValueStmtExpr *SVE,
6111+
AllocationArena arena);
60246112

60256113
SourceLoc getLoc() const { return SourceLoc(); }
60266114
SourceRange getSourceRange() const { return SourceRange(); }
@@ -6044,6 +6132,10 @@ class TypeJoinExpr final : public Expr,
60446132
getMutableElements()[i] = E;
60456133
}
60466134

6135+
/// If this is joining the expression branches for a SingleValueStmtExpr,
6136+
/// this returns the expr node. Otherwise, returns \c nullptr.
6137+
SingleValueStmtExpr *getSingleValueStmtExpr() const { return SVE; }
6138+
60476139
unsigned getNumElements() const { return Bits.TypeJoinExpr.NumElements; }
60486140

60496141
static bool classof(const Expr *E) {

include/swift/AST/ExprNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ EXPR(LazyInitializer, Expr)
205205
EXPR(EditorPlaceholder, Expr)
206206
EXPR(ObjCSelector, Expr)
207207
EXPR(KeyPath, Expr)
208+
EXPR(SingleValueStmt, Expr)
208209
UNCHECKED_EXPR(KeyPathDot, Expr)
209210
UNCHECKED_EXPR(OneWay, Expr)
210211
EXPR(Tap, Expr)

include/swift/AST/Stmt.h

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@ class ASTContext;
3535
class ASTWalker;
3636
class Decl;
3737
class DeclContext;
38+
class Evaluator;
3839
class Expr;
3940
class FuncDecl;
4041
class Pattern;
4142
class PatternBindingDecl;
4243
class VarDecl;
4344
class CaseStmt;
4445
class DoCatchStmt;
46+
class IsSingleValueStmtResult;
4547
class SwitchStmt;
4648

4749
enum class StmtKind {
@@ -133,7 +135,12 @@ class alignas(8) Stmt : public ASTAllocated<Stmt> {
133135

134136
SourceRange getSourceRange() const;
135137
SourceLoc TrailingSemiLoc;
136-
138+
139+
/// Whether the statement can produce a single value, and as such may be
140+
/// treated as an expression.
141+
IsSingleValueStmtResult mayProduceSingleValue(Evaluator &eval) const;
142+
IsSingleValueStmtResult mayProduceSingleValue(ASTContext &ctx) const;
143+
137144
/// isImplicit - Determines whether this statement was implicitly-generated,
138145
/// rather than explicitly written in the AST.
139146
bool isImplicit() const { return Bits.Stmt.Implicit; }
@@ -204,6 +211,10 @@ class BraceStmt final : public Stmt,
204211

205212
ASTNode findAsyncNode();
206213

214+
/// If this brace is wrapping a single expression, returns it. Otherwise
215+
/// returns \c nullptr.
216+
Expr *getSingleExpressionElement() const;
217+
207218
static bool classof(const Stmt *S) { return S->getKind() == StmtKind::Brace; }
208219
};
209220

@@ -711,7 +722,14 @@ class IfStmt : public LabeledConditionalStmt {
711722

712723
Stmt *getElseStmt() const { return Else; }
713724
void setElseStmt(Stmt *s) { Else = s; }
714-
725+
726+
/// Retrieve the complete set of branches for this if statement, including
727+
/// else if statements.
728+
ArrayRef<Stmt *> getBranches(SmallVectorImpl<Stmt *> &scratch) const;
729+
730+
/// Whether the if statement has an unconditional \c else.
731+
bool isSyntacticallyExhaustive() const;
732+
715733
// Implement isa/cast/dyncast/etc.
716734
static bool classof(const Stmt *S) { return S->getKind() == StmtKind::If; }
717735
};
@@ -1283,7 +1301,10 @@ class SwitchStmt final : public LabeledStmt,
12831301
AsCaseStmtRange getCases() const {
12841302
return AsCaseStmtRange(getRawCases(), AsCaseStmtWithSkippingNonCaseStmts());
12851303
}
1286-
1304+
1305+
/// Retrieve the complete set of branches for this switch statement.
1306+
ArrayRef<Stmt *> getBranches(SmallVectorImpl<Stmt *> &scratch) const;
1307+
12871308
static bool classof(const Stmt *S) {
12881309
return S->getKind() == StmtKind::Switch;
12891310
}

0 commit comments

Comments
 (0)