Skip to content

Commit c517b45

Browse files
authored
Merge pull request #38948 from beccadax/the-copypasta-is-stale
[NFC] Factor out ASTContext `operator new`s
2 parents ad1a9c0 + 59bb325 commit c517b45

37 files changed

+139
-413
lines changed

include/swift/AST/ASTAllocated.h

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//===--- ASTAllocated.h - Allocation in the AST Context ---------*- 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_ASTALLOCATED_H
14+
#define SWIFT_AST_ASTALLOCATED_H
15+
16+
#include <cassert>
17+
#include <cstddef>
18+
19+
namespace swift {
20+
class ASTContext;
21+
22+
/// The arena in which a particular ASTContext allocation will go.
23+
enum class AllocationArena {
24+
/// The permanent arena, which is tied to the lifetime of
25+
/// the ASTContext.
26+
///
27+
/// All global declarations and types need to be allocated into this arena.
28+
/// At present, everything that is not a type involving a type variable is
29+
/// allocated in this arena.
30+
Permanent,
31+
/// The constraint solver's temporary arena, which is tied to the
32+
/// lifetime of a particular instance of the constraint solver.
33+
///
34+
/// Any type involving a type variable is allocated in this arena.
35+
ConstraintSolver
36+
};
37+
38+
namespace detail {
39+
void *allocateInASTContext(size_t bytes, const ASTContext &ctx,
40+
AllocationArena arena, unsigned alignment);
41+
}
42+
43+
/// Types inheriting from this class are intended to be allocated in an
44+
/// \c ASTContext allocator; you cannot allocate them by using a normal \c new,
45+
/// and instead you must either provide an \c ASTContext or use a placement
46+
/// \c new.
47+
///
48+
/// The template parameter is a type with the desired alignment. It is usually,
49+
/// but not always, the type that is inheriting \c ASTAllocated.
50+
template <typename AlignTy>
51+
class ASTAllocated {
52+
public:
53+
// Make vanilla new/delete illegal.
54+
void *operator new(size_t Bytes) throw() = delete;
55+
void operator delete(void *Data) throw() = delete;
56+
57+
// Only allow allocation using the allocator in ASTContext
58+
// or by doing a placement new.
59+
void *operator new(size_t bytes, const ASTContext &ctx,
60+
AllocationArena arena = AllocationArena::Permanent,
61+
unsigned alignment = alignof(AlignTy)) {
62+
return detail::allocateInASTContext(bytes, ctx, arena, alignment);
63+
}
64+
65+
void *operator new(size_t Bytes, void *Mem) throw() {
66+
assert(Mem && "placement new into failed allocation");
67+
return Mem;
68+
}
69+
};
70+
71+
}
72+
73+
#endif

include/swift/AST/ASTContext.h

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#ifndef SWIFT_AST_ASTCONTEXT_H
1818
#define SWIFT_AST_ASTCONTEXT_H
1919

20+
#include "swift/AST/ASTAllocated.h"
2021
#include "swift/AST/Evaluator.h"
2122
#include "swift/AST/GenericSignature.h"
2223
#include "swift/AST/Identifier.h"
@@ -145,22 +146,6 @@ namespace syntax {
145146
class SyntaxArena;
146147
}
147148

148-
/// The arena in which a particular ASTContext allocation will go.
149-
enum class AllocationArena {
150-
/// The permanent arena, which is tied to the lifetime of
151-
/// the ASTContext.
152-
///
153-
/// All global declarations and types need to be allocated into this arena.
154-
/// At present, everything that is not a type involving a type variable is
155-
/// allocated in this arena.
156-
Permanent,
157-
/// The constraint solver's temporary arena, which is tied to the
158-
/// lifetime of a particular instance of the constraint solver.
159-
///
160-
/// Any type involving a type variable is allocated in this arena.
161-
ConstraintSolver
162-
};
163-
164149
/// Lists the set of "known" Foundation entities that are used in the
165150
/// compiler.
166151
///

