Skip to content

Commit 373bc85

Browse files
committed
Merge branch 'main' of github.com:apple/swift into declaration-macro-name-lookup
2 parents b6552ed + c4c256a commit 373bc85

Some content is hidden

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

56 files changed

+3602
-278
lines changed

docs/ABI/Mangling.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ Entities
365365
entity-spec ::= decl-name type 'fp' // generic type parameter
366366
entity-spec ::= decl-name type 'fo' // enum element (currently not used)
367367
entity-spec ::= decl-name label-list? type generic-signature? 'fm' // macro
368+
entity-spec ::= context macro-discriminator-list // macro expansion
368369
entity-spec ::= identifier 'Qa' // associated type declaration
369370

370371
ACCESSOR ::= 'm' // materializeForSet
@@ -392,6 +393,10 @@ Entities
392393
RELATED-DISCRIMINATOR ::= [a-j]
393394
RELATED-DISCRIMINATOR ::= [A-J]
394395

396+
macro-discriminator-list ::= macro-discriminator-list? 'fM' macro-expansion-operator INDEX
397+
398+
macro-expansion-operator ::= 'f' // freestanding macro
399+
395400
file-discriminator ::= identifier 'Ll' // anonymous file-discriminated declaration
396401

397402
The identifier in a ``<file-discriminator>`` and the second identifier in a

