Skip to content

Commit 07e56a4

Browse files
authored
Sprinkle LLVM_READONLY on some potentially recursive accessors (#17913)
I picked accessors that not only return the same result every time, but also do no interesting validation work with possible side effects. We have a lot more accessors that return the same result but also force a bunch of things to be loaded or diagnostics to be emitted, and I didn't want to change the behavior of any of those. No intended functionality change; this is just supposed to be a small optimization hint.
1 parent fd1baff commit 07e56a4

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

include/swift/AST/Decl.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,7 @@ class alignas(1 << DeclAlignInBits) Decl {
692692
/// not be serialized.
693693
bool canHaveComment() const;
694694

695+
LLVM_READONLY
695696
DeclContext *getDeclContext() const {
696697
if (auto dc = Context.dyn_cast<DeclContext *>())
697698
return dc;
@@ -706,9 +707,11 @@ class alignas(1 << DeclAlignInBits) Decl {
706707
DeclContext *getInnermostDeclContext() const;
707708

708709
/// \brief Retrieve the module in which this declaration resides.
710+
LLVM_READONLY
709711
ModuleDecl *getModuleContext() const;
710712

711713
/// getASTContext - Return the ASTContext that this decl lives in.
714+
LLVM_READONLY
712715
ASTContext &getASTContext() const {
713716
if (auto dc = Context.dyn_cast<DeclContext *>())
714717
return dc->getASTContext();
@@ -851,6 +854,7 @@ class alignas(1 << DeclAlignInBits) Decl {
851854

852855
/// \brief Retrieve the Clang AST node from which this declaration was
853856
/// synthesized, if any.
857+
LLVM_READONLY
854858
ClangNode getClangNode() const {
855859
if (!Bits.Decl.FromClang)
856860
return ClangNode();
@@ -860,6 +864,7 @@ class alignas(1 << DeclAlignInBits) Decl {
860864

861865
/// \brief Retrieve the Clang declaration from which this declaration was
862866
/// synthesized, if any.
867+
LLVM_READONLY
863868
const clang::Decl *getClangDecl() const {
864869
if (!Bits.Decl.FromClang)
865870
return nullptr;
@@ -869,6 +874,7 @@ class alignas(1 << DeclAlignInBits) Decl {
869874

870875
/// \brief Retrieve the Clang macro from which this declaration was
871876
/// synthesized, if any.
877+
LLVM_READONLY
872878
const clang::MacroInfo *getClangMacro() {
873879
if (!Bits.Decl.FromClang)
874880
return nullptr;
@@ -877,6 +883,7 @@ class alignas(1 << DeclAlignInBits) Decl {
877883
}
878884

879885
/// \brief Return the GenericContext if the Decl has one.
886+
LLVM_READONLY
880887
const GenericContext *getAsGenericContext() const;
881888

882889
bool isPrivateStdlibDecl(bool treatNonBuiltinProtocolsAsPublic = true) const;
@@ -900,6 +907,7 @@ class alignas(1 << DeclAlignInBits) Decl {
900907
}
901908

902909
/// Retrieve the diagnostic engine for diagnostics emission.
910+
LLVM_READONLY
903911
DiagnosticEngine &getDiags() const;
904912

905913
// Make vanilla new/delete illegal for Decls.

include/swift/AST/DeclContext.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ class alignas(1 << DeclContextAlignInBits) DeclContext {
237237
}
238238

239239
public:
240+
LLVM_READONLY
240241
Decl *getAsDeclOrDeclExtensionContext() {
241242
return ParentAndKind.getInt() == ASTHierarchy::Decl ?
242243
reinterpret_cast<Decl*>(this + 1) : nullptr;
@@ -262,40 +263,50 @@ class alignas(1 << DeclContextAlignInBits) DeclContext {
262263
}
263264

264265
/// isModuleContext - Return true if this is a subclass of Module.
266+
LLVM_READONLY
265267
bool isModuleContext() const; // see swift/AST/Module.h
266268

267269
/// \returns true if this is a context with module-wide scope, e.g. a module
268270
/// or a source file.
271+
LLVM_READONLY
269272
bool isModuleScopeContext() const; // see swift/AST/Module.h
270273

271274
/// \returns true if this is a type context, e.g., a struct, a class, an
272275
/// enum, a protocol, or an extension.
276+
LLVM_READONLY
273277
bool isTypeContext() const;
274278

275279
/// \brief Determine whether this is an extension context.
280+
LLVM_READONLY
276281
bool isExtensionContext() const; // see swift/AST/Decl.h
277282

278283
/// If this DeclContext is a NominalType declaration or an
279284
/// extension thereof, return the NominalTypeDecl.
285+
LLVM_READONLY
280286
NominalTypeDecl *getAsNominalTypeOrNominalTypeExtensionContext() const;
281287

282288
/// If this DeclContext is a class, or an extension on a class, return the
283289
/// ClassDecl, otherwise return null.
290+
LLVM_READONLY
284291
ClassDecl *getAsClassOrClassExtensionContext() const;
285292

286293
/// If this DeclContext is an enum, or an extension on an enum, return the
287294
/// EnumDecl, otherwise return null.
295+
LLVM_READONLY
288296
EnumDecl *getAsEnumOrEnumExtensionContext() const;
289297

290298
/// If this DeclContext is a struct, or an extension on a struct, return the
291299
/// StructDecl, otherwise return null.
300+
LLVM_READONLY
292301
StructDecl *getAsStructOrStructExtensionContext() const;
293302

294303
/// If this DeclContext is a protocol, or an extension on a
295304
/// protocol, return the ProtocolDecl, otherwise return null.
305+
LLVM_READONLY
296306
ProtocolDecl *getAsProtocolOrProtocolExtensionContext() const;
297307

298308
/// If this DeclContext is a protocol extension, return the extended protocol.
309+
LLVM_READONLY
299310
ProtocolDecl *getAsProtocolExtensionContext() const;
300311

301312
/// \brief Retrieve the generic parameter 'Self' from a protocol or
@@ -351,6 +362,7 @@ class alignas(1 << DeclContextAlignInBits) DeclContext {
351362

352363
/// Returns this or the first local parent context, or nullptr if it is not
353364
/// contained in one.
365+
LLVM_READONLY
354366
DeclContext *getLocalContext();
355367
const DeclContext *getLocalContext() const {
356368
return const_cast<DeclContext*>(this)->getLocalContext();
@@ -363,6 +375,7 @@ class alignas(1 << DeclContextAlignInBits) DeclContext {
363375
/// destructors).
364376
///
365377
/// \returns the innermost method, or null if there is no such method.
378+
LLVM_READONLY
366379
AbstractFunctionDecl *getInnermostMethodContext();
367380
const AbstractFunctionDecl *getInnermostMethodContext() const {
368381
return const_cast<DeclContext*>(this)->getInnermostMethodContext();
@@ -373,6 +386,7 @@ class alignas(1 << DeclContextAlignInBits) DeclContext {
373386
/// This routine looks through closure, initializer, and local function
374387
/// contexts to find the innermost type context -- nominal type or
375388
/// extension.
389+
LLVM_READONLY
376390
DeclContext *getInnermostTypeContext();
377391
const DeclContext *getInnermostTypeContext() const {
378392
return const_cast<DeclContext *>(this)->getInnermostTypeContext();
@@ -382,6 +396,7 @@ class alignas(1 << DeclContextAlignInBits) DeclContext {
382396
///
383397
/// This routine looks through contexts to find the innermost
384398
/// declaration context that is itself a declaration.
399+
LLVM_READONLY
385400
Decl *getInnermostDeclarationDeclContext();
386401
const Decl *getInnermostDeclarationDeclContext() const {
387402
return
@@ -409,15 +424,18 @@ class alignas(1 << DeclContextAlignInBits) DeclContext {
409424
}
410425

411426
/// Returns the module context that contains this context.
427+
LLVM_READONLY
412428
ModuleDecl *getParentModule() const;
413429

414430
/// Returns the module scope context that contains this context.
415431
///
416432
/// This is either a \c Module or a \c FileUnit.
433+
LLVM_READONLY
417434
DeclContext *getModuleScopeContext() const;
418435

419436
/// Returns the source file that contains this context, or null if this
420437
/// is not within a source file.
438+
LLVM_READONLY
421439
SourceFile *getParentSourceFile() const;
422440

423441
/// Determine whether this declaration context is generic, meaning that it or
@@ -476,6 +494,7 @@ class alignas(1 << DeclContextAlignInBits) DeclContext {
476494

477495
/// Return the ASTContext for a specified DeclContext by
478496
/// walking up to the enclosing module and returning its ASTContext.
497+
LLVM_READONLY
479498
ASTContext &getASTContext() const;
480499

481500
/// Retrieve the set of protocols whose conformances will be

0 commit comments

Comments
 (0)