include/swift/AST/ASTScope.h

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ SourceLoc extractNearestSourceLoc(std::tuple<ASTScopeImpl *, ScopeCreator *>);
119119
/// \code
120120
/// -dump-scope-maps expanded
121121
/// \endcode
122-
class ASTScopeImpl {
122+
class ASTScopeImpl : public ASTAllocated<ASTScopeImpl> {
123123
friend class NodeAdder;
124124
friend class Portion;
125125
friend class GenericTypeOrExtensionWholePortion;
@@ -159,20 +159,9 @@ class ASTScopeImpl {
159159
ASTScopeImpl(const ASTScopeImpl &) = delete;
160160
ASTScopeImpl &operator=(const ASTScopeImpl &) = delete;
161161

162-
// Make vanilla new illegal for ASTScopes.
163-
void *operator new(size_t bytes) = delete;
164162
// Need this because have virtual destructors
165163
void operator delete(void *data) {}
166164

167-
// Only allow allocation of scopes using the allocator of a particular source
168-
// file.
169-
void *operator new(size_t bytes, const ASTContext &ctx,
170-
unsigned alignment = alignof(ASTScopeImpl));
171-
void *operator new(size_t Bytes, void *Mem) {
172-
ASTScopeAssert(Mem, "Allocation failed");
173-
return Mem;
174-
}
175-
176165
#pragma mark - tree declarations
177166
protected:
178167
NullablePtr<ASTScopeImpl> getParent() {
@@ -425,26 +414,15 @@ class ASTSourceFileScope final : public ASTScopeImpl {
425414
expandAScopeThatCreatesANewInsertionPoint(ScopeCreator &);
426415
};
427416

428-
class Portion {
417+
class Portion : public ASTAllocated<ASTScopeImpl> {
429418
public:
430419
const char *portionName;
431420
Portion(const char *n) : portionName(n) {}
432421
virtual ~Portion() {}
433422

434-
// Make vanilla new illegal for ASTScopes.
435-
void *operator new(size_t bytes) = delete;
436423
// Need this because have virtual destructors
437424
void operator delete(void *data) {}
438425

439-
// Only allow allocation of scopes using the allocator of a particular source
440-
// file.
441-
void *operator new(size_t bytes, const ASTContext &ctx,
442-
unsigned alignment = alignof(ASTScopeImpl));
443-
void *operator new(size_t Bytes, void *Mem) {
444-
ASTScopeAssert(Mem, "Allocation failed");
445-
return Mem;
446-
}
447-
448426
/// Return the new insertion point
449427
virtual ASTScopeImpl *expandScope(GenericTypeOrExtensionScope *,
450428
ScopeCreator &) const = 0;

include/swift/AST/Attr.h

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "swift/Basic/OptimizationMode.h"
2727
#include "swift/Basic/Version.h"
2828
#include "swift/Basic/Located.h"
29+
#include "swift/AST/ASTAllocated.h"
2930
#include "swift/AST/Identifier.h"
3031
#include "swift/AST/AttrKind.h"
3132
#include "swift/AST/AutoDiff.h"
@@ -60,7 +61,8 @@ class PatternBindingInitializer;
6061
class TrailingWhereClause;
6162
class TypeExpr;
6263

63-
class alignas(1 << AttrAlignInBits) AttributeBase {
64+
class alignas(1 << AttrAlignInBits) AttributeBase
65+
: public ASTAllocated<AttributeBase> {
6466
public:
6567
/// The location of the '@'.
6668
const SourceLoc AtLoc;
@@ -80,17 +82,6 @@ class alignas(1 << AttrAlignInBits) AttributeBase {
8082
return Range;
8183
}
8284

83-
// Only allow allocation of attributes using the allocator in ASTContext
84-
// or by doing a placement new.
85-
void *operator new(size_t Bytes, ASTContext &C,
86-
unsigned Alignment = alignof(AttributeBase));
87-
88-
void operator delete(void *Data) throw() { }
89-
void *operator new(size_t Bytes, void *Mem) throw() { return Mem; }
90-
91-
// Make vanilla new/delete illegal for attributes.
92-
void *operator new(size_t Bytes) throw() = delete;
93-
9485
AttributeBase(const AttributeBase &) = delete;
9586

9687
protected:

include/swift/AST/AvailabilitySpec.h

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ enum class AvailabilitySpecKind {
4444

4545
/// The root class for specifications of API availability in availability
4646
/// queries.
47-
class AvailabilitySpec {
47+
class AvailabilitySpec : public ASTAllocated<AvailabilitySpec> {
4848
AvailabilitySpecKind Kind;
4949

5050
public:
@@ -53,12 +53,6 @@ class AvailabilitySpec {
5353
AvailabilitySpecKind getKind() const { return Kind; }
5454

5555
SourceRange getSourceRange() const;
56-
57-
void *
58-
operator new(size_t Bytes, ASTContext &C,
59-
unsigned Alignment = alignof(AvailabilitySpec));
60-
void *operator new(size_t Bytes) throw() = delete;
61-
void operator delete(void *Data) throw() = delete;
6256
};
6357

6458
/// An availability specification that guards execution based on the
@@ -133,7 +127,8 @@ class PlatformVersionConstraintAvailabilitySpec : public AvailabilitySpec {
133127
void *
134128
operator new(size_t Bytes, ASTContext &C,
135129
unsigned Alignment = alignof(PlatformVersionConstraintAvailabilitySpec)){
136-
return AvailabilitySpec::operator new(Bytes, C, Alignment);
130+
return AvailabilitySpec::operator new(Bytes, C, AllocationArena::Permanent,
131+
Alignment);
137132
}
138133
};
139134

@@ -180,7 +175,8 @@ class PlatformAgnosticVersionConstraintAvailabilitySpec : public AvailabilitySpe
180175
void *
181176
operator new(size_t Bytes, ASTContext &C,
182177
unsigned Alignment = alignof(PlatformAgnosticVersionConstraintAvailabilitySpec)){
183-
return AvailabilitySpec::operator new(Bytes, C, Alignment);
178+
return AvailabilitySpec::operator new(Bytes, C, AllocationArena::Permanent,
179+
Alignment);
184180
}
185181
};
186182

@@ -214,7 +210,8 @@ class OtherPlatformAvailabilitySpec : public AvailabilitySpec {
214210
void *
215211
operator new(size_t Bytes, ASTContext &C,
216212
unsigned Alignment = alignof(OtherPlatformAvailabilitySpec)) {
217-
return AvailabilitySpec::operator new(Bytes, C, Alignment);
213+
return AvailabilitySpec::operator new(Bytes, C, AllocationArena::Permanent,
214+
Alignment);
218215
}
219216
};
220217

include/swift/AST/Decl.h

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ enum class ArtificialMainKind : uint8_t {
292292
};
293293

294294
/// Decl - Base class for all declarations in Swift.
295-
class alignas(1 << DeclAlignInBits) Decl {
295+
class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
296296
protected:
297297
union { uint64_t OpaqueBits;
298298

@@ -1022,19 +1022,6 @@ class alignas(1 << DeclAlignInBits) Decl {
10221022
/// Retrieve the diagnostic engine for diagnostics emission.
10231023
LLVM_READONLY
10241024
DiagnosticEngine &getDiags() const;
1025-
1026-
// Make vanilla new/delete illegal for Decls.
1027-
void *operator new(size_t Bytes) = delete;
1028-
void operator delete(void *Data) = delete;
1029-
1030-
// Only allow allocation of Decls using the allocator in ASTContext
1031-
// or by doing a placement new.
1032-
void *operator new(size_t Bytes, const ASTContext &C,
1033-
unsigned Alignment = alignof(Decl));
1034-
void *operator new(size_t Bytes, void *Mem) {
1035-
assert(Mem);
1036-
return Mem;
1037-
}
10381025
};
10391026

10401027
/// Allocates memory for a Decl with the given \p baseSize. If necessary,
@@ -1417,6 +1404,7 @@ class ExtensionDecl final : public GenericContext, public Decl,
14171404
}
14181405

14191406
using DeclContext::operator new;
1407+
using DeclContext::operator delete;
14201408
};
14211409

14221410
/// Iterator that walks the extensions of a particular type.
@@ -1971,6 +1959,7 @@ class TopLevelCodeDecl : public DeclContext, public Decl {
19711959
}
19721960

19731961
using DeclContext::operator new;
1962+
using DeclContext::operator delete;
19741963
};
19751964

19761965
/// SerializedTopLevelCodeDeclContext - This represents what was originally a
@@ -2621,6 +2610,7 @@ class GenericTypeDecl : public GenericContext, public TypeDecl {
26212610
// Resolve ambiguity due to multiple base classes.
26222611
using TypeDecl::getASTContext;
26232612
using DeclContext::operator new;
2613+
using DeclContext::operator delete;
26242614
using TypeDecl::getDeclaredInterfaceType;
26252615

26262616
static bool classof(const DeclContext *C) {
@@ -5734,6 +5724,7 @@ class SubscriptDecl : public GenericContext, public AbstractStorageDecl {
57345724
}
57355725

57365726
using DeclContext::operator new;
5727+
using DeclContext::operator delete;
57375728
using Decl::getASTContext;
57385729
};
57395730

@@ -6282,6 +6273,7 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
62826273
bool hasKnownUnsafeSendableFunctionParams() const;
62836274

62846275
using DeclContext::operator new;
6276+
using DeclContext::operator delete;
62856277
using Decl::getASTContext;
62866278
};
62876279

@@ -6813,6 +6805,7 @@ class EnumElementDecl : public DeclContext, public ValueDecl {
68136805
}
68146806

68156807
using DeclContext::operator new;
6808+
using DeclContext::operator delete;
68166809
using Decl::getASTContext;
68176810
};
68186811

include/swift/AST/DeclContext.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#ifndef SWIFT_DECLCONTEXT_H
2020
#define SWIFT_DECLCONTEXT_H
2121

22+
#include "swift/AST/ASTAllocated.h"
2223
#include "swift/AST/Identifier.h"
2324
#include "swift/AST/LookupKinds.h"
2425
#include "swift/AST/ResilienceExpansion.h"
@@ -221,7 +222,8 @@ struct FragileFunctionKind {
221222
/// and therefore can safely access trailing memory. If you need to create a
222223
/// macro context, please see GenericContext for how to minimize new entries in
223224
/// the ASTHierarchy enum below.
224-
class alignas(1 << DeclContextAlignInBits) DeclContext {
225+
class alignas(1 << DeclContextAlignInBits) DeclContext
226+
: public ASTAllocated<DeclContext> {
225227
enum class ASTHierarchy : unsigned {
226228
Decl,
227229
Expr,
@@ -628,10 +630,6 @@ class alignas(1 << DeclContextAlignInBits) DeclContext {
628630
SWIFT_DEBUG_DUMPER(dumpContext());
629631
unsigned printContext(llvm::raw_ostream &OS, unsigned indent = 0,
630632
bool onlyAPartialLine = false) const;
631-
632-
// Only allow allocation of DeclContext using the allocator in ASTContext.
633-
void *operator new(size_t Bytes, ASTContext &C,
634-
unsigned Alignment = alignof(DeclContext));
635633

636634
// Some Decls are DeclContexts, but not all. See swift/AST/Decl.h
637635
static bool classof(const Decl *D);

include/swift/AST/Expr.h

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ enum class AccessSemantics : uint8_t {
138138
};
139139

140140
/// Expr - Base class for all expressions in swift.
141-
class alignas(8) Expr {
141+
class alignas(8) Expr : public ASTAllocated<Expr> {
142142
Expr(const Expr&) = delete;
143143
void operator=(const Expr&) = delete;
144144

@@ -570,20 +570,6 @@ class alignas(8) Expr {
570570
unsigned Indent = 0) const;
571571

572572
void print(ASTPrinter &Printer, const PrintOptions &Opts) const;
573-
574-
// Only allow allocation of Exprs using the allocator in ASTContext
575-
// or by doing a placement new.
576-
void *operator new(size_t Bytes, ASTContext &C,
577-
unsigned Alignment = alignof(Expr));
578-
579-
// Make placement new and vanilla new/delete illegal for Exprs.
580-
void *operator new(size_t Bytes) throw() = delete;
581-
void operator delete(void *Data) throw() = delete;
582-
583-
void *operator new(size_t Bytes, void *Mem) {
584-
assert(Mem);
585-
return Mem;
586-
}
587573
};
588574

589575
/// ErrorExpr - Represents a semantically erroneous subexpression in the AST,
@@ -3867,6 +3853,7 @@ class AbstractClosureExpr : public DeclContext, public Expr {
38673853
}
38683854

38693855
using DeclContext::operator new;
3856+
using DeclContext::operator delete;
38703857
using Expr::dump;
38713858
};
38723859

0 commit comments

Comments
 (0)