include/swift/AST/ASTContext.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ namespace swift {
8787
class LazyContextData;
8888
class LazyIterableDeclContextData;
8989
class LazyMemberLoader;
90+
struct MacroDiscriminatorContext;
9091
class ModuleDependencyInfo;
9192
class PatternBindingDecl;
9293
class PatternBindingInitializer;
@@ -1070,6 +1071,11 @@ class ASTContext final {
10701071
AbstractFunctionDecl *originalAFD, unsigned previousGeneration,
10711072
llvm::SetVector<AutoDiffConfig> &results);
10721073

1074+
/// Retrieve the next macro expansion discriminator within the given
1075+
/// name and context.
1076+
unsigned getNextMacroDiscriminator(MacroDiscriminatorContext context,
1077+
DeclBaseName baseName);
1078+
10731079
/// Retrieve the Clang module loader for this ASTContext.
10741080
///
10751081
/// If there is no Clang module loader, returns a null pointer.

include/swift/AST/ASTMangler.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace swift {
2626

2727
class AbstractClosureExpr;
2828
class ConformancePath;
29+
class MacroExpansionExpr;
2930
class RootProtocolConformance;
3031

3132
namespace Mangle {
@@ -365,6 +366,12 @@ class ASTMangler : public Mangler {
365366
mangleRuntimeAttributeGeneratorEntity(const ValueDecl *decl, CustomAttr *attr,
366367
SymbolKind SKind = SymbolKind::Default);
367368

369+
std::string mangleMacroExpansion(const MacroExpansionExpr *expansion);
370+
std::string mangleMacroExpansion(const MacroExpansionDecl *expansion);
371+
void appendMacroExpansionContext(SourceLoc loc, DeclContext *origDC);
372+
void appendMacroExpansionOperator(
373+
StringRef macroName, unsigned discriminator);
374+
368375
enum SpecialContext {
369376
ObjCContext,
370377
ClangImporterContext,

include/swift/AST/Decl.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,10 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
726726
NumberOfVTableEntries : 2
727727
);
728728

729+
SWIFT_INLINE_BITFIELD(MacroExpansionDecl, Decl, 16,
730+
Discriminator : 16
731+
);
732+
729733
} Bits;
730734

731735
// Storage for the declaration attributes.
@@ -8456,6 +8460,8 @@ class MacroExpansionDecl : public Decl {
84568460
ConcreteDeclRef macroRef;
84578461

84588462
public:
8463+
enum : unsigned { InvalidDiscriminator = 0xFFFF };
8464+
84598465
MacroExpansionDecl(DeclContext *dc, SourceLoc poundLoc, DeclNameRef macro,
84608466
DeclNameLoc macroLoc,
84618467
SourceLoc leftAngleLoc,
@@ -8465,7 +8471,9 @@ class MacroExpansionDecl : public Decl {
84658471
: Decl(DeclKind::MacroExpansion, dc), PoundLoc(poundLoc),
84668472
Macro(macro), MacroLoc(macroLoc),
84678473
LeftAngleLoc(leftAngleLoc), RightAngleLoc(rightAngleLoc),
8468-
GenericArgs(genericArgs), ArgList(args) {}
8474+
GenericArgs(genericArgs), ArgList(args) {
8475+
Bits.MacroExpansionDecl.Discriminator = InvalidDiscriminator;
8476+
}
84698477

84708478
ArrayRef<TypeRepr *> getGenericArgs() const { return GenericArgs; }
84718479

@@ -8486,6 +8494,24 @@ class MacroExpansionDecl : public Decl {
84868494
void setMacroRef(ConcreteDeclRef ref) { macroRef = ref; }
84878495
MacroExpansionExpr *createExpr() const;
84888496

8497+
/// Returns a discriminator which determines this macro expansion's index
8498+
/// in the sequence of macro expansions within the current function.
8499+
unsigned getDiscriminator() const;
8500+
8501+
/// Retrieve the raw discriminator, which may not have been computed yet.
8502+
///
8503+
/// Only use this for queries that are checking for (e.g.) reentrancy or
8504+
/// intentionally do not want to initiate verification.
8505+
unsigned getRawDiscriminator() const {
8506+
return Bits.MacroExpansionDecl.Discriminator;
8507+
}
8508+
8509+
void setDiscriminator(unsigned discriminator) {
8510+
assert(getRawDiscriminator() == InvalidDiscriminator);
8511+
assert(discriminator != InvalidDiscriminator);
8512+
Bits.MacroExpansionDecl.Discriminator = discriminator;
8513+
}
8514+
84898515
static bool classof(const Decl *D) {
84908516
return D->getKind() == DeclKind::MacroExpansion;
84918517
}

include/swift/AST/DiagnosticsSIL.def

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,13 @@ ERROR(sil_moveonlychecker_value_consumed_in_a_loop, none,
743743
"'%0' consumed by a use in a loop", (StringRef))
744744
ERROR(sil_moveonlychecker_exclusivity_violation, none,
745745
"'%0' has consuming use that cannot be eliminated due to a tight exclusivity scope", (StringRef))
746+
ERROR(sil_moveonlychecker_moveonly_field_consumed, none,
747+
"'%0' has a move only field that was consumed before later uses", (StringRef))
746748

749+
NOTE(sil_moveonlychecker_moveonly_field_consumed_here, none,
750+
"move only field consumed here", ())
751+
NOTE(sil_moveonlychecker_boundary_use, none,
752+
"boundary use here", ())
747753
NOTE(sil_moveonlychecker_consuming_use_here, none,
748754
"consuming use", ())
749755
NOTE(sil_moveonlychecker_consuming_closure_use_here, none,

include/swift/AST/Expr.h

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,12 @@ class alignas(8) Expr : public ASTAllocated<Expr> {
363363
: NumPadBits,
364364
NumElements : 32
365365
);
366+
367+
SWIFT_INLINE_BITFIELD(MacroExpansionExpr, Expr, (16-NumExprBits)+16,
368+
: 16 - NumExprBits, // Align and leave room for subclasses
369+
Discriminator : 16
370+
);
371+
366372
} Bits;
367373

368374
private:
@@ -6047,6 +6053,7 @@ class TypeJoinExpr final : public Expr,
60476053

60486054
class MacroExpansionExpr final : public Expr {
60496055
private:
6056+
DeclContext *DC;
60506057
SourceLoc PoundLoc;
60516058
DeclNameRef MacroName;
60526059
DeclNameLoc MacroNameLoc;
@@ -6059,19 +6066,26 @@ class MacroExpansionExpr final : public Expr {
60596066
ConcreteDeclRef macroRef;
60606067

60616068
public:
6062-
explicit MacroExpansionExpr(SourceLoc poundLoc, DeclNameRef macroName,
6069+
enum : unsigned { InvalidDiscriminator = 0xFFFF };
6070+
6071+
explicit MacroExpansionExpr(DeclContext *dc,
6072+
SourceLoc poundLoc, DeclNameRef macroName,
60636073
DeclNameLoc macroNameLoc,
60646074
SourceLoc leftAngleLoc,
60656075
ArrayRef<TypeRepr *> genericArgs,
60666076
SourceLoc rightAngleLoc,
6067-
ArgumentList *argList, bool isImplicit = false,
6077+
ArgumentList *argList,
6078+
bool isImplicit = false,
60686079
Type ty = Type())
6069-
: Expr(ExprKind::MacroExpansion, isImplicit, ty), PoundLoc(poundLoc),
6080+
: Expr(ExprKind::MacroExpansion, isImplicit, ty),
6081+
DC(dc), PoundLoc(poundLoc),
60706082
MacroName(macroName), MacroNameLoc(macroNameLoc),
60716083
LeftAngleLoc(leftAngleLoc), RightAngleLoc(rightAngleLoc),
60726084
GenericArgs(genericArgs),
60736085
ArgList(argList),
6074-
Rewritten(nullptr) { }
6086+
Rewritten(nullptr) {
6087+
Bits.MacroExpansionExpr.Discriminator = InvalidDiscriminator;
6088+
}
60756089

60766090
DeclNameRef getMacroName() const { return MacroName; }
60776091
DeclNameLoc getMacroNameLoc() const { return MacroNameLoc; }
@@ -6093,6 +6107,27 @@ class MacroExpansionExpr final : public Expr {
60936107
ConcreteDeclRef getMacroRef() const { return macroRef; }
60946108
void setMacroRef(ConcreteDeclRef ref) { macroRef = ref; }
60956109

6110+
DeclContext *getDeclContext() const { return DC; }
6111+
void setDeclContext(DeclContext *dc) { DC = dc; }
6112+
6113+
/// Returns a discriminator which determines this macro expansion's index
6114+
/// in the sequence of macro expansions within the current function.
6115+
unsigned getDiscriminator() const;
6116+
6117+
/// Retrieve the raw discriminator, which may not have been computed yet.
6118+
///
6119+
/// Only use this for queries that are checking for (e.g.) reentrancy or
6120+
/// intentionally do not want to initiate verification.
6121+
unsigned getRawDiscriminator() const {
6122+
return Bits.MacroExpansionExpr.Discriminator;
6123+
}
6124+
6125+
void setDiscriminator(unsigned discriminator) {
6126+
assert(getRawDiscriminator() == InvalidDiscriminator);
6127+
assert(discriminator != InvalidDiscriminator);
6128+
Bits.MacroExpansionExpr.Discriminator = discriminator;
6129+
}
6130+
60966131
SourceRange getSourceRange() const;
60976132

60986133
static bool classof(const Expr *E) {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===--- MacroDiscriminatorContext.h - Macro Discriminators -----*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_AST_MACRO_DISCRIMINATOR_CONTEXT_H
14+
#define SWIFT_AST_MACRO_DISCRIMINATOR_CONTEXT_H
15+
16+
#include "swift/AST/Decl.h"
17+
#include "swift/AST/Expr.h"
18+
#include "llvm/ADT/PointerUnion.h"
19+
20+
namespace swift {
21+
22+
/// Describes the context of a macro expansion for the purpose of
23+
/// computing macro expansion discriminators.
24+
struct MacroDiscriminatorContext
25+
: public llvm::PointerUnion<DeclContext *, MacroExpansionExpr *,
26+
MacroExpansionDecl *> {
27+
using PointerUnion::PointerUnion;
28+
29+
static MacroDiscriminatorContext getParentOf(MacroExpansionExpr *expansion);
30+
static MacroDiscriminatorContext getParentOf(MacroExpansionDecl *expansion);
31+
static MacroDiscriminatorContext getParentOf(
32+
SourceLoc loc, DeclContext *origDC
33+
);
34+
};
35+
36+
}
37+
38+
#endif // SWIFT_AST_MACRO_DISCRIMINATOR_CONTEXT_H

include/swift/Basic/FrozenMultiMap.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,11 @@ class FrozenMultiMap {
122122

123123
bool isFrozen() const { return frozen; }
124124

125-
/// Set this map into its frozen state when we
125+
/// Set this map into its frozen state. This stable sorts our internal array
126+
/// to create our map like context.
127+
///
128+
/// After this, one can only use map like operations and non-mutable vector
129+
/// operations instead of full mutable/non-mutable vector operations.
126130
void setFrozen() {
127131
std::stable_sort(storage.begin(), storage.end(),
128132
[&](const std::pair<Key, Optional<Value>> &lhs,
@@ -134,6 +138,13 @@ class FrozenMultiMap {
134138
frozen = true;
135139
}
136140

141+
/// Unfreeze the map, so one can go back to using mutable vector
142+
/// operations. After one calls this until one freezes the map again, one
143+
/// cannot use map operations.
144+
///
145+
/// This allows one to incrementally update the map.
146+
void unfreeze() { frozen = false; }
147+
137148
/// Reset the frozen multimap in an unfrozen state with its storage cleared.
138149
void reset() {
139150
storage.clear();

include/swift/Demangling/DemangleNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ NODE(LazyProtocolWitnessTableAccessor)
148148
NODE(LazyProtocolWitnessTableCacheVariable)
149149
NODE(LocalDeclName)
150150
NODE(Macro)
151+
NODE(MacroExpansion)
151152
CONTEXT_NODE(MaterializeForSet)
152153
NODE(MergedFunction)
153154
NODE(Metatype)

include/swift/Demangling/Demangler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,7 @@ class Demangler : public NodeFactory {
589589
NodePointer demangleGenericRequirement();
590590
NodePointer demangleGenericType();
591591
NodePointer demangleValueWitness();
592+
NodePointer demangleMacroExpansion();
592593

593594
NodePointer demangleTypeMangling();
594595
NodePointer demangleSymbolicReference(unsigned char rawKind);

0 commit comments

Comments
 (0)